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

The following examples show how to use com.intellij.lang.ASTNode#getLastChildNode() . 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: FormatterUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static ASTNode getPreviousLeaf(@Nullable ASTNode node, @Nonnull IElementType... typesToIgnore) {
  ASTNode prev = getPrevious(node, typesToIgnore);
  if (prev == null) {
    return null;
  }

  ASTNode result = prev;
  ASTNode lastChild = prev.getLastChildNode();
  while (lastChild != null) {
    result = lastChild;
    lastChild = lastChild.getLastChildNode();
  }

  for (IElementType type : typesToIgnore) {
    if (result.getElementType() == type) {
      return getPreviousLeaf(result, typesToIgnore);
    }
  }
  return result;
}
 
Example 2
Source File: BuildFileFoldingBuilder.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static ASTNode getLastChildRecursively(ASTNode node) {
  ASTNode child = node;
  while (true) {
    ASTNode newChild = child.getLastChildNode();
    if (newChild == null) {
      return child;
    }
    child = newChild;
  }
}
 
Example 3
Source File: GraphQLFoldingBuilder.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public String getPlaceholderText(@NotNull ASTNode node) {
    final ASTNode first = node.getFirstChildNode();
    final ASTNode last = node.getLastChildNode();
    if (first != null && last != null) {
        return first.getText() + "..." + last.getText();
    }
    return "{...}";
}
 
Example 4
Source File: BashPsiUtils.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the deepest nested ast node which still covers the same part of the file as the parent node. Happens if a single leaf node is
 * contained in several composite parent nodes of the same range, e.g. a var in a combined word.
 *
 * @param parent The element to use as the startin point
 * @return The deepest node inside of parent which covers the same range or (if none exists) the input element
 */
@NotNull
public static ASTNode getDeepestEquivalent(ASTNode parent) {
    ASTNode element = parent;
    while (element.getFirstChildNode() != null && element.getFirstChildNode() == element.getLastChildNode() && element.getTextRange().equals(parent.getTextRange())) {
        element = element.getFirstChildNode();
    }

    return element;
}
 
Example 5
Source File: BashStringImpl.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Override
public String createEquallyWrappedString(String newContent) {
    ASTNode node = getNode();
    ASTNode firstChild = node.getFirstChildNode();
    ASTNode lastChild = node.getLastChildNode();

    StringBuilder result = new StringBuilder(firstChild.getTextLength() + newContent.length() + lastChild.getTextLength());
    return result.append(firstChild.getText()).append(newContent).append(lastChild.getText()).toString();
}
 
Example 6
Source File: BashBlock.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
/**
 * @param node Tree node
 * @return true if node is incomplete
 */
public boolean isIncomplete(@NotNull final ASTNode node) {
    if (node.getElementType() instanceof ILazyParseableElementType) {
        return false;
    }
    ASTNode lastChild = node.getLastChildNode();
    while (lastChild != null &&
            !(lastChild.getElementType() instanceof ILazyParseableElementType) &&
            (lastChild.getPsi() instanceof PsiWhiteSpace || lastChild.getPsi() instanceof PsiComment)) {
        lastChild = lastChild.getTreePrev();
    }
    return lastChild != null && (lastChild.getPsi() instanceof PsiErrorElement || isIncomplete(lastChild));
}
 
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: DefaultStubBuilder.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void pushChildren(ASTNode node, boolean hasStub, StubElement stub) {
  for (ASTNode childNode = node.getLastChildNode(); childNode != null; childNode = childNode.getTreePrev()) {
    if (!skipChildProcessingWhenBuildingStubs(node, childNode)) {
      parentNodes.push(childNode);
      parentStubs.push(stub);
      parentNodesStubbed.push(hasStub);
    }
  }
}
 
Example 9
Source File: TreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ASTNode findChildBackward(ASTNode parent, IElementType type) {
  if (DebugUtil.CHECK_INSIDE_ATOMIC_ACTION_ENABLED) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
  }
  for (ASTNode element = parent.getLastChildNode(); element != null; element = element.getTreePrev()) {
    if (element.getElementType() == type) return element;
  }
  return null;
}
 
Example 10
Source File: TreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static ASTNode findLastLeaf(ASTNode element, boolean expandChameleons) {
  if (element instanceof LeafElement || !expandChameleons && isCollapsedChameleon(element)) {
    return element;
  }
  for (ASTNode child = element.getLastChildNode(); child != null; child = child.getTreePrev()) {
    ASTNode leaf = findLastLeaf(child);
    if (leaf != null) return leaf;
  }
  return null;
}
 
Example 11
Source File: TreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ASTNode getLastChild(ASTNode element) {
  ASTNode child = element;
  while (child != null) {
    element = child;
    child = element.getLastChildNode();
  }
  return element;
}
 
Example 12
Source File: FormatterUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean isIncomplete(@Nullable ASTNode node) {
  ASTNode lastChild = node == null ? null : node.getLastChildNode();
  while (lastChild != null && lastChild.getElementType() == TokenType.WHITE_SPACE) {
    lastChild = lastChild.getTreePrev();
  }
  if (lastChild == null) return false;
  if (lastChild.getElementType() == TokenType.ERROR_ELEMENT) return true;
  return isIncomplete(lastChild);
}
 
Example 13
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 14
Source File: HaxeFoldingBuilder.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
private static FoldingDescriptor buildCodeBlockFolding(@NotNull ASTNode node) {
  final ASTNode openBrace = node.getFirstChildNode();
  final ASTNode closeBrace = node.getLastChildNode();

  return buildBlockFolding(node, openBrace, closeBrace);
}
 
Example 15
Source File: SyntaxTraverser.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public ASTNode last(@Nonnull ASTNode node) {
  return node.getLastChildNode();
}
 
Example 16
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);
  }
}