Search in sources :

Example 51 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.

the class UnusedMessageFormatParameterInspection method checkFile.

@Nullable
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
    if (!(file instanceof PropertiesFile))
        return null;
    PropertiesFile propertiesFile = (PropertiesFile) file;
    final List<IProperty> properties = propertiesFile.getProperties();
    List<ProblemDescriptor> problemDescriptors = new ArrayList<>();
    for (IProperty property : properties) {
        @NonNls String name = property.getName();
        if (name != null) {
            if (name.startsWith("log4j"))
                continue;
            if (name.startsWith(REGEXP + ".") || name.endsWith("." + REGEXP))
                continue;
        }
        String value = property.getValue();
        Set<Integer> parameters = new HashSet<>();
        if (value != null) {
            int index = value.indexOf('{');
            while (index != -1) {
                value = value.substring(index + 1);
                final int comma = value.indexOf(',');
                final int brace = value.indexOf('}');
                //misformatted string
                if (brace == -1)
                    break;
                if (comma == -1) {
                    index = brace;
                } else {
                    index = Math.min(comma, brace);
                }
                try {
                    parameters.add(new Integer(value.substring(0, index)));
                } catch (NumberFormatException e) {
                    break;
                }
                index = value.indexOf('{');
            }
            for (Integer integer : parameters) {
                for (int i = 0; i < integer.intValue(); i++) {
                    if (!parameters.contains(new Integer(i))) {
                        ASTNode[] nodes = property.getPsiElement().getNode().getChildren(null);
                        PsiElement valElement = nodes.length < 3 ? property.getPsiElement() : nodes[2].getPsi();
                        final String message = PropertiesBundle.message("unused.message.format.parameter.problem.descriptor", integer.toString(), Integer.toString(i));
                        final String propertyKey = property.getKey();
                        final LocalQuickFix[] fixes = isOnTheFly ? new LocalQuickFix[] { new RenameElementFix(((Property) property), propertyKey == null ? REGEXP : propertyKey + "." + REGEXP) } : null;
                        problemDescriptors.add(manager.createProblemDescriptor(valElement, message, isOnTheFly, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                        break;
                    }
                }
            }
        }
    }
    return problemDescriptors.isEmpty() ? null : problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) ArrayList(java.util.ArrayList) RenameElementFix(com.intellij.codeInsight.daemon.impl.quickfix.RenameElementFix) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) ASTNode(com.intellij.lang.ASTNode) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 52 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.

the class BaseInspectionVisitor method registerErrorAtOffset.

protected final void registerErrorAtOffset(@NotNull PsiElement location, int offset, int length, ProblemHighlightType highlightType, Object... infos) {
    assert !(location.getTextLength() == 0 || length == 0);
    final LocalQuickFix[] fixes = createAndInitFixes(infos);
    final String description = inspection.buildErrorString(infos);
    final TextRange range = new TextRange(offset, offset + length);
    holder.registerProblem(location, description, highlightType, range, fixes);
}
Also used : LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) TextRange(com.intellij.openapi.util.TextRange)

Example 53 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.

the class GroovyRangeTypeCheckTest method doTest.

public void doTest() {
    myFixture.configureByFile(getTestName(false) + ".groovy");
    final int offset = myFixture.getEditor().getCaretModel().getOffset();
    final PsiElement atCaret = myFixture.getFile().findElementAt(offset);
    final GrRangeExpression range = PsiTreeUtil.getParentOfType(atCaret, GrRangeExpression.class);
    final GroovyRangeTypeCheckInspection inspection = new GroovyRangeTypeCheckInspection();
    final GroovyFix fix = inspection.buildFix(range);
    LocalQuickFix[] fixes = { fix };
    final ProblemDescriptor descriptor = InspectionManager.getInstance(getProject()).createProblemDescriptor(range, "bla-bla", false, fixes, ProblemHighlightType.WEAK_WARNING);
    WriteCommandAction.runWriteCommandAction(null, () -> {
        fix.applyFix(myFixture.getProject(), descriptor);
        PostprocessReformattingAspect.getInstance(getProject()).doPostponedFormatting();
    });
    myFixture.checkResultByFile(getTestName(false) + "_after.groovy");
}
Also used : GroovyFix(org.jetbrains.plugins.groovy.codeInspection.GroovyFix) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) GrRangeExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.arithmetic.GrRangeExpression) PsiElement(com.intellij.psi.PsiElement)

Example 54 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.

the class JavaClassReference method registerFixes.

@Nullable
private List<? extends LocalQuickFix> registerFixes() {
    final List<LocalQuickFix> list = QuickFixFactory.getInstance().registerOrderEntryFixes(new QuickFixActionRegistrarImpl(null), this);
    final String[] extendClasses = getExtendClassNames();
    final String extendClass = extendClasses != null && extendClasses.length > 0 ? extendClasses[0] : null;
    final JavaClassReference[] references = getJavaClassReferenceSet().getAllReferences();
    PsiPackage contextPackage = null;
    for (int i = myIndex; i >= 0; i--) {
        final PsiElement context = references[i].getContext();
        if (context != null) {
            if (context instanceof PsiPackage) {
                contextPackage = (PsiPackage) context;
            }
            break;
        }
    }
    boolean createJavaClass = !canReferencePackage();
    ClassKind kind = createJavaClass ? getClassKind() : null;
    if (createJavaClass && kind == null)
        kind = ClassKind.CLASS;
    final String templateName = JavaClassReferenceProvider.CLASS_TEMPLATE.getValue(getOptions());
    final TextRange range = new TextRange(references[0].getRangeInElement().getStartOffset(), getRangeInElement().getEndOffset());
    final String qualifiedName = range.substring(getElement().getText());
    final CreateClassOrPackageFix action = CreateClassOrPackageFix.createFix(qualifiedName, getScope(getJavaContextFile()), getElement(), contextPackage, kind, extendClass, templateName);
    if (action != null) {
        if (list == null) {
            return Collections.singletonList(action);
        } else {
            final ArrayList<LocalQuickFix> fixes = new ArrayList<>(list.size() + 1);
            fixes.addAll(list);
            fixes.add(action);
            return fixes;
        }
    }
    return list;
}
Also used : CreateClassOrPackageFix(com.intellij.codeInsight.daemon.quickFix.CreateClassOrPackageFix) ArrayList(java.util.ArrayList) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) TextRange(com.intellij.openapi.util.TextRange) QuickFixActionRegistrarImpl(com.intellij.codeInsight.daemon.impl.quickfix.QuickFixActionRegistrarImpl) ClassKind(com.intellij.psi.util.ClassKind) Nullable(org.jetbrains.annotations.Nullable)

Example 55 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.

the class DomElementsHighlightingUtil method createAnnotation.

@Nullable
public static Annotation createAnnotation(final DomElementProblemDescriptor problemDescriptor) {
    return createProblemDescriptors(problemDescriptor, s -> {
        String text = problemDescriptor.getDescriptionTemplate();
        if (StringUtil.isEmpty(text))
            text = null;
        final HighlightSeverity severity = problemDescriptor.getHighlightSeverity();
        TextRange range = s.first;
        if (text == null)
            range = TextRange.from(range.getStartOffset(), 0);
        range = range.shiftRight(s.second.getTextRange().getStartOffset());
        final Annotation annotation = createAnnotation(severity, range, text);
        if (problemDescriptor instanceof DomElementResolveProblemDescriptor) {
            annotation.setTextAttributes(CodeInsightColors.WRONG_REFERENCES_ATTRIBUTES);
        }
        for (LocalQuickFix fix : problemDescriptor.getFixes()) {
            if (fix instanceof IntentionAction)
                annotation.registerFix((IntentionAction) fix);
        }
        return annotation;
    });
}
Also used : HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) TextRange(com.intellij.openapi.util.TextRange) Annotation(com.intellij.lang.annotation.Annotation) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)59 NotNull (org.jetbrains.annotations.NotNull)20 PsiElement (com.intellij.psi.PsiElement)16 Nullable (org.jetbrains.annotations.Nullable)11 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)10 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)10 ASTNode (com.intellij.lang.ASTNode)6 Project (com.intellij.openapi.project.Project)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 XmlTag (com.intellij.psi.xml.XmlTag)6 PsiFile (com.intellij.psi.PsiFile)5 PsiReference (com.intellij.psi.PsiReference)5 ArrayList (java.util.ArrayList)5 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)4 LocalQuickFixProvider (com.intellij.codeInspection.LocalQuickFixProvider)3 QuickFixWrapper (com.intellij.codeInspection.ex.QuickFixWrapper)3 Pair (com.intellij.openapi.util.Pair)3 TextRange (com.intellij.openapi.util.TextRange)3 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)3 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)3