com.intellij.lang.ASTFactory Java Examples

The following examples show how to use com.intellij.lang.ASTFactory. 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: PsiParserFacadeImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public PsiElement createWhiteSpaceFromText(@Nonnull @NonNls String text) throws IncorrectOperationException {
  final FileElement holderElement = DummyHolderFactory.createHolder(myManager, null).getTreeElement();
  final LeafElement newElement = ASTFactory.leaf(TokenType.WHITE_SPACE, holderElement.getCharTable().intern(text));
  holderElement.rawAddChildren(newElement);
  GeneratedMarkerVisitor.markGenerated(newElement.getPsi());
  return newElement.getPsi();
}
 
Example #2
Source File: Factory.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static LeafElement createSingleLeafElement(@Nonnull IElementType type, CharSequence buffer, int startOffset, int endOffset, CharTable table, PsiManager manager, PsiFile originalFile) {
  DummyHolder dummyHolder = DummyHolderFactory.createHolder(manager, table, type.getLanguage());
  dummyHolder.setOriginalFile(originalFile);

  FileElement holderElement = dummyHolder.getTreeElement();

  LeafElement newElement = ASTFactory.leaf(type, holderElement.getCharTable().intern(
          buffer, startOffset, endOffset));
  holderElement.rawAddChildren(newElement);
  CodeEditUtil.setNodeGenerated(newElement, true);
  return newElement;
}
 
Example #3
Source File: Factory.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static LeafElement createSingleLeafElement(@Nonnull IElementType type, CharSequence buffer, int startOffset, int endOffset, CharTable table, PsiManager manager, boolean generatedFlag) {
  final FileElement holderElement = DummyHolderFactory.createHolder(manager, table, type.getLanguage()).getTreeElement();
  final LeafElement newElement = ASTFactory.leaf(type, holderElement.getCharTable().intern(
          buffer, startOffset, endOffset));
  holderElement.rawAddChildren(newElement);
  if(generatedFlag) CodeEditUtil.setNodeGenerated(newElement, true);
  return newElement;
}
 
Example #4
Source File: Factory.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static CompositeElement createCompositeElement(@Nonnull IElementType type,
                                                      final CharTable charTableByTree,
                                                      final PsiManager manager) {
  final FileElement treeElement = DummyHolderFactory.createHolder(manager, null, charTableByTree).getTreeElement();
  final CompositeElement composite = ASTFactory.composite(type);
  treeElement.rawAddChildren(composite);
  return composite;
}
 
Example #5
Source File: CompositeElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void addLeaf(@Nonnull final IElementType leafType, @Nonnull CharSequence leafText, final ASTNode anchorBefore) {
  FileElement holder = new DummyHolder(getManager(), null).getTreeElement();
  final LeafElement leaf = ASTFactory.leaf(leafType, holder.getCharTable().intern(leafText));
  CodeEditUtil.setNodeGenerated(leaf, true);
  holder.rawAddChildren(leaf);

  addChild(leaf, anchorBefore);
}
 
Example #6
Source File: LeafElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public LeafElement rawReplaceWithText(@Nonnull String newText) {
  LeafElement newLeaf = ASTFactory.leaf(getElementType(), newText);
  copyUserDataTo(newLeaf);
  rawReplaceWithList(newLeaf);
  newLeaf.clearCaches();
  return newLeaf;
}
 
Example #7
Source File: ChangeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static LeafElement copyLeafWithText(@Nonnull LeafElement original, @Nonnull String text) {
  LeafElement element = ASTFactory.leaf(original.getElementType(), text);
  original.copyCopyableDataTo(element);
  encodeInformation(element, original);
  TreeUtil.clearCaches(element);
  saveIndentationToCopy(original, element);
  return element;
}
 
Example #8
Source File: SimpleTreePatcher.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public LeafElement split(LeafElement leaf, int offset, final CharTable table) {
  final CharSequence chars = leaf.getChars();
  final LeafElement leftPart = ASTFactory.leaf(leaf.getElementType(), table.intern(chars, 0, offset));
  final LeafElement rightPart = ASTFactory.leaf(leaf.getElementType(), table.intern(chars, offset, chars.length()));
  leaf.rawInsertAfterMe(leftPart);
  leftPart.rawInsertAfterMe(rightPart);
  leaf.rawRemove();
  return leftPart;
}
 
Example #9
Source File: FormatterUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void replaceLastWhiteSpace(final ASTNode astNode, final String whiteSpace, final TextRange textRange) {
  ASTNode lastWS = TreeUtil.findLastLeaf(astNode);
  if (lastWS == null) {
    return;
  }
  if (lastWS.getElementType() != TokenType.WHITE_SPACE) {
    lastWS = null;
  }
  if (lastWS != null && !lastWS.getTextRange().equals(textRange)) {
    return;
  }
  if (whiteSpace.isEmpty() && lastWS == null) {
    return;
  }
  if (lastWS != null && whiteSpace.isEmpty()) {
    lastWS.getTreeParent().removeRange(lastWS, null);
    return;
  }

  LeafElement whiteSpaceElement = ASTFactory.whitespace(whiteSpace);

  if (lastWS == null) {
    astNode.addChild(whiteSpaceElement, null);
  }
  else {
    ASTNode treeParent = lastWS.getTreeParent();
    treeParent.replaceChild(lastWS, whiteSpaceElement);
  }
}
 
Example #10
Source File: ParseUtilBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static TreeElement createTokenElement(Lexer lexer, CharTable table) {
  IElementType tokenType = lexer.getTokenType();
  if (tokenType == null) {
    return null;
  }
  else if (tokenType instanceof ILazyParseableElementType) {
    return ASTFactory.lazy((ILazyParseableElementType)tokenType, LexerUtil.internToken(lexer, table));
  }
  else {
    return ASTFactory.leaf(tokenType, LexerUtil.internToken(lexer, table));
  }
}
 
Example #11
Source File: CsvBlock.java    From intellij-csv-validator with Apache License 2.0 4 votes vote down vote up
private void validateBlocks(List<Block> blocks) {
    if (blocks.isEmpty()) {
        return;
    }
    blocks.add(0, new CsvBlockElement(ASTFactory.leaf(DOCUMENT_START, ""), myFormattingInfo));
}
 
Example #12
Source File: PlainTextTokenTypes.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public ASTNode parseContents(ASTNode chameleon) {
  return ASTFactory.leaf(PLAIN_TEXT, chameleon.getChars());
}
 
Example #13
Source File: CodeEditUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
private static ASTNode makePlaceHolderBetweenTokens(ASTNode left, ASTNode right, boolean forceReformat, boolean normalizeTrailingWS) {
  if (right == null) return left;

  markToReformatBefore(right, false);
  if (left == null) {
    markToReformatBefore(right, true);
  }
  else if (left.getElementType() == TokenType.WHITE_SPACE && left.getTreeNext() == null && normalizeTrailingWS) {
    // handle tailing whitespaces if element on the left has been removed
    final ASTNode prevLeaf = TreeUtil.prevLeaf(left);
    left.getTreeParent().removeChild(left);
    markToReformatBeforeOrInsertWhitespace(prevLeaf, right);
    left = right;
  }
  else if (left.getElementType() == TokenType.WHITE_SPACE && right.getElementType() == TokenType.WHITE_SPACE) {
    final String text;
    final int leftBlankLines = getBlankLines(left.getText());
    final int rightBlankLines = getBlankLines(right.getText());
    final boolean leaveRightText = leftBlankLines < rightBlankLines;
    if (leftBlankLines == 0 && rightBlankLines == 0) {
      text = left.getText() + right.getText();
    }
    else if (leaveRightText) {
      text = right.getText();
    }
    else {
      text = left.getText();
    }
    if (leaveRightText || forceReformat) {
      final LeafElement merged = ASTFactory.whitespace(text);
      if (!leaveRightText) {
        left.getTreeParent().replaceChild(left, merged);
        right.getTreeParent().removeChild(right);
      }
      else {
        right.getTreeParent().replaceChild(right, merged);
        left.getTreeParent().removeChild(left);
      }
      left = merged;
    }
    else {
      right.getTreeParent().removeChild(right);
    }
  }
  else if (left.getElementType() != TokenType.WHITE_SPACE || forceReformat) {
    if (right.getElementType() == TokenType.WHITE_SPACE) {
      markWhitespaceForReformat(right);
    }
    else if (left.getElementType() == TokenType.WHITE_SPACE) {
      markWhitespaceForReformat(left);
    }
    else {
      markToReformatBeforeOrInsertWhitespace(left, right);
    }
  }
  return left;
}
 
Example #14
Source File: CodeEditUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static void markWhitespaceForReformat(final ASTNode right) {
  final String text = right.getText();
  final LeafElement merged = ASTFactory.whitespace(text);
  right.getTreeParent().replaceChild(right, merged);
}