Reformat code should not align comments (KT-4175)
#KT-4175 In Progress
This commit is contained in:
+2
@@ -47,6 +47,7 @@ public class KotlinLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe
|
|||||||
" return 0\n" +
|
" return 0\n" +
|
||||||
" }\n" +
|
" }\n" +
|
||||||
" private fun foo2():Int {\n" +
|
" private fun foo2():Int {\n" +
|
||||||
|
"// todo: something\n" +
|
||||||
" try {" +
|
" try {" +
|
||||||
" return foo1(12, 13, 14)\n" +
|
" return foo1(12, 13, 14)\n" +
|
||||||
" }" +
|
" }" +
|
||||||
@@ -148,6 +149,7 @@ public class KotlinLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe
|
|||||||
case WRAPPING_AND_BRACES_SETTINGS:
|
case WRAPPING_AND_BRACES_SETTINGS:
|
||||||
consumer.showStandardOptions(
|
consumer.showStandardOptions(
|
||||||
// "ALIGN_MULTILINE_CHAINED_METHODS",
|
// "ALIGN_MULTILINE_CHAINED_METHODS",
|
||||||
|
"KEEP_FIRST_COLUMN_COMMENT",
|
||||||
"ALIGN_MULTILINE_EXTENDS_LIST",
|
"ALIGN_MULTILINE_EXTENDS_LIST",
|
||||||
"ALIGN_MULTILINE_PARAMETERS",
|
"ALIGN_MULTILINE_PARAMETERS",
|
||||||
"ALIGN_MULTILINE_PARAMETERS_IN_CALLS",
|
"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.codeStyle.CodeStyleSettings
|
||||||
import com.intellij.psi.formatter.common.AbstractBlock
|
import com.intellij.psi.formatter.common.AbstractBlock
|
||||||
import com.intellij.psi.tree.IElementType
|
import com.intellij.psi.tree.IElementType
|
||||||
|
import com.intellij.psi.tree.TokenSet
|
||||||
import org.jetbrains.kotlin.KtNodeTypes
|
import org.jetbrains.kotlin.KtNodeTypes
|
||||||
import org.jetbrains.kotlin.idea.KotlinLanguage
|
import org.jetbrains.kotlin.idea.KotlinLanguage
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
@@ -62,12 +63,16 @@ class KotlinSpacingBuilder(val codeStyleSettings: CodeStyleSettings) {
|
|||||||
return null
|
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 {
|
conditions.add {
|
||||||
p, l, r ->
|
p, l, r ->
|
||||||
(parent == null || p.node!!.elementType == parent) &&
|
(parent == null || p.node!!.elementType == parent) &&
|
||||||
(left == null || l.node!!.elementType == left) &&
|
(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
|
return this
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -288,6 +288,11 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
|
|||||||
spacingForLeftBrace(right.node!!.firstChildNode)
|
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 = THEN).customRule(leftBraceRuleIfBlockIsWrapped)
|
||||||
inPosition(parent = IF, right = ELSE).customRule(leftBraceRuleIfBlockIsWrapped)
|
inPosition(parent = IF, right = ELSE).customRule(leftBraceRuleIfBlockIsWrapped)
|
||||||
|
|
||||||
|
|||||||
+1
-2
@@ -12,8 +12,7 @@ class A {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun foo() {
|
fun foo() { // abc
|
||||||
// abc
|
|
||||||
class B
|
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
|
||||||
@@ -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
|
||||||
+24
@@ -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() {
|
fun testWithComments() {
|
||||||
val abc = ArrayList<Int>()
|
val abc = ArrayList<Int>()
|
||||||
// .map {
|
// .map {
|
||||||
// it * 2
|
// it * 2
|
||||||
// }
|
// }
|
||||||
.filter {
|
.filter {
|
||||||
it > 4
|
it > 4
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ fun main(args: Array<String>)
|
|||||||
/*2*/
|
/*2*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*3*/
|
/*3*/
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ fun main(args: Array<String>) {
|
|||||||
/*2*/
|
/*2*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*3*/ {
|
/*3*/ {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-2
@@ -12,8 +12,7 @@ class A {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun foo() {
|
fun foo() { // abc
|
||||||
// abc
|
|
||||||
object B
|
object B
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-2
@@ -47,8 +47,7 @@ public var varWithAccessors1: Int
|
|||||||
get() {
|
get() {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
set (value: Int) {
|
set (value: Int) { /**/
|
||||||
/**/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public var varWithAccessors2: 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")
|
||||||
|
}
|
||||||
|
}
|
||||||
+9
@@ -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);
|
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")
|
@TestMetadata("FunctionCallParametersAlign.after.kt")
|
||||||
public void testFunctionCallParametersAlign() throws Exception {
|
public void testFunctionCallParametersAlign() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/FunctionCallParametersAlign.after.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/FunctionCallParametersAlign.after.kt");
|
||||||
@@ -865,6 +877,18 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
|
|||||||
doTestInverted(fileName);
|
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")
|
@TestMetadata("FunctionLineBreak.after.inv.kt")
|
||||||
public void testFunctionLineBreak() throws Exception {
|
public void testFunctionLineBreak() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/FunctionLineBreak.after.inv.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/FunctionLineBreak.after.inv.kt");
|
||||||
|
|||||||
@@ -7241,6 +7241,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("while.kt")
|
||||||
public void testWhile() throws Exception {
|
public void testWhile() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/while.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/while.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user