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());
}
}
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;
}
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);
}
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);
}
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;
}
Aggregations