PY-16335 Preserve formatting of converted collection literals

This commit is contained in:
Mikhail Golubev
2015-07-07 12:40:38 +03:00
parent c00d3ae268
commit 5fee1b116b
4 changed files with 18 additions and 4 deletions
@@ -98,16 +98,17 @@ public abstract class PyBaseConvertCollectionLiteralIntention extends BaseIntent
replacedElement = literal;
}
final String innerText = stripLiteralBraces(replacedElement);
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
final PyExpression newLiteral = elementGenerator.createExpressionFromText(LanguageLevel.forElement(file),
myLeftBrace + stripLiteralBraces(literal) + myRightBrace);
myLeftBrace + innerText + myRightBrace);
replacedElement.replace(newLiteral);
}
@NotNull
private static String stripLiteralBraces(@NotNull PySequenceExpression literal) {
private static String stripLiteralBraces(@NotNull PsiElement literal) {
if (literal instanceof PyTupleExpression) {
return literal.getText().trim();
return literal.getText();
}
final PsiElement firstChild = literal.getFirstChild();
@@ -130,7 +131,7 @@ public abstract class PyBaseConvertCollectionLiteralIntention extends BaseIntent
contentEndOffset = replacedText.length();
}
return literal.getText().substring(contentStartOffset, contentEndOffset).trim();
return literal.getText().substring(contentStartOffset, contentEndOffset);
}
@Nullable
@@ -0,0 +1,4 @@
xs = (<caret>
1, 2, # comment 1
3 # comment 2
)
@@ -0,0 +1,4 @@
xs = [
1, 2, # comment 1
3 # comment 2
]
@@ -103,4 +103,9 @@ public class PyConvertCollectionLiteralIntentionTest extends PyIntentionTestCase
public void testConvertSetToList() {
doIntentionTest(CONVERT_SET_TO_LIST);
}
// PY-16335
public void testConvertLiteralPreservesFormattingAndComments() {
doIntentionTest(CONVERT_TUPLE_TO_LIST);
}
}