mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
SurroundWithQuotesAnnotationParameter: intention replaced by quick fix to the corresponding error
This commit is contained in:
@@ -260,4 +260,6 @@ public abstract class QuickFixFactory {
|
||||
public abstract IntentionAction createAddMissingRequiredAnnotationParametersFix(@NotNull PsiAnnotation annotation,
|
||||
@NotNull PsiMethod[] annotationMethods,
|
||||
@NotNull Collection<String> missedElements);
|
||||
@NotNull
|
||||
public abstract IntentionAction createSurroundWithQuotesAnnotationParameterValueFix(@NotNull PsiAnnotationMemberValue value, @NotNull PsiType expectedType);
|
||||
}
|
||||
|
||||
+4
-1
@@ -153,7 +153,10 @@ public class AnnotationsHighlightUtil {
|
||||
|
||||
String description = JavaErrorMessages.message("annotation.incompatible.types",
|
||||
JavaHighlightUtil.formatType(type), JavaHighlightUtil.formatType(expectedType));
|
||||
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(value).descriptionAndTooltip(description).create();
|
||||
final HighlightInfo info =
|
||||
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(value).descriptionAndTooltip(description).create();
|
||||
QuickFixAction.registerQuickFixAction(info, QuickFixFactory.getInstance().createSurroundWithQuotesAnnotationParameterValueFix(value, expectedType));
|
||||
return info;
|
||||
}
|
||||
|
||||
LOG.error("Unknown annotation member value: " + value);
|
||||
|
||||
+7
@@ -595,4 +595,11 @@ public class EmptyQuickFixFactory extends QuickFixFactory {
|
||||
public IntentionAction createAddMissingRequiredAnnotationParametersFix(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiMethod[] psiMethods, @NotNull Collection<String> strings) {
|
||||
return QuickFixes.EMPTY_FIX;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public IntentionAction createSurroundWithQuotesAnnotationParameterValueFix(@NotNull PsiAnnotationMemberValue value,
|
||||
@NotNull PsiType expectedType) {
|
||||
return QuickFixes.EMPTY_FIX;
|
||||
}
|
||||
}
|
||||
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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.intention.IntentionAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author Dmitry Batkovich
|
||||
*/
|
||||
public class SurroundWithQuotesAnnotationParameterValueFix implements IntentionAction {
|
||||
private final PsiAnnotationMemberValue myValue;
|
||||
private final PsiType myExpectedType;
|
||||
|
||||
public SurroundWithQuotesAnnotationParameterValueFix(final PsiAnnotationMemberValue value, final PsiType expectedType) {
|
||||
myValue = value;
|
||||
myExpectedType = expectedType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
if (!myValue.isValid() || !(myExpectedType instanceof PsiClassType)) {
|
||||
return false;
|
||||
}
|
||||
final PsiClass resolvedType = ((PsiClassType)myExpectedType).resolve();
|
||||
return resolvedType != null && CommonClassNames.JAVA_LANG_STRING.equals(resolvedType.getQualifiedName()) && myValue instanceof PsiLiteralExpression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
String newText = myValue.getText();
|
||||
newText = StringUtil.stripQuotesAroundValue(newText);
|
||||
newText = "\"" + newText + "\"";
|
||||
PsiElement newToken = JavaPsiFacade.getInstance(project).getElementFactory().createExpressionFromText(newText, null);
|
||||
final PsiElement newElement = myValue.replace(newToken);
|
||||
editor.getCaretModel().moveToOffset(newElement.getTextOffset() + newElement.getTextLength());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return "Surround annotation parameter value with quotes";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return getFamilyName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startInWriteAction() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-105
@@ -1,105 +0,0 @@
|
||||
/*
|
||||
* 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.intention.impl;
|
||||
|
||||
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Dmitry Batkovich
|
||||
*/
|
||||
public class SurroundWithQuotesStringAnnotationParameterValueIntention extends PsiElementBaseIntentionAction {
|
||||
private static final Set<IElementType> SUITABLE_TYPES = ContainerUtil.newHashSet(JavaTokenType.LONG_LITERAL,
|
||||
JavaTokenType.FLOAT_LITERAL,
|
||||
JavaTokenType.INTEGER_LITERAL,
|
||||
JavaTokenType.DOUBLE_LITERAL,
|
||||
JavaTokenType.CHARACTER_LITERAL,
|
||||
JavaTokenType.TRUE_KEYWORD,
|
||||
JavaTokenType.FALSE_KEYWORD);
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
|
||||
String newText = element.getText();
|
||||
if (((PsiJavaToken)element).getTokenType().equals(JavaTokenType.CHARACTER_LITERAL)) {
|
||||
newText = newText.substring(1, newText.length() - 1);
|
||||
}
|
||||
newText = "\"" + newText + "\"";
|
||||
PsiElement newToken = JavaPsiFacade.getInstance(project).getElementFactory().createExpressionFromText(newText, null).getFirstChild();
|
||||
final PsiElement newElement = element.replace(newToken);
|
||||
editor.getCaretModel().moveToOffset(newElement.getTextOffset() + newElement.getTextLength());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
|
||||
if (!(element instanceof PsiJavaToken && SUITABLE_TYPES.contains(((PsiJavaToken)element).getTokenType()))) {
|
||||
return false;
|
||||
}
|
||||
final PsiElement literalExpression = element.getParent();
|
||||
if (literalExpression == null) {
|
||||
return false;
|
||||
}
|
||||
final PsiElement nameValuePair = literalExpression.getParent();
|
||||
if (!(nameValuePair instanceof PsiNameValuePair)) {
|
||||
return false;
|
||||
}
|
||||
final PsiAnnotation annotation = PsiTreeUtil.getParentOfType(nameValuePair, PsiAnnotation.class);
|
||||
if (annotation == null) {
|
||||
return false;
|
||||
}
|
||||
final PsiJavaCodeReferenceElement nameRef = annotation.getNameReferenceElement();
|
||||
if (nameRef == null) {
|
||||
return false;
|
||||
}
|
||||
final PsiElement resolved = nameRef.resolve();
|
||||
if (!(resolved instanceof PsiClass)) {
|
||||
return false;
|
||||
}
|
||||
final String parameterName = ((PsiNameValuePair)nameValuePair).getName();
|
||||
final PsiMethod[] methods =
|
||||
((PsiClass)resolved).findMethodsByName(parameterName == null ? PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME : parameterName, false);
|
||||
if (methods.length != 1) {
|
||||
return false;
|
||||
}
|
||||
final PsiType methodReturnType = methods[0].getReturnType();
|
||||
if (!(methodReturnType instanceof PsiClassType)) {
|
||||
return false;
|
||||
}
|
||||
final PsiClass returnTypeClass = ((PsiClassType)methodReturnType).resolve();
|
||||
return returnTypeClass != null && CommonClassNames.JAVA_LANG_STRING.equals(returnTypeClass.getQualifiedName());
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return "Surround annotation parameter value with quotes";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return getFamilyName();
|
||||
}
|
||||
}
|
||||
+7
@@ -755,6 +755,13 @@ public class QuickFixFactoryImpl extends QuickFixFactory {
|
||||
return new AddMissingRequiredAnnotationParametersFix(annotation, annotationMethods, missedElements);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public IntentionAction createSurroundWithQuotesAnnotationParameterValueFix(@NotNull PsiAnnotationMemberValue value,
|
||||
@NotNull PsiType expectedType) {
|
||||
return new SurroundWithQuotesAnnotationParameterValueFix(value, expectedType);
|
||||
}
|
||||
|
||||
private static boolean timeToOptimizeImports(@NotNull PsiFile file) {
|
||||
if (!CodeInsightSettings.getInstance().OPTIMIZE_IMPORTS_ON_THE_FLY) return false;
|
||||
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.codeInsight.intention;
|
||||
package com.intellij.codeInsight.daemon.quickFix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.LightIntentionActionTestCase;
|
||||
|
||||
Reference in New Issue
Block a user