PY-9419 Add new intention action to convert between tuple, list and set literals

This commit is contained in:
Mikhail Golubev
2015-05-25 21:37:47 +03:00
parent 6f48d6f5dc
commit e187993faa
40 changed files with 433 additions and 0 deletions
@@ -0,0 +1,2 @@
[1, 2, 3]
[a, b] = [x, y]
@@ -0,0 +1,2 @@
1, 2, 3
a, b = {x, y}
@@ -0,0 +1,7 @@
<html>
<body>
<span style="font-family: verdana,sans-serif;">
This intention converts tuples and set literals to list literals.
</span>
</body>
</html>
@@ -0,0 +1,2 @@
{1, 2, 3}
a, b = {x, y}
@@ -0,0 +1,2 @@
1, 2, 3
a, b = [x, y]
@@ -0,0 +1,7 @@
<html>
<body>
<span style="font-family: verdana,sans-serif;">
This intention converts tuples and list literals to set literals.
</span>
</body>
</html>
@@ -0,0 +1,2 @@
(1, 2, 3)
a, b = (x, y)
@@ -0,0 +1,2 @@
[1, 2, 3]
a, b = {x, y}
@@ -0,0 +1,7 @@
<html>
<body>
<span style="font-family: verdana,sans-serif;">
This intention converts list literals and set literals to tuples.
</span>
</body>
</html>
+15
View File
@@ -258,6 +258,21 @@
<category>Python</category>
</intentionAction>
<intentionAction>
<className>com.jetbrains.python.codeInsight.intentions.PyConvertLiteralToTupleIntention</className>
<category>Python</category>
</intentionAction>
<intentionAction>
<className>com.jetbrains.python.codeInsight.intentions.PyConvertLiteralToListIntention</className>
<category>Python</category>
</intentionAction>
<intentionAction>
<className>com.jetbrains.python.codeInsight.intentions.PyConvertLiteralToSetIntention</className>
<category>Python</category>
</intentionAction>
<intentionAction>
<className>com.jetbrains.python.codeInsight.intentions.PyTransformConditionalExpressionIntention</className>
<category>Python</category>
@@ -237,6 +237,9 @@ INTN.convert.variadic.param=Convert from variadic to normal parameter(s)
# PyConvertTripleQuotedStringIntention
INTN.triple.quoted.string=Convert triple-quoted string to single-quoted string
INTN.convert.collection.literal.family=Convert collection to {0}
INTN.convert.collection.literal.text=Convert {0} to {1}
# PyTransformConditionalExpressionIntention
INTN.transform.into.if.else.statement=Transform conditional expression into if/else statement
@@ -0,0 +1,153 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.python.codeInsight.intentions;
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Condition;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.psi.*;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import static com.jetbrains.python.psi.PyUtil.as;
/**
* @author Mikhail Golubev
*/
public abstract class PyBaseConvertCollectionLiteralIntention extends BaseIntentionAction {
private final Class<? extends PySequenceExpression> myTargetCollectionClass;
private final String myTargetCollectionName;
private final String myRightBrace;
private final String myLeftBrace;
public PyBaseConvertCollectionLiteralIntention(@NotNull Class<? extends PySequenceExpression> targetCollectionClass,
@NotNull String targetCollectionName,
@NotNull String leftBrace, @NotNull String rightBrace) {
myTargetCollectionClass = targetCollectionClass;
myTargetCollectionName = targetCollectionName;
myLeftBrace = leftBrace;
myRightBrace = rightBrace;
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return PyBundle.message("INTN.convert.collection.literal.family", myTargetCollectionName);
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
final PySequenceExpression literal = findCollectionLiteralUnderCaret(editor, file);
if (myTargetCollectionClass.isInstance(literal)) {
return false;
}
if (literal instanceof PyTupleExpression) {
setText(PyBundle.message("INTN.convert.collection.literal.text", "tuple", myTargetCollectionName));
}
else if (literal instanceof PyListLiteralExpression) {
setText(PyBundle.message("INTN.convert.collection.literal.text", "list", myTargetCollectionName));
}
else if (literal instanceof PySetLiteralExpression) {
setText(PyBundle.message("INTN.convert.collection.literal.text", "set", myTargetCollectionName));
}
else {
return false;
}
return isAvailableForCollection(literal);
}
protected boolean isAvailableForCollection(PySequenceExpression literal) {
return true;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final PySequenceExpression literal = findCollectionLiteralUnderCaret(editor, file);
assert literal != null;
final PsiElement replacedElement;
if (literal instanceof PyTupleExpression && literal.getParent() instanceof PyParenthesizedExpression) {
replacedElement = literal.getParent();
}
else {
replacedElement = literal;
}
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
final PyExpression newLiteral = elementGenerator.createExpressionFromText(LanguageLevel.forElement(file),
myLeftBrace + stripLiteralBraces(literal) + myRightBrace);
replacedElement.replace(newLiteral);
}
@NotNull
private static String stripLiteralBraces(@NotNull PySequenceExpression literal) {
if (literal instanceof PyTupleExpression) {
return literal.getText().trim();
}
final PsiElement firstChild = literal.getFirstChild();
final String replacedText = literal.getText();
final int contentStartOffset;
if (PyTokenTypes.OPEN_BRACES.contains(firstChild.getNode().getElementType())) {
contentStartOffset = firstChild.getTextLength();
}
else {
contentStartOffset = 0;
}
final PsiElement lastChild = literal.getLastChild();
final int contentEndOffset;
if (PyTokenTypes.CLOSE_BRACES.contains(lastChild.getNode().getElementType())) {
contentEndOffset = replacedText.length() - lastChild.getTextLength();
}
else {
contentEndOffset = replacedText.length();
}
return literal.getText().substring(contentStartOffset, contentEndOffset).trim();
}
@Nullable
private static PySequenceExpression findCollectionLiteralUnderCaret(@NotNull Editor editor, @NotNull PsiFile psiFile) {
final int caretOffset = editor.getCaretModel().getOffset();
final PsiElement curElem = psiFile.findElementAt(caretOffset);
final PySequenceExpression seqExpr = PsiTreeUtil.getParentOfType(curElem, PySequenceExpression.class);
if (seqExpr != null) {
return seqExpr;
}
final PyParenthesizedExpression paren = (PyParenthesizedExpression)PsiTreeUtil.findFirstParent(curElem, new Condition<PsiElement>() {
@Override
public boolean value(PsiElement element) {
final PyParenthesizedExpression parenthesizedExpr = as(element, PyParenthesizedExpression.class);
return parenthesizedExpr != null && parenthesizedExpr.getContainedExpression() instanceof PyTupleExpression;
}
});
return paren != null ? ((PyTupleExpression)paren.getContainedExpression()) : null;
}
}
@@ -0,0 +1,27 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.python.codeInsight.intentions;
import com.jetbrains.python.psi.PyListLiteralExpression;
/**
* @author Mikhail Golubev
*/
public class PyConvertLiteralToListIntention extends PyBaseConvertCollectionLiteralIntention {
public PyConvertLiteralToListIntention() {
super(PyListLiteralExpression.class, "list", "[", "]");
}
}
@@ -0,0 +1,44 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.python.codeInsight.intentions;
import com.intellij.openapi.util.Condition;
import com.intellij.util.containers.ContainerUtil;
import com.jetbrains.python.psi.*;
import org.jetbrains.annotations.NotNull;
/**
* @author Mikhail Golubev
*/
public class PyConvertLiteralToSetIntention extends PyBaseConvertCollectionLiteralIntention {
public PyConvertLiteralToSetIntention() {
super(PySetLiteralExpression.class, "set", "{", "}");
}
@Override
protected boolean isAvailableForCollection(PySequenceExpression literal) {
return LanguageLevel.forElement(literal).isAtLeast(LanguageLevel.PYTHON27) && !isInTargetPosition(literal);
}
private static boolean isInTargetPosition(@NotNull final PySequenceExpression sequenceLiteral) {
return ContainerUtil.exists(sequenceLiteral.getElements(), new Condition<PyExpression>() {
@Override
public boolean value(PyExpression expression) {
return expression instanceof PyTargetExpression;
}
});
}
}
@@ -0,0 +1,27 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.python.codeInsight.intentions;
import com.jetbrains.python.psi.PyTupleExpression;
/**
* @author Mikhail Golubev
*/
public class PyConvertLiteralToTupleIntention extends PyBaseConvertCollectionLiteralIntention {
public PyConvertLiteralToTupleIntention() {
super(PyTupleExpression.class, "tuple", "(", ")");
}
}
@@ -0,0 +1 @@
xs = [<caret>1, 2]
@@ -0,0 +1 @@
xs = [<caret>1, 2]
@@ -0,0 +1 @@
xs = {1, 2<caret>}
@@ -0,0 +1 @@
xs = {<caret>1, 2}
@@ -0,0 +1 @@
xs = [x for x, <caret>y in zip('foo', range(3))]
@@ -0,0 +1,2 @@
for x, <caret>y in zip('foo', range(3)):
pass
@@ -0,0 +1,106 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.python.intentions;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.psi.LanguageLevel;
/**
* @author Mikhail Golubev
*/
public class PyConvertCollectionLiteralIntentionTest extends PyIntentionTestCase {
private static final String CONVERT_TUPLE_TO_LIST = PyBundle.message("INTN.convert.collection.literal.text", "tuple", "list");
private static final String CONVERT_TUPLE_TO_SET = PyBundle.message("INTN.convert.collection.literal.text", "tuple", "set");
private static final String CONVERT_LIST_TO_TUPLE = PyBundle.message("INTN.convert.collection.literal.text", "list", "tuple");
private static final String CONVERT_LIST_TO_SET = PyBundle.message("INTN.convert.collection.literal.text", "list", "set");
private static final String CONVERT_SET_TO_TUPLE = PyBundle.message("INTN.convert.collection.literal.text", "set", "tuple");
private static final String CONVERT_SET_TO_LIST = PyBundle.message("INTN.convert.collection.literal.text", "set", "list");
// PY-9419
public void testConvertParenthesizedTupleToList() {
doIntentionTest(CONVERT_TUPLE_TO_LIST);
}
// PY-9419
public void testConvertTupleWithoutParenthesesToList() {
doIntentionTest(CONVERT_TUPLE_TO_LIST);
}
// PY-9419
public void testConvertTupleWithoutClosingParenthesisToList() {
doIntentionTest(CONVERT_TUPLE_TO_LIST);
}
// PY-9419
public void testConvertParenthesizedTupleToSet() {
doIntentionTest(CONVERT_TUPLE_TO_SET);
}
// PY-9419
public void testConvertTupleToSetNotAvailableWithoutSetLiterals() {
runWithLanguageLevel(LanguageLevel.PYTHON25, new Runnable() {
public void run() {
doNegativeTest(CONVERT_TUPLE_TO_SET);
}
});
}
// PY-9419
public void testConvertTupleToSetNotAvailableInAssignmentTarget() {
doNegativeTest(CONVERT_TUPLE_TO_SET);
}
// PY-9419
public void testConvertTupleToSetNotAvailableInForLoop() {
doNegativeTest(CONVERT_TUPLE_TO_SET);
}
// PY-9419
public void testConvertTupleToSetNotAvailableInComprehension() {
doNegativeTest(CONVERT_TUPLE_TO_SET);
}
// PY-9419
public void testConvertListToTuple() {
doIntentionTest(CONVERT_LIST_TO_TUPLE);
}
// PY-9419
public void testConvertListWithoutClosingBracketToTuple() {
doIntentionTest(CONVERT_LIST_TO_TUPLE);
}
// PY-9419
public void testConvertListToSet() {
doIntentionTest(CONVERT_LIST_TO_SET);
}
// PY-9419
public void testConvertSetToTuple() {
doIntentionTest(CONVERT_SET_TO_TUPLE);
}
// PY-9419
public void testConvertSetWithoutClosingBraceToTuple() {
doIntentionTest(CONVERT_SET_TO_TUPLE);
}
// PY-9419
public void testConvertSetToList() {
doIntentionTest(CONVERT_SET_TO_LIST);
}
}