From c0245974916a2e728c697b16f41d8abac6fb25cb Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Thu, 21 Feb 2013 14:09:17 +0400 Subject: [PATCH] fixed PY-7151 Convert triple-quoted string to single-quoted string: do not wrap string with parenthesis if initial string is already inside them --- .../PyConvertTripleQuotedStringIntention.java | 10 ++++++++-- .../afterConvertTripleQuotedParenthesizedString.py | 4 ++++ .../beforeConvertTripleQuotedParenthesizedString.py | 5 +++++ .../testSrc/com/jetbrains/python/PyIntentionTest.java | 4 ++++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 python/testData/intentions/afterConvertTripleQuotedParenthesizedString.py create mode 100644 python/testData/intentions/beforeConvertTripleQuotedParenthesizedString.py diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertTripleQuotedStringIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertTripleQuotedStringIntention.java index bac9abb7e926..5fc125748e1c 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertTripleQuotedStringIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertTripleQuotedStringIntention.java @@ -4,6 +4,7 @@ import com.intellij.codeInsight.intention.impl.BaseIntentionAction; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.text.StringUtil; +import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; @@ -62,6 +63,7 @@ public class PyConvertTripleQuotedStringIntention extends BaseIntentionAction { PyStringLiteralExpression string = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyStringLiteralExpression.class); PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project); if (string != null) { + final PsiElement parent = string.getParent(); String stringText = string.getText(); final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText); String prefix = stringText.substring(0, prefixLength); @@ -70,7 +72,6 @@ public class PyConvertTripleQuotedStringIntention extends BaseIntentionAction { stringText = string.getStringValue(); List subStrings = StringUtil.split(stringText, "\n", false, true); - StringBuilder result = new StringBuilder(); if (subStrings.size() != 1) result.append("("); @@ -91,7 +92,12 @@ public class PyConvertTripleQuotedStringIntention extends BaseIntentionAction { if (subStrings.size() != 1) result.append(")"); PyExpressionStatement e = elementGenerator.createFromText(LanguageLevel.forElement(string), PyExpressionStatement.class, result.toString()); - string.replace(e.getExpression()); + + PyExpression expression = e.getExpression(); + if (parent instanceof PyTupleExpression && expression instanceof PyParenthesizedExpression) + expression = ((PyParenthesizedExpression)expression).getContainedExpression(); + if (expression != null) + string.replace(expression); } } diff --git a/python/testData/intentions/afterConvertTripleQuotedParenthesizedString.py b/python/testData/intentions/afterConvertTripleQuotedParenthesizedString.py new file mode 100644 index 000000000000..a02136a08361 --- /dev/null +++ b/python/testData/intentions/afterConvertTripleQuotedParenthesizedString.py @@ -0,0 +1,4 @@ +t = ("string\n" + "some\n" + "other\n" + "lines\n", ) \ No newline at end of file diff --git a/python/testData/intentions/beforeConvertTripleQuotedParenthesizedString.py b/python/testData/intentions/beforeConvertTripleQuotedParenthesizedString.py new file mode 100644 index 000000000000..d8d22bf70e42 --- /dev/null +++ b/python/testData/intentions/beforeConvertTripleQuotedParenthesizedString.py @@ -0,0 +1,5 @@ +t = ("""string +some +other +lines +""", ) \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyIntentionTest.java b/python/testSrc/com/jetbrains/python/PyIntentionTest.java index 9f91f597cc78..d452abc80d19 100644 --- a/python/testSrc/com/jetbrains/python/PyIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/PyIntentionTest.java @@ -255,6 +255,10 @@ public class PyIntentionTest extends PyTestCase { doTest(PyBundle.message("INTN.triple.quoted.string")); } + public void testConvertTripleQuotedParenthesizedString() { //PY-7151 + doTest(PyBundle.message("INTN.triple.quoted.string")); + } + public void testTransformConditionalExpression() { //PY-3094 doTest(PyBundle.message("INTN.transform.into.if.else.statement")); }