From e13da9d5dd439797007746d3cb81c7d4e78a4510 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Sat, 9 Aug 2014 01:49:06 +0400 Subject: [PATCH] PY-13484 Allow to extract constant from parameter default value --- .../introduce/constant/PyIntroduceConstantHandler.java | 6 ++++++ .../introduceConstant/fromParameterDefaultValue.after.py | 5 +++++ .../introduceConstant/fromParameterDefaultValue.py | 2 ++ .../python/refactoring/PyIntroduceConstantTest.java | 7 ++++++- 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 python/testData/refactoring/introduceConstant/fromParameterDefaultValue.after.py create mode 100644 python/testData/refactoring/introduceConstant/fromParameterDefaultValue.py diff --git a/python/src/com/jetbrains/python/refactoring/introduce/constant/PyIntroduceConstantHandler.java b/python/src/com/jetbrains/python/refactoring/introduce/constant/PyIntroduceConstantHandler.java index af9ff583e3e4..44fdee8abdf7 100644 --- a/python/src/com/jetbrains/python/refactoring/introduce/constant/PyIntroduceConstantHandler.java +++ b/python/src/com/jetbrains/python/refactoring/introduce/constant/PyIntroduceConstantHandler.java @@ -24,6 +24,7 @@ import com.jetbrains.python.codeInsight.controlflow.ScopeOwner; import com.jetbrains.python.codeInsight.imports.AddImportHelper; import com.jetbrains.python.psi.PyExpression; import com.jetbrains.python.psi.PyFile; +import com.jetbrains.python.psi.PyParameterList; import com.jetbrains.python.refactoring.PyReplaceExpressionUtil; import com.jetbrains.python.refactoring.introduce.IntroduceHandler; import com.jetbrains.python.refactoring.introduce.IntroduceOperation; @@ -65,6 +66,11 @@ public class PyIntroduceConstantHandler extends IntroduceHandler { return names; } + @Override + protected boolean isValidIntroduceContext(PsiElement element) { + return super.isValidIntroduceContext(element) || PsiTreeUtil.getParentOfType(element, PyParameterList.class) != null; + } + @Override protected String getHelpId() { return "python.reference.introduceConstant"; diff --git a/python/testData/refactoring/introduceConstant/fromParameterDefaultValue.after.py b/python/testData/refactoring/introduceConstant/fromParameterDefaultValue.after.py new file mode 100644 index 000000000000..3f7191e54968 --- /dev/null +++ b/python/testData/refactoring/introduceConstant/fromParameterDefaultValue.after.py @@ -0,0 +1,5 @@ +a = 1 + 2 + + +def func(x=a + 3): + pass \ No newline at end of file diff --git a/python/testData/refactoring/introduceConstant/fromParameterDefaultValue.py b/python/testData/refactoring/introduceConstant/fromParameterDefaultValue.py new file mode 100644 index 000000000000..507a2df4f623 --- /dev/null +++ b/python/testData/refactoring/introduceConstant/fromParameterDefaultValue.py @@ -0,0 +1,2 @@ +def func(x=1 + 2 + 3): + pass \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceConstantTest.java b/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceConstantTest.java index 769e72077461..a54c68a0db09 100644 --- a/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceConstantTest.java +++ b/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceConstantTest.java @@ -51,7 +51,7 @@ public class PyIntroduceConstantTest extends PyIntroduceTestCase { public void testSuggestUniqueNames() { // PY-4409 doTestSuggestions(PyExpression.class, "S1"); } - + public void testSuggestUniqueNamesGlobalScope() { // PY-4409 doTestSuggestions(PyExpression.class, "S1"); } @@ -60,6 +60,11 @@ public class PyIntroduceConstantTest extends PyIntroduceTestCase { doTestInplace(null); } + // PY-13484 + public void testFromParameterDefaultValue() { + doTest(); + } + @Override protected String getTestDataPath() { return super.getTestDataPath() + "/refactoring/introduceConstant";