Search in sources :

Example 36 with QualifiedName

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

the class PyQualifiedReference method addImplicitResolveResults.

private void addImplicitResolveResults(String referencedName, ResolveResultList ret) {
    final Project project = myElement.getProject();
    final GlobalSearchScope scope = PyProjectScopeBuilder.excludeSdkTestsScope(project);
    final Collection functions = PyFunctionNameIndex.find(referencedName, project, scope);
    final PsiFile containingFile = myElement.getContainingFile();
    final List<QualifiedName> imports;
    if (containingFile instanceof PyFile) {
        imports = collectImports((PyFile) containingFile);
    } else {
        imports = Collections.emptyList();
    }
    for (Object function : functions) {
        if (!(function instanceof PyFunction)) {
            FileBasedIndex.getInstance().scheduleRebuild(StubUpdatingIndex.INDEX_ID, new Throwable("found non-function object " + function + " in function list"));
            break;
        }
        PyFunction pyFunction = (PyFunction) function;
        if (pyFunction.getContainingClass() != null) {
            ret.add(new ImplicitResolveResult(pyFunction, getImplicitResultRate(pyFunction, imports)));
        }
    }
    final Collection attributes = PyInstanceAttributeIndex.find(referencedName, project, scope);
    for (Object attribute : attributes) {
        if (!(attribute instanceof PyTargetExpression)) {
            FileBasedIndex.getInstance().scheduleRebuild(StubUpdatingIndex.INDEX_ID, new Throwable("found non-target expression object " + attribute + " in target expression list"));
            break;
        }
        ret.add(new ImplicitResolveResult((PyTargetExpression) attribute, getImplicitResultRate((PyTargetExpression) attribute, imports)));
    }
}
Also used : Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) QualifiedName(com.intellij.psi.util.QualifiedName) ImplicitResolveResult(com.jetbrains.python.psi.resolve.ImplicitResolveResult)

Example 37 with QualifiedName

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

the class PyQualifiedReference method collectAssignedAttributes.

/**
   * Returns expressions accessible from scope of "anchor" with names that start with  provided "qualifierQName".
   * Can be used for completion.
   */
@NotNull
public static Collection<PyExpression> collectAssignedAttributes(@NotNull final QualifiedName qualifierQName, @NotNull final PsiElement anchor) {
    final Set<String> names = new HashSet<>();
    final List<PyExpression> results = new ArrayList<>();
    for (ScopeOwner owner = ScopeUtil.getScopeOwner(anchor); owner != null; owner = ScopeUtil.getScopeOwner(owner)) {
        final Scope scope = ControlFlowCache.getScope(owner);
        for (final PyTargetExpression target : scope.getTargetExpressions()) {
            final QualifiedName targetQName = target.asQualifiedName();
            if (targetQName != null) {
                if (targetQName.getComponentCount() == qualifierQName.getComponentCount() + 1 && targetQName.matchesPrefix(qualifierQName)) {
                    final String name = target.getName();
                    if (!names.contains(name)) {
                        names.add(name);
                        results.add(target);
                    }
                }
            }
        }
    }
    return results;
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ProjectScope(com.intellij.psi.search.ProjectScope) QualifiedName(com.intellij.psi.util.QualifiedName) NotNull(org.jetbrains.annotations.NotNull)

Example 38 with QualifiedName

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

the class PyTargetExpressionImpl method resolveAssignedValue.

@Nullable
@Override
public PsiElement resolveAssignedValue(@NotNull PyResolveContext resolveContext) {
    final TypeEvalContext context = resolveContext.getTypeEvalContext();
    if (context.maySwitchToAST(this)) {
        final PyExpression value = findAssignedValue();
        if (value != null) {
            final List<PsiElement> results = PyUtil.multiResolveTopPriority(value, resolveContext);
            return !results.isEmpty() ? results.get(0) : null;
        }
        return null;
    } else {
        final QualifiedName qName = getAssignedQName();
        if (qName != null) {
            final ScopeOwner owner = ScopeUtil.getScopeOwner(this);
            if (owner instanceof PyTypedElement) {
                final List<String> components = qName.getComponents();
                if (!components.isEmpty()) {
                    PsiElement resolved = owner;
                    for (String component : components) {
                        if (!(resolved instanceof PyTypedElement)) {
                            return null;
                        }
                        final PyType qualifierType = context.getType((PyTypedElement) resolved);
                        if (qualifierType == null) {
                            return null;
                        }
                        final List<? extends RatedResolveResult> results = qualifierType.resolveMember(component, null, AccessDirection.READ, resolveContext);
                        if (results == null || results.isEmpty()) {
                            return null;
                        }
                        resolved = results.get(0).getElement();
                    }
                    return resolved;
                }
            }
        }
        return null;
    }
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) QualifiedName(com.intellij.psi.util.QualifiedName) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 39 with QualifiedName

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

the class PyImportStatementImpl method multiResolveName.

@NotNull
@Override
public List<RatedResolveResult> multiResolveName(@NotNull String name) {
    final PyImportElement[] elements = getImportElements();
    if (elements.length == 1) {
        final PyImportElement element = elements[0];
        final QualifiedName importedQName = element.getImportedQName();
        if (importedQName != null && importedQName.getComponentCount() > 1 && name.equals(importedQName.getLastComponent())) {
            return ResolveResultList.to(resolveImplicitSubModule());
        }
    }
    return Collections.emptyList();
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) NotNull(org.jetbrains.annotations.NotNull)

Example 40 with QualifiedName

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

the class PythonStringUtil method intersect.

@Nullable
public static String intersect(String fullName, String elementStringValue) {
    QualifiedName fullQName = QualifiedName.fromDottedString(fullName);
    QualifiedName stringQName = QualifiedName.fromDottedString(elementStringValue);
    String[] s1 = stringQName.getComponents().toArray(new String[stringQName.getComponentCount()]);
    String[] s2 = fullQName.getComponents().toArray(new String[fullQName.getComponentCount()]);
    for (int i = s1.length - 1; i >= 0; i--) {
        boolean flag = true;
        if (i > s2.length - 1) {
            continue;
        }
        for (int j = 0; j <= i; j++) {
            if (!s1[i - j].equals(s2[s2.length - j - 1])) {
                flag = false;
                break;
            }
        }
        if (flag) {
            StringBuilder res = new StringBuilder("");
            for (int j = 0; j <= i; j++) {
                if (j > 0) {
                    res.append(".");
                }
                res.append(s1[j]);
            }
            return res.toString();
        }
    }
    return null;
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

QualifiedName (com.intellij.psi.util.QualifiedName)63 Nullable (org.jetbrains.annotations.Nullable)22 NotNull (org.jetbrains.annotations.NotNull)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)9 Project (com.intellij.openapi.project.Project)8 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)7 PsiElement (com.intellij.psi.PsiElement)6 ArrayList (java.util.ArrayList)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)4 Sdk (com.intellij.openapi.projectRoots.Sdk)3 PsiFile (com.intellij.psi.PsiFile)3 PsiDirectory (com.intellij.psi.PsiDirectory)2 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)2 StringRef (com.intellij.util.io.StringRef)2 PyClass (com.jetbrains.python.psi.PyClass)2 PyFunction (com.jetbrains.python.psi.PyFunction)2 PyClassTypeImpl (com.jetbrains.python.psi.types.PyClassTypeImpl)2 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)2 Instruction (com.intellij.codeInsight.controlflow.Instruction)1 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1