mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PY-16335 Preserve formatting of converted collection literals
This commit is contained in:
+5
-4
@@ -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
|
||||
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
xs = (<caret>
|
||||
1, 2, # comment 1
|
||||
3 # comment 2
|
||||
)
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
xs = [
|
||||
1, 2, # comment 1
|
||||
3 # comment 2
|
||||
]
|
||||
+5
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user