Search in sources :

Example 1 with RenameElementFix

use of com.intellij.codeInsight.daemon.impl.quickfix.RenameElementFix 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 2 with RenameElementFix

use of com.intellij.codeInsight.daemon.impl.quickfix.RenameElementFix in project intellij-plugins by JetBrains.

the class ActionScriptAnnotatingVisitor method checkNamedObjectIsInCorrespondingFile.

private void checkNamedObjectIsInCorrespondingFile(final JSNamedElement aClass) {
    final PsiFile containingFile = aClass.getContainingFile();
    if (containingFile.getContext() != null)
        return;
    final VirtualFile file = containingFile.getVirtualFile();
    if (file != null && !file.getNameWithoutExtension().equals(aClass.getName()) && ProjectRootManager.getInstance(containingFile.getProject()).getFileIndex().getSourceRootForFile(file) != null) {
        final ASTNode node = aClass.findNameIdentifier();
        if (node != null) {
            final String name = aClass.getName();
            String nameWithExtension = name + "." + file.getExtension();
            final String message = JSBundle.message(aClass instanceof JSClass ? "javascript.validation.message.class.should.be.in.file" : aClass instanceof JSNamespaceDeclaration ? "javascript.validation.message.namespace.should.be.in.file" : aClass instanceof JSVariable ? "javascript.validation.message.variable.should.be.in.file" : "javascript.validation.message.function.should.be.in.file", name, nameWithExtension);
            final Annotation annotation = myHolder.createErrorAnnotation(node, message);
            annotation.registerFix(new RenameFileFix(nameWithExtension));
            annotation.registerFix(new RenameElementFix(aClass) {

                final String text;

                final String familyName;

                {
                    String term = message.substring(0, message.indexOf(' '));
                    text = super.getText().replace("class", StringUtil.decapitalize(term));
                    familyName = super.getFamilyName().replace("Class", term);
                }

                @NotNull
                @Override
                public String getText() {
                    return text;
                }

                @NotNull
                @Override
                public String getFamilyName() {
                    return familyName;
                }
            });
        }
    }
    checkFileUnderSourceRoot(aClass, new SimpleErrorReportingClient());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) RenameElementFix(com.intellij.codeInsight.daemon.impl.quickfix.RenameElementFix) NotNull(org.jetbrains.annotations.NotNull) Annotation(com.intellij.lang.annotation.Annotation) ASTNode(com.intellij.lang.ASTNode) RenameFileFix(com.intellij.codeInsight.daemon.impl.quickfix.RenameFileFix)

Aggregations

RenameElementFix (com.intellij.codeInsight.daemon.impl.quickfix.RenameElementFix)2 ASTNode (com.intellij.lang.ASTNode)2 RenameFileFix (com.intellij.codeInsight.daemon.impl.quickfix.RenameFileFix)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 Annotation (com.intellij.lang.annotation.Annotation)1 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 NonNls (org.jetbrains.annotations.NonNls)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1