Java Code Examples for com.intellij.psi.impl.source.PsiFileImpl#getTreeElement()

The following examples show how to use com.intellij.psi.impl.source.PsiFileImpl#getTreeElement() . 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: StubBasedPsiElementBase.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures this element is AST-based. This is an expensive operation that might take significant time and allocate lots of objects,
 * so it should be to be avoided if possible.
 *
 * @return an AST node corresponding to this element. If the element is currently operating via stubs,
 * this causes AST to be loaded for the whole file and all stub-based PSI elements in this file (including the current one)
 * to be switched from stub to AST. So, after this call {@link #getStub()} will return null.
 */
@Override
@Nonnull
public ASTNode getNode() {
  if (mySubstrateRef instanceof SubstrateRef.StubRef) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    PsiFileImpl file = (PsiFileImpl)getContainingFile();
    if (!file.isValid()) throw new PsiInvalidElementAccessException(file);

    FileElement treeElement = file.getTreeElement();
    if (treeElement != null && mySubstrateRef instanceof SubstrateRef.StubRef) {
      return notBoundInExistingAst(file, treeElement);
    }

    treeElement = file.calcTreeElement();
    if (mySubstrateRef instanceof SubstrateRef.StubRef) {
      return failedToBindStubToAst(file, treeElement);
    }
  }

  return mySubstrateRef.getNode();
}
 
Example 2
Source File: StubBasedPsiElementBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"NonConstantStringShouldBeStringBuffer", "StringConcatenationInLoop"})
private ASTNode notBoundInExistingAst(@Nonnull PsiFileImpl file, @Nonnull FileElement treeElement) {
  String message = "file=" + file + "; tree=" + treeElement;
  PsiElement each = this;
  while (each != null) {
    message += "\n each of class " + each.getClass() + "; valid=" + each.isValid();
    if (each instanceof StubBasedPsiElementBase) {
      message += "; ref=" + ((StubBasedPsiElementBase<?>)each).mySubstrateRef;
      each = ((StubBasedPsiElementBase<?>)each).getParentByStub();
    }
    else {
      if (each instanceof PsiFile) {
        message += "; same file=" + (each == file) + "; current tree= " + file.getTreeElement() + "; stubTree=" + file.getStubTree() + "; physical=" + file.isPhysical();
      }
      break;
    }
  }
  StubElement<?> eachStub = getStub();
  while (eachStub != null) {
    message += "\n each stub " + (eachStub instanceof PsiFileStubImpl ? ((PsiFileStubImpl<?>)eachStub).getDiagnostics() : eachStub);
    eachStub = eachStub.getParentStub();
  }

  if (ourTraceStubAstBinding) {
    message += dumpCreationTraces(treeElement);
  }
  throw new AssertionError(message);
}
 
Example 3
Source File: BlockSupportImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static DiffLog mergeTrees(@Nonnull final PsiFileImpl fileImpl,
                                 @Nonnull final ASTNode oldRoot,
                                 @Nonnull final ASTNode newRoot,
                                 @Nonnull ProgressIndicator indicator,
                                 @Nonnull CharSequence lastCommittedText) {
  PsiUtilCore.ensureValid(fileImpl);
  if (newRoot instanceof FileElement) {
    FileElement fileImplElement = fileImpl.getTreeElement();
    if (fileImplElement != null) {
      ((FileElement)newRoot).setCharTable(fileImplElement.getCharTable());
    }
  }

  try {
    newRoot.putUserData(TREE_TO_BE_REPARSED, Pair.create(oldRoot, lastCommittedText));
    if (isReplaceWholeNode(fileImpl, newRoot)) {
      DiffLog treeChangeEvent = replaceElementWithEvents(oldRoot, newRoot);
      fileImpl.putUserData(TREE_DEPTH_LIMIT_EXCEEDED, Boolean.TRUE);

      return treeChangeEvent;
    }
    newRoot.getFirstChildNode();  // maybe reparsed in PsiBuilderImpl and have thrown exception here
  }
  catch (ReparsedSuccessfullyException e) {
    // reparsed in PsiBuilderImpl
    return e.getDiffLog();
  }
  finally {
    newRoot.putUserData(TREE_TO_BE_REPARSED, null);
  }

  final ASTShallowComparator comparator = new ASTShallowComparator(indicator);
  final ASTStructure treeStructure = createInterruptibleASTStructure(newRoot, indicator);

  DiffLog diffLog = new DiffLog();
  diffTrees(oldRoot, diffLog, comparator, treeStructure, indicator, lastCommittedText);
  return diffLog;
}
 
Example 4
Source File: FileElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public ASTNode copyElement() {
  PsiFileImpl psiElement = (PsiFileImpl)getPsi();
  PsiFileImpl psiElementCopy = (PsiFileImpl)psiElement.copy();
  return psiElementCopy.getTreeElement();
}