From 273447c075542b4b6e95bc896651fd4ded1607f8 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Tue, 6 Dec 2011 17:15:15 +0400 Subject: [PATCH] fixed PY-5226 "replace + with string formatting operator" produces bad code --- .../intentions/PyStringConcatenationToFormatIntention.java | 5 ++++- python/testData/intentions/afterStringConcatToFormat1.py | 1 + python/testData/intentions/beforeStringConcatToFormat1.py | 1 + python/testSrc/com/jetbrains/python/PyIntentionTest.java | 4 ++++ 4 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 python/testData/intentions/afterStringConcatToFormat1.py create mode 100644 python/testData/intentions/beforeStringConcatToFormat1.py diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java index e5c354b03f3e..7ac5dd45b154 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java @@ -3,10 +3,12 @@ package com.jetbrains.python.codeInsight.intentions; 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; +import com.intellij.util.NotNullFunction; import com.jetbrains.python.PyBundle; import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.psi.*; @@ -80,9 +82,10 @@ public class PyStringConcatenationToFormatIntention extends BaseIntentionAction } StringBuilder stringLiteral = new StringBuilder(); StringBuilder parameters = new StringBuilder("("); + NotNullFunction escaper = StringUtil.escaper(false, null); for (PyExpression expression: getSimpleExpressions((PyBinaryExpression) element)) { if (expression instanceof PyStringLiteralExpression) { - stringLiteral.append(((PyStringLiteralExpression)expression).getStringValue()); + stringLiteral.append(escaper.fun(((PyStringLiteralExpression)expression).getStringValue())); } else { stringLiteral.append("%s"); parameters.append(expression.getText()).append(", "); diff --git a/python/testData/intentions/afterStringConcatToFormat1.py b/python/testData/intentions/afterStringConcatToFormat1.py new file mode 100644 index 000000000000..e2618d480e2a --- /dev/null +++ b/python/testData/intentions/afterStringConcatToFormat1.py @@ -0,0 +1 @@ +result += "%s : %s\n" % (header, value) \ No newline at end of file diff --git a/python/testData/intentions/beforeStringConcatToFormat1.py b/python/testData/intentions/beforeStringConcatToFormat1.py new file mode 100644 index 000000000000..d978be40173a --- /dev/null +++ b/python/testData/intentions/beforeStringConcatToFormat1.py @@ -0,0 +1 @@ +result += header + " : " + value + "\n" \ 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 5e1feb7134b9..83cd27238b5f 100644 --- a/python/testSrc/com/jetbrains/python/PyIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/PyIntentionTest.java @@ -106,6 +106,10 @@ public class PyIntentionTest extends PyTestCase { doTest(PyBundle.message("INTN.replace.plus.with.format.operator")); } + public void testStringConcatToFormat1() { //PY-5226 + doTest(PyBundle.message("INTN.replace.plus.with.format.operator")); + } + public void testConvertFormatOperatorToMethod() { doTest(PyBundle.message("INTN.replace.with.method"), LanguageLevel.PYTHON26); }