IDEA-68003 Java Formatter: Correct formatting of anonymous classes at method call arguments

1. Returned old indent processing for 'child block that doesn't start new line from parent block' use-case;
2. Corrected java blocks indent construction;
This commit is contained in:
Denis Zhdanov
2011-04-14 15:45:42 +04:00
parent 3975825eb4
commit 1bd5fb51dc
4 changed files with 66 additions and 33 deletions
@@ -1372,7 +1372,7 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
);
}
final boolean rBrace = isRBrace(child);
Indent childIndent = rBrace ? Indent.getNoneIndent() : getCodeBlockInternalIndent(childrenIndent, true);
Indent childIndent = rBrace ? Indent.getNoneIndent() : getCodeBlockInternalIndent(childrenIndent, false);
if (!rBrace && child.getElementType() == JavaElementType.CODE_BLOCK
&& (getBraceStyle() == CommonCodeStyleSettings.NEXT_LINE_SHIFTED
|| getBraceStyle() == CommonCodeStyleSettings.NEXT_LINE_SHIFTED2))
@@ -21,13 +21,21 @@ import com.intellij.psi.codeStyle.CodeStyleSettings;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static java.util.Arrays.asList;
/**
* @author lesya
*/
public abstract class AbstractBlockWrapper {
private static final Set<IndentImpl.Type> RELATIVE_INDENT_TYPES = new HashSet<IndentImpl.Type>(asList(
Indent.Type.NORMAL, Indent.Type.CONTINUATION, Indent.Type.CONTINUATION_WITHOUT_FIRST
));
protected WhiteSpace myWhiteSpace;
protected CompositeBlockWrapper myParent;
protected int myStart;
@@ -158,41 +166,40 @@ public abstract class AbstractBlockWrapper {
public IndentData getChildOffset(AbstractBlockWrapper child, CodeStyleSettings.IndentOptions options, int targetBlockStartOffset) {
final boolean childStartsNewLine = child.getWhiteSpace().containsLineFeeds();
IndentImpl.Type childIndentType = child.getIndent().getType();
IndentData childIndent;
// Calculate child indent.
if (childStartsNewLine) {
if (childStartsNewLine
|| (!getWhiteSpace().containsLineFeeds() && RELATIVE_INDENT_TYPES.contains(childIndentType) && indentAlreadyUsedBefore(child)))
{
childIndent = getIndent(options, child, targetBlockStartOffset);
}
else {
childIndent = new IndentData(0);
}
// Enforce indent if child doesn't start new line, e.g. prefer the code below:
// void test() {
// foo("test", new Runnable() {
// public void run() {
// }
// },
// new Runnable() {
// public void run() {
// }
// }
// );
// }
// to this one:
// void test() {
// foo("test", new Runnable() {
// public void run() {
// }
// },
// new Runnable() {
// public void run() {
// }
// }
// );
// }
if (child.getIndent().isEnforceIndentToChildren() && !child.getWhiteSpace().containsLineFeeds()) {
else if (child.getIndent().isEnforceIndentToChildren() && !child.getWhiteSpace().containsLineFeeds()) {
// Enforce indent if child doesn't start new line, e.g. prefer the code below:
// void test() {
// foo("test", new Runnable() {
// public void run() {
// }
// },
// new Runnable() {
// public void run() {
// }
// }
// );
// }
// to this one:
// void test() {
// foo("test", new Runnable() {
// public void run() {
// }
// },
// new Runnable() {
// public void run() {
// }
// }
// );
// }
AlignmentImpl alignment = child.getAlignment();
if (alignment != null) {
// Generally, we want to handle situation like the one below:
@@ -209,7 +216,7 @@ public abstract class AbstractBlockWrapper {
// );
// I.e. we want 'run()' method from the first anonymous class to be aligned with the 'run()' method of the second anonymous class.
AbstractBlockWrapper anchorBlock = alignment.getOffsetRespBlockBefore(child);
AbstractBlockWrapper anchorBlock = alignment.getOffsetRespBlockBefore(child);
if (anchorBlock == null) {
anchorBlock = this;
if (anchorBlock instanceof CompositeBlockWrapper) {
@@ -224,7 +231,10 @@ public abstract class AbstractBlockWrapper {
}
return anchorBlock.getNumberOfSymbolsBeforeBlock();
}
childIndent = childIndent.add(getIndent(options, child, getStartOffset()));
childIndent = getIndent(options, child, getStartOffset());
}
else {
childIndent = new IndentData(0);
}
// Use child indent if it's absolute and the child is contained on new line.
@@ -287,6 +297,15 @@ public abstract class AbstractBlockWrapper {
}
}
/**
* Allows to answer if current wrapped block has a child block that is located before given block and has line feed.
*
* @param child target child block to process
* @return <code>true</code> if current block has a child that is located before the given block and contains line feed;
* <code>false</code> otherwise
*/
protected abstract boolean indentAlreadyUsedBefore(final AbstractBlockWrapper child);
/**
* Allows to retrieve object that encapsulates information about number of symbols before the current block starting
* from the line start. I.e. all symbols (either white space or not) between start of the line where current block begins
@@ -57,6 +57,15 @@ public class CompositeBlockWrapper extends AbstractBlockWrapper{
}
}
@Override
protected boolean indentAlreadyUsedBefore(final AbstractBlockWrapper child) {
for (AbstractBlockWrapper childBefore : myChildren) {
if (childBefore == child) return false;
if (childBefore.getWhiteSpace().containsLineFeeds()) return true;
}
return false;
}
@Override
protected IndentData getNumberOfSymbolsBeforeBlock() {
if (myChildren == null || myChildren.isEmpty()) {
@@ -126,6 +126,11 @@ class LeafBlockWrapper extends AbstractBlockWrapper {
myNextBlock = nextBlock;
}
@Override
protected boolean indentAlreadyUsedBefore(final AbstractBlockWrapper child) {
return false;
}
@Override
protected IndentData getNumberOfSymbolsBeforeBlock() {
int spaces = getWhiteSpace().getSpaces();