Java Code Examples for com.intellij.psi.impl.source.tree.TreeUtil#prevLeaf()

The following examples show how to use com.intellij.psi.impl.source.tree.TreeUtil#prevLeaf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CodeEditUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void markToReformatBeforeOrInsertWhitespace(final ASTNode left, @Nonnull final ASTNode right) {
  final Language leftLang = left != null ? PsiUtilCore.getNotAnyLanguage(left) : null;
  final Language rightLang = PsiUtilCore.getNotAnyLanguage(right);

  ASTNode generatedWhitespace = null;
  if (leftLang != null && leftLang.isKindOf(rightLang)) {
    generatedWhitespace = LanguageTokenSeparatorGenerators.INSTANCE.forLanguage(leftLang).generateWhitespaceBetweenTokens(left, right);
  }
  else if (rightLang.isKindOf(leftLang)) {
    generatedWhitespace = LanguageTokenSeparatorGenerators.INSTANCE.forLanguage(rightLang).generateWhitespaceBetweenTokens(left, right);
  }

  if (generatedWhitespace != null) {
    final TreeUtil.CommonParentState parentState = new TreeUtil.CommonParentState();
    TreeUtil.prevLeaf((TreeElement)right, parentState);
    parentState.nextLeafBranchStart.getTreeParent().addChild(generatedWhitespace, parentState.nextLeafBranchStart);
  }
  else {
    markToReformatBefore(right, true);
  }
}
 
Example 2
Source File: ChangedPsiRangeUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static int getMatchingLength(@Nonnull FileElement treeElement, @Nonnull CharSequence text, boolean fromStart) {
  int patternIndex = fromStart ? 0 : text.length() - 1;
  int finalPatternIndex = fromStart ? text.length() - 1 : 0;
  int direction = fromStart ? 1 : -1;
  ASTNode leaf = fromStart ? TreeUtil.findFirstLeaf(treeElement, false) : TreeUtil.findLastLeaf(treeElement, false);
  int result = 0;
  while (leaf != null && (fromStart ? patternIndex <= finalPatternIndex : patternIndex >= finalPatternIndex)) {
    if (!(leaf instanceof ForeignLeafPsiElement)) {
      CharSequence chars = leaf.getChars();
      if (chars.length() > 0) {
        int matchingLength = getLeafMatchingLength(chars, text, patternIndex, finalPatternIndex, direction);
        result += matchingLength;
        if (matchingLength != chars.length()) {
          break;
        }
        patternIndex += fromStart ? matchingLength : -matchingLength;
      }
    }
    leaf = fromStart ? TreeUtil.nextLeaf(leaf, false) : TreeUtil.prevLeaf(leaf, false);
  }
  return result;
}
 
Example 3
Source File: CodeEditUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static ASTNode addChildren(ASTNode parent, @Nonnull ASTNode first, @Nonnull ASTNode last, ASTNode anchorBefore) {
  ASTNode lastChild = last.getTreeNext();
  ASTNode current = first;
  while (current != lastChild) {
    saveWhitespacesInfo(current);
    checkForOuters(current);
    current = current.getTreeNext();
  }

  if (anchorBefore != null && CommentUtilCore.isComment(anchorBefore)) {
    final ASTNode anchorPrev = anchorBefore.getTreePrev();
    if (anchorPrev != null && anchorPrev.getElementType() == TokenType.WHITE_SPACE) {
      anchorBefore = anchorPrev;
    }
  }

  parent.addChildren(first, lastChild, anchorBefore);
  ASTNode firstAddedLeaf = findFirstLeaf(first, last);
  ASTNode prevLeaf = TreeUtil.prevLeaf(first);
  ASTNode result = first;
  if (firstAddedLeaf != null) {
    ASTNode placeHolderEnd = makePlaceHolderBetweenTokens(prevLeaf, firstAddedLeaf, isFormattingRequired(prevLeaf, first), false);
    if (placeHolderEnd != prevLeaf && first == firstAddedLeaf) {
      result = placeHolderEnd;
    }
    ASTNode lastAddedLeaf = findLastLeaf(first, last);
    placeHolderEnd = makePlaceHolderBetweenTokens(lastAddedLeaf, TreeUtil.nextLeaf(last), true, false);
    if (placeHolderEnd != lastAddedLeaf && lastAddedLeaf == first) {
      result = placeHolderEnd;
    }
  }
  else {
    makePlaceHolderBetweenTokens(prevLeaf, TreeUtil.nextLeaf(last), isFormattingRequired(prevLeaf, first), false);
  }
  return result;
}
 
Example 4
Source File: CodeEditUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void replaceChild(ASTNode parent, @Nonnull ASTNode oldChild, @Nonnull ASTNode newChild) {
  saveWhitespacesInfo(oldChild);
  saveWhitespacesInfo(newChild);
  checkForOuters(oldChild);
  checkForOuters(newChild);

  LeafElement oldFirst = TreeUtil.findFirstLeaf(oldChild);

  parent.replaceChild(oldChild, newChild);
  final LeafElement firstLeaf = TreeUtil.findFirstLeaf(newChild);
  final ASTNode prevToken = TreeUtil.prevLeaf(newChild);
  if (firstLeaf != null) {
    final ASTNode nextLeaf = TreeUtil.nextLeaf(newChild);
    makePlaceHolderBetweenTokens(prevToken, firstLeaf, isFormattingRequired(prevToken, newChild), false);
    if (nextLeaf != null && !CharArrayUtil.containLineBreaks(nextLeaf.getText())) {
      makePlaceHolderBetweenTokens(TreeUtil.prevLeaf(nextLeaf), nextLeaf, false, false);
    }
  }
  else {
    if (oldFirst != null && prevToken == null) {
      ASTNode whitespaceNode = newChild.getTreeNext();
      if (whitespaceNode != null && whitespaceNode.getElementType() == TokenType.WHITE_SPACE) {
        // Replacing non-empty prefix to empty shall remove whitespace
        parent.removeChild(whitespaceNode);
      }
    }

    makePlaceHolderBetweenTokens(prevToken, TreeUtil.nextLeaf(newChild), isFormattingRequired(prevToken, newChild), false);
  }
}
 
Example 5
Source File: FilterPositionUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static PsiElement searchNonSpaceNonCommentBack(PsiElement element, final boolean strict) {
  if(element == null || element.getNode() == null) return null;
  ASTNode leftNeibour = TreeUtil.prevLeaf(element.getNode());
  if (!strict) {
    while (leftNeibour != null && (leftNeibour.getElementType() == TokenType.WHITE_SPACE || leftNeibour.getPsi() instanceof PsiComment)){
      leftNeibour = TreeUtil.prevLeaf(leftNeibour);
    }
  }
  return leftNeibour != null ? leftNeibour.getPsi() : null;

}
 
Example 6
Source File: XQueryXmlSlashTypedHandler.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
protected ASTNode getPreviousNonWhiteSpaceLeaf(ASTNode originalPrevLeaf) {
    ASTNode prevLeaf = originalPrevLeaf;
    while ((prevLeaf = TreeUtil.prevLeaf(prevLeaf)) != null && prevLeaf.getElementType() == TokenType.WHITE_SPACE) {
    }
    return prevLeaf;
}
 
Example 7
Source File: CodeEditUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@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;
}