"Keep indents on empty lines" formatter option: indent before closing brace

This commit is contained in:
Rustam Vishnyakov
2014-09-25 18:16:48 +04:00
parent 8272a35c09
commit fac64f722b
6 changed files with 31 additions and 4 deletions
@@ -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" +
" <caret>\n" +
" <caret>\n" +
" }\n" +
" }\n" +
" while (y > 0);\n" +
@@ -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" +
@@ -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;
}
}
@@ -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);
@@ -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) == '}';
}
}
@@ -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);
}