Search in sources :

Example 1 with ASTNode

use of com.intellij.lang.ASTNode in project buck by facebook.

the class BuckBlock method isIncomplete.

@Override
public boolean isIncomplete() {
    final ASTNode lastChildNode = myNode.getLastChildNode();
    boolean ret = false;
    if (hasElementType(myNode, TokenSet.create(BuckTypes.ARRAY_ELEMENTS))) {
        ret = lastChildNode != null && lastChildNode.getElementType() != BuckTypes.R_BRACKET;
    } else if (hasElementType(myNode, TokenSet.create(BuckTypes.RULE_BODY))) {
        ret = lastChildNode != null && lastChildNode.getElementType() != BuckTypes.R_PARENTHESES;
    } else if (hasElementType(myNode, TokenSet.create(BuckTypes.LIST_ELEMENTS))) {
        ret = lastChildNode != null && lastChildNode.getElementType() != BuckTypes.R_PARENTHESES;
    }
    return ret;
}
Also used : ASTNode(com.intellij.lang.ASTNode)

Example 2 with ASTNode

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

the class GroovyEnterHandler method handleInString.

private static boolean handleInString(Editor editor, int caretOffset, DataContext dataContext, EditorActionHandler originalHandler) {
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null)
        return false;
    final VirtualFile vfile = FileDocumentManager.getInstance().getFile(editor.getDocument());
    assert vfile != null;
    PsiFile file = PsiManager.getInstance(project).findFile(vfile);
    Document document = editor.getDocument();
    String fileText = document.getText();
    if (fileText.length() == caretOffset)
        return false;
    if (!checkStringApplicable(editor, caretOffset))
        return false;
    if (file == null)
        return false;
    PsiDocumentManager.getInstance(project).commitDocument(document);
    final PsiElement stringElement = inferStringPair(file, caretOffset);
    if (stringElement == null)
        return false;
    ASTNode node = stringElement.getNode();
    final IElementType nodeElementType = node.getElementType();
    boolean isInsertIndent = isInsertIndent(caretOffset, stringElement.getTextRange().getStartOffset(), fileText);
    // For simple String literals like 'abc'
    CaretModel caretModel = editor.getCaretModel();
    if (nodeElementType == GroovyTokenTypes.mSTRING_LITERAL) {
        if (isSingleQuoteString(stringElement)) {
            //the case of print '\<caret>'
            if (isSlashBeforeCaret(caretOffset, fileText)) {
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
            } else if (stringElement.getParent() instanceof GrReferenceExpression) {
                TextRange range = stringElement.getTextRange();
                convertEndToMultiline(range.getEndOffset(), document, fileText, '\'');
                document.insertString(range.getStartOffset(), "''");
                caretModel.moveToOffset(caretOffset + 2);
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
            } else {
                EditorModificationUtil.insertStringAtCaret(editor, "'+");
                originalHandler.execute(editor, dataContext);
                EditorModificationUtil.insertStringAtCaret(editor, "'");
                PsiDocumentManager.getInstance(project).commitDocument(document);
                CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
            }
        } else {
            insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
        }
        return true;
    }
    if (GSTRING_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && GSTRING_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.GSTRING) {
        PsiElement parent = stringElement.getParent();
        if (nodeElementType == GroovyTokenTypes.mGSTRING_LITERAL) {
            parent = stringElement;
        } else {
            while (parent != null && !(parent instanceof GrLiteral)) {
                parent = parent.getParent();
            }
        }
        if (parent == null)
            return false;
        if (isDoubleQuotedString(parent)) {
            PsiElement exprSibling = stringElement.getNextSibling();
            boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
            if (rightFromDollar)
                caretOffset--;
            TextRange parentRange = parent.getTextRange();
            if (rightFromDollar || parent.getParent() instanceof GrReferenceExpression) {
                convertEndToMultiline(parent.getTextRange().getEndOffset(), document, fileText, '"');
                document.insertString(parentRange.getStartOffset(), "\"\"");
                caretModel.moveToOffset(caretOffset + 2);
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
                if (rightFromDollar) {
                    caretModel.moveCaretRelatively(1, 0, false, false, true);
                }
            } else if (isSlashBeforeCaret(caretOffset, fileText)) {
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
            } else {
                EditorModificationUtil.insertStringAtCaret(editor, "\"+");
                originalHandler.execute(editor, dataContext);
                EditorModificationUtil.insertStringAtCaret(editor, "\"");
                PsiDocumentManager.getInstance(project).commitDocument(document);
                CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
            }
        } else {
            insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
        }
        return true;
    }
    if (REGEX_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && REGEX_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.REGEX) {
        PsiElement parent = stringElement.getParent();
        if (nodeElementType == GroovyTokenTypes.mREGEX_LITERAL || nodeElementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
            parent = stringElement;
        } else {
            while (parent != null && !(parent instanceof GrLiteral)) {
                parent = parent.getParent();
            }
        }
        if (parent == null || parent.getLastChild() instanceof PsiErrorElement)
            return false;
        PsiElement exprSibling = stringElement.getNextSibling();
        boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
        if (rightFromDollar) {
            caretModel.moveToOffset(caretOffset - 1);
        }
        insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
        if (rightFromDollar) {
            caretModel.moveCaretRelatively(1, 0, false, false, true);
        }
        return true;
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CaretModel(com.intellij.openapi.editor.CaretModel) TextRange(com.intellij.openapi.util.TextRange) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Document(com.intellij.openapi.editor.Document) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) IElementType(com.intellij.psi.tree.IElementType) Project(com.intellij.openapi.project.Project) ASTNode(com.intellij.lang.ASTNode) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 3 with ASTNode

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

the class GroovyEnterHandler method inferStringPair.

@Nullable
private static PsiElement inferStringPair(PsiFile file, int caretOffset) {
    PsiElement stringElement = file.findElementAt(caretOffset - 1);
    if (stringElement == null)
        return null;
    ASTNode node = stringElement.getNode();
    if (node == null)
        return null;
    // For expression injection in GString like "abc ${}<caret>  abc"
    if (!INNER_STRING_TOKENS.contains(node.getElementType()) && checkGStringInjection(stringElement)) {
        stringElement = stringElement.getParent().getParent().getNextSibling();
        if (stringElement == null)
            return null;
    }
    return stringElement;
}
Also used : ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with ASTNode

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

the class GroovyBlockGenerator method addBinaryChildrenRecursively.

/**
   * Adds all children of specified element to given list
   *
   * @param elem
   * @param list
   * @param indent
   * @param aligner
   */
private void addBinaryChildrenRecursively(PsiElement elem, List<Block> list, Indent indent, @Nullable AlignmentProvider.Aligner aligner) {
    if (elem == null)
        return;
    // For binary expressions
    if ((elem instanceof GrBinaryExpression)) {
        GrBinaryExpression myExpr = ((GrBinaryExpression) elem);
        if (myExpr.getLeftOperand() instanceof GrBinaryExpression) {
            addBinaryChildrenRecursively(myExpr.getLeftOperand(), list, Indent.getContinuationWithoutFirstIndent(), aligner);
        }
        PsiElement op = ((GrBinaryExpression) elem).getOperationToken();
        for (ASTNode childNode : visibleChildren(elem.getNode())) {
            PsiElement psi = childNode.getPsi();
            if (!(psi instanceof GrBinaryExpression)) {
                if (op != psi && aligner != null) {
                    aligner.append(psi);
                }
                list.add(new GroovyBlock(childNode, indent, getChildWrap(childNode), myContext));
            }
        }
        if (myExpr.getRightOperand() instanceof GrBinaryExpression) {
            addBinaryChildrenRecursively(myExpr.getRightOperand(), list, Indent.getContinuationWithoutFirstIndent(), aligner);
        }
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 5 with ASTNode

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

the class GroovyBlockGenerator method processNestedChildrenPrefix.

private void processNestedChildrenPrefix(List<Block> list, @Nullable AlignmentProvider.Aligner aligner, boolean topLevel, List<ASTNode> children, int limit, Wrap wrap) {
    ASTNode fst = children.get(0);
    LOG.assertTrue(limit > 0);
    if (NESTED.contains(fst.getElementType())) {
        addNestedChildren(fst.getPsi(), list, aligner, false, wrap);
    } else {
        Indent indent = Indent.getContinuationWithoutFirstIndent();
        list.add(new GroovyBlock(fst, indent, getChildWrap(fst), myContext));
    }
    addNestedChildrenSuffix(list, aligner, topLevel, children, limit);
}
Also used : ASTNode(com.intellij.lang.ASTNode)

Aggregations

ASTNode (com.intellij.lang.ASTNode)839 NotNull (org.jetbrains.annotations.NotNull)154 PsiElement (com.intellij.psi.PsiElement)152 IElementType (com.intellij.psi.tree.IElementType)152 Nullable (org.jetbrains.annotations.Nullable)113 TextRange (com.intellij.openapi.util.TextRange)97 ArrayList (java.util.ArrayList)60 PsiFile (com.intellij.psi.PsiFile)36 IncorrectOperationException (com.intellij.util.IncorrectOperationException)35 Annotation (com.intellij.lang.annotation.Annotation)25 AbstractBlock (com.intellij.psi.formatter.common.AbstractBlock)22 CharTable (com.intellij.util.CharTable)22 FileASTNode (com.intellij.lang.FileASTNode)20 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)20 Document (com.intellij.openapi.editor.Document)20 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)18 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)18 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)18 Project (com.intellij.openapi.project.Project)17 XmlTag (com.intellij.psi.xml.XmlTag)17