mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Introduce Constant adds element after imports (PY-2149)
This commit is contained in:
+10
-3
@@ -3,8 +3,7 @@ package com.jetbrains.python.refactoring.introduce.constant;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.util.containers.HashSet;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.psi.PyExpression;
|
||||
import com.jetbrains.python.psi.PyFile;
|
||||
import com.jetbrains.python.psi.*;
|
||||
import com.jetbrains.python.refactoring.introduce.IntroduceHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -28,7 +27,15 @@ public class ConstantIntroduceHandler extends IntroduceHandler {
|
||||
PsiElement anchor;
|
||||
anchor = expression.getContainingFile();
|
||||
assert anchor instanceof PyFile;
|
||||
return anchor.addBefore(declaration, ((PyFile)anchor).getStatements().get(0));
|
||||
final List<PyStatement> statements = ((PyFile)anchor).getStatements();
|
||||
int targetIndex = 0;
|
||||
while(targetIndex < statements.size() && statements.get(targetIndex) instanceof PyImportStatementBase) {
|
||||
targetIndex++;
|
||||
}
|
||||
if (targetIndex == statements.size()) {
|
||||
return anchor.add(declaration);
|
||||
}
|
||||
return anchor.addBefore(declaration, statements.get(targetIndex));
|
||||
}
|
||||
|
||||
public Collection<String> getSuggestedNames(@NotNull final PyExpression expression) {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import urllib
|
||||
import urllib2
|
||||
|
||||
a = 42
|
||||
|
||||
def foo():
|
||||
return a
|
||||
@@ -0,0 +1,5 @@
|
||||
import urllib
|
||||
import urllib2
|
||||
|
||||
def foo():
|
||||
return <caret>42
|
||||
@@ -22,6 +22,10 @@ public class PyIntroduceConstantTest extends PyLightFixtureTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testInsertAfterImport() { // PY-2149
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
myFixture.configureByFile("/refactoring/introduceConstant/" + getTestName(true) + ".py");
|
||||
ConstantIntroduceHandler handler = new ConstantIntroduceHandler();
|
||||
|
||||
Reference in New Issue
Block a user