Reformat code should not align comments (KT-4175)

#KT-4175 In Progress
This commit is contained in:
Nikolay Krasko
2016-03-09 16:21:05 +03:00
parent 334c6ba71a
commit 6193987d0d
19 changed files with 278 additions and 13 deletions
@@ -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",
@@ -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
}
@@ -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)
+1 -2
View File
@@ -12,8 +12,7 @@ class A {
}
}
fun foo() {
// abc
fun foo() { // abc
class B
}
@@ -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
@@ -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
+46
View File
@@ -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
@@ -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
@@ -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
@@ -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
@@ -35,9 +35,9 @@ fun test3() {
fun testWithComments() {
val abc = ArrayList<Int>()
// .map {
// it * 2
// }
// .map {
// it * 2
// }
.filter {
it > 4
}
@@ -17,7 +17,6 @@ fun main(args: Array<String>)
/*2*/
/*3*/
{
}
@@ -15,7 +15,6 @@ fun main(args: Array<String>) {
/*2*/
/*3*/ {
}
+1 -2
View File
@@ -12,8 +12,7 @@ class A {
}
}
fun foo() {
// abc
fun foo() { // abc
object B
}
+1 -2
View File
@@ -47,8 +47,7 @@ public var varWithAccessors1: Int
get() {
return 1
}
set (value: Int) {
/**/
set (value: Int) { /**/
}
public var varWithAccessors2: Int
@@ -0,0 +1,9 @@
fun doSomething<T>(a: T) {}
fun foo() {
<caret>if (true) {
// val a = 1
// var b = 1
doSomething("test")
}
}
@@ -0,0 +1,9 @@
fun doSomething<T>(a: T) {}
fun foo() {
<caret>if (true) {
// val a = 1
// var b = 1
doSomething("test")
}
}
@@ -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");
@@ -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");