Add quickfix: remove 'init' modifier
This commit is contained in:
@@ -12,6 +12,7 @@ remove.function.body=Remove function body
|
|||||||
make.element.modifier=Make {0} {1}
|
make.element.modifier=Make {0} {1}
|
||||||
add.modifier=Add ''{0}'' modifier
|
add.modifier=Add ''{0}'' modifier
|
||||||
add.modifier.family=Add Modifier
|
add.modifier.family=Add Modifier
|
||||||
|
add.init.keyword=Add 'init' keyword
|
||||||
make.element.in.classifiers.open=Make ''{0}'' in {1} open
|
make.element.in.classifiers.open=Make ''{0}'' in {1} open
|
||||||
make.class.annotation.class=Make ''{0}'' an annotation class
|
make.class.annotation.class=Make ''{0}'' an annotation class
|
||||||
make.class.annotation.class.family=Make Class an Annotation Class
|
make.class.annotation.class.family=Make Class an Annotation Class
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* 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.quickfix
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import kotlin.platform.*
|
||||||
|
import org.jetbrains.kotlin.diagnostics.*
|
||||||
|
import com.intellij.codeInsight.intention.*
|
||||||
|
import org.jetbrains.kotlin.idea.*
|
||||||
|
import com.intellij.openapi.project.*
|
||||||
|
import com.intellij.openapi.editor.*
|
||||||
|
import com.intellij.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||||
|
|
||||||
|
|
||||||
|
public class AddInitKeywordFix(element: JetClassInitializer) : JetIntentionAction<JetClassInitializer>(element) {
|
||||||
|
override fun getText() = JetBundle.message("add.init.keyword")
|
||||||
|
|
||||||
|
override fun getFamilyName() = JetBundle.message("add.init.keyword")
|
||||||
|
|
||||||
|
override fun invoke(project: Project, editor: Editor?, file: JetFile) {
|
||||||
|
val initializer = element.copy() as JetClassInitializer
|
||||||
|
val aClass = JetPsiFactory(file).createClass("""class A {
|
||||||
|
${initializer.getModifierList()?.getText() ?: ""} init ${initializer.getBody().getText()}
|
||||||
|
}"""
|
||||||
|
)
|
||||||
|
val newInitializers = aClass.getAnonymousInitializers()
|
||||||
|
assert(newInitializers.size() == 1)
|
||||||
|
element.replace(newInitializers[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile?): Boolean {
|
||||||
|
return super.isAvailable(project, editor, file)
|
||||||
|
}
|
||||||
|
|
||||||
|
class object : JetSingleIntentionActionFactory() {
|
||||||
|
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||||
|
return AddInitKeywordFix(diagnostic.getPsiElement().getNonStrictParentOfType<JetClassInitializer>() ?: return null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -298,5 +298,6 @@ public class QuickFixRegistrar {
|
|||||||
|
|
||||||
QuickFixes.factories.put(DEPRECATED_CLASS_OBJECT_SYNTAX, ClassObjectToDefaultObjectFix.Factory);
|
QuickFixes.factories.put(DEPRECATED_CLASS_OBJECT_SYNTAX, ClassObjectToDefaultObjectFix.Factory);
|
||||||
QuickFixes.factories.put(DEPRECATED_CLASS_OBJECT_SYNTAX, ClassObjectToDefaultObjectInWholeProjectFix.Factory);
|
QuickFixes.factories.put(DEPRECATED_CLASS_OBJECT_SYNTAX, ClassObjectToDefaultObjectInWholeProjectFix.Factory);
|
||||||
|
QuickFixes.factories.put(INIT_KEYWORD_BEFORE_CLASS_INITIALIZER_EXPECTED, AddInitKeywordFix.Default);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// "Add 'init' keyword" "true"
|
||||||
|
class A {
|
||||||
|
init {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// "Add 'init' keyword" "true"
|
||||||
|
annotation class Ann1
|
||||||
|
annotation class Ann2
|
||||||
|
class A {
|
||||||
|
Ann1 Ann2 init {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// "Add 'init' keyword" "true"
|
||||||
|
class A {
|
||||||
|
<caret>{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// "Add 'init' keyword" "true"
|
||||||
|
annotation class Ann1
|
||||||
|
annotation class Ann2
|
||||||
|
class A {
|
||||||
|
Ann1 Ann2 <caret>{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2871,6 +2871,18 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("beforeAddInitKeyword.kt")
|
||||||
|
public void testAddInitKeyword() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/beforeAddInitKeyword.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("beforeAddInitKeywordWithModifiers.kt")
|
||||||
|
public void testAddInitKeywordWithModifiers() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/beforeAddInitKeywordWithModifiers.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("beforeAddInnerModifier.kt")
|
@TestMetadata("beforeAddInnerModifier.kt")
|
||||||
public void testAddInnerModifier() throws Exception {
|
public void testAddInnerModifier() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/beforeAddInnerModifier.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/beforeAddInnerModifier.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user