Search in sources :

Example 66 with PsiPackage

use of com.intellij.psi.PsiPackage in project intellij-community by JetBrains.

the class PackagePrefixElementFinder method getSubPackages.

@NotNull
@Override
public PsiPackage[] getSubPackages(@NotNull PsiPackage psiPackage, @NotNull GlobalSearchScope scope) {
    final Map<String, PsiPackage> packagesMap = new HashMap<>();
    final String qualifiedName = psiPackage.getQualifiedName();
    for (final String prefix : myPackagePrefixIndex.getAllPackagePrefixes(scope)) {
        if (StringUtil.isEmpty(qualifiedName) || StringUtil.startsWithConcatenation(prefix, qualifiedName, ".")) {
            final int i = prefix.indexOf('.', qualifiedName.length() + 1);
            String childName = i >= 0 ? prefix.substring(0, i) : prefix;
            if (!packagesMap.containsKey(childName)) {
                packagesMap.put(childName, new PsiPackageImpl(psiPackage.getManager(), childName));
            }
        }
    }
    // avoid SOE caused by returning a package as a subpackage of itself
    packagesMap.remove(qualifiedName);
    return packagesMap.values().toArray(new PsiPackage[packagesMap.size()]);
}
Also used : HashMap(com.intellij.util.containers.HashMap) PsiPackageImpl(com.intellij.psi.impl.file.PsiPackageImpl) PsiPackage(com.intellij.psi.PsiPackage) NotNull(org.jetbrains.annotations.NotNull)

Example 67 with PsiPackage

use of com.intellij.psi.PsiPackage in project intellij-community by JetBrains.

the class JavaClassListReferenceProvider method getReferencesByString.

@Override
@NotNull
public PsiReference[] getReferencesByString(String str, @NotNull final PsiElement position, int offsetInPosition) {
    if (position instanceof XmlTag && ((XmlTag) position).getValue().getTextElements().length == 0) {
        return PsiReference.EMPTY_ARRAY;
    }
    if (str.length() < 2) {
        return PsiReference.EMPTY_ARRAY;
    }
    int offset = position.getTextRange().getStartOffset() + offsetInPosition;
    for (PsiElement child = position.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof OuterLanguageElement && child.getTextRange().contains(offset)) {
            return PsiReference.EMPTY_ARRAY;
        }
    }
    NotNullLazyValue<Set<String>> topLevelPackages = new NotNullLazyValue<Set<String>>() {

        @NotNull
        @Override
        protected Set<String> compute() {
            final Set<String> knownTopLevelPackages = new HashSet<>();
            final List<PsiElement> defaultPackages = getDefaultPackages(position.getProject());
            for (final PsiElement pack : defaultPackages) {
                if (pack instanceof PsiPackage) {
                    knownTopLevelPackages.add(((PsiPackage) pack).getName());
                }
            }
            return knownTopLevelPackages;
        }
    };
    final List<PsiReference> results = new ArrayList<>();
    for (int dot = str.indexOf('.'); dot > 0; dot = str.indexOf('.', dot + 1)) {
        int start = dot;
        while (start > 0 && Character.isLetterOrDigit(str.charAt(start - 1))) start--;
        if (dot == start) {
            continue;
        }
        String candidate = str.substring(start, dot);
        if (topLevelPackages.getValue().contains(candidate)) {
            int end = dot;
            while (end < str.length() - 1) {
                end++;
                char ch = str.charAt(end);
                if (ch != '.' && !Character.isJavaIdentifierPart(ch)) {
                    break;
                }
            }
            String s = str.substring(start, end + 1);
            ContainerUtil.addAll(results, new JavaClassReferenceSet(s, position, offsetInPosition + start, false, this) {

                @Override
                public boolean isSoft() {
                    return true;
                }

                @Override
                public boolean isAllowDollarInNames() {
                    return true;
                }
            }.getAllReferences());
            ProgressManager.checkCanceled();
        }
    }
    return ContainerUtil.toArray(results, new PsiReference[results.size()]);
}
Also used : OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) Set(java.util.Set) HashSet(java.util.HashSet) NotNullLazyValue(com.intellij.openapi.util.NotNullLazyValue) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) PsiPackage(com.intellij.psi.PsiPackage) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 68 with PsiPackage

use of com.intellij.psi.PsiPackage in project intellij-community by JetBrains.

the class PatternPackageReferenceSet method resolvePackageName.

@Override
public Collection<PsiPackage> resolvePackageName(@Nullable final PsiPackage context, final String packageName) {
    if (context == null)
        return Collections.emptySet();
    if (packageName.contains("*")) {
        final Set<PsiPackage> packages = new LinkedHashSet<>();
        int indexOf = packageName.indexOf("*");
        if (indexOf == 0 || context.getQualifiedName().startsWith(packageName.substring(0, indexOf))) {
            final Pattern pattern = PatternUtil.fromMask(packageName);
            processSubPackages(context, psiPackage -> {
                String name = psiPackage.getName();
                if (name != null && pattern.matcher(name).matches()) {
                    packages.add(psiPackage);
                }
                return true;
            });
        }
        return packages;
    }
    return super.resolvePackageName(context, packageName);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Pattern(java.util.regex.Pattern) PsiPackage(com.intellij.psi.PsiPackage)

Example 69 with PsiPackage

use of com.intellij.psi.PsiPackage in project intellij-community by JetBrains.

the class PsiMigrationImpl method createClass.

@Override
public PsiClass createClass(String qualifiedName) {
    assertValid();
    ApplicationManager.getApplication().assertWriteAccessAllowed();
    final MigrationClassImpl migrationClass = new MigrationClassImpl(this, qualifiedName);
    final MigrationClassImpl oldMigrationClass = myQNameToClassMap.put(qualifiedName, migrationClass);
    LOG.assertTrue(oldMigrationClass == null, qualifiedName);
    String packageName = parentPackageName(qualifiedName);
    final PsiPackage aPackage = myFacade.findPackage(packageName);
    if (aPackage == null) {
        createPackage(packageName);
    }
    List<PsiClass> psiClasses = getClassesList(packageName);
    psiClasses.add(migrationClass);
    myMigrationManager.migrationModified(false);
    return migrationClass;
}
Also used : PsiClass(com.intellij.psi.PsiClass) PsiPackage(com.intellij.psi.PsiPackage)

Example 70 with PsiPackage

use of com.intellij.psi.PsiPackage in project intellij-community by JetBrains.

the class CyclicDependenciesTest method testNoCycle.

public void testNoCycle() {
    //B->A
    final CyclicDependenciesBuilder builder = new CyclicDependenciesBuilder(myProject, new AnalysisScope(myProject));
    builder.analyze();
    final HashMap<PsiPackage, Set<List<PsiPackage>>> cyclicDependencies = builder.getCyclicDependencies();
    HashMap<String, String[][]> expected = new HashMap<>();
    expected.put("com.b", new String[0][0]);
    expected.put("com.a", new String[0][0]);
    checkResult(expected, cyclicDependencies, true);
}
Also used : JavaAnalysisScope(com.intellij.analysis.JavaAnalysisScope) AnalysisScope(com.intellij.analysis.AnalysisScope) CyclicDependenciesBuilder(com.intellij.cyclicDependencies.CyclicDependenciesBuilder) PsiPackage(com.intellij.psi.PsiPackage)

Aggregations

PsiPackage (com.intellij.psi.PsiPackage)79 PsiDirectory (com.intellij.psi.PsiDirectory)17 Module (com.intellij.openapi.module.Module)16 PsiClass (com.intellij.psi.PsiClass)16 JavaAnalysisScope (com.intellij.analysis.JavaAnalysisScope)12 Project (com.intellij.openapi.project.Project)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)10 NotNull (org.jetbrains.annotations.NotNull)10 CyclicDependenciesBuilder (com.intellij.cyclicDependencies.CyclicDependenciesBuilder)9 PsiFile (com.intellij.psi.PsiFile)9 AnalysisScope (com.intellij.analysis.AnalysisScope)8 PsiElement (com.intellij.psi.PsiElement)8 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)6 HashSet (java.util.HashSet)6 Nullable (org.jetbrains.annotations.Nullable)6 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)4 PackageElement (com.intellij.ide.projectView.impl.nodes.PackageElement)3 List (java.util.List)3 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2