Intentions: Implement 'Add val/var to primary constructor parameter' intention
#KT-4038 In Progress
This commit is contained in:
+1
@@ -0,0 +1 @@
|
||||
class Foo(n: Int, <spot>val s</spot>: String)
|
||||
+1
@@ -0,0 +1 @@
|
||||
class Foo(n: Int, <spot>s</spot>: String)
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention adds val/var keyword to the primary constructor parameter effectively making it a property.
|
||||
</body>
|
||||
</html>
|
||||
@@ -1115,6 +1115,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
|
||||
displayName="Convert object literal to lambda"
|
||||
groupName="Kotlin"
|
||||
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.template.TemplateBuilderImpl
|
||||
import com.intellij.codeInsight.template.TemplateManager
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import org.jetbrains.kotlin.idea.refactoring.ValVarExpression
|
||||
import org.jetbrains.kotlin.psi.KtParameter
|
||||
import org.jetbrains.kotlin.psi.KtParameterList
|
||||
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
|
||||
|
||||
class AddValVarToConstructorParameterIntention : SelfTargetingRangeIntention<KtParameter>(KtParameter::class.java,
|
||||
"Add val/var to primary constructor parameter") {
|
||||
override fun applicabilityRange(element: KtParameter): TextRange? {
|
||||
if (element.valOrVarKeyword != null) return null
|
||||
if ((element.parent as? KtParameterList)?.parent !is KtPrimaryConstructor) return null
|
||||
text = "Add val/var to parameter '${element.name ?: ""}'"
|
||||
return element.nameIdentifier?.textRange
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtParameter, editor: Editor) {
|
||||
val project = element.project
|
||||
|
||||
element.addAfter(KtPsiFactory(project).createValKeyword(), null)
|
||||
|
||||
val parameter = element.createSmartPointer().let {
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments()
|
||||
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.document)
|
||||
it.element
|
||||
} ?: return
|
||||
|
||||
TemplateBuilderImpl(parameter)
|
||||
.apply { replaceElement(parameter.valOrVarKeyword!!, ValVarExpression) }
|
||||
.buildInlineTemplate()
|
||||
.let { TemplateManager.getInstance(project).startTemplate(editor, it) }
|
||||
}
|
||||
}
|
||||
-10
@@ -184,13 +184,3 @@ internal class TypeParameterListExpression(private val mandatoryTypeParameters:
|
||||
// do not offer the user any choices
|
||||
override fun calculateLookupItems(context: ExpressionContext?) = arrayOf<LookupElement>()
|
||||
}
|
||||
|
||||
internal object ValVarExpression: Expression() {
|
||||
private val cachedLookupElements = listOf("val", "var").map { LookupElementBuilder.create(it) }.toTypedArray<LookupElement>()
|
||||
|
||||
override fun calculateResult(context: ExpressionContext?): Result? = TextResult("val")
|
||||
|
||||
override fun calculateQuickResult(context: ExpressionContext?): Result? = calculateResult(context)
|
||||
|
||||
override fun calculateLookupItems(context: ExpressionContext?): Array<LookupElement>? = cachedLookupElements
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.refactoring
|
||||
|
||||
import com.intellij.codeInsight.lookup.LookupElement
|
||||
import com.intellij.codeInsight.lookup.LookupElementBuilder
|
||||
import com.intellij.codeInsight.template.Expression
|
||||
import com.intellij.codeInsight.template.ExpressionContext
|
||||
import com.intellij.codeInsight.template.Result
|
||||
import com.intellij.codeInsight.template.TextResult
|
||||
|
||||
object ValVarExpression: Expression() {
|
||||
private val cachedLookupElements = listOf("val", "var").map { LookupElementBuilder.create(it) }.toTypedArray<LookupElement>()
|
||||
|
||||
override fun calculateResult(context: ExpressionContext?): Result? = TextResult("val")
|
||||
|
||||
override fun calculateQuickResult(context: ExpressionContext?): Result? = calculateResult(context)
|
||||
|
||||
override fun calculateLookupItems(context: ExpressionContext?): Array<LookupElement>? = cachedLookupElements
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterIntention
|
||||
@@ -0,0 +1,3 @@
|
||||
class Foo(<caret>x: Int, y: Int) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class Foo(val<caret> x: Int, y: Int) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(<caret>x: Int, y: Int) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
class Foo(val <caret>x: Int, y: Int) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
class Foo(var <caret>x: Int, y: Int) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
class Foo(x: <caret>Int, y: Int) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
class Foo {
|
||||
constructor(<caret>x: Int, y: Int)
|
||||
}
|
||||
@@ -362,6 +362,51 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/addValOrVar")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class AddValOrVar extends AbstractIntentionTest {
|
||||
@TestMetadata("addVal.kt")
|
||||
public void testAddVal() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addValOrVar/addVal.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInAddValOrVar() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addValOrVar"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("funParameter.kt")
|
||||
public void testFunParameter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addValOrVar/funParameter.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("hasVal.kt")
|
||||
public void testHasVal() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addValOrVar/hasVal.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("hasVar.kt")
|
||||
public void testHasVar() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addValOrVar/hasVar.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("outOfRange.kt")
|
||||
public void testOutOfRange() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addValOrVar/outOfRange.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("secondaryConstructorParameter.kt")
|
||||
public void testSecondaryConstructorParameter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addValOrVar/secondaryConstructorParameter.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/anonymousFunctionToLambda")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user