From ed0e7ca4747392f6f796e60e6ede654cbb093ff3 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Tue, 3 Apr 2012 17:49:37 +0200 Subject: [PATCH] correctly rename all accessors of a property (PY-5948) --- .../rename/RenamePyFunctionProcessor.java | 19 ++++++++++++++++++- .../refactoring/rename/renameProperty.py | 16 ++++++++++++++++ .../rename/renameProperty_after.py | 16 ++++++++++++++++ .../python/refactoring/PyRenameTest.java | 11 +++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 python/testData/refactoring/rename/renameProperty.py create mode 100644 python/testData/refactoring/rename/renameProperty_after.py diff --git a/python/src/com/jetbrains/python/refactoring/rename/RenamePyFunctionProcessor.java b/python/src/com/jetbrains/python/refactoring/rename/RenamePyFunctionProcessor.java index adcf7b83a7f0..eefceb611661 100644 --- a/python/src/com/jetbrains/python/refactoring/rename/RenamePyFunctionProcessor.java +++ b/python/src/com/jetbrains/python/refactoring/rename/RenamePyFunctionProcessor.java @@ -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 renames, String newName, Maybe accessor) { + final PyFunction function = accessor.valueOrNull(); + if (function != null) { + renames.put(function, newName); + } } } diff --git a/python/testData/refactoring/rename/renameProperty.py b/python/testData/refactoring/rename/renameProperty.py new file mode 100644 index 000000000000..553b0cc74893 --- /dev/null +++ b/python/testData/refactoring/rename/renameProperty.py @@ -0,0 +1,16 @@ +class C(object): + def __init__(self): + self._x = None + + @property + def foo(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 diff --git a/python/testData/refactoring/rename/renameProperty_after.py b/python/testData/refactoring/rename/renameProperty_after.py new file mode 100644 index 000000000000..538e52d6601d --- /dev/null +++ b/python/testData/refactoring/rename/renameProperty_after.py @@ -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 diff --git a/python/testSrc/com/jetbrains/python/refactoring/PyRenameTest.java b/python/testSrc/com/jetbrains/python/refactoring/PyRenameTest.java index 1517e2e668a3..f196e27d3f4d 100644 --- a/python/testSrc/com/jetbrains/python/refactoring/PyRenameTest.java +++ b/python/testSrc/com/jetbrains/python/refactoring/PyRenameTest.java @@ -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"); }