use of com.intellij.codeInsight.controlflow.Instruction in project intellij-community by JetBrains.
the class PyCodeFragmentUtil method getOutputElements.
@NotNull
private static List<PsiElement> getOutputElements(@NotNull List<Instruction> subGraph, @NotNull List<Instruction> graph) {
final List<PsiElement> result = new ArrayList<>();
final List<Instruction> outerGraph = new ArrayList<>();
for (Instruction instruction : graph) {
if (!subGraph.contains(instruction)) {
outerGraph.add(instruction);
}
}
final Set<PsiElement> subGraphElements = getSubGraphElements(subGraph);
for (Instruction instruction : getReadInstructions(outerGraph)) {
final PsiElement element = instruction.getElement();
if (element != null) {
final PsiReference reference = element.getReference();
if (reference != null) {
for (PsiElement resolved : multiResolve(reference)) {
if (subGraphElements.contains(resolved)) {
result.add(resolved);
}
}
}
}
}
return result;
}
use of com.intellij.codeInsight.controlflow.Instruction 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.intellij.codeInsight.controlflow.Instruction 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.intellij.codeInsight.controlflow.Instruction in project intellij-community by JetBrains.
the class PyCodeFragmentUtil method analyseSubGraph.
@NotNull
private static AnalysisResult analyseSubGraph(@NotNull List<Instruction> subGraph, int start, int end) {
int returnSources = 0;
int regularSources = 0;
final Set<Instruction> targetInstructions = new HashSet<>();
int starImports = 0;
int outerLoopBreaks = 0;
int yieldExpressions = 0;
for (Pair<Instruction, Instruction> edge : getOutgoingEdges(subGraph)) {
final Instruction sourceInstruction = edge.getFirst();
final Instruction targetInstruction = edge.getSecond();
final PsiElement source = sourceInstruction.getElement();
final PsiElement target = targetInstruction.getElement();
final PyReturnStatement returnStatement = PsiTreeUtil.getParentOfType(source, PyReturnStatement.class, false);
final boolean isExceptTarget = target instanceof PyExceptPart || target instanceof PyFinallyPart;
final boolean isLoopTarget = target instanceof PyWhileStatement || PyForStatementNavigator.getPyForStatementByIterable(target) != null;
if (target != null && !isExceptTarget && !isLoopTarget) {
targetInstructions.add(targetInstruction);
}
if (returnStatement != null && CodeFragmentUtil.getPosition(returnStatement, start, end) == Position.INSIDE) {
returnSources++;
} else if (!isExceptTarget) {
regularSources++;
}
}
final Set<PsiElement> subGraphElements = getSubGraphElements(subGraph);
for (PsiElement element : subGraphElements) {
if (element instanceof PyFromImportStatement) {
final PyFromImportStatement fromImportStatement = (PyFromImportStatement) element;
if (fromImportStatement.getStarImportElement() != null) {
starImports++;
}
}
if (element instanceof PyContinueStatement || element instanceof PyBreakStatement) {
final PyLoopStatement loopStatement = PsiTreeUtil.getParentOfType(element, PyLoopStatement.class);
if (loopStatement != null && !subGraphElements.contains(loopStatement)) {
outerLoopBreaks++;
}
}
if (element instanceof PyYieldExpression) {
yieldExpressions++;
}
}
return new AnalysisResult(starImports, targetInstructions.size(), returnSources, regularSources, outerLoopBreaks, yieldExpressions);
}
use of com.intellij.codeInsight.controlflow.Instruction in project intellij-community by JetBrains.
the class PyCodeFragmentUtil method getInputElements.
@NotNull
public static List<PsiElement> getInputElements(@NotNull List<Instruction> subGraph, @NotNull List<Instruction> graph) {
final List<PsiElement> result = new ArrayList<>();
final Set<PsiElement> subGraphElements = getSubGraphElements(subGraph);
for (Instruction instruction : getReadInstructions(subGraph)) {
final PsiElement element = instruction.getElement();
if (element != null) {
final PsiReference reference = element.getReference();
if (reference != null) {
for (PsiElement resolved : multiResolve(reference)) {
if (!subGraphElements.contains(resolved)) {
result.add(element);
break;
}
}
}
}
}
final List<PsiElement> outputElements = getOutputElements(subGraph, graph);
for (Instruction instruction : getWriteInstructions(subGraph)) {
final PsiElement element = instruction.getElement();
if (element != null) {
final PsiReference reference = element.getReference();
if (reference != null) {
for (PsiElement resolved : multiResolve(reference)) {
if (!subGraphElements.contains(resolved) && outputElements.contains(element)) {
result.add(element);
}
}
}
}
}
return result;
}
Aggregations