From 7687e3d61f1b801811beb548648b787cf5b222f7 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Thu, 16 Jan 2014 18:56:27 +0400 Subject: [PATCH] Formatter: left brace formatting for "try/catch/finally" --- .../plugin/formatter/KotlinSpacingBuilder.kt | 45 +++++++----- .../formatter/TryCatchLineBreak.after.inv.kt | 59 +++++++++++++++ .../formatter/TryCatchLineBreak.after.kt | 68 ++++++++++++++++++ idea/testData/formatter/TryCatchLineBreak.kt | 71 +++++++++++++++++++ .../formatter/JetFormatterTestGenerated.java | 10 +++ 5 files changed, 235 insertions(+), 18 deletions(-) create mode 100644 idea/testData/formatter/TryCatchLineBreak.after.inv.kt create mode 100644 idea/testData/formatter/TryCatchLineBreak.after.kt create mode 100644 idea/testData/formatter/TryCatchLineBreak.kt diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/KotlinSpacingBuilder.kt b/idea/src/org/jetbrains/jet/plugin/formatter/KotlinSpacingBuilder.kt index 90b38135a47..7baf9df6079 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/KotlinSpacingBuilder.kt +++ b/idea/src/org/jetbrains/jet/plugin/formatter/KotlinSpacingBuilder.kt @@ -26,6 +26,7 @@ import org.jetbrains.jet.lexer.JetTokens.* import com.intellij.psi.tree.TokenSet import com.intellij.psi.tree.IElementType import com.intellij.psi.formatter.FormatterUtil +import com.intellij.lang.ASTNode class KotlinSpacingBuilder(val codeStyleSettings: CodeStyleSettings) { @@ -197,29 +198,37 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder { } custom { - val leftBraceRule = { - (parent: ASTBlock, left: ASTBlock, right: ASTBlock) -> - val blockOrExpression = right.getNode()!!.getFirstChildNode() + fun spacingForLeftBrace(block: ASTNode?): Spacing? { val noBlockSpacing = Spacing.createSpacing(1, 1, 0, settings.KEEP_LINE_BREAKS, settings.KEEP_BLANK_LINES_IN_CODE) - if (blockOrExpression != null && blockOrExpression.getElementType() == BLOCK) { - val leftBrace = blockOrExpression.getFirstChildNode() - when { - leftBrace != null && leftBrace.getElementType() == LBRACE -> { - val previousLeaf = FormatterUtil.getPreviousNonWhitespaceLeaf(leftBrace) - val isAfterEolComment = previousLeaf != null && (previousLeaf.getElementType() == EOL_COMMENT) - val keepLineBreaks = jetSettings.LBRACE_ON_NEXT_LINE || isAfterEolComment - val minimumLF = if (jetSettings.LBRACE_ON_NEXT_LINE) 1 else 0 - Spacing.createSpacing(1, 1, minimumLF, keepLineBreaks, 0) - } - else -> noBlockSpacing + if (block != null && block.getElementType() == BLOCK) { + val leftBrace = block.getFirstChildNode() + if (leftBrace != null && leftBrace.getElementType() == LBRACE) { + val previousLeaf = FormatterUtil.getPreviousNonWhitespaceLeaf(leftBrace) + val isAfterEolComment = previousLeaf != null && (previousLeaf.getElementType() == EOL_COMMENT) + val keepLineBreaks = jetSettings.LBRACE_ON_NEXT_LINE || isAfterEolComment + val minimumLF = if (jetSettings.LBRACE_ON_NEXT_LINE) 1 else 0 + return Spacing.createSpacing(1, 1, minimumLF, keepLineBreaks, 0) } } - else noBlockSpacing + return noBlockSpacing } - inPosition(parent = IF, right = THEN).customRule(leftBraceRule) - inPosition(parent = IF, right = ELSE).customRule(leftBraceRule) - inPosition(parent = WHILE, right = BODY).customRule(leftBraceRule) + val leftBraceRule = { + (parent: ASTBlock, left: ASTBlock, right: ASTBlock) -> + spacingForLeftBrace(right.getNode()) + } + + val leftBraceRuleIfBlockIsWrapped = { + (parent: ASTBlock, left: ASTBlock, right: ASTBlock) -> + spacingForLeftBrace(right.getNode()!!.getFirstChildNode()) + } + + inPosition(parent = IF, right = THEN).customRule(leftBraceRuleIfBlockIsWrapped) + inPosition(parent = IF, right = ELSE).customRule(leftBraceRuleIfBlockIsWrapped) + inPosition(parent = WHILE, right = BODY).customRule(leftBraceRuleIfBlockIsWrapped) + inPosition(parent = TRY, right = BLOCK).customRule(leftBraceRule) + inPosition(parent = CATCH, right = BLOCK).customRule(leftBraceRule) + inPosition(parent = FINALLY, right = BLOCK).customRule(leftBraceRule) val spacesInSimpleFunction = if (jetSettings.INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD) 1 else 0 inPosition(parent = FUNCTION_LITERAL, diff --git a/idea/testData/formatter/TryCatchLineBreak.after.inv.kt b/idea/testData/formatter/TryCatchLineBreak.after.inv.kt new file mode 100644 index 00000000000..44d728b6b61 --- /dev/null +++ b/idea/testData/formatter/TryCatchLineBreak.after.inv.kt @@ -0,0 +1,59 @@ +fun f() { + try { + + } + catch (e: Exception) { + + } + finally { + + } + + try { + + } + catch (e: Exception) { + + } + finally { + + } + + try { + + } + catch (e: Exception) { + + } + finally { + + } + + try //eol comment + { + + } + catch (e: Exception) //eol comment + { + + } + finally //eol comment + { + + } + + try //eol comment + { + + } + catch (e: Exception) //eol comment + { + + } + finally //eol comment + { + + } +} + +// SET_TRUE: LBRACE_ON_NEXT_LINE \ No newline at end of file diff --git a/idea/testData/formatter/TryCatchLineBreak.after.kt b/idea/testData/formatter/TryCatchLineBreak.after.kt new file mode 100644 index 00000000000..92347c415a1 --- /dev/null +++ b/idea/testData/formatter/TryCatchLineBreak.after.kt @@ -0,0 +1,68 @@ +fun f() { + try + { + + } + catch (e: Exception) + { + + } + finally + { + + } + + try + { + + } + catch (e: Exception) + { + + } + finally + { + + } + + try + { + + } + catch (e: Exception) + { + + } + finally + { + + } + + try //eol comment + { + + } + catch (e: Exception) //eol comment + { + + } + finally //eol comment + { + + } + + try //eol comment + { + + } + catch (e: Exception) //eol comment + { + + } + finally //eol comment + { + + } +} + +// SET_TRUE: LBRACE_ON_NEXT_LINE \ No newline at end of file diff --git a/idea/testData/formatter/TryCatchLineBreak.kt b/idea/testData/formatter/TryCatchLineBreak.kt new file mode 100644 index 00000000000..2197cd07474 --- /dev/null +++ b/idea/testData/formatter/TryCatchLineBreak.kt @@ -0,0 +1,71 @@ +fun f() { + try { + + } + catch (e: Exception) { + + } + finally { + + } + + try + { + + } + catch (e: Exception) + { + + } + finally + { + + } + + try + + { + + } + catch (e: Exception) + + { + + } + finally + + { + + } + + try //eol comment + { + + } + catch (e: Exception) //eol comment + { + + } + finally //eol comment + { + + } + + try //eol comment + + { + + } + catch (e: Exception) //eol comment + + { + + } + finally //eol comment + + { + + } +} + +// SET_TRUE: LBRACE_ON_NEXT_LINE \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java index 10a2359f320..61baa6b45e4 100644 --- a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java @@ -209,6 +209,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest { doTest("idea/testData/formatter/SpacesAroundUnaryOperations.after.kt"); } + @TestMetadata("TryCatchLineBreak.after.kt") + public void testTryCatchLineBreak() throws Exception { + doTest("idea/testData/formatter/TryCatchLineBreak.after.kt"); + } + @TestMetadata("UnnecessarySpacesInParametersLists.after.kt") public void testUnnecessarySpacesInParametersLists() throws Exception { doTest("idea/testData/formatter/UnnecessarySpacesInParametersLists.after.kt"); @@ -397,6 +402,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest { doTestInverted("idea/testData/formatter/SpacesAroundUnaryOperations.after.inv.kt"); } + @TestMetadata("TryCatchLineBreak.after.inv.kt") + public void testTryCatchLineBreak() throws Exception { + doTestInverted("idea/testData/formatter/TryCatchLineBreak.after.inv.kt"); + } + @TestMetadata("When.after.inv.kt") public void testWhen() throws Exception { doTestInverted("idea/testData/formatter/When.after.inv.kt");