Java Code Examples for com.intellij.psi.impl.source.SourceTreeToPsiMap#treeElementToPsi()

The following examples show how to use com.intellij.psi.impl.source.SourceTreeToPsiMap#treeElementToPsi() . 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: BashSpacingProcessor.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
private void init(final ASTNode child) {
    if (child == null) {
        return;
    }
    ASTNode treePrev = child.getTreePrev();
    while (treePrev != null && SpacingUtil.isWhiteSpace(treePrev)) {
        treePrev = treePrev.getTreePrev();
    }

    if (treePrev == null) {
        init(child.getTreeParent());
    } else {
        myChild2 = child;
        myChild1 = treePrev;
        final CompositeElement parent = (CompositeElement) treePrev.getTreeParent();
        myParent = SourceTreeToPsiMap.treeElementToPsi(parent);
    }
}
 
Example 2
Source File: GraphQLIdentifierManipulator.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Override
public GraphQLIdentifierImpl handleContentChange(@NotNull GraphQLIdentifierImpl element, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
    // replace the NAME leaf element inside the identifier
    final LeafElement renamedLeaf = Factory.createSingleLeafElement(GraphQLElementTypes.NAME, newContent, null, element.getManager());
    final PsiElement renamedPsiElement = SourceTreeToPsiMap.treeElementToPsi(renamedLeaf);
    if (renamedPsiElement != null) {
        element.getFirstChild().replace(renamedPsiElement);
    }
    return element;

}
 
Example 3
Source File: FormatterUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static ASTNode findPreviousWhiteSpace(final ASTNode leafElement, final IElementType whiteSpaceTokenType) {
  final int offset = leafElement.getTextRange().getStartOffset() - 1;
  if (offset < 0) return null;
  final PsiElement psiElement = SourceTreeToPsiMap.treeElementToPsi(leafElement);
  if (psiElement == null) {
    return null;
  }
  final PsiElement found = psiElement.getContainingFile().findElementAt(offset);
  if (found == null) return null;
  final ASTNode treeElement = found.getNode();
  if (treeElement != null && treeElement.getElementType() == whiteSpaceTokenType) return treeElement;
  return null;
}
 
Example 4
Source File: OwnBufferLeafPsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public PsiElement replace(@Nonnull PsiElement newElement) throws IncorrectOperationException {
  LOG.assertTrue(getTreeParent() != null);
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(newElement);
  getTreeParent().replaceChildInternal(this, elementCopy);
  elementCopy = ChangeUtil.decodeInformation(elementCopy);
  final PsiElement result = SourceTreeToPsiMap.treeElementToPsi(elementCopy);

  this.invalidate();
  return result;
}
 
Example 5
Source File: SharedImplUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static PsiElement doReplace(PsiElement psiElement, TreeElement treeElement, PsiElement newElement) {
  CompositeElement treeParent = treeElement.getTreeParent();
  LOG.assertTrue(treeParent != null);
  CheckUtil.checkWritable(psiElement);
  TreeElement elementCopy = ChangeUtil.copyToElement(newElement);
  treeParent.replaceChildInternal(treeElement, elementCopy);
  elementCopy = ChangeUtil.decodeInformation(elementCopy);
  final PsiElement result = SourceTreeToPsiMap.treeElementToPsi(elementCopy);
  treeElement.invalidate();
  return result;
}
 
Example 6
Source File: ANTLRv4FoldingBuilder.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressWarnings("SimplifiableIfStatement")
@Override
protected boolean isRegionCollapsedByDefault(@NotNull ASTNode node) {
    final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(node);
    if (element == null) return false;

    ANTLRv4FoldingSettings settings = ANTLRv4FoldingSettings.getInstance();

    if (RULE_BLOCKS.contains(node.getElementType())) return settings.isCollapseRuleBlocks();
    if (node.getElementType() == TOKENSSPEC) return settings.isCollapseTokens();

    if (element instanceof AtAction) return settings.isCollapseActions();

    if (element instanceof ANTLRv4FileRoot) {
        return settings.isCollapseFileHeader();
    }
    if (node.getElementType() == DOC_COMMENT_TOKEN) {

        PsiElement parent = element.getParent();

        if (parent instanceof ANTLRv4FileRoot) {
            PsiElement firstChild = parent.getFirstChild();
            if (firstChild instanceof PsiWhiteSpace) {
                firstChild = firstChild.getNextSibling();
            }
            if (element.equals(firstChild)) {
                return settings.isCollapseFileHeader();
            }
        }
        return settings.isCollapseDocComments();
    }
    if (isComment(element)) {
        return settings.isCollapseComments();
    }
    return false;

}
 
Example 7
Source File: StubBasedPsiElementBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * @return the parent of this element. Uses stub hierarchy if possible, but might cause an expensive switch to AST
 * if the parent stub doesn't correspond to the parent AST node.
 */
@Override
public PsiElement getParent() {
  T stub = getGreenStub();
  if (stub != null && !((ObjectStubBase<?>)stub).isDangling()) {
    return stub.getParentStub().getPsi();
  }

  return SourceTreeToPsiMap.treeElementToPsi(getNode().getTreeParent());
}
 
Example 8
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 9
Source File: ASTDelegatePsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public PsiElement replace(@Nonnull final PsiElement newElement) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(newElement);
  if (getParent() instanceof ASTDelegatePsiElement) {
    final ASTDelegatePsiElement parentElement = (ASTDelegatePsiElement)getParent();
    parentElement.replaceChildInternal(this, elementCopy);
  }
  else {
    CodeEditUtil.replaceChild(getParent().getNode(), getNode(), elementCopy);
  }
  elementCopy = ChangeUtil.decodeInformation(elementCopy);
  return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}
 
Example 10
Source File: LazyParseablePsiElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public PsiElement findElementAt(int offset) {
  ASTNode leaf = findLeafElementAt(offset);
  return SourceTreeToPsiMap.treeElementToPsi(leaf);
}
 
Example 11
Source File: CompositePsiElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public PsiElement copy() {
  ASTNode elementCopy = copyElement();
  return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}
 
Example 12
Source File: SharedImplUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static PsiElement getNextSibling(ASTNode thisElement) {
  return SourceTreeToPsiMap.treeElementToPsi(thisElement.getTreeNext());
}
 
Example 13
Source File: SharedImplUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public static PsiElement getLastChild(ASTNode element) {
  return SourceTreeToPsiMap.treeElementToPsi(element.getLastChildNode());
}
 
Example 14
Source File: LazyParseablePsiElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public PsiElement copy() {
  ASTNode elementCopy = copyElement();
  return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}
 
Example 15
Source File: SharedImplUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static PsiElement getParent(ASTNode thisElement) {
  return SourceTreeToPsiMap.treeElementToPsi(thisElement.getTreeParent());
}
 
Example 16
Source File: CompositeElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public final PsiElement findChildByRoleAsPsiElement(int role) {
  ASTNode element = findChildByRole(role);
  if (element == null) return null;
  return SourceTreeToPsiMap.treeElementToPsi(element);
}
 
Example 17
Source File: OwnBufferLeafPsiElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public PsiElement copy() {
  ASTNode elementCopy = copyElement();
  return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}
 
Example 18
Source File: ASTDelegatePsiElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
@RequiredReadAction
public PsiElement findElementAt(int offset) {
  ASTNode treeElement = getNode().findLeafElementAt(offset);
  return SourceTreeToPsiMap.treeElementToPsi(treeElement);
}
 
Example 19
Source File: AbstractHaxeNamedComponent.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
@Nullable
public final PsiElement findChildByRoleAsPsiElement(int role) {
  ASTNode element = findChildByRole(role);
  if (element == null) return null;
  return SourceTreeToPsiMap.treeElementToPsi(element);
}
 
Example 20
Source File: LeafPsiElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public PsiElement copy() {
  ASTNode elementCopy = copyElement();
  return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}