Quick Fixes: Implement 'Add val/var to primary constructor parameter' quick-fix for data classes

#KT-4038 Fixed
This commit is contained in:
Alexey Sedunov
2015-12-23 15:30:37 +03:00
parent 6978d842fb
commit c90d283ff5
13 changed files with 129 additions and 58 deletions
+1 -1
View File
@@ -1116,7 +1116,7 @@
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterIntention</className>
<className>org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterAction$Intention</className>
<category>Kotlin</category>
</intentionAction>
@@ -0,0 +1,90 @@
/*
* 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.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiDocumentManager
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.quickfix.KotlinQuickFixAction
import org.jetbrains.kotlin.idea.quickfix.KotlinSingleIntentionActionFactory
import org.jetbrains.kotlin.idea.refactoring.ValVarExpression
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
interface AddValVarToConstructorParameterAction {
companion object {
val actionFamily = "Add val/var to primary constructor parameter"
}
fun getActionText(element: KtParameter) = "Add val/var to parameter '${element.name ?: ""}'"
fun canInvoke(element: KtParameter): Boolean {
return element.valOrVarKeyword == null && (element.parent as? KtParameterList)?.parent is KtPrimaryConstructor
}
fun invoke(element: KtParameter, editor: Editor?) {
val project = element.project
element.addAfter(KtPsiFactory(project).createValKeyword(), null)
if (editor == null) return
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) }
}
class Intention :
SelfTargetingRangeIntention<KtParameter>(KtParameter::class.java, actionFamily),
AddValVarToConstructorParameterAction {
override fun applicabilityRange(element: KtParameter): TextRange? {
if (!canInvoke(element)) return null
if (element.getStrictParentOfType<KtClass>()?.isData() ?: false) return null
text = getActionText(element)
return element.nameIdentifier?.textRange
}
override fun applyTo(element: KtParameter, editor: Editor) = invoke(element, editor)
}
class QuickFix(parameter: KtParameter) :
KotlinQuickFixAction<KtParameter>(parameter),
AddValVarToConstructorParameterAction {
override fun getText() = getActionText(element)
override fun getFamilyName() = actionFamily
override fun invoke(project: Project, editor: Editor?, file: KtFile) = invoke(element, editor)
}
object QuickFixFactory : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic) = QuickFix(Errors.DATA_CLASS_NOT_PROPERTY_PARAMETER.cast(diagnostic).psiElement)
}
}
@@ -1,56 +0,0 @@
/*
* 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) }
}
}
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.idea.inspections.AddModifierFixFactory
import org.jetbrains.kotlin.idea.inspections.AddReflectionQuickFix
import org.jetbrains.kotlin.idea.inspections.AddTestLibQuickFix
import org.jetbrains.kotlin.idea.inspections.InfixCallFix
import org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterAction
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createCallable.*
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createClass.CreateClassFromCallWithConstructorCalleeActionFactory
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createClass.CreateClassFromConstructorCallActionFactory
@@ -360,5 +361,7 @@ public class QuickFixRegistrar : QuickFixContributor {
COMMA_IN_WHEN_CONDITION_WITHOUT_ARGUMENT.registerFactory(CommaInWhenConditionWithoutArgumentFix)
UNSUPPORTED.registerFactory(UnsupportedAsyncFix)
DATA_CLASS_NOT_PROPERTY_PARAMETER.registerFactory(AddValVarToConstructorParameterAction.QuickFixFactory)
}
}
+1 -1
View File
@@ -1 +1 @@
org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterIntention
org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterAction$Intention
+5
View File
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
// ERROR: Data class primary constructor must have only property (val / var) parameters
data class Foo(<caret>x: Int, val y: Int) {
}
+4
View File
@@ -0,0 +1,4 @@
// "Add val/var to parameter 'x'" "true"
data class Foo(<caret>x: Int, val y: Int) {
}
+4
View File
@@ -0,0 +1,4 @@
// "Add val/var to parameter 'x'" "true"
data class Foo(val<caret> x: Int, val y: Int) {
}
@@ -376,6 +376,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addValOrVar"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("dataClass.kt")
public void testDataClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addValOrVar/dataClass.kt");
doTest(fileName);
}
@TestMetadata("funParameter.kt")
public void testFunParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addValOrVar/funParameter.kt");
@@ -509,6 +509,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/addValVar")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddValVar extends AbstractQuickFixTest {
@TestMetadata("addVal.kt")
public void testAddVal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addValVar/addVal.kt");
doTest(fileName);
}
public void testAllFilesPresentInAddValVar() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/addValVar"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
}
@TestMetadata("idea/testData/quickfix/asyncUnsupported")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)