mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
OC-6893 ("Run inspection by name" is not working)
This commit is contained in:
+1
-3
@@ -24,7 +24,6 @@ import com.intellij.codeInspection.visibility.VisibilityInspection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* @author Dmitry Avdeev
|
||||
@@ -60,8 +59,7 @@ public class GlobalInspectionContextTest extends CodeInsightTestCase {
|
||||
InspectionProfileEntry[] tools = profile.getInspectionTools(null);
|
||||
for (InspectionProfileEntry tool : tools) {
|
||||
if (!tool.isEnabledByDefault()) {
|
||||
GlobalInspectionContextImpl context = RunInspectionIntention.createContext(tool.getDisplayName(), Collections.singletonList(tool),
|
||||
(InspectionManagerEx)InspectionManager.getInstance(myProject), null);
|
||||
GlobalInspectionContextImpl context = RunInspectionIntention.createContext(tool, (InspectionManagerEx)InspectionManager.getInstance(myProject), null);
|
||||
context.initializeTools(new ArrayList<Tools>(), new ArrayList<Tools>(), new ArrayList<Tools>());
|
||||
assertEquals(1, context.getTools().size());
|
||||
return;
|
||||
|
||||
@@ -24,7 +24,6 @@ import com.intellij.ide.util.gotoByName.SimpleChooseByNameModel;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.profile.codeInspection.InspectionProfileManager;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.*;
|
||||
@@ -90,11 +89,6 @@ public class GotoInspectionModel extends SimpleChooseByNameModel {
|
||||
return result.toArray(new InspectionProfileEntry[result.size()]);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public InspectionProfileEntry getProfileEntryByName(String shortName) {
|
||||
return myToolShortNames.get(shortName);
|
||||
}
|
||||
|
||||
public String getElementName(final Object element) {
|
||||
if (element instanceof InspectionProfileEntry) {
|
||||
final InspectionProfileEntry entry = (InspectionProfileEntry)element;
|
||||
|
||||
+3
-22
@@ -43,10 +43,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
@@ -81,31 +78,15 @@ public class RunInspectionAction extends GotoActionBase {
|
||||
public void elementChosen(ChooseByNamePopup popup, final Object element) {
|
||||
ApplicationManager.getApplication().invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
InspectionProfileEntry profileEntry = (InspectionProfileEntry)element;
|
||||
LinkedHashSet<InspectionProfileEntry> dependentEntries = new LinkedHashSet<InspectionProfileEntry>();
|
||||
collectDependentInspections(profileEntry, dependentEntries, model);
|
||||
runInspection(project, profileEntry.getDisplayName(), new ArrayList<InspectionProfileEntry>(dependentEntries), virtualFile, psiElement, psiFile);
|
||||
runInspection(project, (InspectionProfileEntry)element, virtualFile, psiElement, psiFile);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void collectDependentInspections(@NotNull InspectionProfileEntry profileEntry,
|
||||
@NotNull LinkedHashSet<InspectionProfileEntry> dependentEntries,
|
||||
@NotNull GotoInspectionModel model) {
|
||||
dependentEntries.add(profileEntry);
|
||||
String mainToolId = profileEntry.getMainToolId();
|
||||
InspectionProfileEntry dependentEntry = mainToolId != null ? model.getProfileEntryByName(mainToolId) : null;
|
||||
|
||||
if (dependentEntry != null && !dependentEntries.contains(dependentEntry)) {
|
||||
collectDependentInspections(dependentEntry, dependentEntries, model);
|
||||
}
|
||||
}
|
||||
|
||||
private static void runInspection(@NotNull Project project,
|
||||
@NotNull String profileName,
|
||||
@NotNull List<InspectionProfileEntry> profileEntries,
|
||||
@NotNull InspectionProfileEntry profileEntry,
|
||||
@Nullable VirtualFile virtualFile,
|
||||
PsiElement psiElement, PsiFile psiFile) {
|
||||
final InspectionManagerEx managerEx = (InspectionManagerEx)InspectionManager.getInstance(project);
|
||||
@@ -158,6 +139,6 @@ public class RunInspectionAction extends GotoActionBase {
|
||||
if (!dlg.isOK()) return;
|
||||
final AnalysisUIOptions uiOptions = AnalysisUIOptions.getInstance(project);
|
||||
scope = dlg.getScope(uiOptions, scope, project, module);
|
||||
RunInspectionIntention.rerunInspection(profileName, profileEntries, managerEx, scope, psiFile);
|
||||
RunInspectionIntention.rerunInspection(profileEntry, managerEx, scope, psiFile);
|
||||
}
|
||||
}
|
||||
|
||||
+19
-16
@@ -35,6 +35,7 @@ import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleUtil;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.profile.codeInspection.InspectionProfileManager;
|
||||
import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
@@ -42,8 +43,8 @@ import com.intellij.util.IncorrectOperationException;
|
||||
import org.jdom.Element;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* User: anna
|
||||
@@ -72,7 +73,7 @@ public class RunInspectionIntention implements IntentionAction, HighPriorityActi
|
||||
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
final InspectionProfileEntry tool =
|
||||
InspectionProjectProfileManager.getInstance(project).getInspectionProfile().getInspectionTool(myShortName, file);
|
||||
InspectionProjectProfileManager.getInstance(project).getInspectionProfile().getInspectionTool(myShortName, file);
|
||||
if (tool instanceof LocalInspectionToolWrapper && ((LocalInspectionToolWrapper)tool).isUnfair()) {
|
||||
return false;
|
||||
}
|
||||
@@ -98,32 +99,34 @@ public class RunInspectionIntention implements IntentionAction, HighPriorityActi
|
||||
final AnalysisUIOptions uiOptions = AnalysisUIOptions.getInstance(project);
|
||||
analysisScope = dlg.getScope(uiOptions, analysisScope, project, module);
|
||||
final InspectionProfileEntry baseTool =
|
||||
InspectionProjectProfileManager.getInstance(project).getInspectionProfile().getInspectionTool(myShortName, file);
|
||||
rerunInspection(baseTool.getDisplayName(), Collections.singletonList(baseTool), managerEx, analysisScope, file);
|
||||
InspectionProjectProfileManager.getInstance(project).getInspectionProfile().getInspectionTool(myShortName, file);
|
||||
rerunInspection(baseTool, managerEx, analysisScope, file);
|
||||
}
|
||||
|
||||
public static void rerunInspection(final String profileName, final List<InspectionProfileEntry> baseTools,
|
||||
final InspectionManagerEx managerEx, final AnalysisScope scope,
|
||||
PsiElement psiElement) {
|
||||
GlobalInspectionContextImpl inspectionContext = createContext(profileName, baseTools, managerEx, psiElement);
|
||||
public static void rerunInspection(final InspectionProfileEntry baseTool, final InspectionManagerEx managerEx, final AnalysisScope scope,
|
||||
PsiElement psiElement) {
|
||||
GlobalInspectionContextImpl inspectionContext = createContext(baseTool, managerEx, psiElement);
|
||||
inspectionContext.doInspections(scope, managerEx);
|
||||
}
|
||||
|
||||
public static GlobalInspectionContextImpl createContext(final String profileName, final List<InspectionProfileEntry> baseTools,
|
||||
InspectionManagerEx managerEx, PsiElement psiElement) {
|
||||
final InspectionProfileImpl model = InspectionProfileImpl.createSimple(profileName, baseTools);
|
||||
public static GlobalInspectionContextImpl createContext(final InspectionProfileEntry baseTool, InspectionManagerEx managerEx, PsiElement psiElement) {
|
||||
final InspectionProfileImpl rootProfile = (InspectionProfileImpl)InspectionProfileManager.getInstance().getRootProfile();
|
||||
LinkedHashSet<InspectionProfileEntry> dependentEntries = new LinkedHashSet<InspectionProfileEntry>();
|
||||
GlobalInspectionContextImpl.collectDependentInspections(baseTool, dependentEntries, rootProfile);
|
||||
InspectionProfileEntry[] toolsArray = dependentEntries.toArray(new InspectionProfileEntry[dependentEntries.size()]);
|
||||
final InspectionProfileImpl model = InspectionProfileImpl.createSimple(baseTool.getDisplayName(), toolsArray);
|
||||
try {
|
||||
Element element = new Element("toCopy");
|
||||
|
||||
for (InspectionProfileEntry baseTool : baseTools) {
|
||||
baseTool.writeSettings(element);
|
||||
model.getInspectionTool(baseTool.getShortName(), psiElement).readSettings(element);
|
||||
for (InspectionProfileEntry tool : dependentEntries) {
|
||||
tool.writeSettings(element);
|
||||
model.getInspectionTool(tool.getShortName(), psiElement).readSettings(element);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
//skip
|
||||
}
|
||||
model.setEditable(profileName);
|
||||
model.setEditable(baseTool.getDisplayName());
|
||||
final GlobalInspectionContextImpl inspectionContext = managerEx.createNewGlobalContext(false);
|
||||
inspectionContext.setExternalProfile(model);
|
||||
return inspectionContext;
|
||||
|
||||
+38
-14
@@ -54,8 +54,10 @@ import com.intellij.psi.search.LocalSearchScope;
|
||||
import com.intellij.psi.search.scope.packageSet.NamedScope;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.ui.content.*;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.Processor;
|
||||
import com.intellij.util.TripleFunction;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.HashMap;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import gnu.trove.THashMap;
|
||||
@@ -70,10 +72,7 @@ import org.jetbrains.annotations.TestOnly;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
public class GlobalInspectionContextImpl extends UserDataHolderBase implements GlobalInspectionContext {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.ex.GlobalInspectionContextImpl");
|
||||
@@ -743,22 +742,47 @@ public class GlobalInspectionContextImpl extends UserDataHolderBase implements G
|
||||
}
|
||||
}
|
||||
|
||||
public static void collectDependentInspections(@NotNull InspectionProfileEntry profileEntry,
|
||||
@NotNull Set<InspectionProfileEntry> dependentEntries,
|
||||
@NotNull InspectionProfileImpl rootProfile) {
|
||||
dependentEntries.add(profileEntry);
|
||||
String mainToolId = profileEntry.getMainToolId();
|
||||
|
||||
if (mainToolId != null) {
|
||||
InspectionProfileEntry dependentEntry = rootProfile.getInspectionTool(mainToolId);
|
||||
|
||||
if (dependentEntry != null) {
|
||||
if (!dependentEntries.contains(dependentEntry)) {
|
||||
collectDependentInspections(dependentEntry, dependentEntries, rootProfile);
|
||||
}
|
||||
}
|
||||
else {
|
||||
LOG.error("Can't find main tool: " + mainToolId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected List<ToolsImpl> getUsedTools() {
|
||||
InspectionProfileImpl profile = new InspectionProfileImpl((InspectionProfileImpl)getCurrentProfile());
|
||||
List<ToolsImpl> tools = profile.getAllEnabledInspectionTools(myProject);
|
||||
THashSet<ToolsImpl> set = null;
|
||||
Set<InspectionProfileEntry> dependentTools = new LinkedHashSet<InspectionProfileEntry>();
|
||||
for (ToolsImpl tool : tools) {
|
||||
String id = tool.getTool().getMainToolId();
|
||||
if (id != null) {
|
||||
InspectionProfileEntry mainTool = profile.getInspectionTool(id);
|
||||
LOG.assertTrue(mainTool != null, "Can't find main tool: " + id);
|
||||
if (set == null) {
|
||||
set = new THashSet<ToolsImpl>(tools, TOOLS_HASHING_STRATEGY);
|
||||
collectDependentInspections(tool.getTool(), dependentTools, profile);
|
||||
}
|
||||
|
||||
if (!dependentTools.isEmpty()) {
|
||||
THashSet<ToolsImpl> set = new THashSet<ToolsImpl>(tools, TOOLS_HASHING_STRATEGY);
|
||||
set.addAll(ContainerUtil.map(dependentTools, new Function<InspectionProfileEntry, ToolsImpl>() {
|
||||
@Override
|
||||
public ToolsImpl fun(InspectionProfileEntry entry) {
|
||||
return new ToolsImpl(entry, entry.getDefaultLevel(), true);
|
||||
}
|
||||
set.add(new ToolsImpl(mainTool, mainTool.getDefaultLevel(), true));
|
||||
}
|
||||
}));
|
||||
return new ArrayList<ToolsImpl>(set);
|
||||
}
|
||||
else {
|
||||
return tools;
|
||||
}
|
||||
return set == null ? tools : new ArrayList<ToolsImpl>(set);
|
||||
}
|
||||
|
||||
private void classifyTool(List<Tools> outGlobalTools,
|
||||
|
||||
@@ -130,7 +130,7 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel,
|
||||
setProfileManager(InspectionProfileManager.getInstance());
|
||||
}
|
||||
|
||||
public static InspectionProfileImpl createSimple(String name, final List<InspectionProfileEntry> tools) {
|
||||
public static InspectionProfileImpl createSimple(String name, final InspectionProfileEntry... tools) {
|
||||
InspectionProfileImpl profile = new InspectionProfileImpl(name, new InspectionToolRegistrar(null) {
|
||||
@Override
|
||||
public List<InspectionToolWrapper> createTools() {
|
||||
|
||||
+1
-1
@@ -463,7 +463,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig
|
||||
final InspectionManagerEx inspectionManager,
|
||||
final InspectionTool... tools) {
|
||||
|
||||
final InspectionProfileImpl profile = InspectionProfileImpl.createSimple("test", Arrays.<InspectionProfileEntry>asList(tools));
|
||||
final InspectionProfileImpl profile = InspectionProfileImpl.createSimple("test", tools);
|
||||
GlobalInspectionContextImpl globalContext = new GlobalInspectionContextImpl(project, inspectionManager.getContentManager()) {
|
||||
@Override
|
||||
protected List<ToolsImpl> getUsedTools() {
|
||||
|
||||
Reference in New Issue
Block a user