mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
correctly rename all accessors of a property (PY-5948)
This commit is contained in:
@@ -3,14 +3,15 @@ package com.jetbrains.python.refactoring.rename;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.refactoring.rename.RenamePsiElementProcessor;
|
||||
import com.intellij.util.Processor;
|
||||
import com.jetbrains.python.PyNames;
|
||||
import com.jetbrains.python.codeInsight.PyCodeInsightSettings;
|
||||
import com.jetbrains.python.psi.Property;
|
||||
import com.jetbrains.python.psi.PyClass;
|
||||
import com.jetbrains.python.psi.PyFunction;
|
||||
import com.jetbrains.python.psi.search.PyOverridingMethodsSearch;
|
||||
import com.jetbrains.python.psi.search.PySuperMethodsSearch;
|
||||
import com.jetbrains.python.toolbox.Maybe;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -89,5 +90,21 @@ public class RenamePyFunctionProcessor extends RenamePyElementProcessor {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
final PyClass containingClass = function.getContainingClass();
|
||||
if (containingClass != null) {
|
||||
final Property property = containingClass.findPropertyByFunction(function);
|
||||
if (property != null) {
|
||||
addRename(allRenames, newName, property.getGetter());
|
||||
addRename(allRenames, newName, property.getSetter());
|
||||
addRename(allRenames, newName, property.getDeleter());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void addRename(Map<PsiElement, String> renames, String newName, Maybe<PyFunction> accessor) {
|
||||
final PyFunction function = accessor.valueOrNull();
|
||||
if (function != null) {
|
||||
renames.put(function, newName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
class C(object):
|
||||
def __init__(self):
|
||||
self._x = None
|
||||
|
||||
@property
|
||||
def fo<caret>o(self):
|
||||
"""I'm the 'x' property."""
|
||||
return self._x
|
||||
|
||||
@foo.setter
|
||||
def foo(self, value):
|
||||
self._x = value
|
||||
|
||||
@foo.deleter
|
||||
def foo(self):
|
||||
del self._x
|
||||
@@ -0,0 +1,16 @@
|
||||
class C(object):
|
||||
def __init__(self):
|
||||
self._x = None
|
||||
|
||||
@property
|
||||
def bar(self):
|
||||
"""I'm the 'x' property."""
|
||||
return self._x
|
||||
|
||||
@bar.setter
|
||||
def bar(self, value):
|
||||
self._x = value
|
||||
|
||||
@bar.deleter
|
||||
def bar(self):
|
||||
del self._x
|
||||
@@ -4,6 +4,7 @@ import com.intellij.codeInsight.TargetElementUtilBase;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.refactoring.BaseRefactoringProcessor;
|
||||
import com.jetbrains.python.fixtures.PyTestCase;
|
||||
import com.jetbrains.python.psi.LanguageLevel;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
@@ -86,6 +87,16 @@ public class PyRenameTest extends PyTestCase {
|
||||
doTest("bar");
|
||||
}
|
||||
|
||||
public void testRenameProperty() { // PY-5948
|
||||
setLanguageLevel(LanguageLevel.PYTHON26);
|
||||
try {
|
||||
doTest("bar");
|
||||
}
|
||||
finally {
|
||||
setLanguageLevel(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void testClassNameConflict() { // PY-2390
|
||||
doRenameConflictTest("Foo", "A class named 'Foo' is already defined in classNameConflict.py");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user