use of com.intellij.codeInsight.controlflow.Instruction in project intellij-community by JetBrains.
the class PyDefUseUtil method getLatestDefs.
private static Collection<Instruction> getLatestDefs(final String varName, final Instruction[] instructions, final int instr, final boolean acceptTypeAssertions, final boolean acceptImplicitImports) {
final Collection<Instruction> result = new LinkedHashSet<>();
ControlFlowUtil.iteratePrev(instr, instructions, instruction -> {
final PsiElement element = instruction.getElement();
final PyImplicitImportNameDefiner implicit = PyUtil.as(element, PyImplicitImportNameDefiner.class);
if (instruction instanceof ReadWriteInstruction) {
final ReadWriteInstruction rwInstruction = (ReadWriteInstruction) instruction;
final ReadWriteInstruction.ACCESS access = rwInstruction.getAccess();
if (access.isWriteAccess() || acceptTypeAssertions && access.isAssertTypeAccess()) {
final String name = elementName(element);
if (Comparing.strEqual(name, varName)) {
result.add(rwInstruction);
return ControlFlowUtil.Operation.CONTINUE;
}
}
} else if (acceptImplicitImports && implicit != null) {
if (!implicit.multiResolveName(varName).isEmpty()) {
result.add(instruction);
return ControlFlowUtil.Operation.CONTINUE;
}
}
return ControlFlowUtil.Operation.NEXT;
});
return result;
}
use of com.intellij.codeInsight.controlflow.Instruction in project intellij-community by JetBrains.
the class PyConvertLambdaToFunctionIntention method isAvailable.
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PyLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyLambdaExpression.class);
if (lambdaExpression != null) {
if (lambdaExpression.getBody() != null) {
final ControlFlow flow = ControlFlowCache.getControlFlow(lambdaExpression);
final List<Instruction> graph = Arrays.asList(flow.getInstructions());
final List<PsiElement> elements = PyCodeFragmentUtil.getInputElements(graph, graph);
if (elements.size() > 0)
return false;
return true;
}
}
return false;
}
use of com.intellij.codeInsight.controlflow.Instruction in project intellij-community by JetBrains.
the class PyHighlightExitPointsHandler method findExitPointsAndStatements.
private static Collection<PsiElement> findExitPointsAndStatements(final ControlFlow flow) {
final List<PsiElement> statements = new ArrayList<>();
final Instruction[] instructions = flow.getInstructions();
for (Instruction instruction : instructions[instructions.length - 1].allPred()) {
final PsiElement element = instruction.getElement();
if (element == null) {
continue;
}
final PsiElement statement = PyPsiUtils.getStatement(element);
if (statement != null) {
statements.add(statement);
}
}
return statements;
}
use of com.intellij.codeInsight.controlflow.Instruction in project intellij-community by JetBrains.
the class PyReferenceExpressionImpl method getTypeByControlFlow.
private static PyType getTypeByControlFlow(@NotNull String name, @NotNull TypeEvalContext context, @NotNull PyExpression anchor, @NotNull ScopeOwner scopeOwner) {
final PyAugAssignmentStatement augAssignment = PsiTreeUtil.getParentOfType(anchor, PyAugAssignmentStatement.class);
final PyElement element = augAssignment != null ? augAssignment : anchor;
try {
final List<Instruction> defs = PyDefUseUtil.getLatestDefs(scopeOwner, name, element, true, false);
if (!defs.isEmpty()) {
final ReadWriteInstruction firstInstruction = PyUtil.as(defs.get(0), ReadWriteInstruction.class);
PyType type = firstInstruction != null ? firstInstruction.getType(context, anchor) : null;
for (int i = 1; i < defs.size(); i++) {
final ReadWriteInstruction instruction = PyUtil.as(defs.get(i), ReadWriteInstruction.class);
type = PyUnionType.union(type, instruction != null ? instruction.getType(context, anchor) : null);
}
return type;
}
} catch (PyDefUseUtil.InstructionNotFoundException ignored) {
}
return null;
}
use of com.intellij.codeInsight.controlflow.Instruction in project intellij-community by JetBrains.
the class ScopeImpl method getDeclaredVariable.
public ScopeVariable getDeclaredVariable(@NotNull final PsiElement anchorElement, @NotNull final String name) throws DFALimitExceededException {
computeScopeVariables();
for (int i = 0; i < myFlow.length; i++) {
Instruction instruction = myFlow[i];
final PsiElement element = instruction.getElement();
if (element == anchorElement) {
return myCachedScopeVariables.get(i).get(name);
}
}
return null;
}
Aggregations