diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertTripleQuotedStringIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertTripleQuotedStringIntention.java index 339f32fc6853..bac9abb7e926 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertTripleQuotedStringIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertTripleQuotedStringIntention.java @@ -9,6 +9,7 @@ import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import com.jetbrains.python.PyBundle; import com.jetbrains.python.psi.*; +import com.jetbrains.python.psi.impl.PyStringLiteralExpressionImpl; import org.jetbrains.annotations.NotNull; import java.util.List; @@ -47,6 +48,8 @@ public class PyConvertTripleQuotedStringIntention extends BaseIntentionAction { if (docStringOwner.getDocStringExpression() == string) return false; } String stringText = string.getText(); + final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText); + stringText = stringText.substring(prefixLength); if (stringText.length() >= 6) { if (stringText.startsWith("'''") && stringText.endsWith("'''") || stringText.startsWith("\"\"\"") && stringText.endsWith("\"\"\"")) return true; @@ -59,15 +62,21 @@ public class PyConvertTripleQuotedStringIntention extends BaseIntentionAction { PyStringLiteralExpression string = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyStringLiteralExpression.class); PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project); if (string != null) { - String stringText = string.getStringValue(); + String stringText = string.getText(); + final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText); + String prefix = stringText.substring(0, prefixLength); + Character firstQuote = stringText.substring(prefixLength).charAt(0); + + stringText = string.getStringValue(); List subStrings = StringUtil.split(stringText, "\n", false, true); - Character firstQuote = string.getText().charAt(0); + StringBuilder result = new StringBuilder(); if (subStrings.size() != 1) result.append("("); boolean lastString = false; for (String s : subStrings) { + result.append(prefix); result.append(firstQuote); String validSubstring = convertToValidSubString(s, firstQuote); diff --git a/python/testData/intentions/afterConvertTripleQuotedUnicodeString.py b/python/testData/intentions/afterConvertTripleQuotedUnicodeString.py new file mode 100644 index 000000000000..08be31282aaa --- /dev/null +++ b/python/testData/intentions/afterConvertTripleQuotedUnicodeString.py @@ -0,0 +1 @@ +s = u"string\n" diff --git a/python/testData/intentions/beforeConvertTripleQuotedUnicodeString.py b/python/testData/intentions/beforeConvertTripleQuotedUnicodeString.py new file mode 100644 index 000000000000..fea80394eb59 --- /dev/null +++ b/python/testData/intentions/beforeConvertTripleQuotedUnicodeString.py @@ -0,0 +1,2 @@ +s = u"""string +""" diff --git a/python/testSrc/com/jetbrains/python/PyIntentionTest.java b/python/testSrc/com/jetbrains/python/PyIntentionTest.java index 01609286713f..9f91f597cc78 100644 --- a/python/testSrc/com/jetbrains/python/PyIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/PyIntentionTest.java @@ -251,6 +251,10 @@ public class PyIntentionTest extends PyTestCase { doTest(PyBundle.message("INTN.triple.quoted.string")); } + public void testConvertTripleQuotedUnicodeString() { //PY-7152 + doTest(PyBundle.message("INTN.triple.quoted.string")); + } + public void testTransformConditionalExpression() { //PY-3094 doTest(PyBundle.message("INTN.transform.into.if.else.statement")); }