use of com.intellij.codeHighlighting.HighlightDisplayLevel in project intellij-community by JetBrains.
the class LightInspectionTestCase method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
for (String environmentClass : getEnvironmentClasses()) {
myFixture.addClass(environmentClass);
}
final InspectionProfileEntry inspection = getInspection();
if (inspection != null) {
myFixture.enableInspections(inspection);
final Project project = myFixture.getProject();
final HighlightDisplayKey displayKey = HighlightDisplayKey.find(inspection.getShortName());
final InspectionProfileImpl currentProfile = ProjectInspectionProfileManager.getInstance(project).getCurrentProfile();
final HighlightDisplayLevel errorLevel = currentProfile.getErrorLevel(displayKey, null);
if (errorLevel == HighlightDisplayLevel.DO_NOT_SHOW) {
currentProfile.setErrorLevel(displayKey, HighlightDisplayLevel.WARNING, project);
}
}
Sdk sdk = ModuleRootManager.getInstance(ModuleManager.getInstance(getProject()).getModules()[0]).getSdk();
if (JAVA_1_7.getSdk().getName().equals(sdk == null ? null : sdk.getName())) {
PsiClass object = JavaPsiFacade.getInstance(getProject()).findClass("java.lang.Object", GlobalSearchScope.allScope(getProject()));
assertNotNull(object);
PsiClass component = JavaPsiFacade.getInstance(getProject()).findClass("java.awt.Component", GlobalSearchScope.allScope(getProject()));
assertNotNull(component);
}
}
use of com.intellij.codeHighlighting.HighlightDisplayLevel in project intellij-community by JetBrains.
the class InspectionProfileConvertor method processElement.
protected boolean processElement(final Element option, final String name) {
if (name.equals(DISPLAY_LEVEL_MAP_OPTION)) {
for (Element e : option.getChild(VALUE_ATT).getChildren()) {
String key = e.getName();
String levelName = e.getAttributeValue(LEVEL_ATT);
HighlightSeverity severity = myManager.getSeverityRegistrar().getSeverity(levelName);
HighlightDisplayLevel level = severity == null ? null : HighlightDisplayLevel.find(severity);
if (level == null)
continue;
myDisplayLevelMap.put(key, level);
}
return true;
}
return false;
}
use of com.intellij.codeHighlighting.HighlightDisplayLevel in project intellij-community by JetBrains.
the class InspectionProfileConvertor method fillErrorLevels.
protected void fillErrorLevels(final InspectionProfileImpl profile) {
//noinspection ConstantConditions
LOG.assertTrue(profile.getInspectionTools(null) != null, "Profile was not correctly init");
//fill error levels
for (final String shortName : myDisplayLevelMap.keySet()) {
//key <-> short name
HighlightDisplayLevel level = myDisplayLevelMap.get(shortName);
HighlightDisplayKey key = HighlightDisplayKey.find(shortName);
if (key == null)
continue;
//set up tools for default profile
if (level != HighlightDisplayLevel.DO_NOT_SHOW) {
profile.enableTool(shortName, null);
}
if (level == null || level == HighlightDisplayLevel.DO_NOT_SHOW) {
level = HighlightDisplayLevel.WARNING;
}
profile.setErrorLevel(key, level, null);
}
}
use of com.intellij.codeHighlighting.HighlightDisplayLevel in project android by JetBrains.
the class AndroidLintInspectionToolProviderTest method checkAllLintChecksRegistered.
@SuppressWarnings("deprecation")
public static boolean checkAllLintChecksRegistered(Project project) throws Exception {
if (ourDone) {
return true;
}
ourDone = true;
// For some reason, I can't just use
// AndroidLintInspectionBase.getInspectionShortNameByIssue
// to iterate the available inspections from unit tests; at runtime this will enumerate all
// the available inspections, but from unit tests (even when extending IdeaTestCase) it's empty.
// So instead we take advantage of the knowledge that all our inspections are named in a particular
// way from the lint issue id's, so we can use reflection to find the classes.
// This won't catch cases if we declare a class there and forget to register in the plugin, but
// it's better than nothing.
Set<String> registered = Sets.newHashSetWithExpectedSize(200);
Set<Issue> quickfixes = Sets.newLinkedHashSetWithExpectedSize(200);
final LintIdeIssueRegistry fullRegistry = new LintIdeIssueRegistry();
List<Issue> allIssues = fullRegistry.getIssues();
for (Issue issue : allIssues) {
if (!isRelevant(issue)) {
continue;
}
String className = "com.android.tools.idea.lint.AndroidLint" + issue.getId() + "Inspection";
try {
Class<?> c = Class.forName(className);
if (AndroidLintInspectionBase.class.isAssignableFrom(c) && ((c.getModifiers() & Modifier.ABSTRACT) == 0)) {
AndroidLintInspectionBase provider = (AndroidLintInspectionBase) c.newInstance();
registered.add(provider.getIssue().getId());
boolean hasQuickFix = true;
try {
provider.getClass().getDeclaredMethod("getQuickFixes", String.class);
} catch (NoSuchMethodException e1) {
try {
provider.getClass().getDeclaredMethod("getQuickFixes", PsiElement.class, PsiElement.class, String.class);
} catch (NoSuchMethodException e2) {
hasQuickFix = false;
}
}
if (hasQuickFix) {
quickfixes.add(provider.getIssue());
}
}
} catch (ClassNotFoundException ignore) {
}
}
final List<Issue> missing = new ArrayList<>();
for (Issue issue : allIssues) {
if (!isRelevant(issue) || registered.contains(issue.getId())) {
continue;
}
// When enabled, adjust this to register class based registrations
assertFalse(LintIdeProject.SUPPORT_CLASS_FILES);
Implementation implementation = issue.getImplementation();
if (implementation.getScope().contains(Scope.CLASS_FILE) || implementation.getScope().contains(Scope.ALL_CLASS_FILES) || implementation.getScope().contains(Scope.JAVA_LIBRARIES)) {
boolean isOk = false;
for (EnumSet<Scope> analysisScope : implementation.getAnalysisScopes()) {
if (!analysisScope.contains(Scope.CLASS_FILE) && !analysisScope.contains(Scope.ALL_CLASS_FILES) && !analysisScope.contains(Scope.JAVA_LIBRARIES)) {
isOk = true;
break;
}
}
if (!isOk) {
System.out.println("Skipping issue " + issue + " because it requires classfile analysis. Consider rewriting in IDEA.");
continue;
}
}
final String inspectionShortName = AndroidLintInspectionBase.getInspectionShortNameByIssue(project, issue);
if (inspectionShortName == null) {
missing.add(issue);
continue;
}
// ELSE: compare severity, enabledByDefault, etc, message, etc
final HighlightDisplayKey key = HighlightDisplayKey.find(inspectionShortName);
if (key == null) {
//fail("No highlight display key for inspection " + inspectionShortName + " for issue " + issue);
System.out.println("No highlight display key for inspection " + inspectionShortName + " for issue " + issue);
continue;
}
final InspectionProfile profile = InspectionProjectProfileManager.getInstance(project).getInspectionProfile();
HighlightDisplayLevel errorLevel = profile.getErrorLevel(key, null);
Severity s;
if (errorLevel == HighlightDisplayLevel.WARNING || errorLevel == HighlightDisplayLevel.WEAK_WARNING) {
s = Severity.WARNING;
} else if (errorLevel == HighlightDisplayLevel.ERROR || errorLevel == HighlightDisplayLevel.NON_SWITCHABLE_ERROR) {
s = Severity.ERROR;
} else if (errorLevel == HighlightDisplayLevel.DO_NOT_SHOW) {
s = Severity.IGNORE;
} else if (errorLevel == HighlightDisplayLevel.INFO) {
s = Severity.INFORMATIONAL;
} else {
//fail("Unexpected error level " + errorLevel);
System.out.println("Unexpected error level " + errorLevel);
continue;
}
Severity expectedSeverity = issue.getDefaultSeverity();
if (expectedSeverity == Severity.FATAL) {
expectedSeverity = Severity.ERROR;
}
if (expectedSeverity != s) {
System.out.println("Wrong severity for " + issue + "; expected " + expectedSeverity + ", got " + s);
}
}
// Spit out registration information for the missing elements
if (!missing.isEmpty()) {
missing.sort((issue1, issue2) -> String.CASE_INSENSITIVE_ORDER.compare(issue1.getId(), issue2.getId()));
StringBuilder sb = new StringBuilder(1000);
sb.append("Missing registration for ").append(missing.size()).append(" issues (out of a total issue count of ").append(allIssues.size()).append(")");
sb.append("\nAdd to android/src/META-INF/android-plugin.xml (and please try to preserve the case insensitive alphabetical order):\n");
for (Issue issue : missing) {
sb.append(" <globalInspection hasStaticDescription=\"true\" shortName=\"");
sb.append(LINT_INSPECTION_PREFIX);
String id = issue.getId();
sb.append(id);
sb.append("\" displayName=\"");
sb.append(XmlUtils.toXmlAttributeValue(issue.getBriefDescription(TextFormat.TEXT)));
sb.append("\" groupKey=\"").append(getCategoryBundleKey(issue.getCategory())).append("\" bundle=\"messages.AndroidBundle\" enabledByDefault=\"");
sb.append(issue.isEnabledByDefault());
sb.append("\" level=\"");
sb.append(issue.getDefaultSeverity() == Severity.ERROR || issue.getDefaultSeverity() == Severity.FATAL ? "ERROR" : issue.getDefaultSeverity() == Severity.WARNING ? "WARNING" : "INFO");
sb.append("\" implementationClass=\"com.android.tools.idea.lint.AndroidLint");
sb.append(id);
sb.append("Inspection\"/>\n");
}
sb.append("\nAdd to com.android.tools.idea.lint:\n");
for (Issue issue : missing) {
String id = issue.getId();
String detectorClass = getDetectorClass(issue).getName();
String detectorName = getDetectorClass(issue).getSimpleName();
String issueName = getIssueFieldName(issue);
String messageKey = getMessageKey(issue);
//noinspection StringConcatenationInsideStringBufferAppend
sb.append("/*\n" + " * Copyright (C) 2016 The Android Open Source Project\n" + " *\n" + " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" + " * you may not use this file except in compliance with the License.\n" + " * You may obtain a copy of the License at\n" + " *\n" + " * http://www.apache.org/licenses/LICENSE-2.0\n" + " *\n" + " * Unless required by applicable law or agreed to in writing, software\n" + " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" + " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + " * See the License for the specific language governing permissions and\n" + " * limitations under the License.\n" + " */\n" + "package com.android.tools.idea.lint;\n" + "\n" + "import " + detectorClass + ";\n" + "import org.jetbrains.android.inspections.lint.AndroidLintInspectionBase;\n" + "import org.jetbrains.android.util.AndroidBundle;\n" + "\n" + "public class AndroidLint" + id + "Inspection extends AndroidLintInspectionBase {\n" + " public AndroidLint" + id + "Inspection() {\n" + " super(AndroidBundle.message(\"android.lint.inspections." + messageKey + "\"), " + detectorName + "." + issueName + ");\n" + " }\n" + "}\n");
}
sb.append("\nAdd to AndroidBundle.properties:\n");
for (Issue issue : missing) {
String messageKey = getMessageKey(issue);
sb.append("android.lint.inspections.").append(messageKey).append("=").append(escapePropertyValue(getBriefDescription(issue))).append("\n");
}
sb.append("\nAdded registrations for ").append(missing.size()).append(" issues (out of a total issue count of ").append(allIssues.size()).append(")\n");
System.out.println("*IF* necessary, add these category descriptors to AndroidBundle.properties:\n");
Set<Category> categories = Sets.newHashSet();
for (Issue issue : missing) {
categories.add(issue.getCategory());
}
List<Category> sorted = Lists.newArrayList(categories);
Collections.sort(sorted);
for (Category category : sorted) {
sb.append(getCategoryBundleKey(category)).append('=').append(escapePropertyValue(("Android > Lint > " + category.getFullName()).replace(":", " > "))).append("\n");
}
System.out.println(sb.toString());
return false;
} else if (LIST_ISSUES_WITH_QUICK_FIXES) {
System.out.println("The following inspections have quickfixes (used for Reporter.java):\n");
List<String> fields = Lists.newArrayListWithExpectedSize(quickfixes.size());
Set<String> imports = Sets.newHashSetWithExpectedSize(quickfixes.size());
// These two are handled by the ResourceTypeInspection's quickfixes; they're
// not handled by lint per se, but on the command line (in HTML reports) they're
// flagged by lint, so include them in the list
quickfixes.add(SupportAnnotationDetector.CHECK_PERMISSION);
quickfixes.add(SupportAnnotationDetector.MISSING_PERMISSION);
quickfixes.add(SupportAnnotationDetector.CHECK_RESULT);
for (Issue issue : quickfixes) {
String detectorName = getDetectorClass(issue).getName();
imports.add(detectorName);
int index = detectorName.lastIndexOf('.');
if (index != -1) {
detectorName = detectorName.substring(index + 1);
}
String issueName = getIssueFieldName(issue);
fields.add(detectorName + "." + issueName);
}
Collections.sort(fields);
List<String> sortedImports = Lists.newArrayList(imports);
Collections.sort(sortedImports);
for (String cls : sortedImports) {
System.out.println("import " + cls + ";");
}
System.out.println();
System.out.println("sStudioFixes = Sets.newHashSet(\n " + Joiner.on(",\n ").join(fields) + "\n);\n");
}
return true;
}
use of com.intellij.codeHighlighting.HighlightDisplayLevel in project intellij-community by JetBrains.
the class SingleInspectionProfilePanel method updateOptionsAndDescriptionPanel.
private void updateOptionsAndDescriptionPanel(final TreePath... paths) {
if (myProfile == null || paths == null || paths.length == 0) {
return;
}
final TreePath path = paths[0];
if (path == null)
return;
final List<InspectionConfigTreeNode> nodes = InspectionsAggregationUtil.getInspectionsNodes(paths);
if (!nodes.isEmpty()) {
final InspectionConfigTreeNode singleNode = paths.length == 1 && ((InspectionConfigTreeNode) paths[0].getLastPathComponent()).getDefaultDescriptor() != null ? ContainerUtil.getFirstItem(nodes) : null;
if (singleNode != null) {
final Descriptor descriptor = singleNode.getDefaultDescriptor();
LOG.assertTrue(descriptor != null);
if (descriptor.loadDescription() != null) {
// need this in order to correctly load plugin-supplied descriptions
final Descriptor defaultDescriptor = singleNode.getDefaultDescriptor();
final String description = defaultDescriptor.loadDescription();
try {
if (!readHTML(myBrowser, SearchUtil.markup(toHTML(myBrowser, description, false), myProfileFilter.getFilter()))) {
readHTML(myBrowser, toHTML(myBrowser, "<b>" + UNDER_CONSTRUCTION + "</b>", false));
}
} catch (Throwable t) {
LOG.error("Failed to load description for: " + defaultDescriptor.getToolWrapper().getTool().getClass() + "; description: " + description, t);
}
} else {
readHTML(myBrowser, toHTML(myBrowser, "Can't find inspection description.", false));
}
} else {
readHTML(myBrowser, toHTML(myBrowser, "Multiple inspections are selected. You can edit them as a single inspection.", false));
}
myOptionsPanel.removeAll();
final Project project = myProjectProfileManager.getProject();
final JPanel severityPanel = new JPanel(new GridBagLayout());
final JPanel configPanelAnchor = new JPanel(new GridLayout());
final Set<String> scopesNames = new THashSet<>();
for (final InspectionConfigTreeNode node : nodes) {
final List<ScopeToolState> nonDefaultTools = myProfile.getNonDefaultTools(node.getDefaultDescriptor().getKey().toString(), project);
for (final ScopeToolState tool : nonDefaultTools) {
scopesNames.add(tool.getScopeName());
}
}
final double severityPanelWeightY;
if (scopesNames.isEmpty()) {
final LevelChooserAction severityLevelChooser = new LevelChooserAction(myProfile.getProfileManager().getOwnSeverityRegistrar(), includeDoNotShow(nodes)) {
@Override
protected void onChosen(final HighlightSeverity severity) {
final HighlightDisplayLevel level = HighlightDisplayLevel.find(severity);
for (final InspectionConfigTreeNode node : nodes) {
final HighlightDisplayKey key = node.getDefaultDescriptor().getKey();
final NamedScope scope = node.getDefaultDescriptor().getScope();
final boolean toUpdate = myProfile.getErrorLevel(key, scope, project) != level;
myProfile.setErrorLevel(key, level, null, project);
if (toUpdate)
node.dropCache();
}
myTreeTable.updateUI();
}
};
final HighlightSeverity severity = ScopesAndSeveritiesTable.getSeverity(ContainerUtil.map(nodes, node -> node.getDefaultDescriptor().getState()));
severityLevelChooser.setChosen(severity);
final ScopesChooser scopesChooser = new ScopesChooser(ContainerUtil.map(nodes, node -> node.getDefaultDescriptor()), myProfile, project, null) {
@Override
protected void onScopesOrderChanged() {
myTreeTable.updateUI();
updateOptionsAndDescriptionPanel();
}
@Override
protected void onScopeAdded() {
myTreeTable.updateUI();
updateOptionsAndDescriptionPanel();
}
};
severityPanel.add(new JLabel(InspectionsBundle.message("inspection.severity")), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.VERTICAL, JBUI.insets(10, 0), 0, 0));
final JComponent severityLevelChooserComponent = severityLevelChooser.createCustomComponent(severityLevelChooser.getTemplatePresentation());
severityPanel.add(severityLevelChooserComponent, new GridBagConstraints(1, 0, 1, 1, 0, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, JBUI.insets(10, 0), 0, 0));
final JComponent scopesChooserComponent = scopesChooser.createCustomComponent(scopesChooser.getTemplatePresentation());
severityPanel.add(scopesChooserComponent, new GridBagConstraints(2, 0, 1, 1, 0, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, JBUI.insets(10, 0), 0, 0));
final JLabel label = new JLabel("", SwingConstants.RIGHT);
severityPanel.add(label, new GridBagConstraints(3, 0, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH, JBUI.insets(2, 0), 0, 0));
severityPanelWeightY = 0.0;
if (singleNode != null) {
setConfigPanel(configPanelAnchor, myProfile.getToolDefaultState(singleNode.getDefaultDescriptor().getKey().toString(), project));
}
} else {
if (singleNode != null) {
for (final Descriptor descriptor : singleNode.getDescriptors().getNonDefaultDescriptors()) {
descriptor.loadConfig();
}
}
final JTable scopesAndScopesAndSeveritiesTable = new ScopesAndSeveritiesTable(new ScopesAndSeveritiesTable.TableSettings(nodes, myProfile, project) {
@Override
protected void onScopeChosen(@NotNull final ScopeToolState state) {
setConfigPanel(configPanelAnchor, state);
configPanelAnchor.revalidate();
configPanelAnchor.repaint();
}
@Override
protected void onSettingsChanged() {
update(false);
}
@Override
protected void onScopeAdded() {
update(true);
}
@Override
protected void onScopesOrderChanged() {
update(true);
}
@Override
protected void onScopeRemoved(final int scopesCount) {
update(scopesCount == 1);
}
private void update(final boolean updateOptionsAndDescriptionPanel) {
Queue<InspectionConfigTreeNode> q = new Queue<>(nodes.size());
for (InspectionConfigTreeNode node : nodes) {
q.addLast(node);
}
while (!q.isEmpty()) {
final InspectionConfigTreeNode inspectionConfigTreeNode = q.pullFirst();
inspectionConfigTreeNode.dropCache();
final TreeNode parent = inspectionConfigTreeNode.getParent();
if (parent != null && parent.getParent() != null) {
q.addLast((InspectionConfigTreeNode) parent);
}
}
myTreeTable.updateUI();
if (updateOptionsAndDescriptionPanel) {
updateOptionsAndDescriptionPanel();
}
}
});
final ToolbarDecorator wrappedTable = ToolbarDecorator.createDecorator(scopesAndScopesAndSeveritiesTable).disableUpDownActions().setRemoveActionUpdater(new AnActionButtonUpdater() {
@Override
public boolean isEnabled(AnActionEvent e) {
final int selectedRow = scopesAndScopesAndSeveritiesTable.getSelectedRow();
final int rowCount = scopesAndScopesAndSeveritiesTable.getRowCount();
return rowCount - 1 != selectedRow;
}
});
final JPanel panel = wrappedTable.createPanel();
panel.setMinimumSize(new Dimension(getMinimumSize().width, 3 * scopesAndScopesAndSeveritiesTable.getRowHeight()));
severityPanel.add(new JBLabel("Severity by Scope"), new GridBagConstraints(0, 0, 1, 1, 1.0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, JBUI.insets(5, 0, 2, 10), 0, 0));
severityPanel.add(panel, new GridBagConstraints(0, 1, 1, 1, 0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, JBUI.insets(0, 0, 0, 0), 0, 0));
severityPanelWeightY = 0.3;
}
myOptionsPanel.add(severityPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, severityPanelWeightY, GridBagConstraints.WEST, GridBagConstraints.BOTH, JBUI.insets(0, 2, 0, 0), 0, 0));
if (configPanelAnchor.getComponentCount() != 0) {
configPanelAnchor.setBorder(IdeBorderFactory.createTitledBorder("Options", false, new JBInsets(7, 0, 0, 0)));
}
GuiUtils.enableChildren(myOptionsPanel, isThoughOneNodeEnabled(nodes));
if (configPanelAnchor.getComponentCount() != 0 || scopesNames.isEmpty()) {
myOptionsPanel.add(configPanelAnchor, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, JBUI.insets(0, 2, 0, 0), 0, 0));
}
myOptionsPanel.revalidate();
} else {
initOptionsAndDescriptionPanel();
}
myOptionsPanel.repaint();
}
Aggregations