correctly return import element as definition of a name; getLatestDefs() accepts name instead of element as search parameter

This commit is contained in:
Dmitry Jemerov
2012-01-16 17:45:19 +01:00
parent 8dc99bee9c
commit 2c36d0f7f8
3 changed files with 15 additions and 9 deletions
@@ -282,8 +282,10 @@ public class PyReferenceExpressionImpl extends PyElementImpl implements PyRefere
if (scopeOwner != null && scopeOwner == PsiTreeUtil.getStubOrPsiParentOfType(target, ScopeOwner.class)) {
PyAugAssignmentStatement augAssignment = PsiTreeUtil.getParentOfType(anchor, PyAugAssignmentStatement.class);
try {
final List<ReadWriteInstruction> defs = PyDefUseUtil.getLatestDefs(scopeOwner, (PyElement)target,
augAssignment != null ? augAssignment : anchor);
final List<ReadWriteInstruction> defs = PyDefUseUtil.getLatestDefs(scopeOwner,
((PyElement)target).getName(),
augAssignment != null ? augAssignment : anchor
);
if (!defs.isEmpty()) {
PyType type = defs.get(0).getType(context);
for (int i = 1; i < defs.size(); i++) {
@@ -11,6 +11,7 @@ import com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction;
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
import com.jetbrains.python.psi.PyElement;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.PyImportElement;
import com.jetbrains.python.psi.PyTargetExpression;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -25,7 +26,7 @@ public class PyDefUseUtil {
}
@NotNull
public static List<ReadWriteInstruction> getLatestDefs(ScopeOwner block, PyElement var, PsiElement anchor) {
public static List<ReadWriteInstruction> getLatestDefs(ScopeOwner block, String varName, PsiElement anchor) {
final ControlFlow controlFlow = ControlFlowCache.getControlFlow(block);
final Instruction[] instructions = controlFlow.getInstructions();
final int instr = ControlFlowUtil.findInstructionNumberByElement(instructions, anchor);
@@ -34,11 +35,11 @@ public class PyDefUseUtil {
}
final boolean[] visited = new boolean[instructions.length];
final Collection<ReadWriteInstruction> result = new LinkedHashSet<ReadWriteInstruction>();
getLatestDefs(var, instructions, instr, visited, result);
getLatestDefs(varName, instructions, instr, visited, result);
return new ArrayList<ReadWriteInstruction>(result);
}
private static void getLatestDefs(final PyElement var,
private static void getLatestDefs(String varName,
final Instruction[] instructions,
final int instr,
final boolean[] visited,
@@ -50,18 +51,21 @@ public class PyDefUseUtil {
final PsiElement element = instruction.getElement();
final String name = elementName(element);
final ReadWriteInstruction.ACCESS access = instruction.getAccess();
if ((access.isWriteAccess() || access.isAssertTypeAccess()) && Comparing.strEqual(name, var.getName())) {
if ((access.isWriteAccess() || access.isAssertTypeAccess()) && Comparing.strEqual(name, varName)) {
result.add(instruction);
return;
}
}
for (Instruction instruction : instructions[instr].allPred()) {
getLatestDefs(var, instructions, instruction.num(), visited, result);
getLatestDefs(varName, instructions, instruction.num(), visited, result);
}
}
@Nullable
private static String elementName(PsiElement element) {
if (element instanceof PyImportElement) {
return ((PyImportElement) element).getVisibleName();
}
return element instanceof PyElement ? ((PyElement)element).getName() : null;
}
@@ -141,7 +141,7 @@ public class PyInlineLocalHandler extends InlineActionHandler {
for (final PsiElement ref : refsToInline) {
final List<PsiElement> elems = new ArrayList<PsiElement>();
final List<ReadWriteInstruction> latestDefs = PyDefUseUtil.getLatestDefs(containerBlock, local, ref);
final List<ReadWriteInstruction> latestDefs = PyDefUseUtil.getLatestDefs(containerBlock, local.getName(), ref);
for (ReadWriteInstruction i : latestDefs) {
elems.add(i.getElement());
}
@@ -216,7 +216,7 @@ public class PyInlineLocalHandler extends InlineActionHandler {
PyTargetExpression local, Project project) {
if (expr != null) {
try {
final List<ReadWriteInstruction> candidates = PyDefUseUtil.getLatestDefs(containerBlock, local, expr);
final List<ReadWriteInstruction> candidates = PyDefUseUtil.getLatestDefs(containerBlock, local.getName(), expr);
if (candidates.size() == 1) {
final PyStatement expression = getAssignmentByLeftPart((PyElement)candidates.get(0).getElement());
return Pair.create(expression, false);