PY-14617 Update references to modules imported via relative imports inside the moved module

Current solution is very crude, because we can't use standard IDE
facilities to find usages of the import element in the a file, once
that file was moved. And exactly that API MoveFileHandler offers to us.
This commit is contained in:
Mikhail Golubev
2015-06-17 13:56:48 +03:00
parent 8166fab58b
commit 566faf4602
13 changed files with 39 additions and 1 deletions
@@ -61,7 +61,7 @@ public class RatedResolveResult implements ResolveResult {
@Override
public String toString() {
return myWhat.toString() + "@" + myRate;
return String.valueOf(myWhat) + "@" + myRate;
}
/**
@@ -16,6 +16,7 @@
package com.jetbrains.python.refactoring.move;
import com.intellij.lang.injection.InjectedLanguageManager;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Couple;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
@@ -33,6 +34,8 @@ import com.jetbrains.python.PyNames;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.PythonFileType;
import com.jetbrains.python.actions.CreatePackageAction;
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil;
import com.jetbrains.python.codeInsight.imports.PyImportOptimizer;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.resolve.PyResolveContext;
@@ -117,6 +120,24 @@ public class PyMoveFileHandler extends MoveFileHandler {
}
final QualifiedName newName = QualifiedNameFinder.findShortestImportableQName(resolved);
replaceWithQualifiedExpression(referenceExpr, newName);
final QualifiedName oldQualifiedName = referenceExpr.asQualifiedName();
if (!Comparing.equal(oldQualifiedName, newName)) {
final ScopeOwner scopeOwner = ScopeUtil.getScopeOwner(importElement);
if (scopeOwner == null) {
continue;
}
scopeOwner.accept(new PyRecursiveElementVisitor() {
@Override
public void visitPyReferenceExpression(PyReferenceExpression node) {
if (Comparing.equal(node.asQualifiedName(), oldQualifiedName)) {
replaceWithQualifiedExpression(node, newName);
}
else {
super.visitPyReferenceExpression(node);
}
}
});
}
}
}
@@ -0,0 +1,5 @@
import pkg.m2
import pkg.subpkg.m3
print(pkg.m2, pkg.subpkg.m3.VAR)
@@ -0,0 +1,5 @@
import m2
import subpkg.m3
print(m2, subpkg.m3.VAR)
@@ -329,6 +329,11 @@ public class PyMoveTest extends PyTestCase {
doMoveFileTest("pkg/subpkg", "");
}
// PY-14617
public void testUsagesOfUnqualifiedOldStyleRelativeImportsInsideMovedModule() {
doMoveFileTest("pkg/m1.py", "");
}
// PY-15324
public void testInterdependentSymbols() {
doMoveSymbolsTest("b.py", "f", "A");