Search in sources :

Example 6 with Instruction

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;
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with Instruction

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;
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with Instruction

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;
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with Instruction

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);
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with Instruction

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;
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Instruction (com.intellij.codeInsight.controlflow.Instruction)33 ReadWriteInstruction (com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction)15 NotNull (org.jetbrains.annotations.NotNull)12 PsiElement (com.intellij.psi.PsiElement)10 ControlFlow (com.intellij.codeInsight.controlflow.ControlFlow)7 ArrayList (java.util.ArrayList)5 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)4 Pair (com.intellij.openapi.util.Pair)3 Scope (com.jetbrains.python.codeInsight.dataflow.scope.Scope)3 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 PyDefUseUtil (com.jetbrains.python.refactoring.PyDefUseUtil)2 TargetElementUtil (com.intellij.codeInsight.TargetElementUtil)1 CannotCreateCodeFragmentException (com.intellij.codeInsight.codeFragment.CannotCreateCodeFragmentException)1 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)1 Language (com.intellij.lang.Language)1 InlineActionHandler (com.intellij.lang.refactoring.InlineActionHandler)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 Logger (com.intellij.openapi.diagnostic.Logger)1