From e89da8ea58acc44f799032787b0d65c1cf88c627 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Thu, 25 Nov 2010 14:15:20 +0300 Subject: [PATCH] separated dictConstructor intention into 2 intentions (back and forth) --- python/src/META-INF/python-plugin-common.xml | 7 +- ...ictConstructorToLiteralFormIntention.java} | 50 ++----------- ...DictLiteralFormToConstructorIntention.java | 74 +++++++++++++++++++ ...y => afterDictConstructorToLiteralForm.py} | 0 .../afterDictLiteralFormToConstructor.py | 1 + ... => beforeDictConstructorToLiteralForm.py} | 0 .../beforeDictLiteralFormToConstructor.py | 1 + .../com/jetbrains/python/PyIntentionTest.java | 8 +- 8 files changed, 94 insertions(+), 47 deletions(-) rename python/src/com/jetbrains/python/codeInsight/intentions/{PyDictConstructorIntention.java => PyDictConstructorToLiteralFormIntention.java} (58%) create mode 100644 python/src/com/jetbrains/python/codeInsight/intentions/PyDictLiteralFormToConstructorIntention.java rename python/testData/intentions/{afterDictConstructor.py => afterDictConstructorToLiteralForm.py} (100%) create mode 100644 python/testData/intentions/afterDictLiteralFormToConstructor.py rename python/testData/intentions/{beforeDictConstructor.py => beforeDictConstructorToLiteralForm.py} (100%) create mode 100644 python/testData/intentions/beforeDictLiteralFormToConstructor.py diff --git a/python/src/META-INF/python-plugin-common.xml b/python/src/META-INF/python-plugin-common.xml index 816b7bb09d77..1b02cfd7d813 100644 --- a/python/src/META-INF/python-plugin-common.xml +++ b/python/src/META-INF/python-plugin-common.xml @@ -134,7 +134,12 @@ - com.jetbrains.python.codeInsight.intentions.PyDictConstructorIntention + com.jetbrains.python.codeInsight.intentions.PyDictConstructorToLiteralFormIntention + Python + + + + com.jetbrains.python.codeInsight.intentions.PyDictLiteralFormToConstructorIntention Python diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyDictConstructorIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyDictConstructorToLiteralFormIntention.java similarity index 58% rename from python/src/com/jetbrains/python/codeInsight/intentions/PyDictConstructorIntention.java rename to python/src/com/jetbrains/python/codeInsight/intentions/PyDictConstructorToLiteralFormIntention.java index 92f2bd40d238..6e606ef572c9 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyDictConstructorIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyDictConstructorToLiteralFormIntention.java @@ -15,12 +15,16 @@ import org.jetbrains.annotations.NotNull; /** * User: catherine */ -public class PyDictConstructorIntention extends BaseIntentionAction { +public class PyDictConstructorToLiteralFormIntention extends BaseIntentionAction { @NotNull public String getFamilyName() { return PyBundle.message("INTN.convert.dict"); } - + + public String getText() { + return PyBundle.message("INTN.convert.dict.constructor.to.dict.literal"); + } + public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { PyCallExpression expression = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyCallExpression.class); @@ -35,26 +39,11 @@ public class PyDictConstructorIntention extends BaseIntentionAction { for (PyExpression argument : argumentList) { if (!(argument instanceof PyKeywordArgument)) return false; } - setText(PyBundle.message("INTN.convert.dict.constructor.to.dict.literal")); return true; } } } } - PyDictLiteralExpression dictExpression = - PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyDictLiteralExpression.class); - - if (dictExpression != null) { - PyKeyValueExpression[] elements = dictExpression.getElements(); - if (elements.length != 0) { - for (PyKeyValueExpression element : elements) { - if (! (element.getKey() instanceof PyStringLiteralExpression)) return false; - } - } - setText(PyBundle.message("INTN.convert.dict.literal.to.dict.constructor")); - return true; - } - return false; } @@ -64,12 +53,6 @@ public class PyDictConstructorIntention extends BaseIntentionAction { PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project); if (expression != null) { replaceDictConstructor(expression, elementGenerator); - return; - } - PyDictLiteralExpression dictExpression = - PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyDictLiteralExpression.class); - if (dictExpression != null) { - replaceDictLiteral(dictExpression, elementGenerator); } } @@ -95,25 +78,4 @@ public class PyDictConstructorIntention extends BaseIntentionAction { "{" + stringBuilder.toString() + "}"); expression.replace(dict); } - - private static void replaceDictLiteral(PyDictLiteralExpression dictExpression, PyElementGenerator elementGenerator) { - PyExpression[] argumentList = dictExpression.getElements(); - StringBuilder stringBuilder = new StringBuilder(); - int size = argumentList.length; - for (int i = 0; i != size; ++i) { - PyExpression argument = argumentList[i]; - if (argument instanceof PyKeyValueExpression) { - PyExpression key = ((PyKeyValueExpression)argument).getKey(); - if (key instanceof PyStringLiteralExpression) - stringBuilder.append(((PyStringLiteralExpression)key).getStringValue()); - stringBuilder.append("="); - stringBuilder.append(((PyKeyValueExpression)argument).getValue().getText()); - if (i != size-1) - stringBuilder.append(", "); - } - } - PyStatement callExpression = elementGenerator.createFromText(LanguageLevel.forElement(dictExpression), PyStatement.class, - "dict(" + stringBuilder.toString() + ")"); - dictExpression.replace(callExpression); - } } diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyDictLiteralFormToConstructorIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyDictLiteralFormToConstructorIntention.java new file mode 100644 index 000000000000..e3940b688574 --- /dev/null +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyDictLiteralFormToConstructorIntention.java @@ -0,0 +1,74 @@ +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.psi.PsiFile; +import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.util.IncorrectOperationException; +import com.jetbrains.python.PyBundle; +import com.jetbrains.python.psi.*; +import com.jetbrains.python.psi.types.PyType; +import com.jetbrains.python.psi.types.TypeEvalContext; +import org.jetbrains.annotations.NotNull; + +/** + * User: catherine + */ +public class PyDictLiteralFormToConstructorIntention extends BaseIntentionAction { + @NotNull + public String getFamilyName() { + return PyBundle.message("INTN.convert.dict"); + } + + public String getText() { + return PyBundle.message("INTN.convert.dict.literal.to.dict.constructor"); + } + + public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { + + PyDictLiteralExpression dictExpression = + PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyDictLiteralExpression.class); + + if (dictExpression != null) { + PyKeyValueExpression[] elements = dictExpression.getElements(); + if (elements.length != 0) { + for (PyKeyValueExpression element : elements) { + if (! (element.getKey() instanceof PyStringLiteralExpression)) return false; + } + } + return true; + } + return false; + } + + public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + PyDictLiteralExpression dictExpression = + PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyDictLiteralExpression.class); + PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project); + if (dictExpression != null) { + replaceDictLiteral(dictExpression, elementGenerator); + } + } + + private static void replaceDictLiteral(PyDictLiteralExpression dictExpression, PyElementGenerator elementGenerator) { + PyExpression[] argumentList = dictExpression.getElements(); + StringBuilder stringBuilder = new StringBuilder(); + int size = argumentList.length; + for (int i = 0; i != size; ++i) { + PyExpression argument = argumentList[i]; + if (argument instanceof PyKeyValueExpression) { + PyExpression key = ((PyKeyValueExpression)argument).getKey(); + if (key instanceof PyStringLiteralExpression) + stringBuilder.append(((PyStringLiteralExpression)key).getStringValue()); + stringBuilder.append("="); + stringBuilder.append(((PyKeyValueExpression)argument).getValue().getText()); + if (i != size-1) + stringBuilder.append(", "); + } + } + PyStatement callExpression = elementGenerator.createFromText(LanguageLevel.forElement(dictExpression), PyStatement.class, + "dict(" + stringBuilder.toString() + ")"); + dictExpression.replace(callExpression); + } +} diff --git a/python/testData/intentions/afterDictConstructor.py b/python/testData/intentions/afterDictConstructorToLiteralForm.py similarity index 100% rename from python/testData/intentions/afterDictConstructor.py rename to python/testData/intentions/afterDictConstructorToLiteralForm.py diff --git a/python/testData/intentions/afterDictLiteralFormToConstructor.py b/python/testData/intentions/afterDictLiteralFormToConstructor.py new file mode 100644 index 000000000000..64c75103fb5c --- /dev/null +++ b/python/testData/intentions/afterDictLiteralFormToConstructor.py @@ -0,0 +1 @@ +a = dict(a=3, b=5) \ No newline at end of file diff --git a/python/testData/intentions/beforeDictConstructor.py b/python/testData/intentions/beforeDictConstructorToLiteralForm.py similarity index 100% rename from python/testData/intentions/beforeDictConstructor.py rename to python/testData/intentions/beforeDictConstructorToLiteralForm.py diff --git a/python/testData/intentions/beforeDictLiteralFormToConstructor.py b/python/testData/intentions/beforeDictLiteralFormToConstructor.py new file mode 100644 index 000000000000..3bd9059b7a87 --- /dev/null +++ b/python/testData/intentions/beforeDictLiteralFormToConstructor.py @@ -0,0 +1 @@ +a = {'a': 3, 'b': 5} \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyIntentionTest.java b/python/testSrc/com/jetbrains/python/PyIntentionTest.java index 75b53fcd7970..c5a4a0112bc3 100644 --- a/python/testSrc/com/jetbrains/python/PyIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/PyIntentionTest.java @@ -113,10 +113,14 @@ public class PyIntentionTest extends PyLightFixtureTestCase { doTest(PyBundle.message("INTN.join.if.text")); } - public void testDictConstructor() { + public void testDictConstructorToLiteralForm() { doTest(PyBundle.message("INTN.convert.dict.constructor.to.dict.literal")); } - + + public void testDictLiteralFormToConstructor() { + doTest(PyBundle.message("INTN.convert.dict.literal.to.dict.constructor")); + } + public void testQuotedString() { doTest(PyBundle.message("INTN.quoted.string.double.to.single")); }