mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Introduce Field honors name of method argument (PY-1580)
This commit is contained in:
@@ -36,6 +36,25 @@ import java.util.*;
|
||||
* @author Alexey.Ivanov
|
||||
*/
|
||||
abstract public class IntroduceHandler implements RefactoringActionHandler {
|
||||
protected static PsiElement findAnchor(List<PsiElement> occurrences) {
|
||||
PsiElement anchor = occurrences.get(0);
|
||||
next:
|
||||
do {
|
||||
PyStatement statement = PsiTreeUtil.getParentOfType(anchor, PyStatement.class);
|
||||
|
||||
final PsiElement parent = statement.getParent();
|
||||
for (PsiElement element : occurrences) {
|
||||
if (!PsiTreeUtil.isAncestor(parent, element, true)) {
|
||||
anchor = statement;
|
||||
continue next;
|
||||
}
|
||||
}
|
||||
|
||||
return statement;
|
||||
}
|
||||
while (true);
|
||||
}
|
||||
|
||||
public enum InitPlace {
|
||||
SAME_METHOD,
|
||||
CONSTRUCTOR,
|
||||
@@ -234,7 +253,8 @@ abstract public class IntroduceHandler implements RefactoringActionHandler {
|
||||
initInConstructor = dialog.getInitPlace();
|
||||
}
|
||||
String assignmentText = name + " = " + expression.getText();
|
||||
PyAssignmentStatement declaration = createDeclaration(project, assignmentText);
|
||||
PsiElement anchor = replaceAll ? findAnchor(occurrences) : PsiTreeUtil.getParentOfType(expression, PyStatement.class);
|
||||
PyAssignmentStatement declaration = createDeclaration(project, assignmentText, anchor);
|
||||
|
||||
assert name != null;
|
||||
declaration = performReplace(project, declaration, expression, occurrences, name, replaceAll, initInConstructor);
|
||||
@@ -245,7 +265,7 @@ abstract public class IntroduceHandler implements RefactoringActionHandler {
|
||||
|
||||
protected abstract String getHelpId();
|
||||
|
||||
protected PyAssignmentStatement createDeclaration(Project project, String assignmentText) {
|
||||
protected PyAssignmentStatement createDeclaration(Project project, String assignmentText, PsiElement anchor) {
|
||||
return PyElementGenerator.getInstance(project).createFromText(PyAssignmentStatement.class, assignmentText);
|
||||
}
|
||||
|
||||
|
||||
+14
-2
@@ -8,6 +8,7 @@ import com.intellij.openapi.editor.SelectionModel;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.util.Function;
|
||||
@@ -107,8 +108,19 @@ public class FieldIntroduceHandler extends IntroduceHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PyAssignmentStatement createDeclaration(Project project, String assignmentText) {
|
||||
return PyElementGenerator.getInstance(project).createFromText(PyAssignmentStatement.class, PyNames.CANONICAL_SELF + "." + assignmentText);
|
||||
protected PyAssignmentStatement createDeclaration(Project project, String assignmentText, PsiElement anchor) {
|
||||
String selfName = PyNames.CANONICAL_SELF;
|
||||
final PyFunction container = PsiTreeUtil.getParentOfType(anchor, PyFunction.class);
|
||||
if (container != null) {
|
||||
final PyParameter[] params = container.getParameterList().getParameters();
|
||||
if (params.length > 0) {
|
||||
final PyNamedParameter named = params[0].getAsNamed();
|
||||
if (named != null) {
|
||||
selfName = named.getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
return PyElementGenerator.getInstance(project).createFromText(PyAssignmentStatement.class, selfName + "." + assignmentText);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+1
-23
@@ -10,10 +10,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
* User: Alexey.Ivanov
|
||||
* Date: Aug 19, 2009
|
||||
* Time: 5:22:02 PM
|
||||
* @author Alexey.Ivanov
|
||||
*/
|
||||
public class VariableIntroduceHandler extends IntroduceHandler {
|
||||
public VariableIntroduceHandler() {
|
||||
@@ -39,25 +36,6 @@ public class VariableIntroduceHandler extends IntroduceHandler {
|
||||
return parent.addBefore(declaration, anchor);
|
||||
}
|
||||
|
||||
private static PsiElement findAnchor(List<PsiElement> occurrences) {
|
||||
PsiElement anchor = occurrences.get(0);
|
||||
next:
|
||||
do {
|
||||
PyStatement statement = PsiTreeUtil.getParentOfType(anchor, PyStatement.class);
|
||||
|
||||
final PsiElement parent = statement.getParent();
|
||||
for (PsiElement element : occurrences) {
|
||||
if (!PsiTreeUtil.isAncestor(parent, element, true)) {
|
||||
anchor = statement;
|
||||
continue next;
|
||||
}
|
||||
}
|
||||
|
||||
return statement;
|
||||
}
|
||||
while (true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getHelpId() {
|
||||
return "refactoring.introduceVariable";
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
class DirectMeta(type):
|
||||
def __init__(cls, arg1, arg2):
|
||||
cls.a = arg1
|
||||
print cls.a
|
||||
@@ -0,0 +1,3 @@
|
||||
class DirectMeta(type):
|
||||
def __init__(cls, arg1, arg2):
|
||||
print a<caret>rg1
|
||||
@@ -57,6 +57,7 @@ public class PythonAllTestsSuite {
|
||||
PySmartEnterTest.class,
|
||||
PyStatementMoverTest.class,
|
||||
PyIntroduceVariableTest.class,
|
||||
PyIntroduceFieldTest.class,
|
||||
PyClassNameCompletionTest.class,
|
||||
PySuppressInspectionsTest.class,
|
||||
PyPropertyTestSuite.PyClassicPropertyTest.class,
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.jetbrains.python.refactoring;
|
||||
|
||||
import com.jetbrains.python.fixtures.PyLightFixtureTestCase;
|
||||
import com.jetbrains.python.refactoring.introduce.field.FieldIntroduceHandler;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class PyIntroduceFieldTest extends PyLightFixtureTestCase {
|
||||
public void testMetaClass() { // PY-1580
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
myFixture.configureByFile("/refactoring/introduceField/" + getTestName(true) + ".py");
|
||||
FieldIntroduceHandler handler = new FieldIntroduceHandler();
|
||||
handler.performAction(myFixture.getProject(), myFixture.getEditor(), myFixture.getFile(), "a", true, false, false);
|
||||
myFixture.checkResultByFile("/refactoring/introduceField/" + getTestName(true) + ".after.py");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user