Search in sources :

Example 56 with PsiNamedElement

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

the class PyIterableVariableMacro method calculateLookupItems.

@Nullable
@Override
public LookupElement[] calculateLookupItems(@NotNull Expression[] params, ExpressionContext context) {
    final List<LookupElement> results = new ArrayList<>();
    final PsiElement element = context.getPsiElementAtStartOffset();
    if (element != null) {
        for (PsiNamedElement iterableElement : getIterableElements(element)) {
            final String name = iterableElement.getName();
            if (name != null) {
                results.add(LookupElementBuilder.createWithSmartPointer(name, iterableElement));
            }
        }
    }
    return results.toArray(new LookupElement[results.size()]);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 57 with PsiNamedElement

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

the class PyIterableVariableMacro method getVisibleNamedElements.

@NotNull
private static List<PsiNamedElement> getVisibleNamedElements(@NotNull PsiElement anchor) {
    final List<PsiNamedElement> results = new ArrayList<>();
    for (ScopeOwner owner = ScopeUtil.getScopeOwner(anchor); owner != null; owner = ScopeUtil.getScopeOwner(owner)) {
        final Scope scope = ControlFlowCache.getScope(owner);
        results.addAll(scope.getNamedElements());
        StreamEx.of(scope.getImportedNameDefiners()).filter(definer -> !PyImplicitImportNameDefiner.class.isInstance(definer)).flatMap(definer -> StreamSupport.stream(definer.iterateNames().spliterator(), false)).select(PsiNamedElement.class).forEach(results::add);
    }
    return results;
}
Also used : TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) PyNames(com.jetbrains.python.PyNames) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PyType(com.jetbrains.python.psi.types.PyType) com.intellij.codeInsight.template(com.intellij.codeInsight.template) ScopeUtil(com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil) PyImplicitImportNameDefiner(com.jetbrains.python.psi.PyImplicitImportNameDefiner) ArrayList(java.util.ArrayList) Nullable(org.jetbrains.annotations.Nullable) PyTypedElement(com.jetbrains.python.psi.PyTypedElement) List(java.util.List) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) PyABCUtil(com.jetbrains.python.psi.types.PyABCUtil) StreamEx(one.util.streamex.StreamEx) PsiElement(com.intellij.psi.PsiElement) ControlFlowCache(com.jetbrains.python.codeInsight.controlflow.ControlFlowCache) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PsiNamedElement(com.intellij.psi.PsiNamedElement) StreamSupport(java.util.stream.StreamSupport) NotNull(org.jetbrains.annotations.NotNull) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) PyImplicitImportNameDefiner(com.jetbrains.python.psi.PyImplicitImportNameDefiner) NotNull(org.jetbrains.annotations.NotNull)

Example 58 with PsiNamedElement

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

the class PyIterableVariableMacro method getIterableElements.

@NotNull
protected List<PsiNamedElement> getIterableElements(@NotNull PsiElement element) {
    final TypeEvalContext typeEvalContext = TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile());
    final List<PsiNamedElement> components = new ArrayList<>();
    for (PsiNamedElement namedElement : getVisibleNamedElements(element)) {
        if (namedElement instanceof PyTypedElement) {
            final PyType type = typeEvalContext.getType((PyTypedElement) namedElement);
            if (type != null && PyABCUtil.isSubtype(type, PyNames.ITERABLE, typeEvalContext)) {
                components.add(namedElement);
            }
        }
    }
    return components;
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PyType(com.jetbrains.python.psi.types.PyType) ArrayList(java.util.ArrayList) PyTypedElement(com.jetbrains.python.psi.PyTypedElement) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) NotNull(org.jetbrains.annotations.NotNull)

Example 59 with PsiNamedElement

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

the class PyStdlibDocumentationLinkProvider method getStdlibUrlFor.

private String getStdlibUrlFor(PsiElement element, QualifiedName moduleName, Sdk sdk) {
    StringBuilder urlBuilder = new StringBuilder(getExternalDocumentationRoot(sdk));
    String qnameString = moduleName.toString();
    if (qnameString.equals("ntpath") || qnameString.equals("posixpath")) {
        qnameString = "os.path";
    } else if (qnameString.equals("nt")) {
        qnameString = "os";
    } else if (qnameString.equals("cPickle")) {
        qnameString = "pickle";
    } else if (qnameString.equals("pyexpat")) {
        qnameString = "xml.parsers.expat";
    }
    final String pyVersion = PythonDocumentationProvider.pyVersion(sdk.getVersionString());
    List<String> modules = pyVersion != null && pyVersion.startsWith("3") ? py3LibraryModules : py2LibraryModules;
    boolean foundModule = false;
    for (String module : modules) {
        if (qnameString.startsWith(module)) {
            urlBuilder.append(module.toLowerCase());
            urlBuilder.append(".html");
            foundModule = true;
            break;
        }
    }
    if (foundModule && element instanceof PsiNamedElement && !(element instanceof PyFile)) {
        urlBuilder.append('#').append(moduleName).append(".");
        if (element instanceof PyFunction) {
            final PyClass containingClass = ((PyFunction) element).getContainingClass();
            if (containingClass != null) {
                urlBuilder.append(containingClass.getName()).append('.');
            }
        }
        urlBuilder.append(((PsiNamedElement) element).getName());
    }
    return urlBuilder.toString();
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiNamedElement(com.intellij.psi.PsiNamedElement) PyFile(com.jetbrains.python.psi.PyFile)

Example 60 with PsiNamedElement

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

the class PyTestFinder method findTestsForClass.

@NotNull
@Override
public Collection<PsiElement> findTestsForClass(@NotNull PsiElement element) {
    PyDocStringOwner source = findSourceElement(element);
    if (source == null)
        return Collections.emptySet();
    String sourceName = source.getName();
    if (sourceName == null)
        return Collections.emptySet();
    List<Pair<? extends PsiNamedElement, Integer>> classesWithProximities = new ArrayList<>();
    if (source instanceof PyClass) {
        Collection<String> names = PyClassNameIndex.allKeys(element.getProject());
        for (String eachName : names) {
            if (eachName.contains(sourceName)) {
                for (PyClass eachClass : PyClassNameIndex.find(eachName, element.getProject(), GlobalSearchScope.projectScope(element.getProject()))) {
                    if (PythonUnitTestUtil.isTestCaseClass(eachClass, null) || PythonDocTestUtil.isDocTestClass(eachClass)) {
                        classesWithProximities.add(new Pair<PsiNamedElement, Integer>(eachClass, TestFinderHelper.calcTestNameProximity(sourceName, eachName)));
                    }
                }
            }
        }
    } else {
        Collection<String> names = PyFunctionNameIndex.allKeys(element.getProject());
        for (String eachName : names) {
            if (eachName.contains(sourceName)) {
                for (PyFunction eachFunction : PyFunctionNameIndex.find(eachName, element.getProject(), GlobalSearchScope.projectScope(element.getProject()))) {
                    if (PythonUnitTestUtil.isTestCaseFunction(eachFunction) || PythonDocTestUtil.isDocTestFunction(eachFunction)) {
                        classesWithProximities.add(new Pair<PsiNamedElement, Integer>(eachFunction, TestFinderHelper.calcTestNameProximity(sourceName, eachName)));
                    }
                }
            }
        }
    }
    return TestFinderHelper.getSortedElements(classesWithProximities, true);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) PyDocStringOwner(com.jetbrains.python.psi.PyDocStringOwner) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiNamedElement (com.intellij.psi.PsiNamedElement)65 PsiElement (com.intellij.psi.PsiElement)29 NotNull (org.jetbrains.annotations.NotNull)16 ArrayList (java.util.ArrayList)14 PsiFile (com.intellij.psi.PsiFile)8 LookupElement (com.intellij.codeInsight.lookup.LookupElement)7 Pair (com.intellij.openapi.util.Pair)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Project (com.intellij.openapi.project.Project)4 UsageInfo (com.intellij.usageView.UsageInfo)4 Nullable (org.jetbrains.annotations.Nullable)4 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)3 PsiReference (com.intellij.psi.PsiReference)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 PyClass (com.jetbrains.python.psi.PyClass)3 PyFunction (com.jetbrains.python.psi.PyFunction)3 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)2 LoadedSymbol (com.google.idea.blaze.base.lang.buildfile.psi.LoadedSymbol)2 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)2