From 5333cfa29dd194966457666959cd942c7ce6ae94 Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Fri, 28 Oct 2016 17:26:33 +0300 Subject: [PATCH] Update PyStringLiteralExpressionManipulator to correctly process unquoted newContent. Fix tests in AppEngineMoveTest. --- .../PyStringLiteralExpressionManipulator.java | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/python/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionManipulator.java b/python/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionManipulator.java index fddf70b67870..653bb4dafb1a 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionManipulator.java +++ b/python/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionManipulator.java @@ -33,10 +33,10 @@ public class PyStringLiteralExpressionManipulator extends AbstractElementManipul public PyStringLiteralExpressionImpl handleContentChange(@NotNull PyStringLiteralExpressionImpl element, @NotNull TextRange range, String newContent) { - final String newName = range.replace(element.getText(), newContent); - final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(element.getProject()); - final PyStringLiteralExpression escaped = elementGenerator.createStringLiteralAlreadyEscaped(newName); + final String escapedText = calculateEscapedText(element.getText(), range, newContent); + + final PyStringLiteralExpression escaped = elementGenerator.createStringLiteralAlreadyEscaped(escapedText); return (PyStringLiteralExpressionImpl)element.replace(escaped); } @@ -44,16 +44,38 @@ public class PyStringLiteralExpressionManipulator extends AbstractElementManipul @Override public PyStringLiteralExpressionImpl handleContentChange(@NotNull PyStringLiteralExpressionImpl element, String newContent) throws IncorrectOperationException { - return handleContentChange(element, super.getRangeInElement(element), newContent); + return handleContentChange(element, TextRange.create(0, element.getTextLength()), newContent); } @NotNull @Override public TextRange getRangeInElement(@NotNull PyStringLiteralExpressionImpl element) { - final Pair pair = PyStringLiteralUtil.getQuotes(element.getText()); - if (pair != null) { - return TextRange.from(pair.first.length(), element.getTextLength() - pair.first.length() - pair.second.length()); + return PyStringLiteralUtil.getStringValueTextRange(element.getText()); + } + + @NotNull + private static String calculateEscapedText(@NotNull String prevText, + @NotNull TextRange range, + String newContent) { + final String newText = range.replace(prevText, newContent); + + if (PyStringLiteralUtil.isQuoted(newText)) { + return newText; } - return super.getRangeInElement(element); + + final Pair quotes = calculateQuotes(prevText); + return quotes.first + newText + quotes.second; + } + + @NotNull + private static Pair calculateQuotes(@NotNull String text) { + final Pair quotes = PyStringLiteralUtil.getQuotes(text); + + if (quotes == null || quotes.first == null && quotes.second == null) return Pair.createNonNull("\"", "\""); + + if (quotes.first == null) return Pair.createNonNull(quotes.second, quotes.second); + if (quotes.second == null) return Pair.createNonNull(quotes.first, quotes.first); + + return quotes; } }