Search in sources :

Example 61 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class PyParameterListImpl method addParameter.

public void addParameter(final PyNamedParameter param) {
    PsiElement paren = getLastChild();
    if (paren != null && ")".equals(paren.getText())) {
        // the closing paren will be this
        ASTNode beforeWhat = paren.getNode();
        PyParameter[] params = getParameters();
        final boolean hasDefaultValue = param.hasDefaultValue();
        boolean isLast = true;
        for (PyParameter p : params) {
            if (!hasDefaultValue && p.hasDefaultValue()) {
                beforeWhat = p.getNode();
                isLast = false;
                break;
            }
            if (p instanceof PyNamedParameter) {
                PyNamedParameter named = (PyNamedParameter) p;
                if (named.isKeywordContainer() || named.isPositionalContainer()) {
                    beforeWhat = p.getNode();
                    isLast = false;
                    break;
                }
            }
        }
        final ASTNode previous = PyPsiUtils.getPrevNonWhitespaceSibling(beforeWhat);
        PyUtil.addListNode(this, param, beforeWhat, !isLast || params.length == 0 || previous.getElementType() == PyTokenTypes.COMMA, isLast, beforeWhat.getElementType() != PyTokenTypes.RPAR);
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement)

Example 62 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class PyPrefixExpressionImpl method getPsiOperator.

@Nullable
public PsiElement getPsiOperator() {
    final ASTNode node = getNode();
    final ASTNode child = node.findChildByType(PyElementTypes.UNARY_OPS);
    return child != null ? child.getPsi() : null;
}
Also used : ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable)

Example 63 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class PyStatementListImpl method addInternal.

@Override
public ASTNode addInternal(ASTNode first, ASTNode last, ASTNode anchor, Boolean before) {
    if (first.getPsi() instanceof PyStatement && getStatements().length == 1) {
        ASTNode treePrev = getNode().getTreePrev();
        if (treePrev != null && treePrev.getElementType() == TokenType.WHITE_SPACE && !treePrev.textContains('\n')) {
            ASTNode lineBreak = ASTFactory.whitespace("\n");
            treePrev.getTreeParent().replaceChild(treePrev, lineBreak);
        }
    }
    return super.addInternal(first, last, anchor, before);
}
Also used : PyStatement(com.jetbrains.python.psi.PyStatement) ASTNode(com.intellij.lang.ASTNode)

Example 64 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class PyStringLiteralExpressionImpl method getType.

public PyType getType(@NotNull TypeEvalContext context, @NotNull TypeEvalContext.Key key) {
    final List<ASTNode> nodes = getStringNodes();
    if (nodes.size() > 0) {
        String text = getStringNodes().get(0).getText();
        PyFile file = PsiTreeUtil.getParentOfType(this, PyFile.class);
        if (file != null) {
            IElementType type = PythonHighlightingLexer.convertStringType(getStringNodes().get(0).getElementType(), text, LanguageLevel.forElement(this), file.hasImportFromFuture(FutureFeature.UNICODE_LITERALS));
            if (PyTokenTypes.UNICODE_NODES.contains(type)) {
                return PyBuiltinCache.getInstance(this).getUnicodeType(LanguageLevel.forElement(this));
            }
        }
    }
    return PyBuiltinCache.getInstance(this).getBytesType(LanguageLevel.forElement(this));
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode)

Example 65 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class PyStringLiteralExpressionImpl method getDecodedFragments.

@Override
@NotNull
public List<Pair<TextRange, String>> getDecodedFragments() {
    if (myDecodedFragments == null) {
        final List<Pair<TextRange, String>> result = new ArrayList<>();
        final int elementStart = getTextRange().getStartOffset();
        final boolean unicodeByDefault = isUnicodeByDefault();
        for (ASTNode node : getStringNodes()) {
            final String text = node.getText();
            final TextRange textRange = getNodeTextRange(text);
            final int offset = node.getTextRange().getStartOffset() - elementStart + textRange.getStartOffset();
            final String encoded = textRange.substring(text);
            final boolean hasRawPrefix = PyStringLiteralUtil.isRawPrefix(PyStringLiteralUtil.getPrefix(text));
            final boolean hasUnicodePrefix = PyStringLiteralUtil.isUnicodePrefix(PyStringLiteralUtil.getPrefix(text));
            result.addAll(getDecodedFragments(encoded, offset, hasRawPrefix, unicodeByDefault || hasUnicodePrefix));
        }
        myDecodedFragments = result;
    }
    return myDecodedFragments;
}
Also used : ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ASTNode (com.intellij.lang.ASTNode)748 PsiElement (com.intellij.psi.PsiElement)142 NotNull (org.jetbrains.annotations.NotNull)132 IElementType (com.intellij.psi.tree.IElementType)127 Nullable (org.jetbrains.annotations.Nullable)99 TextRange (com.intellij.openapi.util.TextRange)91 ArrayList (java.util.ArrayList)53 IncorrectOperationException (com.intellij.util.IncorrectOperationException)35 PsiFile (com.intellij.psi.PsiFile)31 Annotation (com.intellij.lang.annotation.Annotation)22 CharTable (com.intellij.util.CharTable)22 Document (com.intellij.openapi.editor.Document)18 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)17 XmlTag (com.intellij.psi.xml.XmlTag)17 Project (com.intellij.openapi.project.Project)16 AbstractBlock (com.intellij.psi.formatter.common.AbstractBlock)16 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)16 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)15 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)15 PsiReference (com.intellij.psi.PsiReference)14