"Put arguments/parameters on separate lines": respect code style settings

#KT-20420 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-11-29 10:09:53 +09:00
committed by Yan Zhulanow
parent 74ee68e57b
commit a492fe7757
22 changed files with 161 additions and 3 deletions
@@ -5,11 +5,14 @@
package org.jetbrains.kotlin.idea.intentions
import com.intellij.application.options.CodeStyle
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.codeStyle.CodeStyleManager
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.formatter.kotlinCommonSettings
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.allChildren
@@ -22,6 +25,11 @@ abstract class AbstractChopListIntention<TList : KtElement, TElement : KtElement
private val elementClass: Class<TElement>,
textGetter: () -> String
) : SelfTargetingOffsetIndependentIntention<TList>(listClass, textGetter) {
open fun leftParOnNewLine(commonCodeStyleSettings: CommonCodeStyleSettings): Boolean = false
open fun rightParOnNewLine(commonCodeStyleSettings: CommonCodeStyleSettings): Boolean = false
override fun isApplicableTo(element: TList): Boolean {
val elements = element.elements()
if (elements.size <= 1) return false
@@ -34,12 +42,18 @@ abstract class AbstractChopListIntention<TList : KtElement, TElement : KtElement
val document = editor?.document ?: return
val pointer = element.createSmartPointer()
val commonCodeStyleSettings = CodeStyle.getSettings(project).kotlinCommonSettings
val leftParOnNewLine = leftParOnNewLine(commonCodeStyleSettings)
val rightParOnNewLine = rightParOnNewLine(commonCodeStyleSettings)
val elements = element.elements()
if (!hasLineBreakAfter(elements.last())) {
if (rightParOnNewLine && !hasLineBreakAfter(elements.last())) {
element.allChildren.lastOrNull { it.node.elementType == KtTokens.RPAR }?.startOffset?.let { document.insertString(it, "\n") }
}
for (e in elements.asReversed()) {
val maxIndex = elements.size - 1
for ((index, e) in elements.asReversed().withIndex()) {
if (index == maxIndex && !leftParOnNewLine) break
if (!hasLineBreakBefore(e)) {
document.insertString(e.startOffset, "\n")
}
@@ -79,10 +93,26 @@ class ChopParameterListIntention : AbstractChopListIntention<KtParameterList, Kt
if (element.parent is KtFunctionLiteral) return false
return super.isApplicableTo(element)
}
override fun leftParOnNewLine(commonCodeStyleSettings: CommonCodeStyleSettings): Boolean {
return commonCodeStyleSettings.METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE
}
override fun rightParOnNewLine(commonCodeStyleSettings: CommonCodeStyleSettings): Boolean {
return commonCodeStyleSettings.METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE
}
}
class ChopArgumentListIntention : AbstractChopListIntention<KtValueArgumentList, KtValueArgument>(
KtValueArgumentList::class.java,
KtValueArgument::class.java,
KotlinBundle.lazyMessage("put.arguments.on.separate.lines")
)
) {
override fun leftParOnNewLine(commonCodeStyleSettings: CommonCodeStyleSettings): Boolean {
return commonCodeStyleSettings.CALL_PARAMETERS_LPAREN_ON_NEXT_LINE
}
override fun rightParOnNewLine(commonCodeStyleSettings: CommonCodeStyleSettings): Boolean {
return commonCodeStyleSettings.CALL_PARAMETERS_RPAREN_ON_NEXT_LINE
}
}
@@ -0,0 +1,7 @@
// SET_FALSE: CALL_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_TRUE: CALL_PARAMETERS_RPAREN_ON_NEXT_LINE
fun f() {
foo(<caret>1, "a", 2)
}
fun foo(p1: Int, p2: String, p3: Int){}
@@ -0,0 +1,10 @@
// SET_FALSE: CALL_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_TRUE: CALL_PARAMETERS_RPAREN_ON_NEXT_LINE
fun f() {
foo(1,
"a",
2
)
}
fun foo(p1: Int, p2: String, p3: Int){}
@@ -0,0 +1,7 @@
// SET_FALSE: CALL_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_FALSE: CALL_PARAMETERS_RPAREN_ON_NEXT_LINE
fun f() {
foo(<caret>1, "a", 2)
}
fun foo(p1: Int, p2: String, p3: Int){}
@@ -0,0 +1,9 @@
// SET_FALSE: CALL_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_FALSE: CALL_PARAMETERS_RPAREN_ON_NEXT_LINE
fun f() {
foo(1,
"a",
2)
}
fun foo(p1: Int, p2: String, p3: Int){}
@@ -0,0 +1,7 @@
// SET_TRUE: CALL_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_FALSE: CALL_PARAMETERS_RPAREN_ON_NEXT_LINE
fun f() {
foo(<caret>1, "a", 2)
}
fun foo(p1: Int, p2: String, p3: Int){}
@@ -0,0 +1,10 @@
// SET_TRUE: CALL_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_FALSE: CALL_PARAMETERS_RPAREN_ON_NEXT_LINE
fun f() {
foo(
1,
"a",
2)
}
fun foo(p1: Int, p2: String, p3: Int){}
@@ -1,3 +1,5 @@
// SET_TRUE: CALL_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_TRUE: CALL_PARAMETERS_RPAREN_ON_NEXT_LINE
fun f() {
foo(<caret>1, "a", 2)
}
@@ -1,3 +1,5 @@
// SET_TRUE: CALL_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_TRUE: CALL_PARAMETERS_RPAREN_ON_NEXT_LINE
fun f() {
foo(
1,
@@ -1,3 +1,5 @@
// SET_TRUE: METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_TRUE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE
fun foo(p: Int, c: Char,
b: <caret>Boolean) {
}
@@ -1,3 +1,5 @@
// SET_TRUE: METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_TRUE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE
fun foo(
p: Int,
c: Char,
@@ -0,0 +1,4 @@
// SET_FALSE: METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_TRUE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE
fun foo(p: Int, c: Char, b: <caret>Boolean) {
}
@@ -0,0 +1,7 @@
// SET_FALSE: METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_TRUE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE
fun foo(p: Int,
c: Char,
b: Boolean
) {
}
@@ -0,0 +1,4 @@
// SET_FALSE: METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_FALSE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE
fun foo(p: Int, c: Char, b: <caret>Boolean) {
}
@@ -0,0 +1,6 @@
// SET_FALSE: METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_FALSE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE
fun foo(p: Int,
c: Char,
b: Boolean) {
}
@@ -0,0 +1,4 @@
// SET_TRUE: METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_FALSE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE
fun foo(p: Int, c: Char, b: <caret>Boolean) {
}
@@ -0,0 +1,7 @@
// SET_TRUE: METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_FALSE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE
fun foo(
p: Int,
c: Char,
b: Boolean) {
}
@@ -1,2 +1,4 @@
// SET_TRUE: METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_TRUE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE
fun foo(p: Int, c: Char, b: <caret>Boolean) {
}
@@ -1,3 +1,5 @@
// SET_TRUE: METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_TRUE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE
fun foo(
p: Int,
c: Char,
@@ -1,2 +1,4 @@
// SET_TRUE: METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_TRUE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE
fun foo(p: Int, <caret>c: Char) {
}
@@ -1,3 +1,5 @@
// SET_TRUE: METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE
// SET_TRUE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE
fun foo(
p: Int,
<caret> c: Char
@@ -3743,6 +3743,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/intentions/chop/argumentList"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true);
}
@TestMetadata("leftParOnSameLine.kt")
public void testLeftParOnSameLine() throws Exception {
runTest("idea/testData/intentions/chop/argumentList/leftParOnSameLine.kt");
}
@TestMetadata("parOnSameLine.kt")
public void testParOnSameLine() throws Exception {
runTest("idea/testData/intentions/chop/argumentList/parOnSameLine.kt");
}
@TestMetadata("rightParOnSameLine.kt")
public void testRightParOnSameLine() throws Exception {
runTest("idea/testData/intentions/chop/argumentList/rightParOnSameLine.kt");
}
@TestMetadata("threeArgs.kt")
public void testThreeArgs() throws Exception {
runTest("idea/testData/intentions/chop/argumentList/threeArgs.kt");
@@ -3786,11 +3801,26 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/chop/parameterList/hasSomeLineBreaks2.kt");
}
@TestMetadata("leftParOnSameLine.kt")
public void testLeftParOnSameLine() throws Exception {
runTest("idea/testData/intentions/chop/parameterList/leftParOnSameLine.kt");
}
@TestMetadata("oneParameter.kt")
public void testOneParameter() throws Exception {
runTest("idea/testData/intentions/chop/parameterList/oneParameter.kt");
}
@TestMetadata("parOnSameLine.kt")
public void testParOnSameLine() throws Exception {
runTest("idea/testData/intentions/chop/parameterList/parOnSameLine.kt");
}
@TestMetadata("rightParOnSameLine.kt")
public void testRightParOnSameLine() throws Exception {
runTest("idea/testData/intentions/chop/parameterList/rightParOnSameLine.kt");
}
@TestMetadata("threeParameters.kt")
public void testThreeParameters() throws Exception {
runTest("idea/testData/intentions/chop/parameterList/threeParameters.kt");