From 5fee1b116bcd427168f1fafc7948c2e44520cc5c Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Mon, 6 Jul 2015 17:37:18 +0300 Subject: [PATCH] PY-16335 Preserve formatting of converted collection literals --- .../PyBaseConvertCollectionLiteralIntention.java | 9 +++++---- .../convertLiteralPreservesFormattingAndComments.py | 4 ++++ ...convertLiteralPreservesFormattingAndComments_after.py | 4 ++++ .../PyConvertCollectionLiteralIntentionTest.java | 5 +++++ 4 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertLiteralPreservesFormattingAndComments.py create mode 100644 python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertLiteralPreservesFormattingAndComments_after.py diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyBaseConvertCollectionLiteralIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyBaseConvertCollectionLiteralIntention.java index 0ac0409edf1f..daa669af001e 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyBaseConvertCollectionLiteralIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyBaseConvertCollectionLiteralIntention.java @@ -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 diff --git a/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertLiteralPreservesFormattingAndComments.py b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertLiteralPreservesFormattingAndComments.py new file mode 100644 index 000000000000..0c688956c9b7 --- /dev/null +++ b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertLiteralPreservesFormattingAndComments.py @@ -0,0 +1,4 @@ +xs = ( + 1, 2, # comment 1 + 3 # comment 2 +) \ No newline at end of file diff --git a/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertLiteralPreservesFormattingAndComments_after.py b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertLiteralPreservesFormattingAndComments_after.py new file mode 100644 index 000000000000..adccc398ae06 --- /dev/null +++ b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertLiteralPreservesFormattingAndComments_after.py @@ -0,0 +1,4 @@ +xs = [ + 1, 2, # comment 1 + 3 # comment 2 +] \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/intentions/PyConvertCollectionLiteralIntentionTest.java b/python/testSrc/com/jetbrains/python/intentions/PyConvertCollectionLiteralIntentionTest.java index 0e50c05e8d1f..7b10e2bd57cf 100644 --- a/python/testSrc/com/jetbrains/python/intentions/PyConvertCollectionLiteralIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/intentions/PyConvertCollectionLiteralIntentionTest.java @@ -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); + } }