Search in sources :

Example 31 with THashSet

use of gnu.trove.THashSet in project intellij-community by JetBrains.

the class VcsLogJoiner method getRedCommitsAndSavedRedIndex.

@NotNull
private Pair<Integer, Set<CommitId>> getRedCommitsAndSavedRedIndex(@NotNull List<? extends Commit> savedLog, @NotNull Collection<CommitId> previousRefs, @NotNull List<? extends Commit> firstBlock, @NotNull Collection<CommitId> newRefs) {
    Set<CommitId> startRedCommits = new THashSet<>(previousRefs);
    startRedCommits.removeAll(newRefs);
    Set<CommitId> startGreenNodes = new THashSet<>(newRefs);
    for (Commit commit : firstBlock) {
        startGreenNodes.add(commit.getId());
        startGreenNodes.addAll(commit.getParents());
    }
    RedGreenSorter<CommitId, Commit> sorter = new RedGreenSorter<>(startRedCommits, startGreenNodes, savedLog);
    int saveRegIndex = sorter.getFirstSaveIndex();
    return new Pair<>(saveRegIndex, sorter.getAllRedCommit());
}
Also used : GraphCommit(com.intellij.vcs.log.graph.GraphCommit) THashSet(gnu.trove.THashSet) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 32 with THashSet

use of gnu.trove.THashSet in project intellij-community by JetBrains.

the class GenerateVisitorByHierarchyAction method generateVisitorClass.

private static void generateVisitorClass(final String visitorName, final PsiClass baseClass, final PsiDirectory directory, final GlobalSearchScope scope) {
    final THashMap<PsiClass, Set<PsiClass>> classes = new THashMap<>();
    for (PsiClass aClass : ClassInheritorsSearch.search(baseClass, scope, true).findAll()) {
        if (aClass.hasModifierProperty(PsiModifier.ABSTRACT) == baseClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
            final List<PsiClass> implementors = ContainerUtil.findAll(ClassInheritorsSearch.search(aClass).findAll(), psiClass -> !psiClass.hasModifierProperty(PsiModifier.ABSTRACT));
            classes.put(aClass, new THashSet<>(implementors));
        }
    }
    final THashMap<PsiClass, Set<PsiClass>> pathMap = new THashMap<>();
    for (PsiClass aClass : classes.keySet()) {
        final Set<PsiClass> superClasses = new LinkedHashSet<>();
        for (PsiClass superClass : aClass.getSupers()) {
            if (superClass.isInheritor(baseClass, true)) {
                superClasses.add(superClass);
                final Set<PsiClass> superImplementors = classes.get(superClass);
                if (superImplementors != null) {
                    superImplementors.removeAll(classes.get(aClass));
                }
            }
        }
        if (superClasses.isEmpty()) {
            superClasses.add(baseClass);
        }
        pathMap.put(aClass, superClasses);
    }
    pathMap.put(baseClass, Collections.<PsiClass>emptySet());
    final ArrayList<PsiFile> psiFiles = new ArrayList<>();
    for (Set<PsiClass> implementors : classes.values()) {
        for (PsiClass psiClass : implementors) {
            psiFiles.add(psiClass.getContainingFile());
        }
    }
    final Project project = baseClass.getProject();
    final PsiClass visitorClass = JavaPsiFacade.getInstance(project).findClass(visitorName, GlobalSearchScope.projectScope(project));
    if (visitorClass != null) {
        psiFiles.add(visitorClass.getContainingFile());
    }
    final int finalDetectedPrefix = detectClassPrefix(classes.keySet()).length();
    new WriteCommandAction(project, PsiUtilCore.toPsiFileArray(psiFiles)) {

        protected void run(@NotNull final Result result) throws Throwable {
            if (visitorClass == null) {
                final String shortClassName = PsiNameHelper.getShortClassName(visitorName);
                if (directory != null) {
                    final PsiClass visitorClass = JavaDirectoryService.getInstance().createClass(directory, shortClassName);
                    generateVisitorClass(visitorClass, classes, pathMap, finalDetectedPrefix);
                }
            } else {
                generateVisitorClass(visitorClass, classes, pathMap, finalDetectedPrefix);
            }
        }

        @Override
        protected boolean isGlobalUndoAction() {
            return true;
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) THashSet(gnu.trove.THashSet) Result(com.intellij.openapi.application.Result) Project(com.intellij.openapi.project.Project) THashMap(gnu.trove.THashMap)

Example 33 with THashSet

use of gnu.trove.THashSet in project intellij-community by JetBrains.

the class DefaultSymbolNavigationContributor method processElementsWithName.

@Override
public void processElementsWithName(@NotNull String name, @NotNull final Processor<NavigationItem> processor, @NotNull final FindSymbolParameters parameters) {
    GlobalSearchScope scope = parameters.getSearchScope();
    IdFilter filter = parameters.getIdFilter();
    PsiShortNamesCache cache = PsiShortNamesCache.getInstance(scope.getProject());
    String completePattern = parameters.getCompletePattern();
    final Condition<PsiMember> qualifiedMatcher = getQualifiedNameMatcher(completePattern);
    //noinspection UnusedDeclaration
    final Set<PsiMethod> collectedMethods = new THashSet<>();
    boolean success = cache.processFieldsWithName(name, field -> {
        if (isOpenable(field) && qualifiedMatcher.value(field))
            return processor.process(field);
        return true;
    }, scope, filter) && cache.processClassesWithName(name, aClass -> {
        if (isOpenable(aClass) && qualifiedMatcher.value(aClass))
            return processor.process(aClass);
        return true;
    }, scope, filter) && cache.processMethodsWithName(name, method -> {
        if (!method.isConstructor() && isOpenable(method) && qualifiedMatcher.value(method)) {
            collectedMethods.add(method);
        }
        return true;
    }, scope, filter);
    if (success) {
        // hashSuperMethod accesses index and can not be invoked without risk of the deadlock in processMethodsWithName
        Iterator<PsiMethod> iterator = collectedMethods.iterator();
        while (iterator.hasNext()) {
            PsiMethod method = iterator.next();
            if (!hasSuperMethod(method, scope, qualifiedMatcher) && !processor.process(method))
                return;
            ProgressManager.checkCanceled();
            iterator.remove();
        }
    }
}
Also used : NavigationItem(com.intellij.navigation.NavigationItem) java.util(java.util) ArrayUtil(com.intellij.util.ArrayUtil) MinusculeMatcher(com.intellij.psi.codeStyle.MinusculeMatcher) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet) IdFilter(com.intellij.util.indexing.IdFilter) ChooseByNameContributorEx(com.intellij.navigation.ChooseByNameContributorEx) NameUtil(com.intellij.psi.codeStyle.NameUtil) InheritanceUtil(com.intellij.psi.util.InheritanceUtil) Project(com.intellij.openapi.project.Project) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) PsiUtil(com.intellij.psi.util.PsiUtil) Logger(com.intellij.openapi.diagnostic.Logger) ProgressManager(com.intellij.openapi.progress.ProgressManager) StringUtil(com.intellij.openapi.util.text.StringUtil) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) DefaultPsiElementCellRenderer(com.intellij.ide.util.DefaultPsiElementCellRenderer) Nullable(org.jetbrains.annotations.Nullable) PsiSearchScopeUtil(com.intellij.psi.search.PsiSearchScopeUtil) Processor(com.intellij.util.Processor) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) FindSymbolParameters(com.intellij.util.indexing.FindSymbolParameters) Condition(com.intellij.openapi.util.Condition) IdFilter(com.intellij.util.indexing.IdFilter) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) THashSet(gnu.trove.THashSet)

Example 34 with THashSet

use of gnu.trove.THashSet in project intellij-community by JetBrains.

the class PsiClassImplUtil method processDeclarationsInClassNotCached.

private static boolean processDeclarationsInClassNotCached(@NotNull PsiClass aClass, @NotNull final PsiScopeProcessor processor, @NotNull final ResolveState state, @Nullable Set<PsiClass> visited, final PsiElement last, @NotNull final PsiElement place, final boolean isRaw, @NotNull final LanguageLevel languageLevel, @NotNull final GlobalSearchScope resolveScope) {
    if (visited == null)
        visited = new THashSet<>();
    if (!visited.add(aClass))
        return true;
    processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, aClass);
    final ElementClassHint classHint = processor.getHint(ElementClassHint.KEY);
    final NameHint nameHint = processor.getHint(NameHint.KEY);
    if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.FIELD)) {
        if (nameHint != null) {
            final PsiField fieldByName = aClass.findFieldByName(nameHint.getName(state), false);
            if (fieldByName != null && !processor.execute(fieldByName, state))
                return false;
        } else {
            final PsiField[] fields = aClass.getFields();
            for (final PsiField field : fields) {
                if (!processor.execute(field, state))
                    return false;
            }
        }
    }
    PsiElementFactory factory = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory();
    if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.METHOD)) {
        PsiSubstitutor baseSubstitutor = state.get(PsiSubstitutor.KEY);
        final PsiMethod[] methods = nameHint != null ? aClass.findMethodsByName(nameHint.getName(state), false) : aClass.getMethods();
        for (final PsiMethod method : methods) {
            PsiSubstitutor finalSubstitutor = checkRaw(isRaw, factory, method, baseSubstitutor);
            ResolveState methodState = finalSubstitutor == baseSubstitutor ? state : state.put(PsiSubstitutor.KEY, finalSubstitutor);
            if (!processor.execute(method, methodState))
                return false;
        }
    }
    if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.CLASS)) {
        if (last != null && last.getContext() == aClass) {
            // Parameters
            final PsiTypeParameterList list = aClass.getTypeParameterList();
            if (list != null && !list.processDeclarations(processor, ResolveState.initial(), last, place))
                return false;
        }
        if (!(last instanceof PsiReferenceList) && !(last instanceof PsiModifierList)) {
            // Inners
            if (nameHint != null) {
                final PsiClass inner = aClass.findInnerClassByName(nameHint.getName(state), false);
                if (inner != null) {
                    if (!processor.execute(inner, state))
                        return false;
                }
            } else {
                final PsiClass[] inners = aClass.getInnerClasses();
                for (final PsiClass inner : inners) {
                    if (!processor.execute(inner, state))
                        return false;
                }
            }
        }
    }
    if (last instanceof PsiReferenceList)
        return true;
    final Set<PsiClass> visited1 = visited;
    return processSuperTypes(aClass, state.get(PsiSubstitutor.KEY), factory, languageLevel, resolveScope, (superClass, finalSubstitutor) -> processDeclarationsInClass(superClass, processor, state.put(PsiSubstitutor.KEY, finalSubstitutor), visited1, last, place, languageLevel, isRaw, resolveScope));
}
Also used : ElementClassHint(com.intellij.psi.scope.ElementClassHint) THashSet(gnu.trove.THashSet) NameHint(com.intellij.psi.scope.NameHint)

Example 35 with THashSet

use of gnu.trove.THashSet in project intellij-community by JetBrains.

the class JavaLanguageInjectionSupport method createFrom.

private static MethodParameterInjection createFrom(final Project project, final BaseInjection injection, final PsiMethod contextMethod, final boolean includeAllPlaces) {
    final PsiClass[] classes;
    final String className;
    if (contextMethod != null) {
        final PsiClass psiClass = contextMethod.getContainingClass();
        className = psiClass == null ? "" : StringUtil.notNullize(psiClass.getQualifiedName());
        classes = psiClass == null ? PsiClass.EMPTY_ARRAY : new PsiClass[] { psiClass };
    } else {
        String found = null;
        final Pattern pattern = Pattern.compile(".*definedInClass\\(\"([^\"]*)\"\\)+");
        for (InjectionPlace place : injection.getInjectionPlaces()) {
            final Matcher matcher = pattern.matcher(place.getText());
            if (matcher.matches()) {
                found = matcher.group(1);
            }
        }
        if (found == null) {
            // hack to guess at least the class name
            final Matcher matcher = ourPresentationPattern.matcher(injection.getDisplayName());
            if (matcher.matches()) {
                final String pkg = matcher.group(2);
                found = pkg.substring(1, pkg.length() - 1) + "." + matcher.group(1);
            }
        }
        classes = found != null && project.isInitialized() ? JavaPsiFacade.getInstance(project).findClasses(found, GlobalSearchScope.allScope(project)) : PsiClass.EMPTY_ARRAY;
        className = StringUtil.notNullize(classes.length == 0 ? found : classes[0].getQualifiedName());
    }
    final MethodParameterInjection result = new MethodParameterInjection();
    result.copyFrom(injection);
    result.setInjectionPlaces(InjectionPlace.EMPTY_ARRAY);
    result.setClassName(className);
    final ArrayList<MethodInfo> infos = new ArrayList<>();
    if (classes.length > 0) {
        final THashSet<String> visitedSignatures = new THashSet<>();
        final PatternCompiler<PsiElement> compiler = injection.getCompiler();
        for (PsiClass psiClass : classes) {
            for (PsiMethod method : psiClass.getMethods()) {
                final PsiModifierList modifiers = method.getModifierList();
                if (modifiers.hasModifierProperty(PsiModifier.PRIVATE) || modifiers.hasModifierProperty(PsiModifier.PACKAGE_LOCAL))
                    continue;
                boolean add = false;
                final MethodInfo methodInfo = createMethodInfo(method);
                if (!visitedSignatures.add(methodInfo.getMethodSignature()))
                    continue;
                if (isInjectable(method.getReturnType(), method.getProject())) {
                    final int parameterIndex = -1;
                    int index = ArrayUtilRt.find(injection.getInjectionPlaces(), new InjectionPlace(compiler.compileElementPattern(getPatternStringForJavaPlace(method, parameterIndex)), true));
                    final InjectionPlace place = index > -1 ? injection.getInjectionPlaces()[index] : null;
                    methodInfo.setReturnFlag(place != null && place.isEnabled() || includeAllPlaces);
                    add = true;
                }
                final PsiParameter[] parameters = method.getParameterList().getParameters();
                for (int i = 0; i < parameters.length; i++) {
                    final PsiParameter p = parameters[i];
                    if (isInjectable(p.getType(), p.getProject())) {
                        int index = ArrayUtilRt.find(injection.getInjectionPlaces(), new InjectionPlace(compiler.compileElementPattern(getPatternStringForJavaPlace(method, i)), true));
                        final InjectionPlace place = index > -1 ? injection.getInjectionPlaces()[index] : null;
                        methodInfo.getParamFlags()[i] = place != null && place.isEnabled() || includeAllPlaces;
                        add = true;
                    }
                }
                if (add) {
                    infos.add(methodInfo);
                }
            }
        }
    }
    //    else {
    // todo tbd
    //for (InjectionPlace place : injection.getInjectionPlaces()) {
    //  final Matcher matcher = pattern.matcher(place.getText());
    //  if (matcher.matches()) {
    //
    //  }
    //}
    //    }
    result.setMethodInfos(infos);
    result.generatePlaces();
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) InjectionPlace(org.intellij.plugins.intelliLang.inject.config.InjectionPlace) THashSet(gnu.trove.THashSet) MethodParameterInjection(org.intellij.plugins.intelliLang.inject.config.MethodParameterInjection)

Aggregations

THashSet (gnu.trove.THashSet)239 NotNull (org.jetbrains.annotations.NotNull)65 VirtualFile (com.intellij.openapi.vfs.VirtualFile)62 Project (com.intellij.openapi.project.Project)35 File (java.io.File)35 THashMap (gnu.trove.THashMap)31 Nullable (org.jetbrains.annotations.Nullable)31 Module (com.intellij.openapi.module.Module)29 IOException (java.io.IOException)24 PsiElement (com.intellij.psi.PsiElement)21 PsiFile (com.intellij.psi.PsiFile)18 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)16 java.util (java.util)16 Element (org.jdom.Element)14 Pair (com.intellij.openapi.util.Pair)13 Logger (com.intellij.openapi.diagnostic.Logger)12 ContainerUtil (com.intellij.util.containers.ContainerUtil)12 Document (com.intellij.openapi.editor.Document)11 Library (com.intellij.openapi.roots.libraries.Library)11 StringUtil (com.intellij.openapi.util.text.StringUtil)10