fixed PY-7151 Convert triple-quoted string to single-quoted string: do not wrap string with parenthesis if initial string is already inside them

This commit is contained in:
Ekaterina Tuzova
2013-02-21 14:09:17 +04:00
parent 1e99754973
commit c024597491
4 changed files with 21 additions and 2 deletions
@@ -4,6 +4,7 @@ import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
@@ -62,6 +63,7 @@ public class PyConvertTripleQuotedStringIntention extends BaseIntentionAction {
PyStringLiteralExpression string = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyStringLiteralExpression.class);
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
if (string != null) {
final PsiElement parent = string.getParent();
String stringText = string.getText();
final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText);
String prefix = stringText.substring(0, prefixLength);
@@ -70,7 +72,6 @@ public class PyConvertTripleQuotedStringIntention extends BaseIntentionAction {
stringText = string.getStringValue();
List<String> subStrings = StringUtil.split(stringText, "\n", false, true);
StringBuilder result = new StringBuilder();
if (subStrings.size() != 1)
result.append("(");
@@ -91,7 +92,12 @@ public class PyConvertTripleQuotedStringIntention extends BaseIntentionAction {
if (subStrings.size() != 1)
result.append(")");
PyExpressionStatement e = elementGenerator.createFromText(LanguageLevel.forElement(string), PyExpressionStatement.class, result.toString());
string.replace(e.getExpression());
PyExpression expression = e.getExpression();
if (parent instanceof PyTupleExpression && expression instanceof PyParenthesizedExpression)
expression = ((PyParenthesizedExpression)expression).getContainedExpression();
if (expression != null)
string.replace(expression);
}
}
@@ -0,0 +1,4 @@
t = ("string\n"
"some\n"
"other\n"
"lines\n", )
@@ -0,0 +1,5 @@
t = ("""str<caret>ing
some
other
lines
""", )
@@ -255,6 +255,10 @@ public class PyIntentionTest extends PyTestCase {
doTest(PyBundle.message("INTN.triple.quoted.string"));
}
public void testConvertTripleQuotedParenthesizedString() { //PY-7151
doTest(PyBundle.message("INTN.triple.quoted.string"));
}
public void testTransformConditionalExpression() { //PY-3094
doTest(PyBundle.message("INTN.transform.into.if.else.statement"));
}