Search in sources :

Example 1 with PyExpressionCodeFragmentImpl

use of com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl in project intellij-community by JetBrains.

the class PyConsoleSpecificOptionsPanel method createDocument.

@NotNull
public static Document createDocument(@NotNull final Project project, @NotNull String text) {
    text = text.trim();
    final PyExpressionCodeFragmentImpl fragment = new PyExpressionCodeFragmentImpl(project, "start_script.py", text, true);
    return PsiDocumentManager.getInstance(project).getDocument(fragment);
}
Also used : PyExpressionCodeFragmentImpl(com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PyExpressionCodeFragmentImpl

use of com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl in project intellij-community by JetBrains.

the class PyDebuggerEditorsProvider method createDocument.

@NotNull
@Override
public Document createDocument(@NotNull final Project project, @NotNull String text, @Nullable final XSourcePosition sourcePosition, @NotNull EvaluationMode mode) {
    text = text.trim();
    final PyExpressionCodeFragmentImpl fragment = new PyExpressionCodeFragmentImpl(project, "fragment.py", text, true);
    // Bind to context
    final PsiElement element = getContextElement(project, sourcePosition);
    fragment.setContext(element);
    return PsiDocumentManager.getInstance(project).getDocument(fragment);
}
Also used : PyExpressionCodeFragmentImpl(com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PyExpressionCodeFragmentImpl

use of com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl in project intellij-community by JetBrains.

the class NumpyDocStringTypeProvider method getNominalType.

/**
   * Converts literal into type, e.g. -1 -> int, 'fro' -> str
   */
@Nullable
private static PyType getNominalType(@NotNull PsiElement anchor, @NotNull String typeString) {
    final PyExpressionCodeFragmentImpl codeFragment = new PyExpressionCodeFragmentImpl(anchor.getProject(), "dummy.py", typeString, false);
    final PsiElement element = codeFragment.getFirstChild();
    if (element instanceof PyExpressionStatement) {
        final PyExpression expression = ((PyExpressionStatement) element).getExpression();
        final PyBuiltinCache builtinCache = PyBuiltinCache.getInstance(anchor);
        if (expression instanceof PyStringLiteralExpression) {
            return builtinCache.getStrType();
        }
        if (expression instanceof PyNumericLiteralExpression) {
            return builtinCache.getIntType();
        }
    }
    return null;
}
Also used : PyExpressionCodeFragmentImpl(com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl) PsiElement(com.intellij.psi.PsiElement) PyBuiltinCache(com.jetbrains.python.psi.impl.PyBuiltinCache) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with PyExpressionCodeFragmentImpl

use of com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl in project intellij-community by JetBrains.

the class PyUnresolvedReferencesInspectionTest method doEvaluateExpressionTest.

protected void doEvaluateExpressionTest(@NotNull VirtualFile mainFile, @NotNull String expression, int lineNumber) {
    PsiElement element = PyDebuggerEditorsProvider.getContextElement(myFixture.getProject(), XSourcePositionImpl.create(mainFile, lineNumber));
    final PyExpressionCodeFragmentImpl fragment = new PyExpressionCodeFragmentImpl(myFixture.getProject(), "fragment.py", expression, true);
    fragment.setContext(element);
    myFixture.configureFromExistingVirtualFile(fragment.getVirtualFile());
    myFixture.enableInspections(getInspectionClass());
    myFixture.checkHighlighting(isWarning(), isInfo(), isWeakWarning());
}
Also used : PyExpressionCodeFragmentImpl(com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl) PsiElement(com.intellij.psi.PsiElement)

Example 5 with PyExpressionCodeFragmentImpl

use of com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl in project intellij-community by JetBrains.

the class PyTypingTypeProvider method getStringBasedType.

@Nullable
private static Ref<PyType> getStringBasedType(@NotNull String contents, @NotNull PsiElement anchor, @NotNull Context context) {
    final Project project = anchor.getProject();
    final PyExpressionCodeFragmentImpl codeFragment = new PyExpressionCodeFragmentImpl(project, "dummy.py", contents, false);
    codeFragment.setContext(anchor.getContainingFile());
    final PsiElement element = codeFragment.getFirstChild();
    if (element instanceof PyExpressionStatement) {
        final PyExpression expr = ((PyExpressionStatement) element).getExpression();
        if (expr instanceof PyTupleExpression) {
            final PyTupleExpression tupleExpr = (PyTupleExpression) expr;
            final List<PyType> elementTypes = ContainerUtil.map(tupleExpr.getElements(), elementExpr -> Ref.deref(getType(elementExpr, context)));
            return Ref.create(PyTupleType.create(anchor, elementTypes));
        }
        return getType(expr, context);
    }
    return null;
}
Also used : Project(com.intellij.openapi.project.Project) PyExpressionCodeFragmentImpl(com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PyExpressionCodeFragmentImpl (com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl)6 PsiElement (com.intellij.psi.PsiElement)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)2 Project (com.intellij.openapi.project.Project)1 PyBuiltinCache (com.jetbrains.python.psi.impl.PyBuiltinCache)1