IDEA-25183

This commit is contained in:
Dmitry Batkovich
2014-05-28 17:46:34 +04:00
parent 80eba2aaf9
commit 80a7f69972
17 changed files with 418 additions and 1 deletions
@@ -28,6 +28,7 @@ import com.intellij.psi.util.PropertyMemberType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.List;
/**
@@ -251,4 +252,9 @@ public abstract class QuickFixFactory {
@Nullable
public abstract List<LocalQuickFix> registerOrderEntryFixes(@NotNull QuickFixActionRegistrar registrar,
@NotNull PsiReference reference);
@NotNull
public abstract IntentionAction createAddMissingRequiredAnnotationParametersFix(@NotNull PsiAnnotation annotation,
@NotNull PsiMethod[] annotationMethods,
@NotNull Collection<String> missedElements);
}
@@ -294,7 +294,11 @@ public class AnnotationsHighlightUtil {
}
String description = JavaErrorMessages.message("annotation.missing.attribute", buff);
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(nameRef).descriptionAndTooltip(description).create();
HighlightInfo info =
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(nameRef).descriptionAndTooltip(description).create();
QuickFixAction.registerQuickFixAction(info, QuickFixFactory.getInstance().createAddMissingRequiredAnnotationParametersFix(
annotation, annotationMethods, missed));
return info;
}
}
@@ -0,0 +1,163 @@
/*
* Copyright 2000-2014 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 com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.template.TemplateBuilderImpl;
import com.intellij.codeInsight.template.TemplateManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.HashSet;
import gnu.trove.TObjectIntHashMap;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* @author Dmitry Batkovich
*/
public class AddMissingRequiredAnnotationParametersFix implements IntentionAction {
private static final Logger LOG = Logger.getInstance(AddMissingRequiredAnnotationParametersFix.class);
private final PsiAnnotation myAnnotation;
private final PsiMethod[] myAnnotationMethods;
private final Collection<String> myMissedElements;
public AddMissingRequiredAnnotationParametersFix(final PsiAnnotation annotation,
final PsiMethod[] annotationMethods,
final Collection<String> missedElements) {
if (missedElements.isEmpty()) {
throw new IllegalArgumentException("missedElements can't be empty");
}
myAnnotation = annotation;
myAnnotationMethods = annotationMethods;
myMissedElements = missedElements;
}
@NotNull
@Override
public String getText() {
return myMissedElements.size() == 1
? QuickFixBundle.message("add.missing.annotation.single.parameter.fix", ContainerUtil.getFirstItem(myMissedElements))
: QuickFixBundle.message("add.missing.annotation.parameters.fix", StringUtil.join(myMissedElements, ", "));
}
@NotNull
@Override
public String getFamilyName() {
return QuickFixBundle.message("annotations.fix");
}
@Override
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
return true;
}
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
final PsiNameValuePair[] addedParameters = myAnnotation.getParameterList().getAttributes();
final TObjectIntHashMap<String> annotationsOrderMap = getAnnotationsOrderMap();
final SortedSet<Pair<String, PsiAnnotationMemberValue>>
newParameters = new TreeSet<Pair<String, PsiAnnotationMemberValue>>(new Comparator<Pair<String, PsiAnnotationMemberValue>>() {
@Override
public int compare(final Pair<String, PsiAnnotationMemberValue> o1, final Pair<String, PsiAnnotationMemberValue> o2) {
return annotationsOrderMap.get(o1.getFirst()) - annotationsOrderMap.get(o2.getFirst());
}
});
final boolean order = isAlreadyAddedOrdered(annotationsOrderMap, addedParameters);
if (order) {
if (addedParameters.length != 0) {
final PsiAnnotationParameterList parameterList = myAnnotation.getParameterList();
parameterList.deleteChildRange(addedParameters[0], addedParameters[addedParameters.length - 1]);
for (final PsiNameValuePair addedParameter : addedParameters) {
final String name = addedParameter.getName();
final PsiAnnotationMemberValue value = addedParameter.getValue();
if (name == null || value == null) {
LOG.error(String.format("Invalid annotation parameter name = %s, value = %s", name, value));
continue;
}
newParameters.add(Pair.create(name, value));
}
}
}
final PsiExpression nullValue = JavaPsiFacade.getElementFactory(myAnnotation.getProject()).createExpressionFromText(PsiKeyword.NULL, null);
for (final String misssedParameter : myMissedElements) {
newParameters.add(Pair.<String, PsiAnnotationMemberValue>create(misssedParameter, nullValue));
}
TemplateBuilderImpl builder = null;
for (final Pair<String, PsiAnnotationMemberValue> newParameter : newParameters) {
final PsiAnnotationMemberValue value =
myAnnotation.setDeclaredAttributeValue(newParameter.getFirst(), newParameter.getSecond());
if (myMissedElements.contains(newParameter.getFirst())) {
if (builder == null) {
builder = new TemplateBuilderImpl(myAnnotation.getParameterList());
}
builder.replaceElement(value, new EmptyExpression(), true);
}
}
editor.getCaretModel().moveToOffset(myAnnotation.getParameterList().getTextRange().getStartOffset());
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final Document document = documentManager.getDocument(file);
if (document == null) {
throw new IllegalStateException();
}
documentManager.doPostponedOperationsAndUnblockDocument(document);
TemplateManager.getInstance(project).startTemplate(editor, builder.buildInlineTemplate(), null);
}
@Override
public boolean startInWriteAction() {
return true;
}
private TObjectIntHashMap<String> getAnnotationsOrderMap() {
final TObjectIntHashMap<String> map = new TObjectIntHashMap<String>();
for (int i = 0; i < myAnnotationMethods.length; i++) {
map.put(myAnnotationMethods[i].getName(), i);
}
return map;
}
private static boolean isAlreadyAddedOrdered(final TObjectIntHashMap<String> orderMap, final PsiNameValuePair[] addedParameters) {
if (addedParameters.length <= 1) {
return true;
}
int previousOrder = orderMap.get(addedParameters[0].getName());
for (int i = 1; i < addedParameters.length; i++) {
final int currentOrder = orderMap.get(addedParameters[i].getName());
if (currentOrder < previousOrder) {
return false;
}
previousOrder = currentOrder;
}
return true;
}
}
@@ -61,6 +61,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@@ -740,6 +741,14 @@ public class QuickFixFactoryImpl extends QuickFixFactory {
});
}
@NotNull
@Override
public IntentionAction createAddMissingRequiredAnnotationParametersFix(@NotNull final PsiAnnotation annotation,
@NotNull final PsiMethod[] annotationMethods,
@NotNull final Collection<String> missedElements) {
return new AddMissingRequiredAnnotationParametersFix(annotation, annotationMethods, missedElements);
}
private static boolean timeToOptimizeImports(@NotNull PsiFile file) {
if (!CodeInsightSettings.getInstance().OPTIMIZE_IMPORTS_ON_THE_FLY) return false;
@@ -0,0 +1,15 @@
// "Add missing annotation parameters - value3, value2, value1" "true"
class Test {
@MyAnnotation(value3 = , value2 = , value1 = )
void m() {
}
@interface MyAnnotation {
String value3();
String value2();
String value1();
}
}
@@ -0,0 +1,16 @@
// "Add missing annotation parameters - value4, value1" "true"
class Test {
@MyAnnotation(value4 = , value3 = "", value2 = "", value1 = )
void m() {
}
@interface MyAnnotation {
String value4();
String value3();
String value2();
String value1();
}
}
@@ -0,0 +1,16 @@
// "Add missing annotation parameters - value4, value1" "true"
class Test {
@MyAnnotation(value2 = "", value3 = "", value4 = , value1 = )
void m() {
}
@interface MyAnnotation {
String value4();
String value3();
String value2();
String value1();
}
}
@@ -0,0 +1,13 @@
// "Add missing annotation parameter 'value'" "true"
class Test {
@MyAnnotation()
void m() {
}
@interface MyAnnotation {
String value();
}
}
@@ -0,0 +1,14 @@
class Test {
@MyAnnotation(value3 = "value33", value2 = "value22", value1 = "value11")
void m() {
}
@interface MyAnnotation {
String value3();
String value2();
String value1();
}
}
@@ -0,0 +1,15 @@
// "Add missing annotation parameters - value3, value2, value1" "true"
class Test {
@MyAnnotati<caret>on
void m() {
}
@interface MyAnnotation {
String value3();
String value2();
String value1();
}
}
@@ -0,0 +1,16 @@
// "Add missing annotation parameters - value4, value1" "true"
class Test {
@MyAnnotati<caret>on(value3 = "", value2 = "")
void m() {
}
@interface MyAnnotation {
String value4();
String value3();
String value2();
String value1();
}
}
@@ -0,0 +1,16 @@
// "Add missing annotation parameters - value4, value1" "true"
class Test {
@MyAnnotati<caret>on(value2 = "", value3 = "")
void m() {
}
@interface MyAnnotation {
String value4();
String value3();
String value2();
String value1();
}
}
@@ -0,0 +1,13 @@
// "Add missing annotation parameter 'value'" "false"
class Test {
@MyAnnotati<caret>on
void m() {
}
@interface MyAnnotation {
String value() default "";
}
}
@@ -0,0 +1,13 @@
// "Add missing annotation parameter 'value'" "true"
class Test {
@MyAnnotati<caret>on
void m() {
}
@interface MyAnnotation {
String value();
}
}
@@ -0,0 +1,14 @@
class Test {
@MyAnnotati<caret>on
void m() {
}
@interface MyAnnotation {
String value3();
String value2();
String value1();
}
}
@@ -0,0 +1,70 @@
/*
* Copyright 2000-2014 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 com.intellij.codeInsight.daemon.quickFix;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.codeInsight.template.impl.TemplateState;
/**
* @author Dmitry Batkovich
*/
public class AddMissingRequiredAnnotationParametersTest extends LightQuickFixTestCase {
private void doTest() {
doSingleTest(getTestName(false) + ".java");
}
public void testFewParameters() {
doTest();
}
public void testFewParameters2() {
doTest();
}
public void testFewParametersWithoutOrder() {
doTest();
}
public void testSingleParameter() {
doTest();
}
public void testParameterWithDefaultValue() {
doTest();
}
public void testValueTyping() {
configureByFile(getBasePath() + "/beforeValueTyping.java");
TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
doAction("Add missing annotation parameters - value3, value2, value1");
final TemplateState state = TemplateManagerImpl.getTemplateState(getEditor());
assertNotNull(state);
type("\"value33\"");
state.nextTab();
type("\"value22\"");
state.nextTab();
type("\"value11\"");
state.nextTab();
assertTrue(state.isFinished());
checkResultByFile(getBasePath() + "/afterValueTyping.java");
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/addMissingRequiredAnnotationParameters";
}
}
@@ -278,3 +278,7 @@ add.qualifier.original.class.chooser.title=Original class
wrap.array.to.arrays.as.list.parameter.text=Wrap {0, choice, 1#1st|2#2nd|3#3rd|4#{0,number}th} parameter using ''Arrays.asList''
wrap.array.to.arrays.as.list.single.parameter.text=Wrap using ''Arrays.asList''
annotations.fix=Annotations
add.missing.annotation.parameters.fix=Add missing annotation parameters - {0}
add.missing.annotation.single.parameter.fix=Add missing annotation parameter ''{0}''