use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.
the class PatternValidator method checkExpression.
private void checkExpression(PsiExpression expression, final PsiAnnotation[] annotations, ProblemsHolder holder) {
if (annotations.length == 0)
return;
final PsiAnnotation psiAnnotation = annotations[0];
// cache compiled pattern with annotation
CachedValue<Pattern> p = psiAnnotation.getUserData(COMPLIED_PATTERN);
if (p == null) {
final CachedValueProvider<Pattern> provider = () -> {
final String pattern = AnnotationUtilEx.calcAnnotationValue(psiAnnotation, "value");
Pattern p1 = null;
if (pattern != null) {
try {
p1 = Pattern.compile(pattern);
} catch (PatternSyntaxException e) {
// pattern stays null
}
}
return CachedValueProvider.Result.create(p1, (Object[]) annotations);
};
p = CachedValuesManager.getManager(expression.getProject()).createCachedValue(provider, false);
psiAnnotation.putUserData(COMPLIED_PATTERN, p);
}
final Pattern pattern = p.getValue();
if (pattern == null)
return;
List<PsiExpression> nonConstantElements = new SmartList<>();
final Object result = new SubstitutedExpressionEvaluationHelper(expression.getProject()).computeExpression(expression, myConfiguration.getAdvancedConfiguration().getDfaOption(), false, nonConstantElements);
final String o = result == null ? null : String.valueOf(result);
if (o != null) {
if (!pattern.matcher(o).matches()) {
if (annotations.length > 1) {
// the last element contains the element's actual annotation
final String fqn = annotations[annotations.length - 1].getQualifiedName();
assert fqn != null;
final String name = StringUtil.getShortName(fqn);
holder.registerProblem(expression, MessageFormat.format("Expression ''{0}'' doesn''t match ''{1}'' pattern: {2}", o, name, pattern.pattern()));
} else {
holder.registerProblem(expression, MessageFormat.format("Expression ''{0}'' doesn''t match pattern: {1}", o, pattern.pattern()));
}
}
} else if (CHECK_NON_CONSTANT_VALUES) {
for (PsiExpression expr : nonConstantElements) {
final PsiElement e;
if (expr instanceof PsiReferenceExpression) {
e = ((PsiReferenceExpression) expr).resolve();
} else if (expr instanceof PsiMethodCallExpression) {
e = ((PsiMethodCallExpression) expr).getMethodExpression().resolve();
} else {
e = expr;
}
final PsiModifierListOwner owner = e instanceof PsiModifierListOwner ? (PsiModifierListOwner) e : null;
LocalQuickFix quickFix;
if (owner != null && PsiUtilEx.isLanguageAnnotationTarget(owner)) {
PsiAnnotation[] resolvedAnnos = AnnotationUtilEx.getAnnotationFrom(owner, myConfiguration.getAdvancedConfiguration().getPatternAnnotationPair(), true);
if (resolvedAnnos.length == 2 && annotations.length == 2 && Comparing.strEqual(resolvedAnnos[1].getQualifiedName(), annotations[1].getQualifiedName())) {
// both target and source annotated indirectly with the same anno
return;
}
final String classname = myConfiguration.getAdvancedConfiguration().getSubstAnnotationPair().first;
quickFix = AnnotateFix.canApplyOn(owner) ? new AnnotateFix(classname) : new IntroduceVariableFix();
} else {
quickFix = new IntroduceVariableFix();
}
holder.registerProblem(expr, "Unsubstituted expression", quickFix);
}
}
}
use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.
the class PluginXmlDomInspection method annotateExtension.
private static void annotateExtension(Extension extension, DomElementAnnotationHolder holder) {
final ExtensionPoint extensionPoint = extension.getExtensionPoint();
if (extensionPoint == null)
return;
final GenericAttributeValue<PsiClass> interfaceAttribute = extensionPoint.getInterface();
if (DomUtil.hasXml(interfaceAttribute)) {
final PsiClass value = interfaceAttribute.getValue();
if (value != null && value.isDeprecated()) {
holder.createProblem(extension, ProblemHighlightType.LIKE_DEPRECATED, "Deprecated EP '" + extensionPoint.getEffectiveQualifiedName() + "'", null);
return;
}
}
if (ExtensionPoints.ERROR_HANDLER.equals(extensionPoint.getEffectiveQualifiedName()) && extension.exists()) {
String implementation = extension.getXmlTag().getAttributeValue("implementation");
if (ITNReporter.class.getName().equals(implementation)) {
IdeaPlugin plugin = extension.getParentOfType(IdeaPlugin.class, true);
if (plugin != null) {
Vendor vendor = plugin.getVendor();
if (DomUtil.hasXml(vendor) && PluginManagerMain.isDevelopedByJetBrains(vendor.getValue())) {
LocalQuickFix fix = new RemoveDomElementQuickFix(extension);
holder.createProblem(extension, ProblemHighlightType.LIKE_UNUSED_SYMBOL, "Exceptions from plugins developed by JetBrains are reported via ITNReporter automatically," + " there is no need to specify it explicitly", null, fix).highlightWholeElement();
}
}
}
}
final List<? extends DomAttributeChildDescription> descriptions = extension.getGenericInfo().getAttributeChildrenDescriptions();
for (DomAttributeChildDescription attributeDescription : descriptions) {
final GenericAttributeValue attributeValue = attributeDescription.getDomAttributeValue(extension);
if (attributeValue == null || !DomUtil.hasXml(attributeValue))
continue;
// IconsReferencesContributor
if ("icon".equals(attributeDescription.getXmlElementName())) {
annotateResolveProblems(holder, attributeValue);
}
final PsiElement declaration = attributeDescription.getDeclaration(extension.getManager().getProject());
if (declaration instanceof PsiField) {
PsiField psiField = (PsiField) declaration;
if (psiField.isDeprecated()) {
holder.createProblem(attributeValue, ProblemHighlightType.LIKE_DEPRECATED, "Deprecated attribute '" + attributeDescription.getName() + "'", null).highlightWholeElement();
}
}
}
}
use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.
the class BaseInspectionVisitor method registerErrorAtRange.
protected final void registerErrorAtRange(@NotNull PsiElement startLocation, @NotNull PsiElement endLocation, Object... infos) {
if (startLocation.getTextLength() == 0 && startLocation == endLocation) {
return;
}
final LocalQuickFix[] fixes = createAndInitFixes(infos);
final String description = inspection.buildErrorString(infos);
final ProblemDescriptor problemDescriptor = holder.getManager().createProblemDescriptor(startLocation, endLocation, description, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, onTheFly, fixes);
holder.registerProblem(problemDescriptor);
}
use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.
the class GroovyTypeCheckVisitor method processAssignment.
private void processAssignment(@NotNull PsiType expectedType, @NotNull GrExpression expression, @NotNull PsiElement toHighlight, @NotNull @PropertyKey(resourceBundle = GroovyBundle.BUNDLE) String messageKey, @NotNull PsiElement context, @NotNull ApplicableTo position) {
{
// check if current assignment is constructor call
final GrListOrMap initializer = getTupleInitializer(expression);
if (initializer != null) {
processConstructorCall(new GrListOrMapInfo(initializer));
return;
}
}
if (PsiUtil.isRawClassMemberAccess(expression))
return;
if (checkForImplicitEnumAssigning(expectedType, expression, expression))
return;
final PsiType actualType = expression.getType();
if (actualType == null)
return;
final ConversionResult result = TypesUtil.canAssign(expectedType, actualType, context, position);
if (result == ConversionResult.OK)
return;
final List<LocalQuickFix> fixes = ContainerUtil.newArrayList();
{
fixes.add(new GrCastFix(expectedType, expression));
final String varName = getLValueVarName(toHighlight);
if (varName != null) {
fixes.add(new GrChangeVariableType(actualType, varName));
}
}
final String message = GroovyBundle.message(messageKey, actualType.getPresentableText(), expectedType.getPresentableText());
registerError(toHighlight, message, fixes.toArray(new LocalQuickFix[fixes.size()]), result == ConversionResult.ERROR ? ProblemHighlightType.GENERIC_ERROR : ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.
the class GroovyTypeCheckVisitorHelper method genCastFixes.
@NotNull
public static LocalQuickFix[] genCastFixes(@NotNull GrSignature signature, @NotNull PsiType[] argumentTypes, @Nullable GrArgumentList argumentList) {
if (argumentList == null)
return LocalQuickFix.EMPTY_ARRAY;
final List<GrExpression> args = getExpressionArgumentsOfCall(argumentList);
if (args == null)
return LocalQuickFix.EMPTY_ARRAY;
final List<Pair<Integer, PsiType>> allErrors = new ArrayList<>();
final List<GrClosureSignature> signatures = GrClosureSignatureUtil.generateSimpleSignatures(signature);
for (GrClosureSignature closureSignature : signatures) {
final GrClosureSignatureUtil.MapResultWithError<PsiType> map = GrClosureSignatureUtil.mapSimpleSignatureWithErrors(closureSignature, argumentTypes, id, argumentList, 1);
if (map != null) {
final List<Pair<Integer, PsiType>> errors = map.getErrors();
for (Pair<Integer, PsiType> error : errors) {
if (!(error.first == 0 && PsiImplUtil.hasNamedArguments(argumentList))) {
allErrors.add(error);
}
}
}
}
final ArrayList<LocalQuickFix> fixes = new ArrayList<>();
for (Pair<Integer, PsiType> error : allErrors) {
if (args.size() > error.first && error.second != null) {
fixes.add(new ParameterCastFix(error.first, error.second, args.get(error.first)));
}
}
return fixes.toArray(new LocalQuickFix[fixes.size()]);
}
Aggregations