Java Code Examples for com.intellij.util.diff.FlyweightCapableTreeStructure#getChildren()

The following examples show how to use com.intellij.util.diff.FlyweightCapableTreeStructure#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: SyntaxTraverser.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public JBIterable<? extends T> children(@Nonnull final T node) {
  return new JBIterable<T>() {
    @Override
    public Iterator<T> iterator() {
      FlyweightCapableTreeStructure<T> structure = getStructure();
      Ref<T[]> ref = Ref.create();
      int count = structure.getChildren(node, ref);
      if (count == 0) return ContainerUtil.emptyIterator();
      T[] array = ref.get();
      LinkedList<T> list = ContainerUtil.newLinkedList();
      for (int i = 0; i < count; i++) {
        T child = array[i];
        IElementType childType = typeOf(child);
        // tokens and errors getParent() == null
        if (childType == TokenType.WHITE_SPACE || childType == TokenType.BAD_CHARACTER) {
          continue;
        }
        array[i] = null; // do not dispose meaningful TokenNodes
        list.addLast(child);
      }
      structure.disposeChildren(array, count);
      return list.iterator();
    }
  };
}
 
Example 2
Source File: PsiBuilderImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public int getChildren(@Nonnull final LighterASTNode item, @Nonnull final Ref<LighterASTNode[]> into) {
  if (item instanceof LazyParseableToken) {
    final FlyweightCapableTreeStructure<LighterASTNode> tree = ((LazyParseableToken)item).parseContents();
    final LighterASTNode root = tree.getRoot();
    if (root instanceof ProductionMarker) {
      ((ProductionMarker)root).myParent = ((Token)item).myParentNode;
    }
    return tree.getChildren(tree.prepareForGetChildren(root), into);  // todo: set offset shift for kids?
  }

  if (item instanceof Token || item instanceof ErrorItem) return 0;
  StartMarker marker = (StartMarker)item;

  count = 0;
  ProductionMarker child = marker.myFirstChild;
  int lexIndex = marker.myLexemeIndex;
  while (child != null) {
    lexIndex = insertLeaves(lexIndex, child.myLexemeIndex, marker.myBuilder, marker);

    if (child instanceof StartMarker && ((StartMarker)child).myDoneMarker.myCollapse) {
      int lastIndex = ((StartMarker)child).myDoneMarker.myLexemeIndex;
      insertLeaf(child.getTokenType(), marker.myBuilder, child.myLexemeIndex, lastIndex, true, marker);
    }
    else {
      ensureCapacity();
      nodes[count++] = child;
    }

    if (child instanceof StartMarker) {
      lexIndex = ((StartMarker)child).myDoneMarker.myLexemeIndex;
    }
    child = child.myNext;
  }

  insertLeaves(lexIndex, marker.myDoneMarker.myLexemeIndex, marker.myBuilder, marker);
  into.set(nodes == null ? LighterASTNode.EMPTY_ARRAY : nodes);
  nodes = null;

  return count;
}
 
Example 3
Source File: DebugUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static void lightTreeToBuffer(@Nonnull final FlyweightCapableTreeStructure<LighterASTNode> tree,
                                     @Nonnull final LighterASTNode node,
                                     @Nonnull final Appendable buffer,
                                     final int indent,
                                     final boolean skipWhiteSpaces) {
  final IElementType tokenType = node.getTokenType();
  if (skipWhiteSpaces && tokenType == TokenType.WHITE_SPACE) return;

  final boolean isLeaf = (node instanceof LighterASTTokenNode);

  StringUtil.repeatSymbol(buffer, ' ', indent);
  try {
    if (tokenType == TokenType.ERROR_ELEMENT) {
      buffer.append("PsiErrorElement:").append(PsiBuilderImpl.getErrorMessage(node));
    }
    else if (tokenType == TokenType.WHITE_SPACE) {
      buffer.append("PsiWhiteSpace");
    }
    else {
      buffer.append(isLeaf ? "PsiElement" : "Element").append('(').append(tokenType.toString()).append(')');
    }

    if (isLeaf) {
      final String text = ((LighterASTTokenNode)node).getText().toString();
      buffer.append("('").append(fixWhiteSpaces(text)).append("')");
    }
    buffer.append('\n');

    if (!isLeaf) {
      final Ref<LighterASTNode[]> kids = new Ref<LighterASTNode[]>();
      final int numKids = tree.getChildren(tree.prepareForGetChildren(node), kids);
      if (numKids == 0) {
        StringUtil.repeatSymbol(buffer, ' ', indent + 2);
        buffer.append("<empty list>\n");
      }
      else {
        for (int i = 0; i < numKids; i++) {
          lightTreeToBuffer(tree, kids.get()[i], buffer, indent + 2, skipWhiteSpaces);
        }
      }
    }
  }
  catch (IOException e) {
    LOG.error(e);
  }
}