Java Code Examples for com.intellij.lang.ASTNode#getTreePrev()

The following examples show how to use com.intellij.lang.ASTNode#getTreePrev() . 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: ProtoFoldingBuilder.java    From protobuf-jetbrains-plugin with Apache License 2.0 6 votes vote down vote up
private static PsiElement findFurthestSiblingOfSameType(@NotNull PsiElement anchor, boolean after) {
    ASTNode node = anchor.getNode();
    final IElementType expectedType = node.getElementType();
    ASTNode lastSeen = node;
    while (node != null) {
        final IElementType elementType = node.getElementType();
        if (elementType == expectedType) {
            lastSeen = node;
        } else if (elementType == TokenType.WHITE_SPACE) {
            if (expectedType == token(LINE_COMMENT)
                    && node.getText().indexOf('\n', 1) != -1) {
                break;
            }
        } else if (!COMMENT_TOKEN_SET.contains(elementType) || COMMENT_TOKEN_SET.contains(expectedType)) {
            break;
        }
        node = after ? node.getTreeNext() : node.getTreePrev();
    }
    return lastSeen.getPsi();
}
 
Example 2
Source File: FormatterUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private static ASTNode getWsCandidate(@Nullable ASTNode node) {
  if (node == null) return null;
  ASTNode treePrev = node.getTreePrev();
  if (treePrev != null) {
    if (treePrev.getElementType() == TokenType.WHITE_SPACE) {
      return treePrev;
    }
    else if (treePrev.getTextLength() == 0) {
      return getWsCandidate(treePrev);
    }
    else {
      return node;
    }
  }
  final ASTNode treeParent = node.getTreeParent();

  if (treeParent == null || treeParent.getTreeParent() == null) {
    return node;
  }
  else {
    return getWsCandidate(treeParent);
  }
}
 
Example 3
Source File: TreeUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static ASTNode skipElementsBack(@Nullable ASTNode element, TokenSet types) {
  if (element == null) return null;
  if (!types.contains(element.getElementType())) return element;

  ASTNode parent = element.getTreeParent();
  ASTNode prev = element;
  while (prev instanceof CompositeElement) {
    if (!types.contains(prev.getElementType())) return prev;
    prev = prev.getTreePrev();
  }
  if (prev == null) return null;
  ASTNode firstChildNode = parent.getFirstChildNode();
  ASTNode lastRelevant = null;
  while (firstChildNode != prev) {
    if (!types.contains(firstChildNode.getElementType())) lastRelevant = firstChildNode;
    firstChildNode = firstChildNode.getTreeNext();
  }
  return lastRelevant;
}
 
Example 4
Source File: TreeUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static ASTNode prevLeaf(TreeElement start, @Nullable CommonParentState commonParent) {
  while (true) {
    if (start == null) return null;
    if (commonParent != null) {
      if (commonParent.strongWhiteSpaceHolder != null && start.getUserData(UNCLOSED_ELEMENT_PROPERTY) != null) {
        commonParent.strongWhiteSpaceHolder = (CompositeElement)start;
      }
      commonParent.nextLeafBranchStart = start;
    }
    ASTNode prevTree = start;
    ASTNode prev = null;
    while (prev == null && (prevTree = prevTree.getTreePrev()) != null) {
      prev = findLastLeaf(prevTree);
    }
    if (prev != null) {
      if (commonParent != null) commonParent.startLeafBranchStart = (TreeElement)prevTree;
      return prev;
    }
    start = start.getTreeParent();
  }
}
 
Example 5
Source File: HaxeFoldingBuilder.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
private static ASTNode findLastImportNode(@NotNull ASTNode node) {
  ASTNode lastImportNode = node;
  ASTNode nextNode = UsefulPsiTreeUtil.getNextSiblingSkipWhiteSpacesAndComments(node);
  while (nextNode != null && isImportOrUsingStatement(nextNode.getElementType())) {
    lastImportNode = nextNode;
    nextNode = UsefulPsiTreeUtil.getNextSiblingSkipWhiteSpacesAndComments(nextNode);
  }
  if (lastImportNode.getElementType() == WHITE_SPACE) {
    lastImportNode = lastImportNode.getTreePrev();
  }
  return lastImportNode;
}
 
Example 6
Source File: PsiUtil.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * Gets the closest previous sibling, that is not skip, relative to node
 *
 * @param node node to find sibling of
 * @param skip what element type to skip
 * @return non-whitespace sibling, or null if none was found
 */
@Nullable
public static ASTNode getPrevSiblingNotType(@NotNull ASTNode node, @NotNull IElementType skip) {
	ASTNode sibling = node.getTreePrev();
	while (sibling != null) {
		if (sibling.getElementType() == skip) {
			sibling = sibling.getTreePrev();
		} else {
			break;
		}
	}
	return sibling;
}
 
Example 7
Source File: CSharpDocGtTypedHandler.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nullable
public static PsiElement getEndTagNameElement(@Nonnull CSharpDocTagImpl tag)
{
	final ASTNode node = tag.getNode();
	if(node == null)
	{
		return null;
	}

	ASTNode current = node.getLastChildNode();
	ASTNode prev = current;

	while(current != null)
	{
		final IElementType elementType = prev.getElementType();
		if((elementType == CSharpDocTokenType.XML_NAME || elementType == CSharpDocTokenType.XML_TAG_NAME) && current.getElementType() ==
				CSharpDocTokenType.XML_END_TAG_START)
		{
			return prev.getPsi();
		}

		prev = current;
		current = current.getTreePrev();

	}
	return null;
}
 
Example 8
Source File: UsefulPsiTreeUtil.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ASTNode getPrevSiblingSkipWhiteSpacesAndComments(@Nullable ASTNode sibling)
{
	if(sibling == null)
	{
		return null;
	}
	ASTNode result = sibling.getTreePrev();
	while(result != null && isWhitespaceOrComment(result.getPsi()))
	{
		result = result.getTreePrev();
	}
	return result;
}
 
Example 9
Source File: TreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ASTNode skipWhitespaceCommentsAndTokens(final ASTNode node, @Nonnull TokenSet alsoSkip, boolean forward) {
  ASTNode element = node;
  while (true) {
    if (element == null) return null;
    if (!isWhitespaceOrComment(element) && !alsoSkip.contains(element.getElementType())) break;
    element = forward ? element.getTreeNext() : element.getTreePrev();
  }
  return element;
}
 
Example 10
Source File: PostprocessReformattingAspect.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean hasRaiseableEdgeChild(ASTNode node) {
  ASTNode first = node.getFirstChildNode();
  while (first != null && first.getTextLength() == 0) first = first.getTreeNext();

  ASTNode last = node.getLastChildNode();
  while (last != null && last.getTextLength() == 0) last = last.getTreePrev();

  return first == null || last == null || isRaiseable(first) || isRaiseable(last);
}
 
Example 11
Source File: BashPsiUtils.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
public static boolean isSingleChildParent(PsiElement psi, @NotNull IElementType childType) {
    if (psi == null) {
        return false;
    }

    ASTNode child = getDeepestEquivalent(psi.getNode());
    return child.getTreePrev() == null && child.getTreeNext() == null && (child.getElementType() == childType);
}
 
Example 12
Source File: BashPsiUtils.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
public static boolean isSingleChildParent(PsiElement psi) {
    if (psi == null) {
        return false;
    }

    ASTNode child = psi.getNode();
    return child.getTreePrev() == null && child.getTreeNext() == null;
}
 
Example 13
Source File: PsiUtil.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * Gets the closest previous sibling, that is not skip, relative to node
 *
 * @param node node to find sibling of
 * @param skip what element type to skip
 * @return non-whitespace sibling, or null if none was found
 */
@Nullable
public static ASTNode getPrevSiblingNotType(@NotNull ASTNode node, @NotNull IElementType skip) {
	ASTNode sibling = node.getTreePrev();
	while (sibling != null) {
		if (sibling.getElementType() == skip) {
			sibling = sibling.getTreePrev();
		} else {
			break;
		}
	}
	return sibling;
}
 
Example 14
Source File: TreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ASTNode findSiblingBackward(ASTNode start, IElementType elementType) {
  ASTNode child = start;
  while (true) {
    if (child == null) return null;
    if (child.getElementType() == elementType) return child;
    child = child.getTreePrev();
  }
}
 
Example 15
Source File: FormattingRangesExtender.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static TextRange getRangeWithSiblings(@Nonnull ASTNode astNode) {
  Ref<TextRange> result = Ref.create(astNode.getTextRange());
  IElementType elementType = astNode.getElementType();
  ASTNode sibling = astNode.getTreePrev();
  while (sibling != null && processSibling(sibling, result, elementType)) {
    sibling = sibling.getTreePrev();
  }
  sibling = astNode.getTreeNext();
  while (sibling != null && processSibling(sibling, result, elementType)) {
    sibling = sibling.getTreeNext();
  }
  return result.get();
}
 
Example 16
Source File: Parameter.java    From intellij with Apache License 2.0 5 votes vote down vote up
public Expression getDefaultValue() {
  ASTNode node = getNode().getLastChildNode();
  while (node != null) {
    if (BuildElementTypes.EXPRESSIONS.contains(node.getElementType())) {
      return (Expression) node.getPsi();
    }
    if (node.getElementType() == BuildToken.fromKind(TokenKind.EQUALS)) {
      break;
    }
    node = node.getTreePrev();
  }
  return null;
}
 
Example 17
Source File: TreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ASTNode findSiblingBackward(ASTNode start, TokenSet types) {
  ASTNode child = start;
  while (true) {
    if (child == null) return null;
    if (types.contains(child.getElementType())) return child;
    child = child.getTreePrev();
  }
}
 
Example 18
Source File: FormatterUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean isPrecededBy(@Nullable ASTNode node, IElementType expectedType, IElementType... skipTypes) {
  ASTNode prevNode = node == null ? null : node.getTreePrev();
  while (prevNode != null && (isWhitespaceOrEmpty(prevNode) || isOneOf(prevNode, skipTypes))) {
    prevNode = prevNode.getTreePrev();
  }
  if (prevNode == null) return false;
  return prevNode.getElementType() == expectedType;
}
 
Example 19
Source File: ProtoFoldingBuilder.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
@Nullable
private ASTNode findPreviousNonWhitespaceOrCommentNode(@NotNull ASTNode node) {
    ASTNode tmp = node;
    while (tmp != null) {
        IElementType type = tmp.getElementType();
        if (!(tmp instanceof PsiWhiteSpace
                || type == token(COMMENT)
                || type == token(LINE_COMMENT))) {
            break;
        }
        tmp = tmp.getTreePrev();
    }
    return tmp;
}
 
Example 20
Source File: DefaultIndentHelperExtension.java    From consulo with Apache License 2.0 4 votes vote down vote up
@RequiredReadAction
@Override
public int getIndentInner(@Nonnull IndentHelper indentHelper, @Nonnull PsiFile file, @Nonnull ASTNode element, boolean includeNonSpace, int recursionLevel) {
  if (recursionLevel > TOO_BIG_WALK_THRESHOLD) return 0;

  if (element.getTreePrev() != null) {
    ASTNode prev = element.getTreePrev();
    ASTNode lastCompositePrev;
    while (prev instanceof CompositeElement && !TreeUtil.isStrongWhitespaceHolder(prev.getElementType())) {
      lastCompositePrev = prev;
      prev = prev.getLastChildNode();
      if (prev == null) { // element.prev is "empty composite"
        return getIndentInner(indentHelper, file, lastCompositePrev, includeNonSpace, recursionLevel + 1);
      }
    }

    String text = prev.getText();
    int index = Math.max(text.lastIndexOf('\n'), text.lastIndexOf('\r'));

    if (index >= 0) {
      return IndentHelperImpl.getIndent(file, text.substring(index + 1), includeNonSpace);
    }

    if (includeNonSpace) {
      return getIndentInner(indentHelper, file, prev, includeNonSpace, recursionLevel + 1) + IndentHelperImpl.getIndent(file, text, includeNonSpace);
    }


    ASTNode parent = prev.getTreeParent();
    ASTNode child = prev;
    while (parent != null) {
      if (child.getTreePrev() != null) break;
      child = parent;
      parent = parent.getTreeParent();
    }

    if (parent == null) {
      return IndentHelperImpl.getIndent(file, text, includeNonSpace);
    }
    else {
      return getIndentInner(indentHelper, file, prev, includeNonSpace, recursionLevel + 1);
    }
  }
  else {
    if (element.getTreeParent() == null) {
      return 0;
    }
    return getIndentInner(indentHelper, file, element.getTreeParent(), includeNonSpace, recursionLevel + 1);
  }
}