mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
separated dictConstructor intention into 2 intentions (back and forth)
This commit is contained in:
@@ -134,7 +134,12 @@
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>com.jetbrains.python.codeInsight.intentions.PyDictConstructorIntention</className>
|
||||
<className>com.jetbrains.python.codeInsight.intentions.PyDictConstructorToLiteralFormIntention</className>
|
||||
<category>Python</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>com.jetbrains.python.codeInsight.intentions.PyDictLiteralFormToConstructorIntention</className>
|
||||
<category>Python</category>
|
||||
</intentionAction>
|
||||
|
||||
|
||||
+6
-44
@@ -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);
|
||||
}
|
||||
}
|
||||
+74
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
a = dict(a=3, b=5)
|
||||
@@ -0,0 +1 @@
|
||||
a = {'a':<caret> 3, 'b': 5}
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user