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

The following examples show how to use com.intellij.lang.ASTNode#getFirstChildNode() . 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: BlockSupportImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static boolean isReplaceWholeNode(@Nonnull PsiFileImpl fileImpl, @Nonnull ASTNode newRoot) throws ReparsedSuccessfullyException {
  final Boolean data = fileImpl.getUserData(DO_NOT_REPARSE_INCREMENTALLY);
  if (data != null) fileImpl.putUserData(DO_NOT_REPARSE_INCREMENTALLY, null);

  boolean explicitlyMarkedDeep = Boolean.TRUE.equals(data);

  if (explicitlyMarkedDeep || isTooDeep(fileImpl)) {
    return true;
  }

  final ASTNode childNode = newRoot.getFirstChildNode();  // maybe reparsed in PsiBuilderImpl and have thrown exception here
  boolean childTooDeep = isTooDeep(childNode);
  if (childTooDeep) {
    childNode.putUserData(TREE_DEPTH_LIMIT_EXCEEDED, null);
    fileImpl.putUserData(TREE_DEPTH_LIMIT_EXCEEDED, Boolean.TRUE);
  }
  return childTooDeep;
}
 
Example 2
Source File: GLSLTypename.java    From glsl4idea with GNU Lesser General Public License v3.0 6 votes vote down vote up
@NotNull
public GLSLType getType() {
    final ASTNode node = getNode();
    final IElementType type = node.getElementType();

    if (type == GLSLElementTypes.TYPE_SPECIFIER_STRUCT_REFERENCE) {
        GLSLStructDefinition def = getTypeDefinition();
        if (def != null) {
            return def.getType();
        } else {
            // TODO: Check built-in structures
            return GLSLTypes.getUndefinedType(getText());
        }
    }

    if (type == GLSLElementTypes.TYPE_SPECIFIER_PRIMITIVE) {
        final ASTNode child = node.getFirstChildNode();
        GLSLType t = GLSLTypes.getTypeFromName(child.getText());
        if (t != null) return t;
        return GLSLTypes.UNKNOWN_TYPE;
    }

    Logger.getLogger("GLSLTypename").warning("Unknown element type: '" + type + "'");
    return GLSLTypes.UNKNOWN_TYPE;
}
 
Example 3
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 4
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 5
Source File: IndentationFoldingBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
private void collectDescriptors(@Nonnull final ASTNode node, @Nonnull final List<FoldingDescriptor> descriptors) {
  final Queue<ASTNode> toProcess = new LinkedList<ASTNode>();
  toProcess.add(node);
  while (!toProcess.isEmpty()) {
    final ASTNode current = toProcess.remove();
    if (current.getTreeParent() != null
        && current.getTextLength() > 1
        && myTokenSet.contains(current.getElementType()))
    {
      descriptors.add(new FoldingDescriptor(current, current.getTextRange()));
    }
    for (ASTNode child = current.getFirstChildNode(); child != null; child = child.getTreeNext()) {
      toProcess.add(child);
    }
  }
}
 
Example 6
Source File: AlignmentInColumnsHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static List<IElementType> findSubNodeTypes(ASTNode node, TokenSet types) {
  List<IElementType> foundTypes = new SmartList<IElementType>();
  for (ASTNode child = node.getFirstChildNode(); child != null && child.getTreeParent() == node; child = child.getTreeNext()) {
    IElementType type = child.getElementType();
    if (types.contains(type)) {
      foundTypes.add(type);
    }
  }
  return foundTypes;
}
 
Example 7
Source File: CsvFormatHelper.java    From intellij-csv-validator with Apache License 2.0 5 votes vote down vote up
public static Map<Integer, CsvColumnInfo<ASTNode>> createColumnInfoMap(ASTNode root, CodeStyleSettings settings) {
    Map<Integer, CsvColumnInfo<ASTNode>> columnInfoMap = new HashMap<>();
    ASTNode child = root.getFirstChildNode();
    int row = 0;
    while (child != null) {
        if (child.getElementType() == CsvTypes.RECORD) {
            Integer column = 0;
            ASTNode subChild = child.getFirstChildNode();
            while (subChild != null) {
                if (subChild.getElementType() == CsvTypes.FIELD) {
                    int length = getTextLength(subChild, settings);
                    if (!columnInfoMap.containsKey(column)) {
                        columnInfoMap.put(column, new CsvColumnInfo(column, length, row));
                    } else if (columnInfoMap.get(column).getMaxLength() < length) {
                        columnInfoMap.get(column).setMaxLength(length, row);
                    }
                    columnInfoMap.get(column).addElement(subChild);
                    ++column;
                }
                subChild = subChild.getTreeNext();
            }
            ++row;
        }
        child = child.getTreeNext();
    }
    return columnInfoMap;
}
 
Example 8
Source File: CustomFoldingBuilder.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
private void addCustomFoldingRegionsRecursively(@Nonnull FoldingStack foldingStack,
                                                @Nonnull ASTNode node,
                                                @Nonnull List<FoldingDescriptor> descriptors,
                                                int currDepth) {
  FoldingStack localFoldingStack = isCustomFoldingRoot(node) ? new FoldingStack(node) : foldingStack;
  for (ASTNode child = node.getFirstChildNode(); child != null; child = child.getTreeNext()) {
    if (isCustomRegionStart(child)) {
      localFoldingStack.push(child);
    }
    else if (isCustomRegionEnd(child)) {
      if (!localFoldingStack.isEmpty()) {
        ASTNode startNode = localFoldingStack.pop();
        int startOffset = startNode.getTextRange().getStartOffset();
        TextRange range = new TextRange(startOffset, child.getTextRange().getEndOffset());
        descriptors.add(new FoldingDescriptor(startNode, range));
        Set<ASTNode> nodeSet = ourCustomRegionElements.get();
        nodeSet.add(startNode);
        nodeSet.add(child);
      }
    }
    else {
      if (currDepth < myMaxLookupDepth.asInteger()) {
        addCustomFoldingRegionsRecursively(localFoldingStack, child, descriptors, currDepth + 1);
      }
    }
  }
}
 
Example 9
Source File: BashWordImpl.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
public boolean isWrappable() {
    if (isSingleChildParent()) {
        return false;
    }

    ASTNode node = getNode();
    for (ASTNode child = node.getFirstChildNode(); child != null; child = child.getTreeNext()) {
        if (nonWrappableChilds.contains(child.getElementType())) {
            return false;
        }
    }

    return true;
}
 
Example 10
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 11
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 12
Source File: CodeEditUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void checkForOuters(ASTNode element) {
  if (element instanceof OuterLanguageElement && element.getCopyableUserData(OUTER_OK) == null) {
    throw new IllegalArgumentException("Outer element " + element + " is not allowed here");
  }

  ASTNode child = element.getFirstChildNode();
  while (child != null) {
    checkForOuters(child);
    child = child.getTreeNext();
  }
}
 
Example 13
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 14
Source File: BlockSupportImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static DiffLog mergeTrees(@Nonnull final PsiFileImpl fileImpl,
                                 @Nonnull final ASTNode oldRoot,
                                 @Nonnull final ASTNode newRoot,
                                 @Nonnull ProgressIndicator indicator,
                                 @Nonnull CharSequence lastCommittedText) {
  PsiUtilCore.ensureValid(fileImpl);
  if (newRoot instanceof FileElement) {
    FileElement fileImplElement = fileImpl.getTreeElement();
    if (fileImplElement != null) {
      ((FileElement)newRoot).setCharTable(fileImplElement.getCharTable());
    }
  }

  try {
    newRoot.putUserData(TREE_TO_BE_REPARSED, Pair.create(oldRoot, lastCommittedText));
    if (isReplaceWholeNode(fileImpl, newRoot)) {
      DiffLog treeChangeEvent = replaceElementWithEvents(oldRoot, newRoot);
      fileImpl.putUserData(TREE_DEPTH_LIMIT_EXCEEDED, Boolean.TRUE);

      return treeChangeEvent;
    }
    newRoot.getFirstChildNode();  // maybe reparsed in PsiBuilderImpl and have thrown exception here
  }
  catch (ReparsedSuccessfullyException e) {
    // reparsed in PsiBuilderImpl
    return e.getDiffLog();
  }
  finally {
    newRoot.putUserData(TREE_TO_BE_REPARSED, null);
  }

  final ASTShallowComparator comparator = new ASTShallowComparator(indicator);
  final ASTStructure treeStructure = createInterruptibleASTStructure(newRoot, indicator);

  DiffLog diffLog = new DiffLog();
  diffTrees(oldRoot, diffLog, comparator, treeStructure, indicator, lastCommittedText);
  return diffLog;
}
 
Example 15
Source File: TraverseUtil.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 5 votes vote down vote up
public static void collectAstNodesByType(List<ASTNode> nodes, ASTNode rootNode, IElementType type) {
    if (rootNode.getElementType().equals(type)) {
        nodes.add(rootNode);
    }
    for (ASTNode element = rootNode.getFirstChildNode(); element != null; element = element.getTreeNext()) {
        collectAstNodesByType(nodes, element, type);
    }
}
 
Example 16
Source File: TraverseUtil.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 5 votes vote down vote up
public static ASTNode findFirstDeepChildByType(ASTNode root, IElementType type) {
    if (root.getElementType().equals(type)) {
        return root;
    }

    for (ASTNode element = root.getFirstChildNode(); element != null; element = element.getTreeNext()) {
        ASTNode child = findFirstDeepChildByType(element, type);
        if (child != null) {
            return child;
        }
    }

    return null;
}
 
Example 17
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 18
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 19
Source File: RecursiveTreeElementWalkingVisitor.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public ASTNode getFirstChild(@Nonnull ASTNode element) {
  return element.getFirstChildNode();
}
 
Example 20
Source File: SyntaxTraverser.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public ASTNode first(@Nonnull ASTNode node) {
  return node.getFirstChildNode();
}