Search in sources :

Example 6 with InspectionProfile

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

the class DomHighlightingLiteTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    myInspectionProfile = new MockInspectionProfile();
    myAnnotationsManager = new DomElementAnnotationsManagerImpl(getProject()) {

        @Override
        protected InspectionProfile getInspectionProfile(final DomFileElement fileElement) {
            return myInspectionProfile;
        }
    };
    final XmlFile file = createXmlFile("<a/>");
    final MockDomElement rootElement = new MockDomElement() {

        @Override
        @Nullable
        public XmlElement getXmlElement() {
            return getXmlTag();
        }

        @Override
        public XmlTag getXmlTag() {
            return file.getRootTag();
        }

        @NotNull
        @Override
        public Type getDomElementType() {
            return DomElement.class;
        }
    };
    myElement = new MockDomFileElement() {

        @Override
        @Nullable
        public XmlElement getXmlElement() {
            return file;
        }

        @Override
        @NotNull
        public XmlFile getFile() {
            return file;
        }

        @Override
        public DomElement getParent() {
            return null;
        }

        @Override
        @NotNull
        public DomElement getRootElement() {
            return rootElement;
        }

        @NotNull
        @Override
        public Class getRootElementClass() {
            return DomElement.class;
        }

        @Override
        public boolean isValid() {
            return true;
        }
    };
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) MockInspectionProfile(com.intellij.mock.MockInspectionProfile) InspectionProfile(com.intellij.codeInspection.InspectionProfile) NotNull(org.jetbrains.annotations.NotNull) MockInspectionProfile(com.intellij.mock.MockInspectionProfile) XmlElement(com.intellij.psi.xml.XmlElement) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with InspectionProfile

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

the class XmlTagInsertHandler method addRequiredAttributes.

@Nullable
private static StringBuilder addRequiredAttributes(XmlElementDescriptor descriptor, @Nullable XmlTag tag, Template template, PsiFile containingFile) {
    boolean htmlCode = HtmlUtil.hasHtml(containingFile) || HtmlUtil.supportsXmlTypedHandlers(containingFile);
    Set<String> notRequiredAttributes = Collections.emptySet();
    if (tag instanceof HtmlTag) {
        final InspectionProfile profile = InspectionProjectProfileManager.getInstance(tag.getProject()).getCurrentProfile();
        XmlEntitiesInspection inspection = (XmlEntitiesInspection) profile.getUnwrappedTool(XmlEntitiesInspection.REQUIRED_ATTRIBUTES_SHORT_NAME, tag);
        if (inspection != null) {
            StringTokenizer tokenizer = new StringTokenizer(inspection.getAdditionalEntries());
            notRequiredAttributes = new HashSet<>();
            while (tokenizer.hasMoreElements()) notRequiredAttributes.add(tokenizer.nextToken());
        }
    }
    XmlAttributeDescriptor[] attributes = descriptor.getAttributesDescriptors(tag);
    StringBuilder indirectRequiredAttrs = null;
    if (WebEditorOptions.getInstance().isAutomaticallyInsertRequiredAttributes()) {
        final XmlExtension extension = XmlExtension.getExtension(containingFile);
        for (XmlAttributeDescriptor attributeDecl : attributes) {
            String attributeName = attributeDecl.getName(tag);
            boolean shouldBeInserted = extension.shouldBeInserted(attributeDecl);
            if (shouldBeInserted && (tag == null || tag.getAttributeValue(attributeName) == null)) {
                if (!notRequiredAttributes.contains(attributeName)) {
                    if (!extension.isIndirectSyntax(attributeDecl)) {
                        template.addTextSegment(" " + attributeName + "=" + XmlAttributeInsertHandler.getAttributeQuote(htmlCode));
                        template.addVariable(new MacroCallNode(new CompleteMacro()), true);
                        template.addTextSegment(XmlAttributeInsertHandler.getAttributeQuote(htmlCode));
                    } else {
                        if (indirectRequiredAttrs == null)
                            indirectRequiredAttrs = new StringBuilder();
                        indirectRequiredAttrs.append("\n<jsp:attribute name=\"").append(attributeName).append("\"></jsp:attribute>\n");
                    }
                }
            } else if (shouldBeInserted && attributeDecl.isFixed() && attributeDecl.getDefaultValue() != null && !htmlCode) {
                template.addTextSegment(" " + attributeName + "=" + XmlAttributeInsertHandler.getAttributeQuote(false) + attributeDecl.getDefaultValue() + XmlAttributeInsertHandler.getAttributeQuote(false));
            }
        }
    }
    return indirectRequiredAttrs;
}
Also used : InspectionProfile(com.intellij.codeInspection.InspectionProfile) HtmlTag(com.intellij.psi.html.HtmlTag) CompleteMacro(com.intellij.codeInsight.template.macro.CompleteMacro) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) XmlEntitiesInspection(com.intellij.codeInspection.htmlInspections.XmlEntitiesInspection) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with InspectionProfile

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

the class QuickChangeInspectionProfileAction method fillActions.

@Override
protected void fillActions(Project project, @NotNull DefaultActionGroup group, @NotNull DataContext dataContext) {
    final ProjectInspectionProfileManager projectProfileManager = ProjectInspectionProfileManager.getInstance(project);
    InspectionProfile current = projectProfileManager.getCurrentProfile();
    for (InspectionProfile profile : projectProfileManager.getProfiles()) {
        addScheme(group, projectProfileManager, current, profile);
    }
}
Also used : InspectionProfile(com.intellij.codeInspection.InspectionProfile) ProjectInspectionProfileManager(com.intellij.profile.codeInspection.ProjectInspectionProfileManager)

Example 9 with InspectionProfile

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

the class CodeInspectionAction method getAdditionalActionSettings.

@Override
protected JComponent getAdditionalActionSettings(@NotNull final Project project, final BaseAnalysisActionDialog dialog) {
    final AdditionalPanel panel = new AdditionalPanel();
    final InspectionManagerEx manager = (InspectionManagerEx) InspectionManager.getInstance(project);
    final SchemesCombo<InspectionProfileImpl> profiles = (SchemesCombo<InspectionProfileImpl>) panel.myBrowseProfilesCombo.getComboBox();
    final InspectionProfileManager profileManager = InspectionProfileManager.getInstance();
    final ProjectInspectionProfileManager projectProfileManager = ProjectInspectionProfileManager.getInstance(project);
    panel.myBrowseProfilesCombo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final ExternalProfilesComboboxAwareInspectionToolsConfigurable errorConfigurable = createConfigurable(projectProfileManager, profiles);
            final MySingleConfigurableEditor editor = new MySingleConfigurableEditor(project, errorConfigurable, manager);
            if (editor.showAndGet()) {
                reloadProfiles(profiles, profileManager, projectProfileManager, project);
                if (errorConfigurable.mySelectedName != null) {
                    final InspectionProfileImpl profile = (errorConfigurable.mySelectedIsProjectProfile ? projectProfileManager : profileManager).getProfile(errorConfigurable.mySelectedName);
                    profiles.selectScheme(profile);
                }
            } else {
                //if profile was disabled and cancel after apply was pressed
                final InspectionProfile profile = profiles.getSelectedScheme();
                final boolean canExecute = profile != null && profile.isExecutable(project);
                dialog.setOKActionEnabled(canExecute);
            }
        }
    });
    profiles.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            myExternalProfile = profiles.getSelectedScheme();
            final boolean canExecute = myExternalProfile != null && myExternalProfile.isExecutable(project);
            dialog.setOKActionEnabled(canExecute);
            if (canExecute) {
                PropertiesComponent.getInstance(project).setValue(LAST_SELECTED_PROFILE_PROP, (myExternalProfile.isProjectLevel() ? 'p' : 'a') + myExternalProfile.getName());
                manager.setProfile(myExternalProfile.getName());
            }
        }
    });
    reloadProfiles(profiles, profileManager, projectProfileManager, project);
    return panel.myAdditionalPanel;
}
Also used : InspectionProfileImpl(com.intellij.codeInspection.ex.InspectionProfileImpl) InspectionProfile(com.intellij.codeInspection.InspectionProfile) ActionEvent(java.awt.event.ActionEvent) InspectionManagerEx(com.intellij.codeInspection.ex.InspectionManagerEx) ActionListener(java.awt.event.ActionListener) SchemesCombo(com.intellij.application.options.schemes.SchemesCombo) InspectionProfileManager(com.intellij.profile.codeInspection.InspectionProfileManager) ProjectInspectionProfileManager(com.intellij.profile.codeInspection.ProjectInspectionProfileManager) ProjectInspectionProfileManager(com.intellij.profile.codeInspection.ProjectInspectionProfileManager)

Example 10 with InspectionProfile

use of com.intellij.codeInspection.InspectionProfile 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

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