com.intellij.util.CharTable Java Examples

The following examples show how to use com.intellij.util.CharTable. 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: CodeStyleManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static Pair<PsiElement, CharTable> doFindWhiteSpaceNode(@Nonnull PsiFile file, int offset) {
  ASTNode astNode = SourceTreeToPsiMap.psiElementToTree(file);
  if (!(astNode instanceof FileElement)) {
    return new Pair<>(null, null);
  }
  PsiElement elementAt = InjectedLanguageManager.getInstance(file.getProject()).findInjectedElementAt(file, offset);
  final CharTable charTable = ((FileElement)astNode).getCharTable();
  if (elementAt == null) {
    elementAt = findElementInTreeWithFormatterEnabled(file, offset);
  }

  if (elementAt == null) {
    return new Pair<>(null, charTable);
  }
  ASTNode node = elementAt.getNode();
  if (node == null || node.getElementType() != TokenType.WHITE_SPACE) {
    return new Pair<>(null, charTable);
  }
  return Pair.create(elementAt, charTable);
}
 
Example #2
Source File: DummyHolder.java    From consulo with Apache License 2.0 6 votes vote down vote up
public DummyHolder(@Nonnull PsiManager manager, @Nullable TreeElement contentElement, @Nullable PsiElement context, @Nullable CharTable table, @Nullable Boolean validity, Language language) {
  super(TokenType.DUMMY_HOLDER, TokenType.DUMMY_HOLDER, new DummyHolderViewProvider(manager));
  myLanguage = language;
  ((DummyHolderViewProvider)getViewProvider()).setDummyHolder(this);
  myContext = context;
  myTable = table != null ? table : IdentityCharTable.INSTANCE;
  if (contentElement instanceof FileElement) {
    ((FileElement)contentElement).setPsi(this);
    ((FileElement)contentElement).setCharTable(myTable);
    setTreeElementPointer((FileElement)contentElement);
  }
  else if (contentElement != null) {
    getTreeElement().rawAddChildren(contentElement);
    clearCaches();
  }
  myExplicitlyValid = validity;
}
 
Example #3
Source File: BlockSupportImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
protected static ASTNode tryReparseNode(@Nonnull IReparseableElementTypeBase reparseable, @Nonnull ASTNode node, @Nonnull CharSequence newTextStr,
                                        @Nonnull PsiManager manager, @Nonnull Language baseLanguage, @Nonnull CharTable charTable) {
  if (!reparseable.isParsable(node.getTreeParent(), newTextStr, baseLanguage, manager.getProject())) {
    return null;
  }
  ASTNode chameleon;
  if (reparseable instanceof ICustomParsingType) {
    chameleon = ((ICustomParsingType)reparseable).parse(newTextStr, SharedImplUtil.findCharTableByTree(node));
  }
  else if (reparseable instanceof ILazyParseableElementType) {
    chameleon = ((ILazyParseableElementType)reparseable).createNode(newTextStr);
  }
  else {
    throw new AssertionError(reparseable.getClass() + " must either implement ICustomParsingType or extend ILazyParseableElementType");
  }
  if (chameleon == null) {
    return null;
  }
  DummyHolder holder = DummyHolderFactory.createHolder(manager, null, node.getPsi(), charTable);
  holder.getTreeElement().rawAddChildren((TreeElement)chameleon);
  if (!reparseable.isValidReparse(node, chameleon)) {
    return null;
  }
  return chameleon;
}
 
Example #4
Source File: SharedImplUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static CharTable findCharTableByTree(ASTNode tree) {
  while (tree != null) {
    final CharTable userData = tree.getUserData(CharTable.CHAR_TABLE_KEY);
    if (userData != null) return userData;
    if (tree instanceof FileElement) return ((FileElement)tree).getCharTable();
    tree = tree.getTreeParent();
  }
  LOG.error("Invalid root element");
  return null;
}
 
Example #5
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 #6
Source File: TemplateDataElementType.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void insertOuters(TreeElement templateFileElement,
                          @Nonnull CharSequence sourceCode,
                          @Nonnull List<TextRange> outerElementsRanges,
                          final CharTable charTable) {
  TreePatcher templateTreePatcher = TREE_PATCHER.forLanguage(templateFileElement.getPsi().getLanguage());

  int treeOffset = 0;
  LeafElement currentLeaf = TreeUtil.findFirstLeaf(templateFileElement);

  for (TextRange outerElementRange : outerElementsRanges) {
    while (currentLeaf != null && treeOffset < outerElementRange.getStartOffset()) {
      treeOffset += currentLeaf.getTextLength();
      int currentTokenStart = outerElementRange.getStartOffset();
      if (treeOffset > currentTokenStart) {
        currentLeaf = templateTreePatcher.split(currentLeaf, currentLeaf.getTextLength() - (treeOffset - currentTokenStart), charTable);
        treeOffset = currentTokenStart;
      }
      currentLeaf = (LeafElement)TreeUtil.nextLeaf(currentLeaf);
    }

    if (currentLeaf == null) {
      assert outerElementsRanges.get(outerElementsRanges.size() - 1) == outerElementRange : "This should only happen for the last inserted range. Got " +
                                                                                            outerElementsRanges.lastIndexOf(outerElementRange) +
                                                                                            " of " +
                                                                                            (outerElementsRanges.size() - 1);
      ((CompositeElement)templateFileElement)
              .rawAddChildren(createOuterLanguageElement(charTable.intern(outerElementRange.subSequence(sourceCode)), myOuterElementType));
      ((CompositeElement)templateFileElement).subtreeChanged();
      break;
    }

    final OuterLanguageElementImpl newLeaf = createOuterLanguageElement(charTable.intern(outerElementRange.subSequence(sourceCode)), myOuterElementType);
    templateTreePatcher.insert(currentLeaf.getTreeParent(), currentLeaf, newLeaf);
    currentLeaf.getTreeParent().subtreeChanged();
    currentLeaf = newLeaf;
  }
}
 
Example #7
Source File: TemplateDataElementType.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public ASTNode parseContents(ASTNode chameleon) {
  final CharTable charTable = SharedImplUtil.findCharTableByTree(chameleon);
  final FileElement fileElement = TreeUtil.getFileElement((TreeElement)chameleon);
  final PsiFile psiFile = (PsiFile)fileElement.getPsi();
  PsiFile originalPsiFile = psiFile.getOriginalFile();

  final TemplateLanguageFileViewProvider viewProvider = (TemplateLanguageFileViewProvider)originalPsiFile.getViewProvider();

  final Language templateLanguage = getTemplateFileLanguage(viewProvider);
  final CharSequence sourceCode = chameleon.getChars();

  RangesCollector collector = new RangesCollector();
  final PsiFile templatePsiFile = createTemplateFile(psiFile, templateLanguage, sourceCode, viewProvider, collector);

  final FileElement templateFileElement = ((PsiFileImpl)templatePsiFile).calcTreeElement();

  DebugUtil.startPsiModification("template language parsing");
  try {
    prepareParsedTemplateFile(templateFileElement);
    insertOuters(templateFileElement, sourceCode, collector.myRanges, charTable);

    TreeElement childNode = templateFileElement.getFirstChildNode();

    DebugUtil.checkTreeStructure(templateFileElement);
    DebugUtil.checkTreeStructure(chameleon);
    if (fileElement != chameleon) {
      DebugUtil.checkTreeStructure(psiFile.getNode());
      DebugUtil.checkTreeStructure(originalPsiFile.getNode());
    }

    return childNode;
  }
  finally {
    DebugUtil.finishPsiModification();
  }
}
 
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: ChangeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static TreeElement generateTreeElement(@Nullable PsiElement original, @Nonnull CharTable table, @Nonnull final PsiManager manager) {
  if (original == null) return null;
  PsiUtilCore.ensureValid(original);
  if (SourceTreeToPsiMap.hasTreeElement(original)) {
    return copyElement((TreeElement)SourceTreeToPsiMap.psiElementToTree(original), table);
  }
  else {
    for (TreeGenerator generator : TreeGenerator.EP_NAME.getExtensionList()) {
      final TreeElement element = generator.generateTreeFor(original, table, manager);
      if (element != null) return element;
    }
    return null;
  }
}
 
Example #10
Source File: ChangeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static TreeElement copyElement(TreeElement original, final PsiElement context, CharTable table) {
  final TreeElement element = (TreeElement)original.clone();
  final PsiManager manager = original.getManager();
  DummyHolderFactory.createHolder(manager, element, context, table).getTreeElement();
  encodeInformation(element, original);
  TreeUtil.clearCaches(element);
  saveIndentationToCopy(original, element);
  return element;
}
 
Example #11
Source File: SharedImplUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static PsiElement addRange(PsiElement thisElement,
                                  PsiElement first,
                                  PsiElement last,
                                  ASTNode anchor,
                                  Boolean before) throws IncorrectOperationException {
  CheckUtil.checkWritable(thisElement);
  final CharTable table = findCharTableByTree(SourceTreeToPsiMap.psiElementToTree(thisElement));

  TreeElement copyFirst = null;
  ASTNode copyLast = null;
  ASTNode next = SourceTreeToPsiMap.psiElementToTree(last).getTreeNext();
  ASTNode parent = null;
  for (ASTNode element = SourceTreeToPsiMap.psiElementToTree(first); element != next; element = element.getTreeNext()) {
    TreeElement elementCopy = ChangeUtil.copyElement((TreeElement)element, table);
    if (element == first.getNode()) {
      copyFirst = elementCopy;
    }
    if (element == last.getNode()) {
      copyLast = elementCopy;
    }
    if (parent == null) {
      parent = elementCopy.getTreeParent();
    }
    else {
      if(elementCopy.getElementType() == TokenType.WHITE_SPACE)
        CodeEditUtil.setNodeGenerated(elementCopy, true);
      parent.addChild(elementCopy, null);
    }
  }
  if (copyFirst == null) return null;
  copyFirst = ((CompositeElement)SourceTreeToPsiMap.psiElementToTree(thisElement)).addInternal(copyFirst, copyLast, anchor, before);
  for (TreeElement element = copyFirst; element != null; element = element.getTreeNext()) {
    element = ChangeUtil.decodeInformation(element);
    if (element.getTreePrev() == null) {
      copyFirst = element;
    }
  }
  return SourceTreeToPsiMap.treeElementToPsi(copyFirst);
}
 
Example #12
Source File: SqliteMagicLightMethodGenerator.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public TreeElement generateTreeFor(PsiElement original, CharTable table, PsiManager manager) {
  TreeElement result = null;
  if (original instanceof SqliteMagicLightMethodBuilder) {
    result = ChangeUtil.copyElement((TreeElement) SourceTreeToPsiMap.psiElementToTree(original), table);
  }
  return result;
}
 
Example #13
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 #14
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 #15
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 #16
Source File: LombokLightMethodTreeGenerator.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
public TreeElement generateTreeFor(PsiElement original, CharTable table, PsiManager manager) {
  TreeElement result = null;
  if (original instanceof LombokLightMethodBuilder) {
    result = ChangeUtil.copyElement((TreeElement) SourceTreeToPsiMap.psiElementToTree(original), table);
  }
  return result;
}
 
Example #17
Source File: DummyHolderFactory.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public DummyHolder createHolder(@Nonnull PsiManager manager, CharTable table, boolean validity) {
  return new DummyHolder(manager, table, validity);
}
 
Example #18
Source File: LighterAST.java    From consulo with Apache License 2.0 4 votes vote down vote up
public LighterAST(@Nonnull CharTable charTable) {
  myCharTable = charTable;
}
 
Example #19
Source File: LighterAST.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public CharTable getCharTable() {
  return myCharTable;
}
 
Example #20
Source File: FCTSBackedLighterAST.java    From consulo with Apache License 2.0 4 votes vote down vote up
public FCTSBackedLighterAST(@Nonnull CharTable charTable, @Nonnull FlyweightCapableTreeStructure<LighterASTNode> treeStructure) {
  super(charTable);
  myTreeStructure = treeStructure;
}
 
Example #21
Source File: DummyHolder.java    From consulo with Apache License 2.0 4 votes vote down vote up
public DummyHolder(@Nonnull PsiManager manager, final CharTable table, final Language language) {
  this(manager, null, null, table, null, language);
}
 
Example #22
Source File: DummyHolder.java    From consulo with Apache License 2.0 4 votes vote down vote up
public DummyHolder(@Nonnull PsiManager manager, PsiElement context, CharTable table) {
  this(manager, null, context, table);
}
 
Example #23
Source File: FileASTNode.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
CharTable getCharTable();
 
Example #24
Source File: DummyHolder.java    From consulo with Apache License 2.0 4 votes vote down vote up
public DummyHolder(@Nonnull PsiManager manager, @Nullable TreeElement contentElement, PsiElement context, @Nullable CharTable table) {
  this(manager, contentElement, context, table, null, language(context, PlainTextLanguage.INSTANCE));
}
 
Example #25
Source File: DummyHolder.java    From consulo with Apache License 2.0 4 votes vote down vote up
public DummyHolder(@Nonnull PsiManager manager, CharTable table, boolean validity) {
  this(manager, null, null, table, Boolean.valueOf(validity), PlainTextLanguage.INSTANCE);
}
 
Example #26
Source File: ChangeUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static TreeElement copyElement(@Nonnull TreeElement original, CharTable table) {
  CompositeElement treeParent = original.getTreeParent();
  return copyElement(original, treeParent == null ? null : treeParent.getPsi(), table);
}
 
Example #27
Source File: FileElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void setCharTable(@Nonnull CharTable table) {
  myCharTable = table;
}
 
Example #28
Source File: FileElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
@Nonnull
public CharTable getCharTable() {
  return myCharTable;
}
 
Example #29
Source File: TreeElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public ASTNode copyElement() {
  CharTable table = SharedImplUtil.findCharTableByTree(this);
  return ChangeUtil.copyElement(this, table);
}
 
Example #30
Source File: DummyHolderFactory.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static DummyHolder createHolder(@Nonnull PsiManager manager, final CharTable table, final Language language) {
  return INSTANCE.createHolder(manager, table, language);
}