report 'trait' keyword as deprecated, provide quickfix for replacing with 'interface'
This commit is contained in:
@@ -152,7 +152,8 @@ public open class JetPsiChecker : Annotator, HighlightRangeExtension {
|
||||
|
||||
when (factory) {
|
||||
Errors.DEPRECATED_SYMBOL,
|
||||
Errors.DEPRECATED_SYMBOL_WITH_MESSAGE
|
||||
Errors.DEPRECATED_SYMBOL_WITH_MESSAGE,
|
||||
Errors.DEPRECATED_TRAIT_KEYWORD
|
||||
-> annotation.setTextAttributes(CodeInsightColors.DEPRECATED_ATTRIBUTES)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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 com.intellij.codeInsight.intention.IntentionAction
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.createIntentionFactory
|
||||
import org.jetbrains.kotlin.lexer.JetTokens
|
||||
import org.jetbrains.kotlin.psi.JetClass
|
||||
import org.jetbrains.kotlin.psi.JetFile
|
||||
import org.jetbrains.kotlin.psi.JetPsiFactory
|
||||
|
||||
public class DeprecatedTraitSyntaxFix(element: PsiElement): JetIntentionAction<PsiElement>(element) {
|
||||
override fun getFamilyName() = "Replace with 'interface'"
|
||||
override fun getText() = getFamilyName()
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: JetFile?)
|
||||
= replaceWithInterfaceKeyword(element)
|
||||
|
||||
|
||||
companion object : JetSingleIntentionActionFactory() {
|
||||
fun replaceWithInterfaceKeyword(element: PsiElement) {
|
||||
val cls = JetPsiFactory(element.getProject()).createClass("interface A {}")
|
||||
element.replace(cls.getNode().findChildByType(JetTokens.INTERFACE_KEYWORD)!!.getPsi())
|
||||
}
|
||||
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction =
|
||||
DeprecatedTraitSyntaxFix(diagnostic.getPsiElement())
|
||||
|
||||
public fun createWholeProjectFixFactory(): JetSingleIntentionActionFactory = createIntentionFactory {
|
||||
JetWholeProjectForEachElementOfTypeFix.createByPredicate<JetClass>(
|
||||
predicate = { it.getNode().findChildByType(JetTokens.TRAIT_KEYWORD) != null },
|
||||
taskProcessor = { replaceWithInterfaceKeyword(it.getNode().findChildByType(JetTokens.TRAIT_KEYWORD).getPsi())},
|
||||
modalTitle = "Replacing deprecated trait syntax",
|
||||
name = "Replace with 'interface' in whole project",
|
||||
familyName = "Replace with 'interface' in whole project"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,6 +105,9 @@ public class QuickFixRegistrar {
|
||||
true));
|
||||
QuickFixes.factories.put(OPEN_MODIFIER_IN_TRAIT, RemoveModifierFix.createRemoveModifierFromListOwnerFactory(OPEN_KEYWORD, true));
|
||||
QuickFixes.factories.put(TRAIT_CAN_NOT_BE_FINAL, removeFinalModifierFactory);
|
||||
QuickFixes.factories.put(DEPRECATED_TRAIT_KEYWORD, DeprecatedTraitSyntaxFix.Companion);
|
||||
QuickFixes.factories.put(DEPRECATED_TRAIT_KEYWORD, DeprecatedTraitSyntaxFix.Companion.createWholeProjectFixFactory());
|
||||
|
||||
QuickFixes.factories.put(REDUNDANT_PROJECTION, RemoveModifierFix.createRemoveProjectionFactory(true));
|
||||
QuickFixes.factories.put(INCOMPATIBLE_MODIFIERS, RemoveModifierFix.createRemoveModifierFactory(false));
|
||||
QuickFixes.factories.put(VARIANCE_ON_TYPE_PARAMETER_OF_FUNCTION_OR_PROPERTY, RemoveModifierFix.createRemoveVarianceFactory());
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Replace with 'interface'" "true"
|
||||
|
||||
<caret>trait Foo {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Replace with 'interface'" "true"
|
||||
|
||||
<caret>interface Foo {
|
||||
|
||||
}
|
||||
@@ -974,6 +974,7 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
||||
doTestWithExtraFile(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/modifiers")
|
||||
|
||||
@@ -3277,6 +3277,20 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/migration/traitToInterface")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class TraitToInterface extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInTraitToInterface() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration/traitToInterface"), Pattern.compile("^(\\w+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("traitToInterface.kt")
|
||||
public void testTraitToInterface() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/traitToInterface/traitToInterface.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/modifiers")
|
||||
|
||||
Reference in New Issue
Block a user