Search in sources :

Example 1 with InboundSemiGraph

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;
}
Also used : PsiImmediateClassType(com.intellij.psi.impl.source.PsiImmediateClassType) TypeConversionUtil(com.intellij.psi.util.TypeConversionUtil) java.util(java.util) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) OverriddenUsageInfo(com.intellij.refactoring.typeMigration.usageInfo.OverriddenUsageInfo) DFSTBuilder(com.intellij.util.graph.DFSTBuilder) UsageInfo(com.intellij.usageView.UsageInfo) SearchScope(com.intellij.psi.search.SearchScope) PsiExtendedTypeVisitor(com.intellij.refactoring.typeCook.deductive.PsiExtendedTypeVisitor) ContainerUtil(com.intellij.util.containers.ContainerUtil) THashMap(gnu.trove.THashMap) TypeMigrationUsageInfo(com.intellij.refactoring.typeMigration.usageInfo.TypeMigrationUsageInfo) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Comparing(com.intellij.openapi.util.Comparing) RenameProcessor(com.intellij.refactoring.rename.RenameProcessor) Semaphore(com.intellij.util.concurrency.Semaphore) Project(com.intellij.openapi.project.Project) PsiUtil(com.intellij.psi.util.PsiUtil) Messages(com.intellij.openapi.ui.Messages) Logger(com.intellij.openapi.diagnostic.Logger) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) JavaLanguage(com.intellij.lang.java.JavaLanguage) MultiMap(com.intellij.util.containers.MultiMap) OverriderUsageInfo(com.intellij.refactoring.typeMigration.usageInfo.OverriderUsageInfo) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) StringUtil(com.intellij.openapi.util.text.StringUtil) OverridingMethodsSearch(com.intellij.psi.search.searches.OverridingMethodsSearch) GenerateMembersUtil(com.intellij.codeInsight.generation.GenerateMembersUtil) TextRange(com.intellij.openapi.util.TextRange) TestOnly(org.jetbrains.annotations.TestOnly) Nullable(org.jetbrains.annotations.Nullable) InboundSemiGraph(com.intellij.util.graph.InboundSemiGraph) PsiSearchScopeUtil(com.intellij.psi.search.PsiSearchScopeUtil) GraphGenerator(com.intellij.util.graph.GraphGenerator) Pair(com.intellij.openapi.util.Pair) ApplicationManager(com.intellij.openapi.application.ApplicationManager) GetterSetterPrototypeProvider(com.intellij.codeInsight.generation.GetterSetterPrototypeProvider) com.intellij.psi(com.intellij.psi) com.intellij.util(com.intellij.util) NotNull(org.jetbrains.annotations.NotNull) javax.swing(javax.swing) TextRange(com.intellij.openapi.util.TextRange) TypeMigrationUsageInfo(com.intellij.refactoring.typeMigration.usageInfo.TypeMigrationUsageInfo) InboundSemiGraph(com.intellij.util.graph.InboundSemiGraph) DFSTBuilder(com.intellij.util.graph.DFSTBuilder) THashMap(gnu.trove.THashMap) MultiMap(com.intellij.util.containers.MultiMap) Pair(com.intellij.openapi.util.Pair)

Example 2 with InboundSemiGraph

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);
    }
}
Also used : InboundSemiGraph(com.intellij.util.graph.InboundSemiGraph) THashMap(gnu.trove.THashMap) PsiElement(com.intellij.psi.PsiElement) Element(org.jdom.Element) DFSTBuilder(com.intellij.util.graph.DFSTBuilder) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 3 with InboundSemiGraph

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;
}
Also used : Chunk(com.intellij.util.Chunk) InboundSemiGraph(com.intellij.util.graph.InboundSemiGraph) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Module(com.intellij.openapi.module.Module) File(java.io.File)

Example 4 with InboundSemiGraph

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)));
}
Also used : InboundSemiGraph(com.intellij.util.graph.InboundSemiGraph) SmartList(com.intellij.util.SmartList) DFSTBuilder(com.intellij.util.graph.DFSTBuilder) Module(com.intellij.openapi.module.Module)

Aggregations

InboundSemiGraph (com.intellij.util.graph.InboundSemiGraph)4 DFSTBuilder (com.intellij.util.graph.DFSTBuilder)3 Module (com.intellij.openapi.module.Module)2 THashMap (gnu.trove.THashMap)2 GenerateMembersUtil (com.intellij.codeInsight.generation.GenerateMembersUtil)1 GetterSetterPrototypeProvider (com.intellij.codeInsight.generation.GetterSetterPrototypeProvider)1 JavaLanguage (com.intellij.lang.java.JavaLanguage)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 Logger (com.intellij.openapi.diagnostic.Logger)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 Project (com.intellij.openapi.project.Project)1 ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)1 Messages (com.intellij.openapi.ui.Messages)1 Comparing (com.intellij.openapi.util.Comparing)1 Pair (com.intellij.openapi.util.Pair)1 TextRange (com.intellij.openapi.util.TextRange)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 com.intellij.psi (com.intellij.psi)1 PsiElement (com.intellij.psi.PsiElement)1 PsiImmediateClassType (com.intellij.psi.impl.source.PsiImmediateClassType)1