use of com.jetbrains.python.codeInsight.dataflow.scope.Scope 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;
}
use of com.jetbrains.python.codeInsight.dataflow.scope.Scope 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;
}
use of com.jetbrains.python.codeInsight.dataflow.scope.Scope in project intellij-community by JetBrains.
the class PyTargetExpressionImpl method getUseScope.
@NotNull
@Override
public SearchScope getUseScope() {
if (isQualified()) {
return super.getUseScope();
}
final ScopeOwner owner = ScopeUtil.getScopeOwner(this);
if (owner != null) {
final Scope scope = ControlFlowCache.getScope(owner);
if (scope.isGlobal(getName())) {
return GlobalSearchScope.projectScope(getProject());
}
if (scope.isNonlocal(getName())) {
return new LocalSearchScope(getContainingFile());
}
}
// find highest level function containing our var
PyElement container = this;
while (true) {
PyElement parentContainer = PsiTreeUtil.getParentOfType(container, PyFunction.class, PyClass.class);
if (parentContainer instanceof PyClass) {
if (isQualified()) {
return super.getUseScope();
}
break;
}
if (parentContainer == null) {
break;
}
container = parentContainer;
}
if (container instanceof PyFunction) {
return new LocalSearchScope(container);
}
return super.getUseScope();
}
use of com.jetbrains.python.codeInsight.dataflow.scope.Scope in project intellij-community by JetBrains.
the class ScopeImpl method getNamedElements.
@NotNull
@Override
public Collection<PsiNamedElement> getNamedElements(String name, boolean includeNestedGlobals) {
if (myNamedElements == null) {
collectDeclarations();
}
if (myNamedElements.containsKey(name)) {
final Collection<PsiNamedElement> elements = myNamedElements.get(name);
elements.forEach(PyPsiUtils::assertValid);
return elements;
}
if (includeNestedGlobals && isGlobal(name)) {
for (Scope scope : myNestedScopes) {
final Collection<PsiNamedElement> globals = scope.getNamedElements(name, true);
if (!globals.isEmpty()) {
globals.forEach(PyPsiUtils::assertValid);
return globals;
}
}
}
return Collections.emptyList();
}
use of com.jetbrains.python.codeInsight.dataflow.scope.Scope in project intellij-community by JetBrains.
the class ControlFlowCache method getScope.
@NotNull
public static Scope getScope(@NotNull ScopeOwner element) {
SoftReference<Scope> ref = element.getUserData(SCOPE_KEY);
Scope scope = SoftReference.dereference(ref);
if (scope == null) {
scope = new ScopeImpl(element);
element.putUserData(SCOPE_KEY, new SoftReference<>(scope));
}
return scope;
}
Aggregations