Search in sources :

Example 1 with PyFromImportStatement

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

the class UnresolvedRefAddFutureImportQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement element = descriptor.getPsiElement();
    PyFile file = (PyFile) element.getContainingFile();
    PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    PyFromImportStatement statement = elementGenerator.createFromText(LanguageLevel.forElement(element), PyFromImportStatement.class, "from __future__ import with_statement");
    file.addBefore(statement, file.getStatements().get(0));
}
Also used : PyFromImportStatement(com.jetbrains.python.psi.PyFromImportStatement) PyFile(com.jetbrains.python.psi.PyFile) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PyFromImportStatement

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

the class PyResolveProcessor method tryAddResult.

private boolean tryAddResult(@Nullable PsiElement element, @Nullable PyImportedNameDefiner definer) {
    final ScopeOwner owner = ScopeUtil.getScopeOwner(definer != null ? definer : element);
    if (myOwner == null) {
        myOwner = owner;
    }
    final boolean sameScope = owner == myOwner;
    if (sameScope) {
        // XXX: In 'from foo import foo' inside __init__.py the preferred result is explicitly imported 'foo'
        if (definer instanceof PyFromImportStatement) {
            myImplicitlyImportedResults.put(element, definer);
        } else {
            myResults.put(element, definer);
        }
    }
    return sameScope;
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PyFromImportStatement(com.jetbrains.python.psi.PyFromImportStatement)

Aggregations

PyFromImportStatement (com.jetbrains.python.psi.PyFromImportStatement)2 PsiElement (com.intellij.psi.PsiElement)1 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)1 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)1 PyFile (com.jetbrains.python.psi.PyFile)1