From 6193987d0d2b2bb558eecb4ed02b1488b13cbe25 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Wed, 9 Mar 2016 16:21:05 +0300 Subject: [PATCH] Reformat code should not align comments (KT-4175) #KT-4175 In Progress --- ...tlinLanguageCodeStyleSettingsProvider.java | 2 + .../idea/formatter/KotlinSpacingBuilder.kt | 9 +++- .../idea/formatter/kotlinSpacingRules.kt | 5 ++ idea/testData/formatter/ClassInBody.after.kt | 3 +- .../FormatFirstColumnComments.after.inv.kt | 46 +++++++++++++++++++ .../FormatFirstColumnComments.after.kt | 46 +++++++++++++++++++ .../formatter/FormatFirstColumnComments.kt | 46 +++++++++++++++++++ ...lumnCommentsBeforeDeclaration.after.inv.kt | 24 ++++++++++ ...stColumnCommentsBeforeDeclaration.after.kt | 24 ++++++++++ ...matFirstColumnCommentsBeforeDeclaration.kt | 24 ++++++++++ .../FunctionLiteralsInChainCalls.after.kt | 6 +-- .../IfElseWithTrickyComments.after.inv.kt | 1 - .../IfElseWithTrickyComments.after.kt | 1 - idea/testData/formatter/ObjectInBody.after.kt | 3 +- .../formatter/SpacesInDeclarations.after.kt | 3 +- .../removeBracesFromIfWithCommentedCode.kt | 9 ++++ ...moveBracesFromIfWithCommentedCode.kt.after | 9 ++++ .../formatter/FormatterTestGenerated.java | 24 ++++++++++ .../intentions/IntentionTestGenerated.java | 6 +++ 19 files changed, 278 insertions(+), 13 deletions(-) create mode 100644 idea/testData/formatter/FormatFirstColumnComments.after.inv.kt create mode 100644 idea/testData/formatter/FormatFirstColumnComments.after.kt create mode 100644 idea/testData/formatter/FormatFirstColumnComments.kt create mode 100644 idea/testData/formatter/FormatFirstColumnCommentsBeforeDeclaration.after.inv.kt create mode 100644 idea/testData/formatter/FormatFirstColumnCommentsBeforeDeclaration.after.kt create mode 100644 idea/testData/formatter/FormatFirstColumnCommentsBeforeDeclaration.kt create mode 100644 idea/testData/intentions/removeBraces/removeBracesFromIfWithCommentedCode.kt create mode 100644 idea/testData/intentions/removeBraces/removeBracesFromIfWithCommentedCode.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.java b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.java index 0b39f6ae378..d5c1ab78513 100644 --- a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.java +++ b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.java @@ -47,6 +47,7 @@ public class KotlinLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe " return 0\n" + " }\n" + " private fun foo2():Int {\n" + + "// todo: something\n" + " try {" + " return foo1(12, 13, 14)\n" + " }" + @@ -148,6 +149,7 @@ public class KotlinLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe case WRAPPING_AND_BRACES_SETTINGS: consumer.showStandardOptions( // "ALIGN_MULTILINE_CHAINED_METHODS", + "KEEP_FIRST_COLUMN_COMMENT", "ALIGN_MULTILINE_EXTENDS_LIST", "ALIGN_MULTILINE_PARAMETERS", "ALIGN_MULTILINE_PARAMETERS_IN_CALLS", diff --git a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinSpacingBuilder.kt b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinSpacingBuilder.kt index b1828a3ede7..a9c94fa7e3a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinSpacingBuilder.kt +++ b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinSpacingBuilder.kt @@ -24,6 +24,7 @@ import com.intellij.openapi.util.TextRange import com.intellij.psi.codeStyle.CodeStyleSettings import com.intellij.psi.formatter.common.AbstractBlock import com.intellij.psi.tree.IElementType +import com.intellij.psi.tree.TokenSet import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.idea.KotlinLanguage import org.jetbrains.kotlin.lexer.KtTokens @@ -62,12 +63,16 @@ class KotlinSpacingBuilder(val codeStyleSettings: CodeStyleSettings) { return null } - fun inPosition(parent: IElementType? = null, left: IElementType? = null, right: IElementType? = null): CustomSpacingBuilder { + fun inPosition(parent: IElementType? = null, left: IElementType? = null, right: IElementType? = null, + parentSet: TokenSet? = null, leftSet: TokenSet? = null, rightSet: TokenSet? = null): CustomSpacingBuilder { conditions.add { p, l, r -> (parent == null || p.node!!.elementType == parent) && (left == null || l.node!!.elementType == left) && - (right == null || r.node!!.elementType == right) + (right == null || r.node!!.elementType == right) && + (parentSet == null || parentSet.contains(p.node!!.elementType)) && + (leftSet == null || leftSet.contains(l.node!!.elementType)) && + (rightSet == null || rightSet.contains(r.node!!.elementType)) } return this } diff --git a/idea/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt b/idea/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt index 85c9ce5dc7f..15bcd9ff18e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt +++ b/idea/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt @@ -288,6 +288,11 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder { spacingForLeftBrace(right.node!!.firstChildNode) } + if (kotlinCommonSettings.KEEP_FIRST_COLUMN_COMMENT) { + inPosition(rightSet = TokenSet.create(EOL_COMMENT, BLOCK_COMMENT)).spacing( + Spacing.createKeepingFirstColumnSpacing(0, Integer.MAX_VALUE, settings.KEEP_LINE_BREAKS, kotlinCommonSettings.KEEP_BLANK_LINES_IN_CODE)) + } + inPosition(parent = IF, right = THEN).customRule(leftBraceRuleIfBlockIsWrapped) inPosition(parent = IF, right = ELSE).customRule(leftBraceRuleIfBlockIsWrapped) diff --git a/idea/testData/formatter/ClassInBody.after.kt b/idea/testData/formatter/ClassInBody.after.kt index a0749a18b62..9f7de4b8fdf 100644 --- a/idea/testData/formatter/ClassInBody.after.kt +++ b/idea/testData/formatter/ClassInBody.after.kt @@ -12,8 +12,7 @@ class A { } } -fun foo() { - // abc +fun foo() { // abc class B } diff --git a/idea/testData/formatter/FormatFirstColumnComments.after.inv.kt b/idea/testData/formatter/FormatFirstColumnComments.after.inv.kt new file mode 100644 index 00000000000..33688909a4b --- /dev/null +++ b/idea/testData/formatter/FormatFirstColumnComments.after.inv.kt @@ -0,0 +1,46 @@ +package format.test + +class LineComments { + fun test() { + // Should not be formatted + // Format + // Format + // Normal + } +} + +class KDocComments { + /** + * Always ident for KDocs + */ + /** + * Format + */ + fun test() { + /** + * Always ident for KDocs + */ + /** + * Format + */ + /** + * Normal + */ + } +} + +class MultilineComments { + fun test() { + /* + * Should not be formatted + */ + /* + * Format + */ + /* + * Normal + */ + } +} + +// SET_TRUE: KEEP_FIRST_COLUMN_COMMENT \ No newline at end of file diff --git a/idea/testData/formatter/FormatFirstColumnComments.after.kt b/idea/testData/formatter/FormatFirstColumnComments.after.kt new file mode 100644 index 00000000000..5eaabc136ce --- /dev/null +++ b/idea/testData/formatter/FormatFirstColumnComments.after.kt @@ -0,0 +1,46 @@ +package format.test + +class LineComments { + fun test() { +// Should not be formatted + // Format + // Format + // Normal + } +} + +class KDocComments { + /** + * Always ident for KDocs + */ + /** + * Format + */ + fun test() { + /** + * Always ident for KDocs + */ + /** + * Format + */ + /** + * Normal + */ + } +} + +class MultilineComments { + fun test() { +/* + * Should not be formatted + */ + /* + * Format + */ + /* + * Normal + */ + } +} + +// SET_TRUE: KEEP_FIRST_COLUMN_COMMENT \ No newline at end of file diff --git a/idea/testData/formatter/FormatFirstColumnComments.kt b/idea/testData/formatter/FormatFirstColumnComments.kt new file mode 100644 index 00000000000..0153bbf308a --- /dev/null +++ b/idea/testData/formatter/FormatFirstColumnComments.kt @@ -0,0 +1,46 @@ +package format.test + +class LineComments { + fun test() { +// Should not be formatted + // Format + // Format + // Normal + } +} + +class KDocComments { +/** + * Always ident for KDocs + */ + /** + * Format + */ + fun test() { +/** + * Always ident for KDocs + */ + /** + * Format + */ + /** + * Normal + */ + } +} + +class MultilineComments { + fun test() { +/* + * Should not be formatted + */ + /* + * Format + */ + /* + * Normal + */ + } +} + +// SET_TRUE: KEEP_FIRST_COLUMN_COMMENT \ No newline at end of file diff --git a/idea/testData/formatter/FormatFirstColumnCommentsBeforeDeclaration.after.inv.kt b/idea/testData/formatter/FormatFirstColumnCommentsBeforeDeclaration.after.inv.kt new file mode 100644 index 00000000000..311308eba53 --- /dev/null +++ b/idea/testData/formatter/FormatFirstColumnCommentsBeforeDeclaration.after.inv.kt @@ -0,0 +1,24 @@ +package format.test + +// TODO: Comment on first column shouldn't be formatted, but now there's no way to adjust rule for the first comment in parent declaration. + +class LineComments { + // Should not be formatted + // Format + // Format + fun test() { + } +} + +class MultilineComments { + /* + * Should not be formatted + */ + /* + * Format + */ + fun test() { + } +} + +// SET_TRUE: KEEP_FIRST_COLUMN_COMMENT \ No newline at end of file diff --git a/idea/testData/formatter/FormatFirstColumnCommentsBeforeDeclaration.after.kt b/idea/testData/formatter/FormatFirstColumnCommentsBeforeDeclaration.after.kt new file mode 100644 index 00000000000..311308eba53 --- /dev/null +++ b/idea/testData/formatter/FormatFirstColumnCommentsBeforeDeclaration.after.kt @@ -0,0 +1,24 @@ +package format.test + +// TODO: Comment on first column shouldn't be formatted, but now there's no way to adjust rule for the first comment in parent declaration. + +class LineComments { + // Should not be formatted + // Format + // Format + fun test() { + } +} + +class MultilineComments { + /* + * Should not be formatted + */ + /* + * Format + */ + fun test() { + } +} + +// SET_TRUE: KEEP_FIRST_COLUMN_COMMENT \ No newline at end of file diff --git a/idea/testData/formatter/FormatFirstColumnCommentsBeforeDeclaration.kt b/idea/testData/formatter/FormatFirstColumnCommentsBeforeDeclaration.kt new file mode 100644 index 00000000000..ba0c1442524 --- /dev/null +++ b/idea/testData/formatter/FormatFirstColumnCommentsBeforeDeclaration.kt @@ -0,0 +1,24 @@ +package format.test + +// TODO: Comment on first column shouldn't be formatted, but now there's no way to adjust rule for the first comment in parent declaration. + +class LineComments { +// Should not be formatted + // Format + // Format + fun test() { + } +} + +class MultilineComments { + /* + * Should not be formatted + */ + /* + * Format + */ + fun test() { + } +} + +// SET_TRUE: KEEP_FIRST_COLUMN_COMMENT \ No newline at end of file diff --git a/idea/testData/formatter/FunctionLiteralsInChainCalls.after.kt b/idea/testData/formatter/FunctionLiteralsInChainCalls.after.kt index 3b29c05c5f0..3efb11e9125 100644 --- a/idea/testData/formatter/FunctionLiteralsInChainCalls.after.kt +++ b/idea/testData/formatter/FunctionLiteralsInChainCalls.after.kt @@ -35,9 +35,9 @@ fun test3() { fun testWithComments() { val abc = ArrayList() - // .map { - // it * 2 - // } +// .map { +// it * 2 +// } .filter { it > 4 } diff --git a/idea/testData/formatter/IfElseWithTrickyComments.after.inv.kt b/idea/testData/formatter/IfElseWithTrickyComments.after.inv.kt index 45b47872549..c852ac4aa6d 100644 --- a/idea/testData/formatter/IfElseWithTrickyComments.after.inv.kt +++ b/idea/testData/formatter/IfElseWithTrickyComments.after.inv.kt @@ -17,7 +17,6 @@ fun main(args: Array) /*2*/ - /*3*/ { } diff --git a/idea/testData/formatter/IfElseWithTrickyComments.after.kt b/idea/testData/formatter/IfElseWithTrickyComments.after.kt index ca11e9393e8..ce1ec485465 100644 --- a/idea/testData/formatter/IfElseWithTrickyComments.after.kt +++ b/idea/testData/formatter/IfElseWithTrickyComments.after.kt @@ -15,7 +15,6 @@ fun main(args: Array) { /*2*/ - /*3*/ { } diff --git a/idea/testData/formatter/ObjectInBody.after.kt b/idea/testData/formatter/ObjectInBody.after.kt index ec40cb88ab1..3473a9f071a 100644 --- a/idea/testData/formatter/ObjectInBody.after.kt +++ b/idea/testData/formatter/ObjectInBody.after.kt @@ -12,8 +12,7 @@ class A { } } -fun foo() { - // abc +fun foo() { // abc object B } diff --git a/idea/testData/formatter/SpacesInDeclarations.after.kt b/idea/testData/formatter/SpacesInDeclarations.after.kt index caeaa3d3f67..b5a665f7c38 100644 --- a/idea/testData/formatter/SpacesInDeclarations.after.kt +++ b/idea/testData/formatter/SpacesInDeclarations.after.kt @@ -47,8 +47,7 @@ public var varWithAccessors1: Int get() { return 1 } - set (value: Int) { - /**/ + set (value: Int) { /**/ } public var varWithAccessors2: Int diff --git a/idea/testData/intentions/removeBraces/removeBracesFromIfWithCommentedCode.kt b/idea/testData/intentions/removeBraces/removeBracesFromIfWithCommentedCode.kt new file mode 100644 index 00000000000..7656398ef6c --- /dev/null +++ b/idea/testData/intentions/removeBraces/removeBracesFromIfWithCommentedCode.kt @@ -0,0 +1,9 @@ +fun doSomething(a: T) {} + +fun foo() { + if (true) { + // val a = 1 + // var b = 1 + doSomething("test") + } +} diff --git a/idea/testData/intentions/removeBraces/removeBracesFromIfWithCommentedCode.kt.after b/idea/testData/intentions/removeBraces/removeBracesFromIfWithCommentedCode.kt.after new file mode 100644 index 00000000000..7656398ef6c --- /dev/null +++ b/idea/testData/intentions/removeBraces/removeBracesFromIfWithCommentedCode.kt.after @@ -0,0 +1,9 @@ +fun doSomething(a: T) {} + +fun foo() { + if (true) { + // val a = 1 + // var b = 1 + doSomething("test") + } +} diff --git a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java index d7b96570c53..f3cb2eac241 100644 --- a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java @@ -247,6 +247,18 @@ public class FormatterTestGenerated extends AbstractFormatterTest { doTest(fileName); } + @TestMetadata("FormatFirstColumnComments.after.kt") + public void testFormatFirstColumnComments() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/FormatFirstColumnComments.after.kt"); + doTest(fileName); + } + + @TestMetadata("FormatFirstColumnCommentsBeforeDeclaration.after.kt") + public void testFormatFirstColumnCommentsBeforeDeclaration() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/FormatFirstColumnCommentsBeforeDeclaration.after.kt"); + doTest(fileName); + } + @TestMetadata("FunctionCallParametersAlign.after.kt") public void testFunctionCallParametersAlign() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/FunctionCallParametersAlign.after.kt"); @@ -865,6 +877,18 @@ public class FormatterTestGenerated extends AbstractFormatterTest { doTestInverted(fileName); } + @TestMetadata("FormatFirstColumnComments.after.inv.kt") + public void testFormatFirstColumnComments() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/FormatFirstColumnComments.after.inv.kt"); + doTestInverted(fileName); + } + + @TestMetadata("FormatFirstColumnCommentsBeforeDeclaration.after.inv.kt") + public void testFormatFirstColumnCommentsBeforeDeclaration() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/FormatFirstColumnCommentsBeforeDeclaration.after.inv.kt"); + doTestInverted(fileName); + } + @TestMetadata("FunctionLineBreak.after.inv.kt") public void testFunctionLineBreak() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/FunctionLineBreak.after.inv.kt"); diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index ecf501fabb1..3190bd2d2e3 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -7241,6 +7241,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("removeBracesFromIfWithCommentedCode.kt") + public void testRemoveBracesFromIfWithCommentedCode() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/removeBracesFromIfWithCommentedCode.kt"); + doTest(fileName); + } + @TestMetadata("while.kt") public void testWhile() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/while.kt");