Search in sources :

Example 1 with StubBasedPsiElement

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

the class ScopeUtil method getScopeOwner.

/**
   * Return the scope owner for the element.
   *
   * Scope owner is not always the first ScopeOwner parent of the element. Some elements are resolved in outer scopes.
   *
   * This method does not access AST if underlying PSI is stub based.
   */
@Nullable
public static ScopeOwner getScopeOwner(@Nullable final PsiElement element) {
    if (element == null) {
        return null;
    }
    if (element instanceof StubBasedPsiElement) {
        final StubElement stub = ((StubBasedPsiElement) element).getStub();
        if (stub != null) {
            StubElement parentStub = stub.getParentStub();
            while (parentStub != null) {
                final PsiElement parent = parentStub.getPsi();
                if (parent instanceof ScopeOwner) {
                    return (ScopeOwner) parent;
                }
                parentStub = parentStub.getParentStub();
            }
            return null;
        }
    }
    return CachedValuesManager.getCachedValue(element, () -> CachedValueProvider.Result.create(calculateScopeOwnerByAST(element), PsiModificationTracker.MODIFICATION_COUNT));
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) StubBasedPsiElement(com.intellij.psi.StubBasedPsiElement) StubElement(com.intellij.psi.stubs.StubElement) StubBasedPsiElement(com.intellij.psi.StubBasedPsiElement) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with StubBasedPsiElement

use of com.intellij.psi.StubBasedPsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoPsiTreeUtil method getStubChildrenOfTypeAsList.

@NotNull
public static <T extends PsiElement> List<T> getStubChildrenOfTypeAsList(@Nullable PsiElement element, @NotNull Class<T> aClass) {
    if (element == null)
        return Collections.emptyList();
    StubElement<?> stub = element instanceof StubBasedPsiElement ? ((StubBasedPsiElement) element).getStub() : null;
    if (stub == null) {
        return getChildrenOfTypeAsList(element, aClass);
    }
    List<T> result = new SmartList<T>();
    for (StubElement childStub : stub.getChildrenStubs()) {
        PsiElement child = childStub.getPsi();
        if (aClass.isInstance(child)) {
            //noinspection unchecked
            result.add((T) child);
        }
    }
    return result;
}
Also used : StubBasedPsiElement(com.intellij.psi.StubBasedPsiElement) SmartList(com.intellij.util.SmartList) StubElement(com.intellij.psi.stubs.StubElement) StubBasedPsiElement(com.intellij.psi.StubBasedPsiElement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)2 StubBasedPsiElement (com.intellij.psi.StubBasedPsiElement)2 StubElement (com.intellij.psi.stubs.StubElement)2 SmartList (com.intellij.util.SmartList)1 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1