mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
fixed PY-5157 Convert dict literal to dict constructor: invalid code with space in keys
This commit is contained in:
+4
-8
@@ -3,6 +3,7 @@ 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.text.StringUtil;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
@@ -38,23 +39,18 @@ public class PyDictLiteralFormToConstructorIntention extends BaseIntentionAction
|
||||
|
||||
if (dictExpression != null) {
|
||||
PyKeyValueExpression[] elements = dictExpression.getElements();
|
||||
boolean canConvert = true;
|
||||
if (elements.length != 0) {
|
||||
for (PyKeyValueExpression element : elements) {
|
||||
PyExpression key = element.getKey();
|
||||
if (! (key instanceof PyStringLiteralExpression)) return false;
|
||||
String str = ((PyStringLiteralExpression)key).getStringValue();
|
||||
if (PyNames.isReserved(str)) return false;
|
||||
|
||||
if(str.length() == 0 || Character.isDigit(str.charAt(0))) return false;
|
||||
try {
|
||||
Integer.parseInt(str) ;
|
||||
canConvert = false;
|
||||
} catch (NumberFormatException e) {
|
||||
// pass
|
||||
}
|
||||
if (!StringUtil.isJavaIdentifier(str)) return false;
|
||||
}
|
||||
}
|
||||
if (canConvert) return true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
a = {'a b':<caret> 3, 'b': 5}
|
||||
@@ -0,0 +1 @@
|
||||
a = {'1b':<caret> 3, 'b': 5}
|
||||
@@ -156,6 +156,17 @@ public class PyIntentionTest extends PyTestCase {
|
||||
assertNull(action);
|
||||
}
|
||||
|
||||
public void testDictLiteralFormToConstructor2() { //PY-5157
|
||||
myFixture.configureByFile("intentions/beforeDictLiteralFormToConstructor2" + ".py");
|
||||
final IntentionAction action = myFixture.getAvailableIntention(PyBundle.message("INTN.convert.dict.literal.to.dict.constructor"));
|
||||
assertNull(action);
|
||||
}
|
||||
public void testDictLiteralFormToConstructor3() {
|
||||
myFixture.configureByFile("intentions/beforeDictLiteralFormToConstructor3" + ".py");
|
||||
final IntentionAction action = myFixture.getAvailableIntention(PyBundle.message("INTN.convert.dict.literal.to.dict.constructor"));
|
||||
assertNull(action);
|
||||
}
|
||||
|
||||
public void testQuotedString() { //PY-2915
|
||||
doTest(PyBundle.message("INTN.quoted.string.double.to.single"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user