use of com.jetbrains.python.codeInsight.dataflow.scope.Scope in project intellij-community by JetBrains.
the class PyReferenceImpl method getResultsFromProcessor.
protected List<RatedResolveResult> getResultsFromProcessor(@NotNull String referencedName, @NotNull PyResolveProcessor processor, @Nullable PsiElement realContext, @Nullable PsiElement resolveRoof) {
boolean unreachableLocalDeclaration = false;
boolean resolveInParentScope = false;
final ResolveResultList resultList = new ResolveResultList();
final ScopeOwner referenceOwner = ScopeUtil.getScopeOwner(realContext);
final TypeEvalContext typeEvalContext = myContext.getTypeEvalContext();
ScopeOwner resolvedOwner = processor.getOwner();
if (resolvedOwner != null && !processor.getResults().isEmpty()) {
final Collection<PsiElement> resolvedElements = processor.getElements();
final Scope resolvedScope = ControlFlowCache.getScope(resolvedOwner);
if (!resolvedScope.isGlobal(referencedName)) {
if (resolvedOwner == referenceOwner) {
final List<Instruction> instructions = PyDefUseUtil.getLatestDefs(resolvedOwner, referencedName, realContext, false, true);
// TODO: Use the results from the processor as a cache for resolving to latest defs
final ResolveResultList latestDefs = resolveToLatestDefs(instructions, realContext, referencedName, typeEvalContext);
if (!latestDefs.isEmpty()) {
return latestDefs;
} else if (resolvedOwner instanceof PyClass || instructions.isEmpty() && allInOwnScopeComprehensions(resolvedElements)) {
resolveInParentScope = true;
} else if (PyiUtil.isInsideStubAnnotation(myElement)) {
for (PsiElement element : resolvedElements) {
resultList.poke(element, getRate(element, typeEvalContext));
}
return resultList;
} else {
unreachableLocalDeclaration = true;
}
} else if (referenceOwner != null) {
final Scope referenceScope = ControlFlowCache.getScope(referenceOwner);
if (referenceScope.containsDeclaration(referencedName)) {
unreachableLocalDeclaration = true;
}
}
}
}
if (!unreachableLocalDeclaration) {
if (resolveInParentScope) {
processor = new PyResolveProcessor(referencedName);
resolvedOwner = ScopeUtil.getScopeOwner(resolvedOwner);
if (resolvedOwner != null) {
PyResolveUtil.scopeCrawlUp(processor, resolvedOwner, referencedName, resolveRoof);
}
}
for (Map.Entry<PsiElement, PyImportedNameDefiner> entry : processor.getResults().entrySet()) {
final PsiElement resolved = entry.getKey();
final PyImportedNameDefiner definer = entry.getValue();
if (resolved != null) {
if (typeEvalContext.maySwitchToAST(resolved) && isInnerComprehension(realContext, resolved)) {
continue;
}
if (skipClassForwardReferences(referenceOwner, resolved)) {
continue;
}
if (definer == null) {
resultList.poke(resolved, getRate(resolved, typeEvalContext));
} else {
resultList.poke(definer, getRate(definer, typeEvalContext));
resultList.add(new ImportedResolveResult(resolved, getRate(resolved, typeEvalContext), definer));
}
} else if (definer != null) {
resultList.add(new ImportedResolveResult(null, RatedResolveResult.RATE_LOW, definer));
}
}
if (!resultList.isEmpty()) {
return resultList;
}
}
return resolveByReferenceResolveProviders();
}
use of com.jetbrains.python.codeInsight.dataflow.scope.Scope in project intellij-community by JetBrains.
the class PyCodeFragmentUtil method getGlobalWrites.
@NotNull
private static Set<String> getGlobalWrites(@NotNull List<Instruction> instructions, @NotNull ScopeOwner owner) {
final Scope scope = ControlFlowCache.getScope(owner);
final Set<String> globalWrites = new LinkedHashSet<>();
for (Instruction instruction : getWriteInstructions(instructions)) {
if (instruction instanceof ReadWriteInstruction) {
final String name = ((ReadWriteInstruction) instruction).getName();
final PsiElement element = instruction.getElement();
if (scope.isGlobal(name) || (owner instanceof PsiFile && element instanceof PsiNamedElement && isUsedOutside((PsiNamedElement) element, instructions))) {
globalWrites.add(name);
}
}
}
return globalWrites;
}
use of com.jetbrains.python.codeInsight.dataflow.scope.Scope in project intellij-community by JetBrains.
the class PyCodeFragmentUtil method getNonlocalWrites.
@NotNull
private static Set<String> getNonlocalWrites(@NotNull List<Instruction> instructions, @NotNull ScopeOwner owner) {
final Scope scope = ControlFlowCache.getScope(owner);
final Set<String> nonlocalWrites = new LinkedHashSet<>();
for (Instruction instruction : getWriteInstructions(instructions)) {
if (instruction instanceof ReadWriteInstruction) {
final String name = ((ReadWriteInstruction) instruction).getName();
if (scope.isNonlocal(name)) {
nonlocalWrites.add(name);
}
}
}
return nonlocalWrites;
}
use of com.jetbrains.python.codeInsight.dataflow.scope.Scope in project intellij-community by JetBrains.
the class PyExtractMethodUtil method processGlobalWrites.
private static void processGlobalWrites(@NotNull final PyFunction function, @NotNull final PyCodeFragment fragment) {
final Set<String> globalWrites = fragment.getGlobalWrites();
final Set<String> newGlobalNames = new LinkedHashSet<>();
final Scope scope = ControlFlowCache.getScope(function);
for (String name : globalWrites) {
if (!scope.isGlobal(name)) {
newGlobalNames.add(name);
}
}
if (!newGlobalNames.isEmpty()) {
final PyElementGenerator generator = PyElementGenerator.getInstance(function.getProject());
final PyGlobalStatement globalStatement = generator.createFromText(LanguageLevel.forElement(function), PyGlobalStatement.class, "global " + StringUtil.join(newGlobalNames, ", "));
final PyStatementList statementList = function.getStatementList();
statementList.addBefore(globalStatement, statementList.getFirstChild());
}
}
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;
}
Aggregations