Search in sources :

Example 6 with ASTNode

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

the class GroovyBlockGenerator method calculateAlignments.

private void calculateAlignments(List<ASTNode> children, boolean classLevel) {
    List<GrStatement> currentGroup = null;
    boolean spock = true;
    for (ASTNode child : children) {
        PsiElement psi = child.getPsi();
        if (psi instanceof GrLabeledStatement) {
            alignGroup(currentGroup, spock, classLevel);
            currentGroup = ContainerUtil.newArrayList();
            spock = true;
        } else if (currentGroup != null && spock && isTablePart(psi)) {
            currentGroup.add((GrStatement) psi);
        } else if (psi instanceof GrVariableDeclaration) {
            GrVariable[] variables = ((GrVariableDeclaration) psi).getVariables();
            if (variables.length > 0) {
                if (!classLevel || currentGroup == null || fieldGroupEnded(psi) || spock) {
                    alignGroup(currentGroup, spock, classLevel);
                    currentGroup = ContainerUtil.newArrayList();
                    spock = false;
                }
                currentGroup.add((GrStatement) psi);
            }
        } else {
            if (shouldSkip(classLevel, psi))
                continue;
            alignGroup(currentGroup, spock, classLevel);
            currentGroup = null;
        }
    }
    if (currentGroup != null) {
        alignGroup(currentGroup, spock, classLevel);
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) ASTNode(com.intellij.lang.ASTNode) GrLabeledStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 7 with ASTNode

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

the class GroovyIndentProcessor method getChildIndent.

/**
   * Calculates indent, based on code style, between parent block and child node
   *
   * @param parentBlock parent block
   * @param child       child node
   * @return indent
   */
@NotNull
public Indent getChildIndent(@NotNull final GroovyBlock parentBlock, @NotNull final ASTNode child) {
    myChildType = child.getElementType();
    if (parentBlock instanceof ClosureBodyBlock) {
        if (myChildType == GroovyElementTypes.PARAMETERS_LIST) {
            return Indent.getNoneIndent();
        } else if (myChildType != GroovyTokenTypes.mLCURLY && myChildType != GroovyTokenTypes.mRCURLY) {
            return Indent.getNormalIndent();
        }
    }
    if (parentBlock instanceof GrLabelBlock) {
        ASTNode first = parentBlock.getNode().getFirstChildNode();
        return child == first ? Indent.getNoneIndent() : Indent.getLabelIndent();
    }
    if (GSTRING_TOKENS_INNER.contains(myChildType)) {
        return Indent.getAbsoluteNoneIndent();
    }
    final PsiElement parent = parentBlock.getNode().getPsi();
    if (parent instanceof GroovyPsiElement) {
        myBlock = parentBlock;
        myChild = child.getPsi();
        ((GroovyPsiElement) parent).accept(this);
        if (myResult != null)
            return myResult;
    }
    return Indent.getNoneIndent();
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrLabelBlock(org.jetbrains.plugins.groovy.formatter.blocks.GrLabelBlock) ASTNode(com.intellij.lang.ASTNode) ClosureBodyBlock(org.jetbrains.plugins.groovy.formatter.blocks.ClosureBodyBlock) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with ASTNode

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

the class GroovySpacingProcessor method processParentheses.

private boolean processParentheses(@NotNull IElementType left, @NotNull IElementType right, @NotNull Boolean spaceWithin, @Nullable Boolean spaceWithinEmpty, @Nullable Boolean leftLF, @Nullable Boolean rightLF) {
    if (myType1 == left && myType2 == right && spaceWithinEmpty != null) {
        createSpaceInCode(spaceWithinEmpty);
        return true;
    } else if (myType1 == left) {
        final ASTNode rparenth = findFrom(myChild1, right, true);
        if (rparenth == null || leftLF == null) {
            createSpaceInCode(spaceWithin);
        } else {
            final TextRange range = new TextRange(myChild1.getStartOffset(), rparenth.getTextRange().getEndOffset());
            createDependentLFSpacing(leftLF, spaceWithin, range);
        }
        return true;
    } else if (myType2 == right) {
        final ASTNode lparenth = findFrom(myChild1, left, false);
        if (lparenth == null || rightLF == null) {
            createSpaceInCode(spaceWithin);
        } else {
            final TextRange range = new TextRange(lparenth.getStartOffset(), myChild2.getTextRange().getEndOffset());
            createDependentLFSpacing(rightLF, spaceWithin, range);
        }
        return true;
    } else {
        return false;
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange)

Example 9 with ASTNode

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

the class XsltBreakpointHandler method getActualLineNumber.

public static int getActualLineNumber(Project project, @Nullable XSourcePosition position) {
    if (position == null) {
        return -1;
    }
    final PsiElement element = findContextElement(project, position);
    if (element == null) {
        return -1;
    }
    if (element instanceof XmlToken) {
        final IElementType tokenType = ((XmlToken) element).getTokenType();
        if (tokenType == XmlTokenType.XML_START_TAG_START || tokenType == XmlTokenType.XML_NAME) {
            final PsiManager psiManager = PsiManager.getInstance(project);
            final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
            final PsiFile psiFile = psiManager.findFile(position.getFile());
            if (psiFile == null) {
                return -1;
            }
            final Document document = documentManager.getDocument(psiFile);
            if (document == null) {
                return -1;
            }
            if (document.getLineNumber(element.getTextRange().getStartOffset()) == position.getLine()) {
                final XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, false);
                if (tag != null) {
                    final ASTNode node = tag.getNode();
                    assert node != null;
                    // TODO: re-check if/when Xalan is supported
                    final ASTNode end = XmlChildRole.START_TAG_END_FINDER.findChild(node);
                    if (end != null) {
                        return document.getLineNumber(end.getTextRange().getEndOffset()) + 1;
                    } else {
                        final ASTNode end2 = XmlChildRole.EMPTY_TAG_END_FINDER.findChild(node);
                        if (end2 != null) {
                            return document.getLineNumber(end2.getTextRange().getEndOffset()) + 1;
                        }
                    }
                }
            }
        }
    }
    return -1;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 10 with ASTNode

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

the class YAMLQuotedTextImpl method getContentRanges.

@NotNull
@Override
public List<TextRange> getContentRanges() {
    final ASTNode firstContentNode = getFirstContentNode();
    if (firstContentNode == null) {
        return Collections.emptyList();
    }
    List<TextRange> result = new ArrayList<>();
    TextRange contentRange = TextRange.create(firstContentNode.getStartOffset(), getTextRange().getEndOffset()).shiftRight(-getTextRange().getStartOffset());
    final List<String> lines = StringUtil.split(contentRange.substring(getText()), "\n", true, false);
    // First line has opening quote
    int cumulativeOffset = contentRange.getStartOffset();
    for (int i = 0; i < lines.size(); ++i) {
        final String line = lines.get(i);
        int lineStart = 0;
        int lineEnd = line.length();
        if (i == 0) {
            lineStart++;
        } else {
            while (lineStart < line.length() && YAMLGrammarCharUtil.isSpaceLike(line.charAt(lineStart))) {
                lineStart++;
            }
        }
        if (i == lines.size() - 1) {
            // Last line has closing quote
            lineEnd--;
        } else {
            while (lineEnd > lineStart && YAMLGrammarCharUtil.isSpaceLike(line.charAt(lineEnd - 1))) {
                lineEnd--;
            }
        }
        result.add(TextRange.create(lineStart, lineEnd).shiftRight(cumulativeOffset));
        cumulativeOffset += line.length() + 1;
    }
    return result;
}
Also used : ASTNode(com.intellij.lang.ASTNode) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull)

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