use of com.intellij.util.graph.InboundSemiGraph in project intellij-community by JetBrains.
the class TypeMigrationLabeler method sortMigratedUsages.
private TypeMigrationUsageInfo[] sortMigratedUsages(TypeMigrationUsageInfo[] infos) {
final DFSTBuilder<TypeMigrationUsageInfo> builder = new DFSTBuilder<>(GraphGenerator.generate(new InboundSemiGraph<TypeMigrationUsageInfo>() {
@Override
public Collection<TypeMigrationUsageInfo> getNodes() {
final Set<TypeMigrationUsageInfo> infos = new HashSet<>();
for (Map.Entry<TypeMigrationUsageInfo, HashSet<Pair<TypeMigrationUsageInfo, PsiType>>> entry : myRootsTree.entrySet()) {
infos.add(entry.getKey());
infos.addAll(ContainerUtil.map(entry.getValue(), pair -> pair.getFirst()));
}
return infos;
}
@Override
public Iterator<TypeMigrationUsageInfo> getIn(TypeMigrationUsageInfo n) {
final HashSet<Pair<TypeMigrationUsageInfo, PsiType>> rawNodes = myRootsTree.get(n);
if (rawNodes == null) {
return Collections.<TypeMigrationUsageInfo>emptyList().iterator();
}
final List<TypeMigrationUsageInfo> in = ContainerUtil.map(rawNodes, pair -> pair.getFirst());
return in.iterator();
}
}));
final Comparator<TypeMigrationUsageInfo> cmp = builder.comparator();
Arrays.sort(infos, (info1, info2) -> {
final TypeMigrationUsageInfo i1 = info1.getOwnerRoot();
final TypeMigrationUsageInfo i2 = info2.getOwnerRoot();
if (i1 == null && i2 == null) {
return 0;
}
if (i1 == null) {
return 1;
}
if (i2 == null) {
return -1;
}
final PsiElement element1 = info1.getElement();
final PsiElement element2 = info2.getElement();
LOG.assertTrue(element1 != null && element2 != null);
if (element1.equals(element2)) {
return 0;
}
final TextRange range1 = element1.getTextRange();
final TextRange range2 = element2.getTextRange();
if (range1.contains(range2)) {
return 1;
}
if (range2.contains(range1)) {
return -1;
}
final int res = cmp.compare(i1, i2);
if (res != 0) {
return res;
}
return range2.getStartOffset() - range1.getStartOffset();
});
return infos;
}
use of com.intellij.util.graph.InboundSemiGraph in project intellij-community by JetBrains.
the class InspectionProfileImpl method initialize.
@Override
protected void initialize(@Nullable Project project) {
SchemeDataHolder<? super InspectionProfileImpl> dataHolder = myDataHolder;
if (dataHolder != null) {
myDataHolder = null;
Element element = dataHolder.read();
if (element.getName().equals("component")) {
element = element.getChild("profile");
}
assert element != null;
readExternal(element);
}
if (myBaseProfile != null) {
myBaseProfile.initInspectionTools(project);
}
final List<InspectionToolWrapper> tools;
try {
tools = createTools(project);
} catch (ProcessCanceledException ignored) {
return;
}
final Map<String, List<String>> dependencies = new THashMap<>();
for (InspectionToolWrapper toolWrapper : tools) {
addTool(project, toolWrapper, dependencies);
}
DFSTBuilder<String> builder = new DFSTBuilder<>(GraphGenerator.generate(new InboundSemiGraph<String>() {
@Override
public Collection<String> getNodes() {
return dependencies.keySet();
}
@Override
public Iterator<String> getIn(String n) {
return dependencies.get(n).iterator();
}
}));
if (builder.isAcyclic()) {
myScopesOrder = ArrayUtil.toStringArray(builder.getSortedNodes());
}
copyToolsConfigurations(project);
initialized = true;
if (dataHolder != null) {
// should be only after set myInitialized
dataHolder.updateDigest(this);
}
}
use of com.intellij.util.graph.InboundSemiGraph in project intellij-community by JetBrains.
the class GenerationOptionsImpl method createModuleChunks.
private ModuleChunk[] createModuleChunks(String[] representativeModuleNames) {
final Set<String> mainModuleNames = new HashSet<>(Arrays.asList(representativeModuleNames));
final Graph<Chunk<Module>> chunkGraph = ModuleCompilerUtil.toChunkGraph(ModuleManager.getInstance(myProject).moduleGraph());
final Map<Chunk<Module>, ModuleChunk> map = new HashMap<>();
final Map<ModuleChunk, Chunk<Module>> reverseMap = new HashMap<>();
for (final Chunk<Module> chunk : chunkGraph.getNodes()) {
final Set<Module> modules = chunk.getNodes();
final ModuleChunk moduleChunk = new ModuleChunk(modules.toArray(new Module[modules.size()]));
for (final Module module : modules) {
if (mainModuleNames.contains(module.getName())) {
moduleChunk.setMainModule(module);
break;
}
}
map.put(chunk, moduleChunk);
reverseMap.put(moduleChunk, chunk);
}
final Graph<ModuleChunk> moduleChunkGraph = GraphGenerator.generate(CachingSemiGraph.cache(new InboundSemiGraph<ModuleChunk>() {
public Collection<ModuleChunk> getNodes() {
return map.values();
}
public Iterator<ModuleChunk> getIn(ModuleChunk n) {
final Chunk<Module> chunk = reverseMap.get(n);
final Iterator<Chunk<Module>> in = chunkGraph.getIn(chunk);
return new Iterator<ModuleChunk>() {
public boolean hasNext() {
return in.hasNext();
}
public ModuleChunk next() {
return map.get(in.next());
}
public void remove() {
throw new IncorrectOperationException("Method is not supported");
}
};
}
}));
final Collection<ModuleChunk> nodes = moduleChunkGraph.getNodes();
final ModuleChunk[] moduleChunks = nodes.toArray(new ModuleChunk[nodes.size()]);
for (ModuleChunk moduleChunk : moduleChunks) {
final Iterator<ModuleChunk> depsIterator = moduleChunkGraph.getIn(moduleChunk);
List<ModuleChunk> deps = new ArrayList<>();
while (depsIterator.hasNext()) {
deps.add(depsIterator.next());
}
moduleChunk.setDependentChunks(deps.toArray(new ModuleChunk[deps.size()]));
ContainerUtil.addAll(myCustomCompilers, moduleChunk.getCustomCompilers());
}
Arrays.sort(moduleChunks, new ChunksComparator());
if (generateSingleFile) {
final File baseDir = BuildProperties.getProjectBaseDir(myProject);
for (ModuleChunk chunk : moduleChunks) {
chunk.setBaseDir(baseDir);
}
}
return moduleChunks;
}
use of com.intellij.util.graph.InboundSemiGraph in project intellij-community by JetBrains.
the class ModifiableModelCommitter method createDFSTBuilder.
private static DFSTBuilder<RootModelImpl> createDFSTBuilder(List<RootModelImpl> rootModels, final ModifiableModuleModel moduleModel) {
final Map<String, RootModelImpl> nameToModel = ContainerUtil.newHashMap();
for (RootModelImpl rootModel : rootModels) {
String name = rootModel.getModule().getName();
LOG.assertTrue(!nameToModel.containsKey(name), name);
nameToModel.put(name, rootModel);
}
Module[] modules = moduleModel.getModules();
for (Module module : modules) {
String name = module.getName();
if (!nameToModel.containsKey(name)) {
RootModelImpl rootModel = ((ModuleRootManagerImpl) ModuleRootManager.getInstance(module)).getRootModel();
nameToModel.put(name, rootModel);
}
}
final Collection<RootModelImpl> allRootModels = nameToModel.values();
InboundSemiGraph<RootModelImpl> graph = new InboundSemiGraph<RootModelImpl>() {
@Override
public Collection<RootModelImpl> getNodes() {
return allRootModels;
}
@Override
public Iterator<RootModelImpl> getIn(RootModelImpl rootModel) {
OrderEnumerator entries = rootModel.orderEntries().withoutSdk().withoutLibraries().withoutModuleSourceEntries();
List<String> namesList = entries.process(new RootPolicy<List<String>>() {
@Override
public List<String> visitModuleOrderEntry(ModuleOrderEntry moduleOrderEntry, List<String> strings) {
Module module = moduleOrderEntry.getModule();
if (module != null && !module.isDisposed()) {
strings.add(module.getName());
} else {
final Module moduleToBeRenamed = moduleModel.getModuleToBeRenamed(moduleOrderEntry.getModuleName());
if (moduleToBeRenamed != null && !moduleToBeRenamed.isDisposed()) {
strings.add(moduleToBeRenamed.getName());
}
}
return strings;
}
}, new ArrayList<>());
String[] names = ArrayUtil.toStringArray(namesList);
List<RootModelImpl> result = new ArrayList<>();
for (String name : names) {
RootModelImpl depRootModel = nameToModel.get(name);
if (depRootModel != null) {
// it is ok not to find one
result.add(depRootModel);
}
}
return result.iterator();
}
};
return new DFSTBuilder<>(GraphGenerator.generate(CachingSemiGraph.cache(graph)));
}
Aggregations