mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import pkg.m2
|
||||
import pkg.subpkg.m3
|
||||
|
||||
|
||||
print(pkg.m2, pkg.subpkg.m3.VAR)
|
||||
+1
@@ -0,0 +1 @@
|
||||
VAR = 42
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import m2
|
||||
import subpkg.m3
|
||||
|
||||
|
||||
print(m2, subpkg.m3.VAR)
|
||||
+1
@@ -0,0 +1 @@
|
||||
VAR = 42
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user