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

The following examples show how to use com.intellij.lang.ASTNode#getChildren() . 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 void collectDescriptorsRecursively(@NotNull ASTNode node,
                                                  @NotNull Document document,
                                                  @NotNull List<FoldingDescriptor> descriptors) {
    final IElementType type = node.getElementType();
    if (type == token(COMMENT)
            && spanMultipleLines(node, document)) {
        descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
    }
    if (type == token(LINE_COMMENT)) {
        final Couple<PsiElement> commentRange = expandLineCommentsRange(node.getPsi());
        final int startOffset = commentRange.getFirst().getTextRange().getStartOffset();
        final int endOffset = commentRange.getSecond().getTextRange().getEndOffset();
        if (document.getLineNumber(startOffset) != document.getLineNumber(endOffset)) {
            descriptors.add(new FoldingDescriptor(node, new TextRange(startOffset, endOffset)));
        }
    }
    if (PROVIDERS.keySet().contains(type)
            && spanMultipleLines(node, document)) {
        descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
    }
    for (ASTNode child : node.getChildren(null)) {
        collectDescriptorsRecursively(child, document, descriptors);
    }
}
 
Example 2
Source File: ASTNodeWalker.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
/**
 * Walk the AST tree, calling the given lambda for each node.
 * Parent nodes are processed first, then their children.
 *
 * @param tree Parent AST node that we want to visit.
 * @param lambda Visitor code.  Return true to keep processing; false, to stop.
 * @return the return value of the last lambda processed.
 */
public boolean walk(ASTNode tree, Lambda<ASTNode> lambda) {
  if (null == tree) return true;

  if (!lambda.process(tree)) {
    return false;
  }

  ASTNode[] children = tree.getChildren(null); // 2016 and later: TokenSet.ANY
  for (ASTNode child : children) {
    if (!walk(child, lambda)) {
      return false;
    }
  }
  return true;
}
 
Example 3
Source File: WeaveFolding.java    From mule-intellij-plugins with Apache License 2.0 5 votes vote down vote up
private static void collectDescriptorsRecursively(@NotNull ASTNode node,
                                                  @NotNull Document document,
                                                  @NotNull List<FoldingDescriptor> descriptors) {
    final IElementType type = node.getElementType();
    if ((isObject(type) || isArray(type)) && spanMultipleLines(node, document)) {
        descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
    }
    for (ASTNode child : node.getChildren(null)) {
        collectDescriptorsRecursively(child, document, descriptors);
    }
}
 
Example 4
Source File: Util.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Find all elements for given node that are keywords.
 */
public static Collection<PsiElement> findKeywords(ASTNode parent) {
    ASTNode[] keywords = parent.getChildren(ProtoParserDefinition.KEYWORDS);
    List<PsiElement> result = new ArrayList<>(keywords.length);
    for (ASTNode keyword : keywords) {
        result.add((PsiElement) keyword);
    }
    return result;
}
 
Example 5
Source File: GraphQLFoldingBuilder.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
private static void buildFolding(ASTNode node, List<FoldingDescriptor> list) {
    boolean isBlock = GraphQLBlock.INDENT_PARENTS.contains(node.getElementType());
    if(!isBlock && GraphQLElementTypes.QUOTED_STRING.equals(node.getElementType())) {
        // triple quoted multi-line strings should support folding
        ASTNode quote = node.findChildByType(GraphQLElementTypes.OPEN_QUOTE);
        isBlock = quote != null && quote.getTextLength() == 3;
    }
    if (isBlock && !node.getTextRange().isEmpty()) {
        final TextRange range = node.getTextRange();
        list.add(new FoldingDescriptor(node, range));
    }
    for (ASTNode child : node.getChildren(null)) {
        buildFolding(child, list);
    }
}
 
Example 6
Source File: IgnoreHLint.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
@Override
public void invoke(@NotNull final Project project, Editor editor, final PsiFile file) throws IncorrectOperationException {
    PsiWhiteSpace newline = HaskellElementFactory.createNewLine(project);
    HaskellPpragma ppragma = HaskellElementFactory.createPpragmaFromText(
            project, "{-# ANN module (\"HLint: ignore " + hint + "\"::String) #-}");
    FileASTNode fileNode = file.getNode();

    ASTNode[] nodes;
    ASTNode node;

    // If the user has imports, place the pragma after them.
    node = fileNode.findChildByType(HaskellTypes.BODY);
    if (node != null) {
        nodes = node.getChildren(TokenSet.create(HaskellTypes.IMPDECL));
        if (nodes.length > 0) {
            PsiElement element = node.getPsi();
            element.addBefore(newline, element.addAfter(ppragma, nodes[nodes.length - 1].getPsi()));
            return;
        }
    }

    // If the user has a module declaration, place the pragma after it.
    node = fileNode.findChildByType(HaskellTypes.MODULEDECL);
    if (node != null) {
        file.addBefore(newline, file.addAfter(ppragma, node.getPsi()));
        return;
    }

    // If the user has any existing pragmas, place the pragma after them.
    nodes = fileNode.getChildren(TokenSet.create(HaskellTypes.PPRAGMA));
    if (nodes.length > 0) {
        file.addBefore(newline, file.addAfter(ppragma, nodes[nodes.length - 1].getPsi()));
        return;
    }

    // Otherwise, just insert the pragma at the top of the file.
    file.addAfter(newline, file.addBefore(ppragma, file.getFirstChild()));
}
 
Example 7
Source File: ConceptFoldingBuilder.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull ASTNode astNode, @NotNull Document document) {
    List<FoldingDescriptor> descriptors = new ArrayList<>();
    for (ASTNode node : astNode.getChildren(TokenSet.create(ConceptTokenTypes.CONCEPT)))
        addNode(descriptors, node, node.findChildByType(ConceptTokenTypes.CONCEPT_HEADING));
    return descriptors.toArray(new FoldingDescriptor[0]);
}
 
Example 8
Source File: DebugUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void checkParentChildConsistent(@Nonnull ASTNode element) {
  ASTNode treeParent = element.getTreeParent();
  if (treeParent == null) return;
  ASTNode[] elements = treeParent.getChildren(null);
  if (ArrayUtil.find(elements, element) == -1) {
    throw new IncorrectTreeStructureException(element, "child cannot be found among parents children");
  }
  //LOG.debug("checked consistence: "+System.identityHashCode(element));
}
 
Example 9
Source File: BashBlockGenerator.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
private static ASTNode[] getBashChildren(final ASTNode node) {
    return node.getChildren(null);
}
 
Example 10
Source File: SpecFoldingBuilder.java    From Intellij-Plugin with Apache License 2.0 4 votes vote down vote up
private void addNodes(@NotNull ASTNode astNode, List<FoldingDescriptor> descriptors, IElementType pNode, IElementType cNode) {
    for (ASTNode node : astNode.getChildren(TokenSet.create(pNode)))
        addNode(descriptors, node, node.findChildByType(cNode));
}
 
Example 11
Source File: MakefileLanguage.java    From CppTools with Apache License 2.0 4 votes vote down vote up
@Nullable
public StructureViewBuilder getStructureViewBuilder(final PsiFile psiFile) {
  return new TreeBasedStructureViewBuilder() {
    @NotNull
    @Override
    public StructureViewModel createStructureViewModel(@Nullable Editor editor) {
      return createStructureViewModel(); // todo, new api Idea 14.1
    }

    @NotNull
    public StructureViewModel createStructureViewModel() {
      return new TextEditorBasedStructureViewModel(psiFile) {
        protected PsiFile getPsiFile()   // TODO: change abstract method to constructor parameter?
        {
          return psiFile;
        }

        @NotNull
        public StructureViewTreeElement getRoot() {
          return new PsiTreeElementBase<PsiElement>(psiFile) {
            @NotNull
            public Collection<StructureViewTreeElement> getChildrenBase() {
              final List<StructureViewTreeElement> children = new ArrayList<StructureViewTreeElement>();
              for(PsiElement el:psiFile.getChildren()) {
                final ASTNode node = el.getNode();

                if(node.getElementType() == MakefileTokenTypes.STATEMENT) {
                  for(final ASTNode el2:node.getChildren(null)) {
                    if (el2.getElementType() == MakefileTokenTypes.TARGET_IDENTIFIER) {
                      children.add(new PsiTreeElementBase(el2.getPsi()) {
                        public void navigate(boolean b) {
                          final Navigatable descriptor = EditSourceUtil.getDescriptor(el2.getPsi());
                          if (descriptor != null) descriptor.navigate(b);
                        }

                        public boolean canNavigate() {
                          return true;
                        }

                        public boolean canNavigateToSource() {
                          return canNavigate();
                        }

                        public Collection getChildrenBase() {
                          return Collections.emptyList();
                        }

                        public String getPresentableText() {
                          return el2.getText();
                        }
                      });
                    }
                  }
                }
              }
              return children;
            }

            public String getPresentableText() {
              return "root";
            }
          };
        }

        @NotNull
        public Grouper[] getGroupers() {
          return new Grouper[0];
        }

        @NotNull
        public Sorter[] getSorters() {
          return new Sorter[] {Sorter.ALPHA_SORTER};
        }

        @NotNull
        public Filter[] getFilters() {
          return new Filter[0];
        }

        @NotNull
        protected Class[] getSuitableClasses()
        {
          return new Class[0];
        }
      };
    }
  };
}