Search in sources :

Example 21 with LeafElement

use of com.intellij.psi.impl.source.tree.LeafElement in project intellij-community by JetBrains.

the class CodeEditUtil method makePlaceHolderBetweenTokens.

@Nullable
private static ASTNode makePlaceHolderBetweenTokens(ASTNode left, ASTNode right, boolean forceReformat, boolean normalizeTrailingWS) {
    if (right == null)
        return left;
    markToReformatBefore(right, false);
    if (left == null) {
        markToReformatBefore(right, true);
    } else if (left.getElementType() == TokenType.WHITE_SPACE && left.getTreeNext() == null && normalizeTrailingWS) {
        // handle tailing whitespaces if element on the left has been removed
        final ASTNode prevLeaf = TreeUtil.prevLeaf(left);
        left.getTreeParent().removeChild(left);
        markToReformatBeforeOrInsertWhitespace(prevLeaf, right);
        left = right;
    } else if (left.getElementType() == TokenType.WHITE_SPACE && right.getElementType() == TokenType.WHITE_SPACE) {
        final String text;
        final int leftBlankLines = getBlankLines(left.getText());
        final int rightBlankLines = getBlankLines(right.getText());
        final boolean leaveRightText = leftBlankLines < rightBlankLines;
        if (leftBlankLines == 0 && rightBlankLines == 0) {
            text = left.getText() + right.getText();
        } else if (leaveRightText) {
            text = right.getText();
        } else {
            text = left.getText();
        }
        if (leaveRightText || forceReformat) {
            final LeafElement merged = ASTFactory.whitespace(text);
            if (!leaveRightText) {
                left.getTreeParent().replaceChild(left, merged);
                right.getTreeParent().removeChild(right);
            } else {
                right.getTreeParent().replaceChild(right, merged);
                left.getTreeParent().removeChild(left);
            }
            left = merged;
        } else {
            right.getTreeParent().removeChild(right);
        }
    } else if (left.getElementType() != TokenType.WHITE_SPACE || forceReformat) {
        if (right.getElementType() == TokenType.WHITE_SPACE) {
            markWhitespaceForReformat(right);
        } else if (left.getElementType() == TokenType.WHITE_SPACE) {
            markWhitespaceForReformat(left);
        } else {
            markToReformatBeforeOrInsertWhitespace(left, right);
        }
    }
    return left;
}
Also used : LeafElement(com.intellij.psi.impl.source.tree.LeafElement) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with LeafElement

use of com.intellij.psi.impl.source.tree.LeafElement in project intellij-community by JetBrains.

the class CodeEditUtil method findFirstLeaf.

@Nullable
private static ASTNode findFirstLeaf(ASTNode first, ASTNode last) {
    do {
        final LeafElement leaf = TreeUtil.findFirstLeaf(first);
        if (leaf != null)
            return leaf;
        first = first.getTreeNext();
        if (first == null)
            return null;
    } while (first != last);
    return null;
}
Also used : LeafElement(com.intellij.psi.impl.source.tree.LeafElement) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with LeafElement

use of com.intellij.psi.impl.source.tree.LeafElement in project intellij-community by JetBrains.

the class CodeEditUtil method markWhitespaceForReformat.

private static void markWhitespaceForReformat(final ASTNode right) {
    final String text = right.getText();
    final LeafElement merged = ASTFactory.whitespace(text);
    right.getTreeParent().replaceChild(right, merged);
}
Also used : LeafElement(com.intellij.psi.impl.source.tree.LeafElement)

Example 24 with LeafElement

use of com.intellij.psi.impl.source.tree.LeafElement in project intellij-community by JetBrains.

the class LowLevelSearchUtil method findNextLeafElementAt.

private static TreeElement findNextLeafElementAt(ASTNode scopeNode, TreeElement last, int offset) {
    int offsetR = offset;
    if (last != null) {
        offsetR -= last.getStartOffset() - scopeNode.getStartOffset() + last.getTextLength();
        while (offsetR >= 0) {
            TreeElement next = last.getTreeNext();
            if (next == null) {
                last = last.getTreeParent();
                continue;
            }
            int length = next.getTextLength();
            offsetR -= length;
            last = next;
        }
        scopeNode = last;
        offsetR += scopeNode.getTextLength();
    }
    return (LeafElement) scopeNode.findLeafElementAt(offsetR);
}
Also used : LeafElement(com.intellij.psi.impl.source.tree.LeafElement) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 25 with LeafElement

use of com.intellij.psi.impl.source.tree.LeafElement in project intellij-community by JetBrains.

the class GrBlockImpl method addStatementBefore.

@Override
@NotNull
public GrStatement addStatementBefore(@NotNull GrStatement element, @Nullable GrStatement anchor) throws IncorrectOperationException {
    if (anchor == null && getRBrace() == null) {
        throw new IncorrectOperationException();
    }
    if (anchor != null && !this.equals(anchor.getParent())) {
        throw new IncorrectOperationException();
    }
    final LeafElement nls = Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n", 0, 1, null, getManager());
    PsiElement actualAnchor = anchor == null ? getRBrace() : anchor;
    if (mayUseNewLinesAsSeparators()) {
        PsiElement prev = actualAnchor.getPrevSibling();
        if (prev instanceof GrParameterList && prev.getTextLength() == 0 && prev.getPrevSibling() != null) {
            prev = prev.getPrevSibling();
        }
        if (!PsiUtil.isLineFeed(prev)) {
            addBefore(nls.getPsi(), actualAnchor);
        }
    }
    element = (GrStatement) addBefore(element, actualAnchor);
    if (mayUseNewLinesAsSeparators()) {
        addBefore(nls.getPsi(), actualAnchor);
    } else {
        addBefore(Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n", 0, 1, null, getManager()).getPsi(), actualAnchor);
    }
    return element;
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) IncorrectOperationException(com.intellij.util.IncorrectOperationException) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) PsiElement(com.intellij.psi.PsiElement) LazyParseablePsiElement(com.intellij.psi.impl.source.tree.LazyParseablePsiElement) ASTDelegatePsiElement(com.intellij.extapi.psi.ASTDelegatePsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

LeafElement (com.intellij.psi.impl.source.tree.LeafElement)41 ASTNode (com.intellij.lang.ASTNode)15 PsiElement (com.intellij.psi.PsiElement)9 NotNull (org.jetbrains.annotations.NotNull)8 Nullable (org.jetbrains.annotations.Nullable)6 TreeElement (com.intellij.psi.impl.source.tree.TreeElement)4 IElementType (com.intellij.psi.tree.IElementType)4 PsiFragment (com.intellij.dupLocator.util.PsiFragment)3 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)3 FileElement (com.intellij.psi.impl.source.tree.FileElement)3 EquivalenceDescriptorProvider (com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider)2 Project (com.intellij.openapi.project.Project)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)2 CharTable (com.intellij.util.CharTable)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1