Search in sources :

Example 16 with InspectionProfile

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

the class PostHighlightingVisitor method collectHighlights.

void collectHighlights(@NotNull HighlightInfoHolder result, @NotNull ProgressIndicator progress) {
    DaemonCodeAnalyzerEx daemonCodeAnalyzer = DaemonCodeAnalyzerEx.getInstanceEx(myProject);
    FileStatusMap fileStatusMap = daemonCodeAnalyzer.getFileStatusMap();
    InspectionProfile profile = InspectionProjectProfileManager.getInstance(myProject).getCurrentProfile();
    boolean unusedSymbolEnabled = profile.isToolEnabled(myDeadCodeKey, myFile);
    GlobalUsageHelper globalUsageHelper = myRefCountHolder.getGlobalUsageHelper(myFile, myDeadCodeInspection, unusedSymbolEnabled);
    boolean errorFound = false;
    if (unusedSymbolEnabled) {
        final FileViewProvider viewProvider = myFile.getViewProvider();
        final Set<Language> relevantLanguages = viewProvider.getLanguages();
        for (Language language : relevantLanguages) {
            progress.checkCanceled();
            PsiElement psiRoot = viewProvider.getPsi(language);
            if (!HighlightingLevelManager.getInstance(myProject).shouldInspect(psiRoot))
                continue;
            List<PsiElement> elements = CollectHighlightsUtil.getElementsInRange(psiRoot, 0, myFile.getTextLength());
            for (PsiElement element : elements) {
                progress.checkCanceled();
                if (element instanceof PsiIdentifier) {
                    PsiIdentifier identifier = (PsiIdentifier) element;
                    HighlightInfo info = processIdentifier(identifier, progress, globalUsageHelper);
                    if (info != null) {
                        errorFound |= info.getSeverity() == HighlightSeverity.ERROR;
                        result.add(info);
                    }
                }
            }
        }
    }
    HighlightDisplayKey unusedImportKey = HighlightDisplayKey.find(UnusedImportLocalInspection.SHORT_NAME);
    if (isUnusedImportEnabled(unusedImportKey)) {
        PsiImportList importList = ((PsiJavaFile) myFile).getImportList();
        if (importList != null) {
            final PsiImportStatementBase[] imports = importList.getAllImportStatements();
            for (PsiImportStatementBase statement : imports) {
                progress.checkCanceled();
                final HighlightInfo info = processImport(statement, unusedImportKey);
                if (info != null) {
                    errorFound |= info.getSeverity() == HighlightSeverity.ERROR;
                    result.add(info);
                }
            }
        }
    }
    if (errorFound) {
        fileStatusMap.setErrorFoundFlag(myProject, myDocument, true);
    }
    optimizeImportsOnTheFlyLater(progress);
}
Also used : InspectionProfile(com.intellij.codeInspection.InspectionProfile) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) Language(com.intellij.lang.Language)

Example 17 with InspectionProfile

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

the class DisableInspectionToolAction method isAvailable.

@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    final InspectionProjectProfileManager profileManager = InspectionProjectProfileManager.getInstance(project);
    InspectionProfile inspectionProfile = profileManager.getCurrentProfile();
    InspectionToolWrapper toolWrapper = inspectionProfile.getInspectionTool(myToolId, project);
    return toolWrapper == null || toolWrapper.getDefaultLevel() != HighlightDisplayLevel.NON_SWITCHABLE_ERROR;
}
Also used : InspectionProfile(com.intellij.codeInspection.InspectionProfile) InspectionProjectProfileManager(com.intellij.profile.codeInspection.InspectionProjectProfileManager)

Example 18 with InspectionProfile

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

the class CodeCleanupAction method runInspections.

@Override
protected void runInspections(Project project, AnalysisScope scope) {
    final InspectionProfile profile = myExternalProfile != null ? myExternalProfile : InspectionProjectProfileManager.getInstance(project).getCurrentProfile();
    final InspectionManager managerEx = InspectionManager.getInstance(project);
    final GlobalInspectionContextBase globalContext = (GlobalInspectionContextBase) managerEx.createNewGlobalContext(false);
    globalContext.codeCleanup(scope, profile, getTemplatePresentation().getText(), null, false);
}
Also used : InspectionManager(com.intellij.codeInspection.InspectionManager) InspectionProfile(com.intellij.codeInspection.InspectionProfile) GlobalInspectionContextBase(com.intellij.codeInspection.ex.GlobalInspectionContextBase)

Example 19 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 20 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)

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