From 105c368c091686ceba5fa30648138184e5a123b8 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Mon, 2 May 2016 21:10:01 +0300 Subject: [PATCH] PY-16553 Remove comma from one element tuple when converting it to another collection --- .../PyBaseConvertCollectionLiteralIntention.java | 12 ++++++++++++ .../convertOneElementTupleToList.py | 1 + .../convertOneElementTupleToList_after.py | 1 + .../convertOneElementTupleWithCommentToList.py | 4 ++++ ...nvertOneElementTupleWithCommentToList_after.py | 4 ++++ ...nvertOneElementTupleWithoutParenthesesToSet.py | 1 + ...neElementTupleWithoutParenthesesToSet_after.py | 1 + .../PyConvertCollectionLiteralIntentionTest.java | 15 +++++++++++++++ 8 files changed, 39 insertions(+) create mode 100644 python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleToList.py create mode 100644 python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleToList_after.py create mode 100644 python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithCommentToList.py create mode 100644 python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithCommentToList_after.py create mode 100644 python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithoutParenthesesToSet.py create mode 100644 python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithoutParenthesesToSet_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 75e2d79408ee..d6dd06ae2709 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyBaseConvertCollectionLiteralIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyBaseConvertCollectionLiteralIntention.java @@ -26,6 +26,7 @@ import com.intellij.util.IncorrectOperationException; import com.jetbrains.python.PyBundle; import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.psi.*; +import com.jetbrains.python.psi.impl.PyPsiUtils; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -103,6 +104,17 @@ public abstract class PyBaseConvertCollectionLiteralIntention extends BaseIntent @NotNull protected PsiElement prepareOriginalElementCopy(@NotNull PsiElement copy) { + final PySequenceExpression sequence = unwrapCollection(copy); + if (sequence instanceof PyTupleExpression) { + final PyExpression[] elements = sequence.getElements(); + if (elements.length == 1) { + final PsiElement next = PyPsiUtils.getNextNonCommentSibling(elements[0], true); + // Strictly speaking single element tuple must contain trailing comma, but lets check explicitly nonetheless + if (next != null && next.getNode().getElementType() == PyTokenTypes.COMMA) { + next.delete(); + } + } + } return copy; } diff --git a/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleToList.py b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleToList.py new file mode 100644 index 000000000000..7a88eac62fac --- /dev/null +++ b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleToList.py @@ -0,0 +1 @@ +(42,) \ No newline at end of file diff --git a/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleToList_after.py b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleToList_after.py new file mode 100644 index 000000000000..33b341560e4f --- /dev/null +++ b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleToList_after.py @@ -0,0 +1 @@ +[42] \ No newline at end of file diff --git a/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithCommentToList.py b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithCommentToList.py new file mode 100644 index 000000000000..36aa90a6efc5 --- /dev/null +++ b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithCommentToList.py @@ -0,0 +1,4 @@ +( + 42 # foo + , +) \ No newline at end of file diff --git a/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithCommentToList_after.py b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithCommentToList_after.py new file mode 100644 index 000000000000..8676fdc2f3ae --- /dev/null +++ b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithCommentToList_after.py @@ -0,0 +1,4 @@ +[ + 42 # foo + +] \ No newline at end of file diff --git a/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithoutParenthesesToSet.py b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithoutParenthesesToSet.py new file mode 100644 index 000000000000..76b47c07f7f3 --- /dev/null +++ b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithoutParenthesesToSet.py @@ -0,0 +1 @@ +42, \ No newline at end of file diff --git a/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithoutParenthesesToSet_after.py b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithoutParenthesesToSet_after.py new file mode 100644 index 000000000000..7227c29799ae --- /dev/null +++ b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithoutParenthesesToSet_after.py @@ -0,0 +1 @@ +{42} \ 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 daa2206d2e4d..ba2211fe2b58 100644 --- a/python/testSrc/com/jetbrains/python/intentions/PyConvertCollectionLiteralIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/intentions/PyConvertCollectionLiteralIntentionTest.java @@ -128,4 +128,19 @@ public class PyConvertCollectionLiteralIntentionTest extends PyIntentionTestCase public void testConvertOneElementListWithCommaAfterCommentToTuple() { doIntentionTest(CONVERT_LIST_TO_TUPLE); } + + // PY-16553 + public void testConvertOneElementTupleToList() { + doIntentionTest(CONVERT_TUPLE_TO_LIST); + } + + // PY-16553 + public void testConvertOneElementTupleWithoutParenthesesToSet() { + doIntentionTest(CONVERT_TUPLE_TO_SET); + } + + // PY-16553 + public void testConvertOneElementTupleWithCommentToList() { + doIntentionTest(CONVERT_TUPLE_TO_LIST); + } } -- 2.32.0