Search in sources :

Example 1 with PyFromImportStatementStub

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

the class PyFromImportStatementImpl method getImportSourceQName.

public QualifiedName getImportSourceQName() {
    final PyFromImportStatementStub stub = getStub();
    if (stub != null) {
        final QualifiedName qName = stub.getImportSourceQName();
        if (qName != null && qName.getComponentCount() == 0) {
            // relative import only: from .. import the_name
            return null;
        }
        return qName;
    }
    final PyReferenceExpression importSource = getImportSource();
    if (importSource == null) {
        return null;
    }
    return importSource.asQualifiedName();
}
Also used : PyFromImportStatementStub(com.jetbrains.python.psi.stubs.PyFromImportStatementStub) QualifiedName(com.intellij.psi.util.QualifiedName)

Example 2 with PyFromImportStatementStub

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

the class PyFromImportStatementImpl method getImportElements.

@NotNull
public PyImportElement[] getImportElements() {
    final PyFromImportStatementStub stub = getStub();
    if (stub != null) {
        return stub.getChildrenByType(PyElementTypes.IMPORT_ELEMENT, count -> new PyImportElement[count]);
    }
    List<PyImportElement> result = new ArrayList<>();
    final ASTNode importKeyword = getNode().findChildByType(PyTokenTypes.IMPORT_KEYWORD);
    if (importKeyword != null) {
        for (ASTNode node = importKeyword.getTreeNext(); node != null; node = node.getTreeNext()) {
            if (node.getElementType() == PyElementTypes.IMPORT_ELEMENT) {
                result.add((PyImportElement) node.getPsi());
            }
        }
    }
    return result.toArray(new PyImportElement[result.size()]);
}
Also used : PyFromImportStatementStub(com.jetbrains.python.psi.stubs.PyFromImportStatementStub) ArrayList(java.util.ArrayList) ASTNode(com.intellij.lang.ASTNode) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PyFromImportStatementStub

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

the class PyFromImportStatementImpl method getRelativeLevel.

public int getRelativeLevel() {
    final PyFromImportStatementStub stub = getStub();
    if (stub != null) {
        return stub.getRelativeLevel();
    }
    int result = 0;
    ASTNode seeker = getNode().getFirstChildNode();
    while (seeker != null && (seeker.getElementType() == PyTokenTypes.FROM_KEYWORD || seeker.getElementType() == TokenType.WHITE_SPACE)) {
        seeker = seeker.getTreeNext();
    }
    while (seeker != null && seeker.getElementType() == PyTokenTypes.DOT) {
        result++;
        seeker = seeker.getTreeNext();
    }
    return result;
}
Also used : PyFromImportStatementStub(com.jetbrains.python.psi.stubs.PyFromImportStatementStub) ASTNode(com.intellij.lang.ASTNode)

Aggregations

PyFromImportStatementStub (com.jetbrains.python.psi.stubs.PyFromImportStatementStub)3 ASTNode (com.intellij.lang.ASTNode)2 QualifiedName (com.intellij.psi.util.QualifiedName)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1