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;
}
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;
}
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);
}
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);
}
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;
}
Aggregations