Formatter: support "while on new line" option

This commit is contained in:
Pavel V. Talanov
2014-01-17 18:59:35 +04:00
parent 5caabc20f6
commit 7dc7db04b3
9 changed files with 86 additions and 22 deletions
@@ -142,7 +142,8 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti
"ALIGN_MULTILINE_PARAMETERS", "ALIGN_MULTILINE_PARAMETERS",
"ALIGN_MULTILINE_PARAMETERS_IN_CALLS", "ALIGN_MULTILINE_PARAMETERS_IN_CALLS",
"ALIGN_MULTILINE_METHOD_BRACKETS", "ALIGN_MULTILINE_METHOD_BRACKETS",
"ELSE_ON_NEW_LINE" "ELSE_ON_NEW_LINE",
"WHILE_ON_NEW_LINE"
); );
consumer.renameStandardOption(CodeStyleSettingsCustomizable.WRAPPING_SWITCH_STATEMENT, "'when' statements"); consumer.renameStandardOption(CodeStyleSettingsCustomizable.WRAPPING_SWITCH_STATEMENT, "'when' statements");
consumer.showCustomOption(JetCodeStyleSettings.class, "ALIGN_IN_COLUMNS_CASE_BRANCH", "Align in columns 'case' branches", consumer.showCustomOption(JetCodeStyleSettings.class, "ALIGN_IN_COLUMNS_CASE_BRANCH", "Align in columns 'case' branches",
@@ -27,6 +27,7 @@ import com.intellij.psi.tree.TokenSet
import com.intellij.psi.tree.IElementType import com.intellij.psi.tree.IElementType
import com.intellij.psi.formatter.FormatterUtil import com.intellij.psi.formatter.FormatterUtil
import com.intellij.lang.ASTNode import com.intellij.lang.ASTNode
import org.jetbrains.jet.plugin.formatter.KotlinSpacingBuilder.CustomSpacingBuilder
class KotlinSpacingBuilder(val codeStyleSettings: CodeStyleSettings) { class KotlinSpacingBuilder(val codeStyleSettings: CodeStyleSettings) {
@@ -161,8 +162,6 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
betweenInside(WHILE_KEYWORD, LPAR, WHILE).spacing(1, 1, 0, false, 0) betweenInside(WHILE_KEYWORD, LPAR, WHILE).spacing(1, 1, 0, false, 0)
betweenInside(WHILE_KEYWORD, LPAR, DO_WHILE).spacing(1, 1, 0, false, 0) betweenInside(WHILE_KEYWORD, LPAR, DO_WHILE).spacing(1, 1, 0, false, 0)
aroundInside(WHILE_KEYWORD, DO_WHILE).spaces(1)
// TODO: Ask for better API // TODO: Ask for better API
// Type of the declaration colon // Type of the declaration colon
beforeInside(COLON, PROPERTY).spaceIf(jetSettings.SPACE_BEFORE_TYPE_COLON) beforeInside(COLON, PROPERTY).spaceIf(jetSettings.SPACE_BEFORE_TYPE_COLON)
@@ -188,20 +187,27 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
betweenInside(REFERENCE_EXPRESSION, FUNCTION_LITERAL_EXPRESSION, CALL_EXPRESSION).spaces(1) betweenInside(REFERENCE_EXPRESSION, FUNCTION_LITERAL_EXPRESSION, CALL_EXPRESSION).spaces(1)
} }
custom { custom {
if (jetCommonSettings.ELSE_ON_NEW_LINE) {
inPosition(parent = IF, right = ELSE_KEYWORD) fun CustomSpacingBuilder.ruleForKeywordOnNewLine(shouldBeOnNewLine: Boolean, keyword: IElementType, parent: IElementType) {
.lineBreakIfLineBreakInParent(numSpacesOtherwise = 1) if (shouldBeOnNewLine) {
} inPosition(parent = parent, right = keyword)
else { .lineBreakIfLineBreakInParent(numSpacesOtherwise = 1)
inPosition(parent = IF, left = THEN, right = ELSE_KEYWORD).customRule { }
parent, left, right -> else {
// do not remove linebreak if "then" expression is not a block inPosition(parent = parent, right = keyword).customRule {
val expressionOrBlock = left.getNode()!!.getFirstChildNode() parent, left, right ->
val keepLineBreaks = expressionOrBlock == null || expressionOrBlock.getElementType() != BLOCK // do not remove linebreak if expression to the left is not a block
Spacing.createSpacing(1, 1, 0, keepLineBreaks, 0) val previousNonWhitespaceLeaf = FormatterUtil.getPreviousNonWhitespaceLeaf(right.getNode())
val keepLineBreaks = previousNonWhitespaceLeaf == null || previousNonWhitespaceLeaf.getElementType() != RBRACE
Spacing.createSpacing(1, 1, 0, keepLineBreaks, 0)
}
} }
} }
ruleForKeywordOnNewLine(jetCommonSettings.ELSE_ON_NEW_LINE, keyword = ELSE_KEYWORD, parent = IF)
ruleForKeywordOnNewLine(jetCommonSettings.WHILE_ON_NEW_LINE, keyword = WHILE_KEYWORD, parent = DO_WHILE)
fun spacingForLeftBrace(block: ASTNode?, blockType: IElementType = BLOCK): Spacing? { fun spacingForLeftBrace(block: ASTNode?, blockType: IElementType = BLOCK): Spacing? {
if (block != null && block.getElementType() == blockType) { if (block != null && block.getElementType() == blockType) {
val leftBrace = block.findChildByType(LBRACE) val leftBrace = block.findChildByType(LBRACE)
@@ -6,12 +6,10 @@ fun test() {
} while (true) } while (true)
do { do {
} } while (true)
while (true)
do { do {
} } while (true)
while (true)
do { do {
} while (true) } while (true)
@@ -0,0 +1,16 @@
fun f() {
do {
} while (true)
do {
} while (true)
do a += 1 while (true)
do a += 1
while (true)
}
// SET_TRUE: WHILE_ON_NEW_LINE
@@ -0,0 +1,18 @@
fun f() {
do {
}
while (true)
do {
}
while (true)
do a += 1 while (true)
do a += 1
while (true)
}
// SET_TRUE: WHILE_ON_NEW_LINE
+17
View File
@@ -0,0 +1,17 @@
fun f() {
do {
} while (true)
do {
}
while (true)
do a += 1 while (true)
do a += 1
while (true)
}
// SET_TRUE: WHILE_ON_NEW_LINE
@@ -269,6 +269,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
doTest("idea/testData/formatter/WhileLineBreak.after.kt"); doTest("idea/testData/formatter/WhileLineBreak.after.kt");
} }
@TestMetadata("WhileOnNewLine.after.kt")
public void testWhileOnNewLine() throws Exception {
doTest("idea/testData/formatter/WhileOnNewLine.after.kt");
}
@TestMetadata("WhileSpacing.after.kt") @TestMetadata("WhileSpacing.after.kt")
public void testWhileSpacing() throws Exception { public void testWhileSpacing() throws Exception {
doTest("idea/testData/formatter/WhileSpacing.after.kt"); doTest("idea/testData/formatter/WhileSpacing.after.kt");
@@ -477,6 +482,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
doTestInverted("idea/testData/formatter/WhileLineBreak.after.inv.kt"); doTestInverted("idea/testData/formatter/WhileLineBreak.after.inv.kt");
} }
@TestMetadata("WhileOnNewLine.after.inv.kt")
public void testWhileOnNewLine() throws Exception {
doTestInverted("idea/testData/formatter/WhileOnNewLine.after.inv.kt");
}
public static Test innerSuite() { public static Test innerSuite() {
TestSuite suite = new TestSuite("FormatterInverted"); TestSuite suite = new TestSuite("FormatterInverted");
suite.addTestSuite(FormatterInverted.class); suite.addTestSuite(FormatterInverted.class);
+1 -2
View File
@@ -25,7 +25,6 @@ class Test() {
do { do {
System.out.println("Ok") System.out.println("Ok")
} } while (One.myContainer.myBoolean)
while (One.myContainer.myBoolean)
} }
} }
+1 -2
View File
@@ -25,7 +25,6 @@ open class Test() {
do { do {
System.out?.println("Ok") System.out?.println("Ok")
} } while (One.myContainer?.myBoolean!!)
while (One.myContainer?.myBoolean!!)
} }
} }