From 8b00f3b3c0d1b194a3a71f35eeca1face06ee690 Mon Sep 17 00:00:00 2001 From: Yaroslav Lepenkin Date: Tue, 14 Jul 2015 19:29:50 +0300 Subject: [PATCH] [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 --- .../FormatterBasedLineIndentInfoBuilder.java | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/psi/codeStyle/autodetect/FormatterBasedLineIndentInfoBuilder.java b/platform/lang-impl/src/com/intellij/psi/codeStyle/autodetect/FormatterBasedLineIndentInfoBuilder.java index 164956c51e4a..e18a834445e7 100644 --- a/platform/lang-impl/src/com/intellij/psi/codeStyle/autodetect/FormatterBasedLineIndentInfoBuilder.java +++ b/platform/lang-impl/src/com/intellij/psi/codeStyle/autodetect/FormatterBasedLineIndentInfoBuilder.java @@ -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 normallyIndentedBlocks = ContainerUtil.filter(getBlocksStartingNewLine(), new Condition() { @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 subBlocks = block.getSubBlocks(); + block = subBlocks.isEmpty() ? null : subBlocks.get(0); + } + + return true; + } @NotNull private List getBlocksStartingNewLine() {