diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java index 42b79a66560c..1e1be0521d9d 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java @@ -41,8 +41,12 @@ public class PyStringConcatenationToFormatIntention extends BaseIntentionAction while (element.getParent() instanceof PyBinaryExpression) { element = element.getParent(); } - if (((PyBinaryExpression)element).getOperator() != PyTokenTypes.PLUS) { - return false; + + final Collection operators = getOperators((PyBinaryExpression)element); + for (PyElementType operator : operators) { + if (operator != PyTokenTypes.PLUS) { + return false; + } } final Collection expressions = getSimpleExpressions((PyBinaryExpression)element); @@ -83,6 +87,18 @@ public class PyStringConcatenationToFormatIntention extends BaseIntentionAction return res; } + private static Collection getOperators(@NotNull PyBinaryExpression expression) { + List res = new ArrayList(); + if (expression.getLeftExpression() instanceof PyBinaryExpression) { + res.addAll(getOperators((PyBinaryExpression)expression.getLeftExpression())); + } + if (expression.getRightExpression() instanceof PyBinaryExpression) { + res.addAll(getOperators((PyBinaryExpression)expression.getRightExpression())); + } + res.add(expression.getOperator()); + return res; + } + public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { PsiElement element = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyBinaryExpression.class, false); while (element.getParent() instanceof PyBinaryExpression) { diff --git a/python/testData/intentions/beforeStringConcatToFormat5.py b/python/testData/intentions/beforeStringConcatToFormat5.py new file mode 100644 index 000000000000..1060ea9514f6 --- /dev/null +++ b/python/testData/intentions/beforeStringConcatToFormat5.py @@ -0,0 +1,4 @@ +x = "" +y = "" +z = "" +i = x % y + z diff --git a/python/testSrc/com/jetbrains/python/PyIntentionTest.java b/python/testSrc/com/jetbrains/python/PyIntentionTest.java index 9ad75e13bf23..e5cfd68cd328 100644 --- a/python/testSrc/com/jetbrains/python/PyIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/PyIntentionTest.java @@ -125,7 +125,11 @@ public class PyIntentionTest extends PyTestCase { doTest(PyBundle.message("INTN.replace.plus.with.format.operator")); } - public void testStringConcatToFormat4() { //PY-7968 + public void testStringConcatToFormat4() { //PY-7969 + doNegativeTest(PyBundle.message("INTN.replace.plus.with.format.operator")); + } + + public void testStringConcatToFormat5() { //PY-7968 doNegativeTest(PyBundle.message("INTN.replace.plus.with.format.operator")); }