From 2e6bf2bdeacb1c72543078fcd069897113903aef Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Fri, 20 Sep 2013 13:43:55 +0400 Subject: [PATCH] fixed PY-10843 Converting from concatenation to str.format intention does not work in PyCharm --- .../PyStringConcatenationToFormatIntention.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java index 174f15dd148c..78d919d4b251 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java @@ -109,9 +109,10 @@ public class PyStringConcatenationToFormatIntention extends BaseIntentionAction 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) { + while (element != null && element.getParent() instanceof PyBinaryExpression) { element = element.getParent(); } + if (element == null) return; final LanguageLevel languageLevel = LanguageLevel.forElement(element); final boolean useFormatMethod = languageLevel.isAtLeast(LanguageLevel.PYTHON27); @@ -133,7 +134,7 @@ public class PyStringConcatenationToFormatIntention extends BaseIntentionAction } stringLiteral.append(escaper.fun(value)); } else { - addParamToString(stringLiteral, paramCount, languageLevel); + addParamToString(stringLiteral, paramCount, useFormatMethod); parameters.add(expression.getText()); ++paramCount; } @@ -166,8 +167,8 @@ public class PyStringConcatenationToFormatIntention extends BaseIntentionAction } } - private static void addParamToString(StringBuilder stringLiteral, int i, LanguageLevel level) { - if (level.isPy3K()) + private static void addParamToString(StringBuilder stringLiteral, int i, boolean useFormatMethod) { + if (useFormatMethod) stringLiteral.append("{").append(i).append("}"); else stringLiteral.append("%s");