From f0918e0113d4aadb96d5347ffd1c17db3b276196 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Wed, 14 May 2014 14:13:00 +0400 Subject: [PATCH] fixed PY-12401 inline refactoring looses comments --- .../python/refactoring/inline/PyInlineLocalHandler.java | 9 ++++++++- python/testData/refactoring/inlinelocal/comment.after.py | 2 ++ .../testData/refactoring/inlinelocal/comment.before.py | 2 ++ .../jetbrains/python/refactoring/PyInlineLocalTest.java | 4 ++++ 4 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 python/testData/refactoring/inlinelocal/comment.after.py create mode 100644 python/testData/refactoring/inlinelocal/comment.before.py diff --git a/python/src/com/jetbrains/python/refactoring/inline/PyInlineLocalHandler.java b/python/src/com/jetbrains/python/refactoring/inline/PyInlineLocalHandler.java index c2433df9a7dd..a582b1728d47 100644 --- a/python/src/com/jetbrains/python/refactoring/inline/PyInlineLocalHandler.java +++ b/python/src/com/jetbrains/python/refactoring/inline/PyInlineLocalHandler.java @@ -42,6 +42,7 @@ import com.intellij.refactoring.util.CommonRefactoringUtil; import com.intellij.refactoring.util.RefactoringMessageDialog; import com.intellij.util.Query; import com.jetbrains.python.PyBundle; +import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.PythonLanguage; import com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction; import com.jetbrains.python.codeInsight.controlflow.ScopeOwner; @@ -95,7 +96,7 @@ public class PyInlineLocalHandler extends InlineActionHandler { invoke(project, editor, (PyTargetExpression)element, refExpr); } - public static void invoke(final Project project, final Editor editor, PyTargetExpression local, PyReferenceExpression refExpr) { + public static void invoke(final Project project, final Editor editor, final PyTargetExpression local, PyReferenceExpression refExpr) { if (!CommonRefactoringUtil.checkReadOnlyStatus(project, local)) return; final HighlightManager highlightManager = HighlightManager.getInstance(project); @@ -187,6 +188,12 @@ public class PyInlineLocalHandler extends InlineActionHandler { PsiElement[] exprs = new PsiElement[refsToInline.length]; final PyExpression value = prepareValue(def, localName, project); final PyExpression withParent = PyElementGenerator.getInstance(project).createExpressionFromText("(" + value.getText() + ")"); + final PsiElement lastChild = def.getLastChild(); + if (lastChild != null && lastChild.getNode().getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) { + final PsiElement parent = def.getParent(); + if (parent != null) parent.addBefore(lastChild, def); + } + for (int i = 0, refsToInlineLength = refsToInline.length; i < refsToInlineLength; i++) { PsiElement element = refsToInline[i]; if (PyReplaceExpressionUtil.isNeedParenthesis((PyExpression)element, value)) { diff --git a/python/testData/refactoring/inlinelocal/comment.after.py b/python/testData/refactoring/inlinelocal/comment.after.py new file mode 100644 index 000000000000..2ee8e7cb36b2 --- /dev/null +++ b/python/testData/refactoring/inlinelocal/comment.after.py @@ -0,0 +1,2 @@ +# some clever comment +value = "aaaaaa" + "bbbbb" diff --git a/python/testData/refactoring/inlinelocal/comment.before.py b/python/testData/refactoring/inlinelocal/comment.before.py new file mode 100644 index 000000000000..4fe5976cbb39 --- /dev/null +++ b/python/testData/refactoring/inlinelocal/comment.before.py @@ -0,0 +1,2 @@ +temp = "aaaaaa" # some clever comment +value = temp + "bbbbb" diff --git a/python/testSrc/com/jetbrains/python/refactoring/PyInlineLocalTest.java b/python/testSrc/com/jetbrains/python/refactoring/PyInlineLocalTest.java index e31c25a898b8..4f02da1038ff 100644 --- a/python/testSrc/com/jetbrains/python/refactoring/PyInlineLocalTest.java +++ b/python/testSrc/com/jetbrains/python/refactoring/PyInlineLocalTest.java @@ -80,4 +80,8 @@ public class PyInlineLocalTest extends PyTestCase { public void testPy5832() { doTest(); } + + public void testComment() { + doTest(); + } }