KT-17970 Intention actions to format parameter/argument list placing each on separate line

#KT-17970 Fixed
This commit is contained in:
Valentin Kipyatkov
2017-04-04 17:10:54 +03:00
parent 47fec6c9d5
commit 92a763552c
28 changed files with 293 additions and 0 deletions
@@ -20,6 +20,7 @@ import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.lexer.KtTokens;
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
@@ -78,4 +79,14 @@ public class KtParameterList extends KtElementImplStub<KotlinPlaceHolderStub<KtP
if (!(parent instanceof KtFunction)) return null;
return (KtFunction) parent;
}
@Nullable
public PsiElement getRightParenthesis() {
return findChildByType(KtTokens.RPAR);
}
@Nullable
public PsiElement getLeftParenthesis() {
return findChildByType(KtTokens.LPAR);
}
}
@@ -0,0 +1,7 @@
fun foo() {
bar(
<spot>1,
"abcd",
false</spot>
)
}
@@ -0,0 +1,3 @@
fun foo() {
bar(<spot>1, "abcd", false</spot>)
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention formats a function call placing each argument on separate line
</body>
</html>
@@ -0,0 +1,7 @@
fun foo(
<spot>param1: String,
param2: Int,
param3: Any</spot>
) {
bar()
}
@@ -0,0 +1,3 @@
fun foo(<spot>param1: String, param2: Int, param3: Any</spot>) {
bar()
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention formats parameter list in a declaration placing each parameter on separate line
</body>
</html>
+10
View File
@@ -1575,6 +1575,16 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ChopParameterListIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ChopArgumentListIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupName="Kotlin"
@@ -0,0 +1,108 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.intentions
import com.intellij.codeInsight.intention.LowPriorityAction
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.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.siblings
import org.jetbrains.kotlin.psi.psiUtil.startOffset
abstract class AbstractChopListIntention<TList : KtElement, TElement : KtElement>(
private val listClass: Class<TList>,
private val elementClass: Class<TElement>,
text: String
) : SelfTargetingOffsetIndependentIntention<TList>(listClass, text), LowPriorityAction {
override fun isApplicableTo(element: TList): Boolean {
val elements = element.elements()
if (elements.size <= 1) return false
if (elements.dropLast(1).all { hasLineBreakAfter(it) }) return false
return true
}
override fun applyTo(list: TList, editor: Editor?) {
val project = list.project
val document = editor!!.document
val startOffset = list.startOffset
val elements = list.elements()
if (!hasLineBreakAfter(elements.last())) {
val rpar = list.allChildren.lastOrNull { it.node.elementType == KtTokens.RPAR }
rpar?.startOffset?.let { document.insertString(it, "\n") }
}
for (element in elements.asReversed()) {
if (!hasLineBreakBefore(element)) {
document.insertString(element.startOffset, "\n")
}
}
val documentManager = PsiDocumentManager.getInstance(project)
documentManager.commitDocument(document)
val psiFile = documentManager.getPsiFile(document)!!
val newList = PsiTreeUtil.getParentOfType(psiFile.findElementAt(startOffset)!!, listClass)!!
CodeStyleManager.getInstance(project).adjustLineIndent(psiFile, newList.textRange)
}
private fun hasLineBreakAfter(element: TElement): Boolean {
return element
.siblings(withItself = false)
.takeWhile { !elementClass.isInstance(it) }
.any { it is PsiWhiteSpace && it.textContains('\n') }
}
private fun hasLineBreakBefore(element: TElement): Boolean {
return element
.siblings(withItself = false, forward = false)
.takeWhile { !elementClass.isInstance(it) }
.any { it is PsiWhiteSpace && it.textContains('\n') }
}
private fun TList.elements(): List<TElement> {
return allChildren
.filter { elementClass.isInstance(it) }
.map {
@Suppress("UNCHECKED_CAST")
it as TElement
}
.toList()
}
}
class ChopParameterListIntention : AbstractChopListIntention<KtParameterList, KtParameter>(
KtParameterList::class.java,
KtParameter::class.java,
"Put parameters on separate lines"
) {
override fun isApplicableTo(element: KtParameterList): Boolean {
if (element.parent is KtFunctionLiteral) return false
return super.isApplicableTo(element)
}
}
class ChopArgumentListIntention : AbstractChopListIntention<KtValueArgumentList, KtValueArgument>(
KtValueArgumentList::class.java,
KtValueArgument::class.java,
"Put arguments on separate lines"
)
+1
View File
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ChopArgumentListIntention
@@ -0,0 +1,5 @@
fun f() {
foo(<caret>1, "a", 2)
}
fun foo(p1: Int, p2: String, p3: Int){}
@@ -0,0 +1,9 @@
fun f() {
foo(
1,
"a",
2
)
}
fun foo(p1: Int, p2: String, p3: Int){}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ChopParameterListIntention
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
fun foo(
<caret>c: Char,
b: Boolean
) {
}
@@ -0,0 +1,3 @@
fun foo(p: Int, c: Char,
b: <caret>Boolean) {
}
@@ -0,0 +1,6 @@
fun foo(
p: Int,
c: Char,
b: <caret>Boolean
) {
}
@@ -0,0 +1,4 @@
fun foo(
p: Int, c: Char, b: <caret>Boolean
) {
}
@@ -0,0 +1,6 @@
fun foo(
p: Int,
c: Char,
b: <caret>Boolean
) {
}
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
fun foo(<caret>c: Char) {
}
@@ -0,0 +1,2 @@
fun foo(p: Int, c: Char, b: <caret>Boolean) {
}
@@ -0,0 +1,6 @@
fun foo(
p: Int,
c: Char,
b: <caret>Boolean
) {
}
@@ -0,0 +1,2 @@
fun foo(p: Int, <caret>c: Char) {
}
@@ -0,0 +1,5 @@
fun foo(
p: Int,
<caret> c: Char
) {
}
@@ -3,6 +3,7 @@
// ACTION: Add parameter to constructor 'Foo'
// ACTION: Create secondary constructor
// ERROR: Too many arguments for public constructor Foo(a: Int) defined in Foo
// ACTION: Put arguments on separate lines
// ACTION: To raw string literal
class Foo(a: Int)
@@ -3,6 +3,7 @@
// ACTION: Make internal
// ACTION: Make private
// ACTION: Rename reference
// ACTION: Put arguments on separate lines
// ACTION: Convert to expression body
// ERROR: Unresolved reference: foo
// ERROR: Unresolved reference: bar
@@ -3,6 +3,7 @@
// ACTION: Convert parameter to receiver
// ACTION: Rename to _
// ACTION: Specify return type explicitly
// ACTION: Put parameters on separate lines
fun foo(block: (String, Int) -> Unit) {
block("", 1)
@@ -4,6 +4,7 @@
// ACTION: Create function 'join'
// ACTION: Flip ','
// ACTION: Introduce local variable
// ACTION: Put arguments on separate lines
//this test checks that there is no ArrayIndexOutOfBoundsException when there are more arguments than parameters
fun <T> array1(vararg a : T) = a
@@ -3144,6 +3144,75 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/chop")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Chop extends AbstractIntentionTest {
public void testAllFilesPresentInChop() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/chop"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("idea/testData/intentions/chop/argumentList")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ArgumentList extends AbstractIntentionTest {
public void testAllFilesPresentInArgumentList() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/chop/argumentList"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("threeArgs.kt")
public void testThreeArgs() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/chop/argumentList/threeArgs.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/chop/parameterList")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ParameterList extends AbstractIntentionTest {
public void testAllFilesPresentInParameterList() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/chop/parameterList"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("hasAllLineBreaks.kt")
public void testHasAllLineBreaks() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/chop/parameterList/hasAllLineBreaks.kt");
doTest(fileName);
}
@TestMetadata("hasSomeLineBreaks1.kt")
public void testHasSomeLineBreaks1() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/chop/parameterList/hasSomeLineBreaks1.kt");
doTest(fileName);
}
@TestMetadata("hasSomeLineBreaks2.kt")
public void testHasSomeLineBreaks2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/chop/parameterList/hasSomeLineBreaks2.kt");
doTest(fileName);
}
@TestMetadata("oneParameter.kt")
public void testOneParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/chop/parameterList/oneParameter.kt");
doTest(fileName);
}
@TestMetadata("threeParameters.kt")
public void testThreeParameters() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/chop/parameterList/threeParameters.kt");
doTest(fileName);
}
@TestMetadata("twoParameters.kt")
public void testTwoParameters() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/chop/parameterList/twoParameters.kt");
doTest(fileName);
}
}
}
@TestMetadata("idea/testData/intentions/conventionNameCalls")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)