Search in sources :

Example 1 with CleanupLocalInspectionTool

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

the class RunInspectionAction method runInspection.

public static void runInspection(@NotNull final Project project, @NotNull String shortName, @Nullable VirtualFile virtualFile, PsiElement psiElement, PsiFile psiFile) {
    final PsiElement element = psiFile == null ? psiElement : psiFile;
    final InspectionProfile currentProfile = InspectionProjectProfileManager.getInstance(project).getCurrentProfile();
    final InspectionToolWrapper toolWrapper = element != null ? currentProfile.getInspectionTool(shortName, element) : currentProfile.getInspectionTool(shortName, project);
    LOGGER.assertTrue(toolWrapper != null, "Missed inspection: " + shortName);
    final InspectionManagerEx managerEx = (InspectionManagerEx) InspectionManager.getInstance(project);
    final Module module = virtualFile != null ? ModuleUtilCore.findModuleForFile(virtualFile, project) : null;
    AnalysisScope analysisScope = null;
    if (psiFile != null) {
        analysisScope = new AnalysisScope(psiFile);
    } else {
        if (virtualFile != null && virtualFile.isDirectory()) {
            final PsiDirectory psiDirectory = PsiManager.getInstance(project).findDirectory(virtualFile);
            if (psiDirectory != null) {
                analysisScope = new AnalysisScope(psiDirectory);
            }
        }
        if (analysisScope == null && virtualFile != null) {
            analysisScope = new AnalysisScope(project, Arrays.asList(virtualFile));
        }
        if (analysisScope == null) {
            analysisScope = new AnalysisScope(project);
        }
    }
    final AnalysisUIOptions options = AnalysisUIOptions.getInstance(project);
    final FileFilterPanel fileFilterPanel = new FileFilterPanel();
    fileFilterPanel.init(options);
    final AnalysisScope initialAnalysisScope = analysisScope;
    final BaseAnalysisActionDialog dialog = new BaseAnalysisActionDialog("Run '" + toolWrapper.getDisplayName() + "'", AnalysisScopeBundle.message("analysis.scope.title", InspectionsBundle.message("inspection.action.noun")), project, analysisScope, module != null ? module.getName() : null, true, options, psiElement) {

        private InspectionToolWrapper myUpdatedSettingsToolWrapper;

        @Nullable
        @Override
        protected JComponent getAdditionalActionSettings(Project project) {
            final JPanel fileFilter = fileFilterPanel.getPanel();
            if (toolWrapper.getTool().createOptionsPanel() != null) {
                JPanel additionPanel = new JPanel();
                additionPanel.setLayout(new BoxLayout(additionPanel, BoxLayout.Y_AXIS));
                additionPanel.add(fileFilter);
                //new InheritOptionsForToolPanel(toolWrapper.getShortName(), project);
                myUpdatedSettingsToolWrapper = copyToolWithSettings(toolWrapper);
                additionPanel.add(new TitledSeparator(IdeBundle.message("goto.inspection.action.choose.inherit.settings.from")));
                JComponent optionsPanel = myUpdatedSettingsToolWrapper.getTool().createOptionsPanel();
                LOGGER.assertTrue(optionsPanel != null);
                if (UIUtil.hasScrollPane(optionsPanel)) {
                    additionPanel.add(optionsPanel);
                } else {
                    additionPanel.add(ScrollPaneFactory.createScrollPane(optionsPanel, SideBorder.NONE));
                }
                return additionPanel;
            } else {
                return fileFilter;
            }
        }

        @NotNull
        @Override
        public AnalysisScope getScope(@NotNull AnalysisUIOptions uiOptions, @NotNull AnalysisScope defaultScope, @NotNull Project project, Module module) {
            final AnalysisScope scope = super.getScope(uiOptions, defaultScope, project, module);
            final GlobalSearchScope filterScope = fileFilterPanel.getSearchScope();
            if (filterScope == null) {
                return scope;
            }
            scope.setFilter(filterScope);
            return scope;
        }

        private AnalysisScope getScope() {
            return getScope(options, initialAnalysisScope, project, module);
        }

        private InspectionToolWrapper getToolWrapper() {
            return myUpdatedSettingsToolWrapper == null ? toolWrapper : myUpdatedSettingsToolWrapper;
        }

        @NotNull
        @Override
        protected Action[] createActions() {
            final List<Action> actions = new ArrayList<>();
            final boolean hasFixAll = toolWrapper.getTool() instanceof CleanupLocalInspectionTool;
            actions.add(new AbstractAction(hasFixAll ? AnalysisScopeBundle.message("action.analyze.verb") : CommonBundle.getOkButtonText()) {

                {
                    putValue(DEFAULT_ACTION, Boolean.TRUE);
                }

                @Override
                public void actionPerformed(ActionEvent e) {
                    RunInspectionIntention.rerunInspection(getToolWrapper(), managerEx, getScope(), null);
                    close(DialogWrapper.OK_EXIT_CODE);
                }
            });
            if (hasFixAll) {
                actions.add(new AbstractAction("Fix All") {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        InspectionToolWrapper wrapper = getToolWrapper();
                        InspectionProfileImpl cleanupToolProfile = RunInspectionIntention.createProfile(wrapper, managerEx, null);
                        managerEx.createNewGlobalContext(false).codeCleanup(getScope(), cleanupToolProfile, "Cleanup by " + wrapper.getDisplayName(), null, false);
                        close(DialogWrapper.OK_EXIT_CODE);
                    }
                });
            }
            actions.add(getCancelAction());
            if (SystemInfo.isMac) {
                Collections.reverse(actions);
            }
            return actions.toArray(new Action[actions.size()]);
        }
    };
    dialog.showAndGet();
}
Also used : InspectionProfileImpl(com.intellij.codeInspection.ex.InspectionProfileImpl) ActionEvent(java.awt.event.ActionEvent) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull) BaseAnalysisActionDialog(com.intellij.analysis.BaseAnalysisActionDialog) AnalysisScope(com.intellij.analysis.AnalysisScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) AnalysisUIOptions(com.intellij.analysis.AnalysisUIOptions) InspectionProfile(com.intellij.codeInspection.InspectionProfile) InspectionManagerEx(com.intellij.codeInspection.ex.InspectionManagerEx) Project(com.intellij.openapi.project.Project) CleanupLocalInspectionTool(com.intellij.codeInspection.CleanupLocalInspectionTool) TitledSeparator(com.intellij.ui.TitledSeparator) Module(com.intellij.openapi.module.Module) InspectionToolWrapper(com.intellij.codeInspection.ex.InspectionToolWrapper)

Aggregations

AnalysisScope (com.intellij.analysis.AnalysisScope)1 AnalysisUIOptions (com.intellij.analysis.AnalysisUIOptions)1 BaseAnalysisActionDialog (com.intellij.analysis.BaseAnalysisActionDialog)1 CleanupLocalInspectionTool (com.intellij.codeInspection.CleanupLocalInspectionTool)1 InspectionProfile (com.intellij.codeInspection.InspectionProfile)1 InspectionManagerEx (com.intellij.codeInspection.ex.InspectionManagerEx)1 InspectionProfileImpl (com.intellij.codeInspection.ex.InspectionProfileImpl)1 InspectionToolWrapper (com.intellij.codeInspection.ex.InspectionToolWrapper)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 TitledSeparator (com.intellij.ui.TitledSeparator)1 ActionEvent (java.awt.event.ActionEvent)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1