fixed PY-2689 Convert double-quoted string to single-quoted string: loses data with multi-line strings

This commit is contained in:
Ekaterina Tuzova
2011-01-14 15:14:56 +03:00
parent 7dd5212972
commit aebad6f0db
4 changed files with 18 additions and 5 deletions
@@ -60,7 +60,7 @@ public class PyQuotedStringIntention extends BaseIntentionAction {
string.replace(st);
}
if (stringText.startsWith("\"") && stringText.endsWith("\"")) {
String result = convertDoubleToSingleQuoted(stringText);
String result = convertDoubleToSingleQuoted(string.getStringValue());
PyStringLiteralExpression st = elementGenerator.createStringLiteralAlreadyEscaped(result);
string.replace(st);
}
@@ -68,7 +68,8 @@ public class PyQuotedStringIntention extends BaseIntentionAction {
}
private static String convertDoubleToSingleQuoted(String stringText) {
StringBuilder stringBuilder = new StringBuilder();
StringBuilder stringBuilder = new StringBuilder("'");
boolean skipNext = false;
char[] charArr = stringText.toCharArray();
for (int i = 0; i != charArr.length; ++i) {
@@ -78,7 +79,8 @@ public class PyQuotedStringIntention extends BaseIntentionAction {
continue;
}
if (ch == '"') {
stringBuilder.append('\'');
continue;
// stringBuilder.append('\'');
}
else if (ch == '\'') {
stringBuilder.append("\\\'");
@@ -91,11 +93,13 @@ public class PyQuotedStringIntention extends BaseIntentionAction {
stringBuilder.append(ch);
}
}
stringBuilder.append("'");
return stringBuilder.toString();
}
private static String convertSingleToDoubleQuoted(String stringText) {
StringBuilder stringBuilder = new StringBuilder();
StringBuilder stringBuilder = new StringBuilder("\"");
boolean skipNext = false;
char[] charArr = stringText.toCharArray();
for (int i = 0; i != charArr.length; ++i) {
@@ -105,7 +109,8 @@ public class PyQuotedStringIntention extends BaseIntentionAction {
continue;
}
if (ch == '\'') {
stringBuilder.append('"');
continue;
//stringBuilder.append('"');
}
else if (ch == '"') {
stringBuilder.append("\\\"");
@@ -118,6 +123,7 @@ public class PyQuotedStringIntention extends BaseIntentionAction {
stringBuilder.append(ch);
}
}
stringBuilder.append("\"");
return stringBuilder.toString();
}
}
@@ -0,0 +1 @@
test_function('String resuming string'.format(a, b))
@@ -0,0 +1,2 @@
test_function("St<caret>ring "
"resuming string".format(a, b))
@@ -145,6 +145,10 @@ public class PyIntentionTest extends PyLightFixtureTestCase {
doTest(PyBundle.message("INTN.quoted.string.single.to.double"));
}
public void testDoubledQuotedString() { //PY-2689
doTest(PyBundle.message("INTN.quoted.string.double.to.single"));
}
public void testConvertLambdaToFunction() {
doTest(PyBundle.message("INTN.convert.lambda.to.function"));
}