From d37cc7d16f6f9e1982c0e02a0867f8faf636ac17 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Fri, 28 Dec 2012 18:30:38 +0400 Subject: [PATCH] Cleanup --- .../refactoring/PyReplaceExpressionUtil.java | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java b/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java index 3e649113d72a..77cb8d769f06 100644 --- a/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java +++ b/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java @@ -85,28 +85,30 @@ public class PyReplaceExpressionUtil implements PyElementTypes { final LanguageLevel languageLevel = LanguageLevel.forElement(oldExpression); final List substitutions = new PyStringFormatParser(fullText).parseSubstitutions(); - if (valueExpression instanceof PyTupleExpression && !containsStringFormatting(fullText, textRange)) { - // 'foo%s' % (x,) -> '%s%s' % (s, x) - // TODO: Support dict literals and dict() function - // TODO: It is possible to resolve to a tuple or dict literal and modify them - final String newLiteralText = prefix + "%s" + suffix; - final PyStringLiteralExpression newLiteralExpression = generator.createStringLiteralAlreadyEscaped(newLiteralText); - oldExpression.replace(newLiteralExpression); + if (valueExpression != null && !containsStringFormatting(fullText, textRange)) { + if (valueExpression instanceof PyTupleExpression) { + // 'foo%s' % (x,) -> '%s%s' % (s, x) + // TODO: Support dict literals and dict() function + // TODO: It is possible to resolve to a tuple or dict literal and modify them + final String newLiteralText = prefix + "%s" + suffix; + final PyStringLiteralExpression newLiteralExpression = generator.createStringLiteralAlreadyEscaped(newLiteralText); + oldExpression.replace(newLiteralExpression); - final PyTupleExpression tuple = (PyTupleExpression)valueExpression; - final PyExpression[] members = tuple.getElements(); - final List positional = PyStringFormatParser.getPositionalSubstitutions(substitutions); - final int i = getPositionInRanges(PyStringFormatParser.substitutionsToRanges(positional), textRange); - final int n = members.length; - if (n > 0 && i <= n) { - final boolean last = i == n; - final ASTNode trailingComma = PyPsiUtils.getNextComma(members[n - 1].getNode()); - if (trailingComma != null) { - tuple.getNode().removeChild(trailingComma); + final PyTupleExpression tuple = (PyTupleExpression)valueExpression; + final PyExpression[] members = tuple.getElements(); + final List positional = PyStringFormatParser.getPositionalSubstitutions(substitutions); + final int i = getPositionInRanges(PyStringFormatParser.substitutionsToRanges(positional), textRange); + final int n = members.length; + if (n > 0 && i <= n) { + final boolean last = i == n; + final ASTNode trailingComma = PyPsiUtils.getNextComma(members[n - 1].getNode()); + if (trailingComma != null) { + tuple.getNode().removeChild(trailingComma); + } + final PyExpression before = last ? null : members[i]; + PyUtil.addListNode(tuple, newExpression, before != null ? before.getNode() : null, i == 0 || !last, last, !last); + return newExpression; } - final PyExpression before = last ? null : members[i]; - PyUtil.addListNode(tuple, newExpression, before != null ? before.getNode() : null, i == 0 || !last, last, !last); - return newExpression; } return null; }