fixed PY-5157 Convert dict literal to dict constructor: invalid code with space in keys

This commit is contained in:
Ekaterina Tuzova
2011-12-02 16:40:55 +01:00
parent 1de2fbaba1
commit 79ddbf26f2
4 changed files with 17 additions and 8 deletions
@@ -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"));
}