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

The following examples show how to use com.intellij.lang.ASTNode#getTreeNext() . 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: SharedImplUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void acceptChildren(PsiElementVisitor visitor, ASTNode root) {
  ASTNode childNode = root.getFirstChildNode();

  while (childNode != null) {
    final PsiElement psi;
    if (childNode instanceof PsiElement) {
      psi = (PsiElement)childNode;
    }
    else {
      psi = childNode.getPsi();
    }
    psi.accept(visitor);

    childNode = childNode.getTreeNext();
  }
}
 
Example 2
Source File: FormatterUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private static ASTNode getNextOrPrevious(@Nullable ASTNode node, boolean isNext, @Nonnull IElementType... typesToIgnore) {
  if (node == null) return null;

  ASTNode each = isNext ? node.getTreeNext() : node.getTreePrev();
  ASTNode parent = node.getTreeParent();
  while (each == null && parent != null) {
    each = isNext ? parent.getTreeNext() : parent.getTreePrev();
    parent = parent.getTreeParent();
  }

  if (each == null) {
    return null;
  }

  for (IElementType type : typesToIgnore) {
    if (each.getElementType() == type) {
      return getNextOrPrevious(each, isNext, typesToIgnore);
    }
  }

  return each;
}
 
Example 3
Source File: ProtoFileBlock.java    From protobuf-jetbrains-plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected List<Block> buildChildren() {
    List<Block> blocks = new ArrayList<>();
    ASTNode child = myNode.getFirstChildNode();
    while (child != null) {
        if (!FormatterUtil.containsWhiteSpacesOnly(child)) {
            IElementType elementType = child.getElementType();
            if (ProtoParserDefinition.rule(ProtoParser.RULE_proto).equals(elementType)) {
                appendProtoBlocks(child, blocks);
            } else {
                // Comments are not part of root rule, we have to append them separately
                blocks.add(new LeafBlock(child, Alignment.createAlignment(), Indent.getNoneIndent(), settings));
            }
        }
        child = child.getTreeNext();
    }
    return blocks;
}
 
Example 4
Source File: ASTDelegatePsiElement.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
@Nonnull
protected <T extends PsiElement> List<T> findChildrenByType(TokenSet elementType) {
  List<T> result = EMPTY;
  ASTNode child = getNode().getFirstChildNode();
  while (child != null) {
    final IElementType tt = child.getElementType();
    if (elementType.contains(tt)) {
      if (result == EMPTY) {
        result = new ArrayList<T>();
      }
      result.add((T)child.getPsi());
    }
    child = child.getTreeNext();
  }
  return result;
}
 
Example 5
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 6
Source File: GLSLFoldingBuilder.java    From glsl4idea with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void appendDescriptors(final ASTNode node, final List<FoldingDescriptor> descriptors) {
    IElementType type = node.getElementType();

    final TextRange textRange = node.getTextRange();
    //Don't add folding to 0-length nodes, crashes in new FoldingDescriptor
    if(textRange.getLength() <= 0)return;

    if (type == GLSLTokenTypes.COMMENT_BLOCK || type == GLSLElementTypes.COMPOUND_STATEMENT) {
        descriptors.add(new FoldingDescriptor(node, textRange));
    }

    ASTNode child = node.getFirstChildNode();
    while (child != null) {
        appendDescriptors(child, descriptors);
        child = child.getTreeNext();
    }
}
 
Example 7
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 8
Source File: PsiUtil.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * Gets the closest next sibling, where the type is not skip, relative to node
 *
 * @param node node to find sibling of
 * @param skip the token to skip
 * @return non-skip sibling, or null if none was found
 */
@Nullable
public static ASTNode getNextSiblingNotType(@NotNull ASTNode node, @NotNull IElementType skip) {
	ASTNode sibling = node.getTreeNext();
	while (sibling != null) {
		if (sibling.getElementType() == skip) {
			sibling = sibling.getTreeNext();
		} else {
			break;
		}
	}
	return sibling;
}
 
Example 9
Source File: FormatterUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean isFollowedBy(@Nullable ASTNode node, IElementType expectedType, IElementType... skipTypes) {
  ASTNode nextNode = node == null ? null : node.getTreeNext();
  while (nextNode != null && (isWhitespaceOrEmpty(nextNode) || isOneOf(nextNode, skipTypes))) {
    nextNode = nextNode.getTreeNext();
  }
  if (nextNode == null) return false;
  return nextNode.getElementType() == expectedType;
}
 
Example 10
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 11
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 12
Source File: CompositeElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * @return First element that was appended (for example whitespaces could be skipped)
 */
public TreeElement addInternal(TreeElement first, ASTNode last, @Nullable ASTNode anchor, @Nullable Boolean before) {
  ASTNode anchorBefore;
  if (anchor == null) {
    anchorBefore = before == null || before.booleanValue() ? null : getFirstChildNode();
  }
  else {
    anchorBefore = before.booleanValue() ? anchor : anchor.getTreeNext();
  }
  return (TreeElement)CodeEditUtil.addChildren(this, first, last, anchorBefore);
}
 
Example 13
Source File: TreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ASTNode skipElements(ASTNode element, TokenSet types) {
  while (true) {
    if (element == null) return null;
    if (!types.contains(element.getElementType())) break;
    element = element.getTreeNext();
  }
  return element;
}
 
Example 14
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 15
Source File: ProtoFileBlock.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
private void appendProtoBlocks(ASTNode protoRootNode, List<Block> blocks) {
    ASTNode child = protoRootNode.getFirstChildNode();
    Alignment alignment = Alignment.createAlignment();
    while (child != null) {
        if (!FormatterUtil.containsWhiteSpacesOnly(child)) {
            Block block = createBlock(child, alignment, Indent.getNoneIndent(), settings);
            blocks.add(block);
        }
        child = child.getTreeNext();
    }
}
 
Example 16
Source File: TreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ASTNode findSibling(ASTNode start, IElementType elementType) {
  ASTNode child = start;
  while (true) {
    if (child == null) return null;
    if (child.getElementType() == elementType) return child;
    child = child.getTreeNext();
  }
}
 
Example 17
Source File: SyntaxTraverser.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public ASTNode next(@Nonnull ASTNode node) {
  return node.getTreeNext();
}
 
Example 18
Source File: AlignmentInColumnsHelper.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Allows to answer if given node should be aligned to the previous node of the same type according to the given alignment config
 * assuming that given node is a variable declaration.
 *
 * @param node                         target node which alignment strategy is to be defined
 * @param config                       alignment config to use for processing
 * @param blankLinesToBeKeptOnReformat corresponding KEEP_LINE_IN_* formatting setting
 * @return <code>true</code> if given node should be aligned to the previous one; <code>false</code> otherwise
 */
@SuppressWarnings({"MethodMayBeStatic"})
public boolean useDifferentVarDeclarationAlignment(ASTNode node, AlignmentInColumnsConfig config, int blankLinesToBeKeptOnReformat) {
  ASTNode prev = getPreviousAdjacentNodeOfTargetType(node, config, blankLinesToBeKeptOnReformat);
  if (prev == null) {
    return true;
  }

  ASTNode curr = deriveNodeOfTargetType(node, TokenSet.create(prev.getElementType()));
  if (curr == null) {
    return true;
  }

  // The main idea is to avoid alignment like the one below:
  //     private final int    i;
  //                   double d;
  // I.e. we want to avoid alignment-implied long indents from the start of line.
  // Please note that we do allow alignment like below:
  //     private final int    i;
  //     private       double d;
  ASTNode prevSubNode = getSubNodeThatStartsNewLine(prev.getFirstChildNode(), config);
  ASTNode currSubNode = getSubNodeThatStartsNewLine(curr.getFirstChildNode(), config);
  while (true) {
    boolean prevNodeIsDefined = prevSubNode != null;
    boolean currNodeIsDefined = currSubNode != null;
    // Check if one sub-node starts from new line and another one doesn't start.
    if (prevNodeIsDefined ^ currNodeIsDefined) {
      return true;
    }
    if (prevSubNode == null) {
      break;
    }
    if (prevSubNode.getElementType() != currSubNode.getElementType()
        /*|| StringUtil.countNewLines(prevSubNode.getChars()) != StringUtil.countNewLines(currSubNode.getChars())*/) {
      return true;
    }
    prevSubNode = getSubNodeThatStartsNewLine(prevSubNode.getTreeNext(), config);
    currSubNode = getSubNodeThatStartsNewLine(currSubNode.getTreeNext(), config);
  }

  // There is a possible declaration like the one below
  //     int i1 = 1;
  //     int i2, i3 = 2;
  // Three fields are declared here - 'i1', 'i2' and 'i3'. So, the check if field 'i2' contains assignment should be
  // performed against 'i3'.
  ASTNode currentFieldToUse = curr;
  ASTNode nextNode = curr.getTreeNext();

  for (; nextNode != null && nextNode.getTreeParent() == curr.getTreeParent(); nextNode = nextNode.getTreeNext()) {
    IElementType type = nextNode.getElementType();
    if (config.getWhiteSpaceTokenTypes().contains(type)) {
      ASTNode previous = nextNode.getTreePrev();
      if ((previous != null && previous.getElementType() == curr.getElementType()) || StringUtil.countNewLines(nextNode.getChars()) > 1) {
        break;
      }
      continue;
    }

    if (config.getCommentTokenTypes().contains(type)) {
      continue;
    }

    if (type == curr.getElementType()) {
      currentFieldToUse = nextNode;
    }
  }

  List<IElementType> prevTypes = findSubNodeTypes(prev, config.getDistinguishableTypes());
  List<IElementType> currTypes = findSubNodeTypes(currentFieldToUse, config.getDistinguishableTypes());

  return !prevTypes.equals(currTypes);
}
 
Example 19
Source File: DebugUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static void treeToBuffer(@Nonnull final Appendable buffer,
                                @Nonnull final ASTNode root,
                                final int indent,
                                final boolean skipWhiteSpaces,
                                final boolean showRanges,
                                final boolean showChildrenRanges,
                                final boolean usePsi,
                                PairConsumer<PsiElement, Consumer<PsiElement>> extra) {
  if (skipWhiteSpaces && root.getElementType() == TokenType.WHITE_SPACE) return;

  StringUtil.repeatSymbol(buffer, ' ', indent);
  try {
    PsiElement psiElement = null;
    if (root instanceof CompositeElement) {
      if (usePsi) {
        psiElement = root.getPsi();
        if (psiElement != null) {
          buffer.append(psiElement.toString());
        }
        else {
          buffer.append(root.getElementType().toString());
        }
      }
      else {
        buffer.append(root.toString());
      }
    }
    else {
      final String text = fixWhiteSpaces(root.getText());
      buffer.append(root.toString()).append("('").append(text).append("')");
    }
    if (showRanges) buffer.append(root.getTextRange().toString());
    buffer.append("\n");
    if (root instanceof CompositeElement) {
      ASTNode child = root.getFirstChildNode();

      if (child == null) {
        StringUtil.repeatSymbol(buffer, ' ', indent + 2);
        buffer.append("<empty list>\n");
      }
      else {
        while (child != null) {
          treeToBuffer(buffer, child, indent + 2, skipWhiteSpaces, showChildrenRanges, showChildrenRanges, usePsi, extra);
          child = child.getTreeNext();
        }
      }
    }
    if (psiElement != null && extra != null ) {
      extra.consume(psiElement, new Consumer<PsiElement>() {
        @Override
        public void consume(PsiElement element) {
          treeToBuffer(buffer, element.getNode(), indent + 2, skipWhiteSpaces, showChildrenRanges, showChildrenRanges, usePsi, null);
        }
      });
    }
  }
  catch (IOException e) {
    LOG.error(e);
  }
}
 
Example 20
Source File: RecursiveTreeElementWalkingVisitor.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public ASTNode getNextSibling(@Nonnull ASTNode element) {
  return element.getTreeNext();
}