PY-16553 Remove comma from one element tuple when converting it to another collection
authorMikhail Golubev <mikhail.golubev@jetbrains.com>
Mon, 2 May 2016 18:10:01 +0000 (21:10 +0300)
committerMikhail Golubev <mikhail.golubev@jetbrains.com>
Tue, 3 May 2016 11:54:00 +0000 (14:54 +0300)
python/src/com/jetbrains/python/codeInsight/intentions/PyBaseConvertCollectionLiteralIntention.java
python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleToList.py [new file with mode: 0644]
python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleToList_after.py [new file with mode: 0644]
python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithCommentToList.py [new file with mode: 0644]
python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithCommentToList_after.py [new file with mode: 0644]
python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithoutParenthesesToSet.py [new file with mode: 0644]
python/testData/intentions/PyConvertCollectionLiteralIntentionTest/convertOneElementTupleWithoutParenthesesToSet_after.py [new file with mode: 0644]
python/testSrc/com/jetbrains/python/intentions/PyConvertCollectionLiteralIntentionTest.java

index 75e2d79408eee09402383298917e6da87a8b9017..d6dd06ae2709329b0fc1d99dc8c9fd81684b939a 100644 (file)
@@ -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 (file)
index 0000000..7a88eac
--- /dev/null
@@ -0,0 +1 @@
+(<caret>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 (file)
index 0000000..33b3415
--- /dev/null
@@ -0,0 +1 @@
+[<caret>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 (file)
index 0000000..36aa90a
--- /dev/null
@@ -0,0 +1,4 @@
+(
+    42 <caret> # 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 (file)
index 0000000..8676fdc
--- /dev/null
@@ -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 (file)
index 0000000..76b47c0
--- /dev/null
@@ -0,0 +1 @@
+42<caret>,
\ 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 (file)
index 0000000..7227c29
--- /dev/null
@@ -0,0 +1 @@
+{42}
\ No newline at end of file
index daa2206d2e4dd981357ccdc491363a419abf6d59..ba2211fe2b5898ebbdce92aa18d7c1ecdd3304f1 100644 (file)
@@ -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);
+  }
 }