PY-13668 Fix anchor element for local import insertion

This commit is contained in:
Mikhail Golubev
2014-08-11 16:01:36 +04:00
parent 043299ef98
commit d6ce01047e
9 changed files with 30 additions and 3 deletions
@@ -74,8 +74,7 @@ public class AddImportHelper {
@Nullable
public static PsiElement getLocalInsertPosition(@NotNull PyElement anchor) {
final PyStatement enclosingStatement = PsiTreeUtil.getParentOfType(anchor, PyStatement.class, false);
return enclosingStatement != null ? enclosingStatement.getFirstChild() : null;
return PsiTreeUtil.getParentOfType(anchor, PyStatement.class, false);
}
public enum ImportPriority {
@@ -52,7 +52,7 @@ class ImportCandidateHolder implements Comparable<ImportCandidateHolder> {
* Creates new instance.
*
* @param importable an element that could be imported either from import element or from file.
* @param file the file which is the source of the importable
* @param file the file which is the source of the importable (module for symbols, containing directory for modules and packages)
* @param importElement an existing import element that can be a source for the importable.
* @param path import path for the file, as a qualified name (a.b.c)
* For top-level imported symbols it's <em>qualified name of containing module</em> (or package for __init__.py).
@@ -1,5 +1,6 @@
def func():
for _ range(10):
from package.module import foo
foo
# <ref>
@@ -1,6 +1,7 @@
def func():
try:
import module
module
# <ref>
except:
@@ -0,0 +1,6 @@
def func():
if True:
import module
module
# <ref>
@@ -0,0 +1,3 @@
def func():
if True: module
# <ref>
@@ -0,0 +1,5 @@
def func():
import module
module
# <ref>
@@ -0,0 +1,2 @@
def func(): module
# <ref>
@@ -69,6 +69,16 @@ public class PyAddImportTest extends PyTestCase {
doAddLocalImport("module", null);
}
// PY-13668
public void testLocalImportInlineFunctionBody() {
testLocalImport();
}
// PY-13668
public void testLocalImportInlineBranch() {
testLocalImport();
}
/**
* Add local import statement
* @param name reference name in corresponding import element