mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
resolve context data of class-based views (PY-10542)
This commit is contained in:
@@ -19,6 +19,7 @@ public class PyBlockEvaluator {
|
||||
private final Set<String> myDeclarationsToTrack = new HashSet<String>();
|
||||
private String myCurrentFilePath;
|
||||
private Object myReturnValue;
|
||||
private boolean myEvaluateCollectionItems = true;
|
||||
|
||||
public PyBlockEvaluator() {
|
||||
myVisitedFiles = new HashSet<PyFile>();
|
||||
@@ -177,6 +178,7 @@ public class PyBlockEvaluator {
|
||||
private PyEvaluator prepareEvaluator() {
|
||||
PyEvaluator evaluator = createEvaluator();
|
||||
evaluator.setNamespace(myNamespace);
|
||||
evaluator.setEvaluateCollectionItems(myEvaluateCollectionItems);
|
||||
return evaluator;
|
||||
}
|
||||
|
||||
@@ -231,4 +233,8 @@ public class PyBlockEvaluator {
|
||||
public Object getReturnValue() {
|
||||
return myReturnValue;
|
||||
}
|
||||
|
||||
public void setEvaluateCollectionItems(boolean evaluateCollectionItems) {
|
||||
myEvaluateCollectionItems = evaluateCollectionItems;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,11 +14,16 @@ import java.util.*;
|
||||
public class PyEvaluator {
|
||||
private Set<PyExpression> myVisited = new HashSet<PyExpression>();
|
||||
private Map<String, Object> myNamespace;
|
||||
private boolean myEvaluateCollectionItems = true;
|
||||
|
||||
public void setNamespace(Map<String, Object> namespace) {
|
||||
myNamespace = namespace;
|
||||
}
|
||||
|
||||
public void setEvaluateCollectionItems(boolean evaluateCollectionItems) {
|
||||
myEvaluateCollectionItems = evaluateCollectionItems;
|
||||
}
|
||||
|
||||
public Object evaluate(PyExpression expr) {
|
||||
if (expr == null || myVisited.contains(expr)) {
|
||||
return null;
|
||||
@@ -34,7 +39,8 @@ public class PyEvaluator {
|
||||
for (PyKeyValueExpression keyValueExpression : ((PyDictLiteralExpression)expr).getElements()) {
|
||||
Object dictKey = evaluate(keyValueExpression.getKey());
|
||||
if (dictKey != null) {
|
||||
result.put(dictKey, evaluate(keyValueExpression.getValue()));
|
||||
PyExpression value = keyValueExpression.getValue();
|
||||
result.put(dictKey, myEvaluateCollectionItems ? evaluate(value) : value);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@@ -42,7 +48,7 @@ public class PyEvaluator {
|
||||
else {
|
||||
List<Object> result = new ArrayList<Object>();
|
||||
for (PyExpression element : elements) {
|
||||
result.add(evaluate(element));
|
||||
result.add(myEvaluateCollectionItems ? evaluate(element) : element);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.intellij.psi.PsiFileFactory;
|
||||
import com.jetbrains.python.fixtures.PyTestCase;
|
||||
import com.jetbrains.python.psi.PyFile;
|
||||
import com.jetbrains.python.psi.PyFunction;
|
||||
import com.jetbrains.python.psi.PyStringLiteralExpression;
|
||||
import com.jetbrains.python.psi.impl.PyBlockEvaluator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -53,6 +54,13 @@ public class PyBlockEvaluatorTest extends PyTestCase {
|
||||
assertEquals("c", map.get("b"));
|
||||
}
|
||||
|
||||
public void testDictNoEvaluate() {
|
||||
PyBlockEvaluator eval = doEvaluate("a={'b': 'c'}", true);
|
||||
Map map = (Map) eval.getValue("a");
|
||||
assertEquals(1, map.size());
|
||||
assertTrue(map.get("b") instanceof PyStringLiteralExpression);
|
||||
}
|
||||
|
||||
public void testDictAssign() {
|
||||
PyBlockEvaluator eval = doEvaluate("a={}\na['b']='c'");
|
||||
Map map = (Map) eval.getValue("a");
|
||||
@@ -76,7 +84,14 @@ public class PyBlockEvaluatorTest extends PyTestCase {
|
||||
}
|
||||
|
||||
private PyBlockEvaluator doEvaluate(String text) {
|
||||
return doEvaluate(text, false);
|
||||
}
|
||||
|
||||
private PyBlockEvaluator doEvaluate(String text, boolean skipEvaluatingCollectionItems) {
|
||||
PyBlockEvaluator eval = new PyBlockEvaluator();
|
||||
if (skipEvaluatingCollectionItems) {
|
||||
eval.setEvaluateCollectionItems(false);
|
||||
}
|
||||
PyFile file = (PyFile)PsiFileFactory.getInstance(myFixture.getProject()).createFileFromText("a.py", PythonFileType.INSTANCE, text);
|
||||
eval.evaluate(file);
|
||||
return eval;
|
||||
|
||||
Reference in New Issue
Block a user