report 'trait' keyword as deprecated, provide quickfix for replacing with 'interface'

This commit is contained in:
Dmitry Jemerov
2015-05-13 19:29:15 +02:00
parent 3dce96e01c
commit dc9523016a
13 changed files with 109 additions and 1 deletions
@@ -142,6 +142,7 @@ public interface Errors {
DiagnosticFactory0<JetDelegatorToSuperClass> SUPERTYPE_NOT_INITIALIZED = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> DEPRECATED_TRAIT_KEYWORD = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<JetTypeReference> DELEGATION_NOT_TO_TRAIT = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetTypeReference> SUPERTYPE_NOT_A_CLASS_OR_TRAIT = DiagnosticFactory0.create(ERROR);
@@ -262,6 +262,7 @@ public class DefaultErrorMessages {
MAP.put(ENUM_ENTRY_AFTER_ENUM_MEMBER, "Enum entry ''{0}'' is not allowed after a member", NAME);
MAP.put(DELEGATION_IN_TRAIT, "Interfaces cannot use delegation");
MAP.put(DELEGATION_NOT_TO_TRAIT, "Only interfaces can be delegated to");
MAP.put(DEPRECATED_TRAIT_KEYWORD, "'trait' keyword is deprecated, use 'interface' instead");
MAP.put(UNMET_TRAIT_REQUIREMENT, "Super interface ''{0}'' requires subclasses to extend ''{1}''", NAME, NAME);
MAP.put(NO_CONSTRUCTOR, "This class does not have a constructor");
MAP.put(NOT_A_CLASS, "Not a class");
@@ -247,6 +247,11 @@ public class DeclarationsChecker {
checkTypeParameters(aClass);
if (aClass.isInterface()) {
ASTNode traitKeyword = aClass.getNode().findChildByType(JetTokens.TRAIT_KEYWORD);
if (traitKeyword != null) {
trace.report(Errors.DEPRECATED_TRAIT_KEYWORD.on(traitKeyword.getPsi()));
}
checkTraitModifiers(aClass);
checkConstructorInTrait(aClass);
}
@@ -0,0 +1,2 @@
<!DEPRECATED_TRAIT_KEYWORD!>trait<!> A {
}
@@ -0,0 +1,7 @@
package
internal interface A {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -181,6 +181,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("DeprecatedTraitKeyword.kt")
public void testDeprecatedTraitKeyword() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/DeprecatedTraitKeyword.kt");
doTest(fileName);
}
@TestMetadata("DiamondFunction.kt")
public void testDiamondFunction() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/DiamondFunction.kt");
@@ -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")