QuickFix for NOT_AN_ANNOTATION_CLASS error.
This commit is contained in:
committed by
Andrey Breslav
parent
8fad23691f
commit
c3d5350315
@@ -13,6 +13,8 @@ make.element.modifier=Make {0} {1}
|
||||
add.modifier=Add ''{0}'' modifier
|
||||
add.supertype.modifier=Add ''{0}'' modifier to supertype
|
||||
add.supertype.modifier.family=Add modifier to supertype
|
||||
make.class.annotation.class=Make ''{0}'' an annotation class
|
||||
make.class.annotation.class.family=Make class an annotation class
|
||||
add.star.projections=Add ''{0}''
|
||||
make.element.not.modifier=Make {0} not {1}
|
||||
remove.modifier=Remove ''{0}'' modifier
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright 2010-2013 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.jet.plugin.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 com.intellij.psi.PsiFile;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
||||
import org.jetbrains.jet.lang.psi.JetAnnotationEntry;
|
||||
import org.jetbrains.jet.lang.psi.JetClass;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.plugin.JetBundle;
|
||||
import org.jetbrains.jet.plugin.caches.resolve.KotlinCacheManager;
|
||||
|
||||
import static org.jetbrains.jet.lexer.JetTokens.ANNOTATION_KEYWORD;
|
||||
|
||||
public class MakeClassAnAnnotationClassFix extends JetIntentionAction<JetAnnotationEntry> {
|
||||
private final JetAnnotationEntry annotationEntry;
|
||||
private JetType annotationType;
|
||||
private JetClass annotationClass;
|
||||
|
||||
public MakeClassAnAnnotationClassFix(@NotNull JetAnnotationEntry annotationEntry) {
|
||||
super(annotationEntry);
|
||||
this.annotationEntry = annotationEntry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
if (!super.isAvailable(project, editor, file)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
BindingContext context = KotlinCacheManager.getInstance(project).getDeclarationsFromProject(project).getBindingContext();
|
||||
AnnotationDescriptor annotationDescriptor = context.get(BindingContext.ANNOTATION, annotationEntry);
|
||||
if (annotationDescriptor == null) {
|
||||
return false;
|
||||
}
|
||||
annotationType = annotationDescriptor.getType();
|
||||
DeclarationDescriptor declarationDescriptor = annotationType.getConstructor().getDeclarationDescriptor();
|
||||
if (declarationDescriptor == null) {
|
||||
return false;
|
||||
}
|
||||
PsiElement declaration = BindingContextUtils.descriptorToDeclaration(context, declarationDescriptor);
|
||||
if (declaration instanceof JetClass) {
|
||||
annotationClass = (JetClass) declaration;
|
||||
return annotationClass.isWritable();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return JetBundle.message("make.class.annotation.class", annotationType);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return JetBundle.message("make.class.annotation.class.family");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
annotationClass.replace(AddModifierFix.addModifier(annotationClass, ANNOTATION_KEYWORD, null, project, false));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JetIntentionActionFactory createFactory() {
|
||||
return new JetIntentionActionFactory() {
|
||||
@Nullable
|
||||
@Override
|
||||
public IntentionAction createAction(Diagnostic diagnostic) {
|
||||
JetAnnotationEntry annotation = QuickFixUtil.getParentElementOfType(diagnostic, JetAnnotationEntry.class);
|
||||
return annotation == null ? null : new MakeClassAnAnnotationClassFix(annotation);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,5 +176,7 @@ public class QuickFixes {
|
||||
|
||||
factories.put(OPEN_MODIFIER_IN_ENUM, RemoveModifierFix.createRemoveModifierFromListOwnerFactory(OPEN_KEYWORD));
|
||||
factories.put(ILLEGAL_ENUM_ANNOTATION, RemoveModifierFix.createRemoveModifierFromListOwnerFactory(ENUM_KEYWORD));
|
||||
|
||||
factories.put(NOT_AN_ANNOTATION_CLASS, MakeClassAnAnnotationClassFix.createFactory());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Make 'fancy' an annotation class" "true"
|
||||
annotation class fancy
|
||||
|
||||
[fancy] class foo {}
|
||||
@@ -0,0 +1,3 @@
|
||||
// "Make 'String' an annotation class" "false"
|
||||
// ERROR: 'String' is not an annotation class
|
||||
[String<caret>] class foo {}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Make 'fancy' an annotation class" "true"
|
||||
class fancy
|
||||
|
||||
[fancy<caret>] class foo {}
|
||||
@@ -403,6 +403,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/modifiers"), Pattern.compile("^before(\\w+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeCannotMakeClassAnnotation.kt")
|
||||
public void testCannotMakeClassAnnotation() throws Exception {
|
||||
doTest("idea/testData/quickfix/modifiers/beforeCannotMakeClassAnnotation.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("beforeFinalTrait.kt")
|
||||
public void testFinalTrait() throws Exception {
|
||||
doTest("idea/testData/quickfix/modifiers/beforeFinalTrait.kt");
|
||||
@@ -418,6 +423,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest("idea/testData/quickfix/modifiers/beforeIllegalEnumAnnotation2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("beforeNotAnAnnotationClass.kt")
|
||||
public void testNotAnAnnotationClass() throws Exception {
|
||||
doTest("idea/testData/quickfix/modifiers/beforeNotAnAnnotationClass.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("beforeOpenMemberInFinalClass1.kt")
|
||||
public void testOpenMemberInFinalClass1() throws Exception {
|
||||
doTest("idea/testData/quickfix/modifiers/beforeOpenMemberInFinalClass1.kt");
|
||||
|
||||
Reference in New Issue
Block a user