[Indent detector] when checking indent normality (NONE or NORMAL), check indent sum of all blocks starting current offset.

Instead of checking indent of the outer block starting current offset, check all descendant blocks, starting the same offset. Total indent considered normal if it consists of no more than one normal indents and any number of none indents
This commit is contained in:
Yaroslav Lepenkin
2015-07-14 19:58:59 +03:00
parent 212a2ae46f
commit 8b00f3b3c0
@@ -20,6 +20,7 @@ import com.intellij.lang.LanguageFormatting;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
@@ -59,9 +60,7 @@ public class FormatterBasedLineIndentInfoBuilder {
List<Block> normallyIndentedBlocks = ContainerUtil.filter(getBlocksStartingNewLine(), new Condition<Block>() {
@Override
public boolean value(Block block) {
Indent indent = block.getIndent();
Indent.Type type = indent != null ? indent.getType() : null;
return type == Indent.Type.NONE || type == Indent.Type.NORMAL;
return hasTotallyNormalOrNoneIndent(block);
}
});
@@ -74,6 +73,29 @@ public class FormatterBasedLineIndentInfoBuilder {
}
});
}
private static boolean hasTotallyNormalOrNoneIndent(Block block) {
final TextRange range = block.getTextRange();
final int startOffset = range.getStartOffset();
boolean startOffsetAlreadyHasNormalIndent = false;
while (block != null && range.getStartOffset() == startOffset) {
Indent.Type type = block.getIndent() != null ? block.getIndent().getType() : null;
if (type == Indent.Type.NONE || type == Indent.Type.NORMAL && !startOffsetAlreadyHasNormalIndent) {
startOffsetAlreadyHasNormalIndent = true;
}
else {
return false;
}
List<Block> subBlocks = block.getSubBlocks();
block = subBlocks.isEmpty() ? null : subBlocks.get(0);
}
return true;
}
@NotNull
private List<Block> getBlocksStartingNewLine() {