From fac64f722b249990a5af6462f99da281a43a6708 Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Thu, 25 Sep 2014 16:55:54 +0400 Subject: [PATCH] "Keep indents on empty lines" formatter option: indent before closing brace --- .../formatter/java/JavaFormatterInEditorTest.java | 2 +- .../psi/formatter/java/JavaFormatterTest.java | 2 +- .../src/com/intellij/formatting/IndentInfo.java | 9 ++++++++- .../com/intellij/formatting/InitialInfoBuilder.java | 4 ++++ .../com/intellij/formatting/LeafBlockWrapper.java | 6 ++++++ .../src/com/intellij/formatting/WhiteSpace.java | 12 +++++++++++- 6 files changed, 31 insertions(+), 4 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterInEditorTest.java b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterInEditorTest.java index e7286b128e10..86b876d97918 100644 --- a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterInEditorTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterInEditorTest.java @@ -157,7 +157,7 @@ public class JavaFormatterInEditorTest extends LightPlatformCodeInsightTestCase " public void foo(boolean a, int x, int y, int z) {\n" + " do {\n" + " if (x > 0) {\n" + - " \n" + + " \n" + " }\n" + " }\n" + " while (y > 0);\n" + diff --git a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterTest.java b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterTest.java index 8dc7be30e806..18a89580698b 100644 --- a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterTest.java @@ -3149,7 +3149,7 @@ public void testSCR260() throws Exception { " do {\n" + " \n" + " if (x > 0) {\n" + - " \n" + + " \n" + " } else if (x < 0) {\n" + " \n" + " int r;\n" + diff --git a/platform/lang-impl/src/com/intellij/formatting/IndentInfo.java b/platform/lang-impl/src/com/intellij/formatting/IndentInfo.java index 3e9c1d7e3ff0..1b79d6a494ad 100644 --- a/platform/lang-impl/src/com/intellij/formatting/IndentInfo.java +++ b/platform/lang-impl/src/com/intellij/formatting/IndentInfo.java @@ -28,6 +28,7 @@ public class IndentInfo { /** @see WhiteSpace#setForceSkipTabulationsUsage(boolean) */ private final boolean myForceSkipTabulationsUsage; + private boolean myIndentEmptyLines; // Additional indent on empty lines (before the end of code block) public IndentInfo(final int lineFeeds, final int indentSpaces, final int spaces) { this(lineFeeds, indentSpaces, spaces, false); @@ -57,7 +58,8 @@ public class IndentInfo { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < myLineFeeds; i ++) { if (options.KEEP_INDENTS_ON_EMPTY_LINES && i > 0) { - generateLineWhitespace(buffer, options, myIndentSpaces, 0, true); + int spaces = myIndentEmptyLines ? myIndentSpaces + options.INDENT_SIZE : myIndentSpaces; + generateLineWhitespace(buffer, options, spaces, 0, true); } buffer.append('\n'); } @@ -103,4 +105,9 @@ public class IndentInfo { } } } + + public IndentInfo setIndentEmptyLines(boolean indentEmptyLines) { + myIndentEmptyLines = indentEmptyLines; + return this; + } } diff --git a/platform/lang-impl/src/com/intellij/formatting/InitialInfoBuilder.java b/platform/lang-impl/src/com/intellij/formatting/InitialInfoBuilder.java index 9b1968edb1e4..8b7b5fb5c019 100644 --- a/platform/lang-impl/src/com/intellij/formatting/InitialInfoBuilder.java +++ b/platform/lang-impl/src/com/intellij/formatting/InitialInfoBuilder.java @@ -326,6 +326,10 @@ class InitialInfoBuilder { myCurrentWhiteSpace.setKeepFirstColumn(myCurrentSpaceProperty.shouldKeepFirstColumn()); } + if (info.isEndOfCodeBlock()) { + myCurrentWhiteSpace.setBeforeCodeBlockEnd(true); + } + info.setSpaceProperty(myCurrentSpaceProperty); myCurrentWhiteSpace = new WhiteSpace(textRange.getEndOffset(), false); if (myReadOnlyMode) myCurrentWhiteSpace.setReadOnly(true); diff --git a/platform/lang-impl/src/com/intellij/formatting/LeafBlockWrapper.java b/platform/lang-impl/src/com/intellij/formatting/LeafBlockWrapper.java index 2b1b2f93a163..8ba16b4ddddd 100644 --- a/platform/lang-impl/src/com/intellij/formatting/LeafBlockWrapper.java +++ b/platform/lang-impl/src/com/intellij/formatting/LeafBlockWrapper.java @@ -16,6 +16,7 @@ package com.intellij.formatting; +import com.intellij.lang.ASTNode; import com.intellij.openapi.util.TextRange; import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import org.jetbrains.annotations.Nullable; @@ -217,4 +218,9 @@ class LeafBlockWrapper extends AbstractBlockWrapper { public TextRange getTextRange() { return new TextRange(myStart, myEnd); } + + public boolean isEndOfCodeBlock() { + ASTNode node = getNode(); + return node != null && node.getTextLength() == 1 && node.getChars().charAt(0) == '}'; + } } diff --git a/platform/lang-impl/src/com/intellij/formatting/WhiteSpace.java b/platform/lang-impl/src/com/intellij/formatting/WhiteSpace.java index 9ce89c6368d6..1df92e2803bb 100644 --- a/platform/lang-impl/src/com/intellij/formatting/WhiteSpace.java +++ b/platform/lang-impl/src/com/intellij/formatting/WhiteSpace.java @@ -59,6 +59,7 @@ class WhiteSpace { private CharSequence myInitial; private int myFlags; private boolean myForceSkipTabulationsUsage; + private boolean myIsBeforeCodeBlockEnd; private static final byte FIRST = 1; private static final byte SAFE = 0x2; @@ -272,7 +273,10 @@ class WhiteSpace { * {@link WhiteSpace} object */ public String generateWhiteSpace(CommonCodeStyleSettings.IndentOptions options) { - return new IndentInfo(getLineFeeds(), myIndentSpaces, mySpaces, myForceSkipTabulationsUsage).generateNewWhiteSpace(options); + return + new IndentInfo(getLineFeeds(), myIndentSpaces, mySpaces, myForceSkipTabulationsUsage) + .setIndentEmptyLines(myIsBeforeCodeBlockEnd) + .generateNewWhiteSpace(options); } /** @@ -749,6 +753,12 @@ class WhiteSpace { assert (flags & 0x7F) == (myFlags & 0x7F); } + @NotNull + public WhiteSpace setBeforeCodeBlockEnd(boolean isBeforeCodeBlockEnd) { + myIsBeforeCodeBlockEnd = isBeforeCodeBlockEnd; + return this; + } + public TextRange getTextRange() { return new TextRange(myStart, myEnd); }