Put arguments/parameters on separate/one line should update trailing comma

#KT-36411 Fixed
This commit is contained in:
Dmitry Gridin
2020-03-03 14:47:21 +07:00
parent 173f90c89c
commit b4898e4043
11 changed files with 102 additions and 9 deletions
@@ -24,6 +24,7 @@ import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.allChildren
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
import org.jetbrains.kotlin.psi.psiUtil.siblings
import org.jetbrains.kotlin.psi.psiUtil.startOffset
@@ -42,8 +43,8 @@ abstract class AbstractChopListIntention<TList : KtElement, TElement : KtElement
override fun applyTo(element: TList, editor: Editor?) {
val project = element.project
val document = editor!!.document
val startOffset = element.startOffset
val document = editor?.document ?: return
val pointer = element.createSmartPointer()
val elements = element.elements()
if (!hasLineBreakAfter(elements.last())) {
@@ -58,9 +59,7 @@ abstract class AbstractChopListIntention<TList : KtElement, TElement : KtElement
val documentManager = PsiDocumentManager.getInstance(project)
documentManager.commitDocument(document)
val psiFile = documentManager.getPsiFile(document) ?: return
val newList = PsiTreeUtil.getParentOfType(psiFile.findElementAt(startOffset) ?: return, listClass) ?: return
CodeStyleManager.getInstance(project).adjustLineIndent(psiFile, newList.textRange)
pointer.element?.let { CodeStyleManager.getInstance(project).reformat(it) }
}
protected fun hasLineBreakAfter(element: TElement): Boolean {
@@ -6,7 +6,10 @@
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.codeStyle.CodeStyleManager
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
@@ -23,14 +26,20 @@ abstract class AbstractJoinListIntention<TList : KtElement, TElement : KtElement
}
override fun applyTo(element: TList, editor: Editor?) {
val document = editor!!.document
val document = editor?.document ?: return
val elements = element.elements()
val pointer = element.createSmartPointer()
nextBreak(elements.last())?.let { document.deleteString(it.startOffset, it.endOffset) }
elements.dropLast(1).asReversed().forEach {
nextBreak(it)?.let { document.replaceString(it.startOffset, it.endOffset, " ") }
elements.dropLast(1).asReversed().forEach { tElement ->
nextBreak(tElement)?.let { document.replaceString(it.startOffset, it.endOffset, " ") }
}
prevBreak(elements.first())?.let { document.deleteString(it.startOffset, it.endOffset) }
val project = element.project
val documentManager = PsiDocumentManager.getInstance(project)
documentManager.commitDocument(document)
pointer.element?.let { CodeStyleManager.getInstance(project).reformat(it) }
}
}
@@ -0,0 +1,8 @@
// SET_TRUE: ALLOW_TRAILING_COMMA
// REGISTRY: kotlin.formatter.allowTrailingCommaOnCallSite true
fun f() {
foo(<caret>1, "a", 2)
}
fun foo(p1: Int, p2: String, p3: Int){}
@@ -0,0 +1,12 @@
// SET_TRUE: ALLOW_TRAILING_COMMA
// REGISTRY: kotlin.formatter.allowTrailingCommaOnCallSite true
fun f() {
foo(
1,
"a",
2,
)
}
fun foo(p1: Int, p2: String, p3: Int){}
@@ -0,0 +1,5 @@
// SET_TRUE: ALLOW_TRAILING_COMMA
fun foo(p: Int, c: Char,
b: <caret>Boolean) {
}
@@ -0,0 +1,8 @@
// SET_TRUE: ALLOW_TRAILING_COMMA
fun foo(
p: Int,
c: Char,
b: <caret>Boolean,
) {
}
@@ -0,0 +1,12 @@
// INTENTION_TEXT: "Put arguments on one line"
// REGISTRY: kotlin.formatter.allowTrailingCommaOnCallSite true
fun foo(a: Int = 1, b: Int = 1, c: Int = 1) {}
fun bar(a: Int, b: Int, c: Int) {
foo(
a,
b,<caret>
c,
)
}
@@ -0,0 +1,8 @@
// INTENTION_TEXT: "Put arguments on one line"
// REGISTRY: kotlin.formatter.allowTrailingCommaOnCallSite true
fun foo(a: Int = 1, b: Int = 1, c: Int = 1) {}
fun bar(a: Int, b: Int, c: Int) {
foo(a, b, c)
}
@@ -0,0 +1,8 @@
// INTENTION_TEXT: "Put parameters on one line"
// SET_TRUE: ALLOW_TRAILING_COMMA
fun test(
a: Int,
b: Int,<caret>
c: Int,
) {}
@@ -0,0 +1,4 @@
// INTENTION_TEXT: "Put parameters on one line"
// SET_TRUE: ALLOW_TRAILING_COMMA
fun test(a: Int, b: Int, c: Int) {}
@@ -3609,6 +3609,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
public void testThreeArgs() throws Exception {
runTest("idea/testData/intentions/chop/argumentList/threeArgs.kt");
}
@TestMetadata("threeArgsWithTrailingComma.kt")
public void testThreeArgsWithTrailingComma() throws Exception {
runTest("idea/testData/intentions/chop/argumentList/threeArgsWithTrailingComma.kt");
}
}
@TestMetadata("idea/testData/intentions/chop/parameterList")
@@ -3633,6 +3638,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/chop/parameterList/hasSomeLineBreaks1.kt");
}
@TestMetadata("hasSomeLineBreaks1WithTrailingComma.kt")
public void testHasSomeLineBreaks1WithTrailingComma() throws Exception {
runTest("idea/testData/intentions/chop/parameterList/hasSomeLineBreaks1WithTrailingComma.kt");
}
@TestMetadata("hasSomeLineBreaks2.kt")
public void testHasSomeLineBreaks2() throws Exception {
runTest("idea/testData/intentions/chop/parameterList/hasSomeLineBreaks2.kt");
@@ -10001,6 +10011,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/joinArgumentList/hasLineBreaks.kt");
}
@TestMetadata("hasLineBreaksWithTrailingComma.kt")
public void testHasLineBreaksWithTrailingComma() throws Exception {
runTest("idea/testData/intentions/joinArgumentList/hasLineBreaksWithTrailingComma.kt");
}
@TestMetadata("noArg.kt")
public void testNoArg() throws Exception {
runTest("idea/testData/intentions/joinArgumentList/noArg.kt");
@@ -10197,6 +10212,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/joinParameterList/hasLineBreaks.kt");
}
@TestMetadata("hasLineBreaksWithTrailingComma.kt")
public void testHasLineBreaksWithTrailingComma() throws Exception {
runTest("idea/testData/intentions/joinParameterList/hasLineBreaksWithTrailingComma.kt");
}
@TestMetadata("noLineBreak.kt")
public void testNoLineBreak() throws Exception {
runTest("idea/testData/intentions/joinParameterList/noLineBreak.kt");