Search in sources :

Example 21 with InspectionProfile

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

the class DumpInspectionDescriptionsAction method actionPerformed.

@Override
public void actionPerformed(final AnActionEvent event) {
    final InspectionProfile profile = InspectionProfileManager.getInstance().getCurrentProfile();
    final InspectionToolWrapper[] tools = profile.getInspectionTools(null);
    final Collection<String> classes = ContainerUtil.newTreeSet();
    final Map<String, Collection<String>> groups = ContainerUtil.newTreeMap();
    final String tempDirectory = FileUtil.getTempDirectory();
    final File descDirectory = new File(tempDirectory, "inspections");
    if (!descDirectory.mkdirs() && !descDirectory.isDirectory()) {
        LOG.error("Unable to create directory: " + descDirectory.getAbsolutePath());
        return;
    }
    for (InspectionToolWrapper toolWrapper : tools) {
        classes.add(getInspectionClass(toolWrapper).getName());
        final String group = getGroupName(toolWrapper);
        Collection<String> names = groups.get(group);
        if (names == null)
            groups.put(group, (names = ContainerUtil.newTreeSet()));
        names.add(toolWrapper.getShortName());
        final URL url = getDescriptionUrl(toolWrapper);
        if (url != null) {
            doDump(new File(descDirectory, toolWrapper.getShortName() + ".html"), new Processor() {

                @Override
                public void process(BufferedWriter writer) throws Exception {
                    writer.write(ResourceUtil.loadText(url));
                }
            });
        }
    }
    doNotify("Inspection descriptions dumped to\n" + descDirectory.getAbsolutePath());
    final File fqnListFile = new File(tempDirectory, "inspection_fqn_list.txt");
    final boolean fqnOk = doDump(fqnListFile, new Processor() {

        @Override
        public void process(BufferedWriter writer) throws Exception {
            for (String name : classes) {
                writer.write(name);
                writer.newLine();
            }
        }
    });
    if (fqnOk) {
        doNotify("Inspection class names dumped to\n" + fqnListFile.getAbsolutePath());
    }
    final File groupsFile = new File(tempDirectory, "inspection_groups.txt");
    final boolean groupsOk = doDump(groupsFile, new Processor() {

        @Override
        public void process(BufferedWriter writer) throws Exception {
            for (Map.Entry<String, Collection<String>> entry : groups.entrySet()) {
                writer.write(entry.getKey());
                writer.write(':');
                writer.newLine();
                for (String name : entry.getValue()) {
                    writer.write("  ");
                    writer.write(name);
                    writer.newLine();
                }
            }
        }
    });
    if (groupsOk) {
        doNotify("Inspection groups dumped to\n" + fqnListFile.getAbsolutePath());
    }
}
Also used : InspectionProfile(com.intellij.codeInspection.InspectionProfile) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter) Collection(java.util.Collection) LocalInspectionToolWrapper(com.intellij.codeInspection.ex.LocalInspectionToolWrapper) InspectionToolWrapper(com.intellij.codeInspection.ex.InspectionToolWrapper) File(java.io.File)

Example 22 with InspectionProfile

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

the class GroovyPostHighlightingPass method doCollectInformation.

@Override
public void doCollectInformation(@NotNull final ProgressIndicator progress) {
    ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
    VirtualFile virtualFile = myFile.getViewProvider().getVirtualFile();
    if (!fileIndex.isInContent(virtualFile)) {
        return;
    }
    final InspectionProfile profile = InspectionProjectProfileManager.getInstance(myProject).getCurrentProfile();
    final HighlightDisplayKey unusedDefKey = HighlightDisplayKey.find(GroovyUnusedDeclarationInspection.SHORT_NAME);
    final boolean deadCodeEnabled = profile.isToolEnabled(unusedDefKey, myFile);
    final UnusedDeclarationInspectionBase deadCodeInspection = (UnusedDeclarationInspectionBase) profile.getUnwrappedTool(UnusedDeclarationInspectionBase.SHORT_NAME, myFile);
    final GlobalUsageHelper usageHelper = new GlobalUsageHelper() {

        @Override
        public boolean isCurrentFileAlreadyChecked() {
            return false;
        }

        @Override
        public boolean isLocallyUsed(@NotNull PsiNamedElement member) {
            return false;
        }

        @Override
        public boolean shouldCheckUsages(@NotNull PsiMember member) {
            return deadCodeInspection == null || !deadCodeInspection.isEntryPoint(member);
        }
    };
    final List<HighlightInfo> unusedDeclarations = new ArrayList<>();
    final Map<GrParameter, Boolean> usedParams = new HashMap<>();
    myFile.accept(new PsiRecursiveElementWalkingVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            if (element instanceof GrReferenceExpression && !((GrReferenceElement) element).isQualified()) {
                GroovyResolveResult[] results = ((GrReferenceExpression) element).multiResolve(false);
                if (results.length == 0) {
                    results = ((GrReferenceExpression) element).multiResolve(true);
                }
                for (GroovyResolveResult result : results) {
                    PsiElement resolved = result.getElement();
                    if (resolved instanceof GrParameter && resolved.getContainingFile() == myFile) {
                        usedParams.put((GrParameter) resolved, Boolean.TRUE);
                    }
                }
            }
            if (deadCodeEnabled && element instanceof GrNamedElement && element instanceof PsiModifierListOwner && !UnusedSymbolUtil.isImplicitUsage(element.getProject(), (PsiModifierListOwner) element, progress) && !GroovySuppressableInspectionTool.isElementToolSuppressedIn(element, GroovyUnusedDeclarationInspection.SHORT_NAME)) {
                PsiElement nameId = ((GrNamedElement) element).getNameIdentifierGroovy();
                if (nameId.getNode().getElementType() == GroovyTokenTypes.mIDENT) {
                    String name = ((GrNamedElement) element).getName();
                    if (element instanceof GrTypeDefinition && !UnusedSymbolUtil.isClassUsed(myProject, element.getContainingFile(), (GrTypeDefinition) element, progress, usageHelper)) {
                        HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(nameId, "Class " + name + " is unused", HighlightInfoType.UNUSED_SYMBOL);
                        QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createSafeDeleteFix(element), unusedDefKey);
                        ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
                    } else if (element instanceof GrMethod) {
                        GrMethod method = (GrMethod) element;
                        if (!UnusedSymbolUtil.isMethodReferenced(method.getProject(), method.getContainingFile(), method, progress, usageHelper)) {
                            String message = (method.isConstructor() ? "Constructor" : "Method") + " " + name + " is unused";
                            HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(nameId, message, HighlightInfoType.UNUSED_SYMBOL);
                            QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createSafeDeleteFix(method), unusedDefKey);
                            ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
                        }
                    } else if (element instanceof GrField && isFieldUnused((GrField) element, progress, usageHelper)) {
                        HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(nameId, "Property " + name + " is unused", HighlightInfoType.UNUSED_SYMBOL);
                        QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createSafeDeleteFix(element), unusedDefKey);
                        ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
                    } else if (element instanceof GrParameter) {
                        if (!usedParams.containsKey(element)) {
                            usedParams.put((GrParameter) element, Boolean.FALSE);
                        }
                    }
                }
            }
            super.visitElement(element);
        }
    });
    final Set<GrImportStatement> unusedImports = new HashSet<>(PsiUtil.getValidImportStatements(myFile));
    unusedImports.removeAll(GroovyImportUtil.findUsedImports(myFile));
    myUnusedImports = unusedImports;
    if (deadCodeEnabled) {
        for (GrParameter parameter : usedParams.keySet()) {
            if (usedParams.get(parameter))
                continue;
            PsiElement scope = parameter.getDeclarationScope();
            if (scope instanceof GrMethod) {
                GrMethod method = (GrMethod) scope;
                if (methodMayHaveUnusedParameters(method)) {
                    PsiElement identifier = parameter.getNameIdentifierGroovy();
                    HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(identifier, "Parameter " + parameter.getName() + " is unused", HighlightInfoType.UNUSED_SYMBOL);
                    QuickFixAction.registerQuickFixAction(highlightInfo, GroovyQuickFixFactory.getInstance().createRemoveUnusedGrParameterFix(parameter), unusedDefKey);
                    ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
                }
            } else if (scope instanceof GrClosableBlock) {
            //todo Max Medvedev
            }
        }
    }
    myUnusedDeclarations = unusedDeclarations;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) NotNull(org.jetbrains.annotations.NotNull) GrNamedElement(org.jetbrains.plugins.groovy.lang.psi.GrNamedElement) InspectionProfile(com.intellij.codeInspection.InspectionProfile) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) UnusedDeclarationInspectionBase(com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex)

Example 23 with InspectionProfile

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

the class IncompletePropertyInspection method getInstance.

@NotNull
public static IncompletePropertyInspection getInstance(PsiElement element) {
    final InspectionProjectProfileManager profileManager = InspectionProjectProfileManager.getInstance(element.getProject());
    InspectionProfile inspectionProfile = profileManager.getCurrentProfile();
    return (IncompletePropertyInspection) inspectionProfile.getUnwrappedTool(TOOL_KEY, element);
}
Also used : InspectionProfile(com.intellij.codeInspection.InspectionProfile) InspectionProjectProfileManager(com.intellij.profile.codeInspection.InspectionProjectProfileManager) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with InspectionProfile

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

the class PyCompatibilityInspection method getInstance.

@Nullable
public static PyCompatibilityInspection getInstance(@NotNull PsiElement element) {
    final InspectionProfile inspectionProfile = InspectionProjectProfileManager.getInstance(element.getProject()).getCurrentProfile();
    final String toolName = PyCompatibilityInspection.class.getSimpleName();
    return (PyCompatibilityInspection) inspectionProfile.getUnwrappedTool(toolName, element);
}
Also used : InspectionProfile(com.intellij.codeInspection.InspectionProfile) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with InspectionProfile

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

the class HtmlUtil method getEntitiesString.

@Nullable
public static String getEntitiesString(@Nullable PsiElement context, @NotNull String inspectionName) {
    if (context == null)
        return null;
    PsiFile containingFile = context.getContainingFile().getOriginalFile();
    final InspectionProfile profile = InspectionProjectProfileManager.getInstance(context.getProject()).getCurrentProfile();
    XmlEntitiesInspection inspection = (XmlEntitiesInspection) profile.getUnwrappedTool(inspectionName, containingFile);
    if (inspection != null) {
        return inspection.getAdditionalEntries();
    }
    return null;
}
Also used : InspectionProfile(com.intellij.codeInspection.InspectionProfile) PsiFile(com.intellij.psi.PsiFile) XmlEntitiesInspection(com.intellij.codeInspection.htmlInspections.XmlEntitiesInspection)

Aggregations

InspectionProfile (com.intellij.codeInspection.InspectionProfile)28 Nullable (org.jetbrains.annotations.Nullable)10 InspectionToolWrapper (com.intellij.codeInspection.ex.InspectionToolWrapper)7 HighlightDisplayKey (com.intellij.codeInsight.daemon.HighlightDisplayKey)6 NotNull (org.jetbrains.annotations.NotNull)5 Project (com.intellij.openapi.project.Project)4 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiFile (com.intellij.psi.PsiFile)3 UnusedDeclarationInspectionBase (com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase)2 InspectionManagerEx (com.intellij.codeInspection.ex.InspectionManagerEx)2 InspectionProfileImpl (com.intellij.codeInspection.ex.InspectionProfileImpl)2 Language (com.intellij.lang.Language)2 Module (com.intellij.openapi.module.Module)2 InspectionProjectProfileManager (com.intellij.profile.codeInspection.InspectionProjectProfileManager)2 ProjectInspectionProfileManager (com.intellij.profile.codeInspection.ProjectInspectionProfileManager)2 PsiElement (com.intellij.psi.PsiElement)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 ActionEvent (java.awt.event.ActionEvent)2 AnalysisScope (com.intellij.analysis.AnalysisScope)1