From 2c36d0f7f8a240533f7563cb47d53dfd89809801 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Mon, 16 Jan 2012 17:45:19 +0100 Subject: [PATCH] correctly return import element as definition of a name; getLatestDefs() accepts name instead of element as search parameter --- .../python/psi/impl/PyReferenceExpressionImpl.java | 6 ++++-- .../jetbrains/python/refactoring/PyDefUseUtil.java | 14 +++++++++----- .../refactoring/inline/PyInlineLocalHandler.java | 4 ++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/python/src/com/jetbrains/python/psi/impl/PyReferenceExpressionImpl.java b/python/src/com/jetbrains/python/psi/impl/PyReferenceExpressionImpl.java index 59f774786407..9d5c9c011042 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyReferenceExpressionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyReferenceExpressionImpl.java @@ -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 defs = PyDefUseUtil.getLatestDefs(scopeOwner, (PyElement)target, - augAssignment != null ? augAssignment : anchor); + final List 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++) { diff --git a/python/src/com/jetbrains/python/refactoring/PyDefUseUtil.java b/python/src/com/jetbrains/python/refactoring/PyDefUseUtil.java index f392db729d29..2b69b228a9b8 100644 --- a/python/src/com/jetbrains/python/refactoring/PyDefUseUtil.java +++ b/python/src/com/jetbrains/python/refactoring/PyDefUseUtil.java @@ -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 getLatestDefs(ScopeOwner block, PyElement var, PsiElement anchor) { + public static List 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 result = new LinkedHashSet(); - getLatestDefs(var, instructions, instr, visited, result); + getLatestDefs(varName, instructions, instr, visited, result); return new ArrayList(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; } diff --git a/python/src/com/jetbrains/python/refactoring/inline/PyInlineLocalHandler.java b/python/src/com/jetbrains/python/refactoring/inline/PyInlineLocalHandler.java index fad17d346af9..c44c33a74a73 100644 --- a/python/src/com/jetbrains/python/refactoring/inline/PyInlineLocalHandler.java +++ b/python/src/com/jetbrains/python/refactoring/inline/PyInlineLocalHandler.java @@ -141,7 +141,7 @@ public class PyInlineLocalHandler extends InlineActionHandler { for (final PsiElement ref : refsToInline) { final List elems = new ArrayList(); - final List latestDefs = PyDefUseUtil.getLatestDefs(containerBlock, local, ref); + final List 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 candidates = PyDefUseUtil.getLatestDefs(containerBlock, local, expr); + final List 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);