com.intellij.psi.impl.DebugUtil Java Examples

The following examples show how to use com.intellij.psi.impl.DebugUtil. 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: BaseCppTestCase.java    From CppTools with Apache License 2.0 6 votes vote down vote up
protected void doParseTest(final String fileName, @NotNull @NonNls final String ext) throws Throwable {
  Runnable action = new Runnable() {
    public void run() {
      try {
        myFixture.testHighlighting(fileName + (ext.length() > 0 ? "." + ext : ""));
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      String s = DebugUtil.psiToString(myFixture.getFile(), true);
      final String expected = LoadTextUtil.loadText(
        LocalFileSystem.getInstance().findFileByIoFile(new File(getTestDataPath() + File.separator + fileName + ".txt"))
      ).toString();

      assertEquals(
        expected,
        s
      );
    }
  };
  BuildState.invokeOnEDTSynchroneously(action);
}
 
Example #2
Source File: TreeElement.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void rawRemove() {
  final TreeElement next = getTreeNext();
  final CompositeElement parent = getTreeParent();
  final TreeElement prev = getTreePrev();

  if (prev != null) {
    prev.setTreeNext(next);
  }
  else if (parent != null) {
    parent.setFirstChildNode(next);
  }

  if (next != null) {
    next.setTreePrev(prev);
  }
  else if (parent != null) {
    parent.setLastChildNode(prev);
  }

  DebugUtil.checkTreeStructure(parent);
  DebugUtil.checkTreeStructure(prev);
  DebugUtil.checkTreeStructure(next);

  invalidate();
}
 
Example #3
Source File: StubBasedPsiElementBase.java    From consulo with Apache License 2.0 6 votes vote down vote up
private ASTNode failedToBindStubToAst(@Nonnull PsiFileImpl file, @Nonnull final FileElement fileElement) {
  VirtualFile vFile = file.getVirtualFile();
  StubTree stubTree = file.getStubTree();
  final String stubString = stubTree != null ? ((PsiFileStubImpl)stubTree.getRoot()).printTree() : null;
  final String astString = RecursionManager.doPreventingRecursion("failedToBindStubToAst", true, () -> DebugUtil.treeToString(fileElement, true));

  @NonNls final String message =
          "Failed to bind stub to AST for element " + getClass() + " in " + (vFile == null ? "<unknown file>" : vFile.getPath()) + "\nFile:\n" + file + "@" + System.identityHashCode(file);

  final String creationTraces = ourTraceStubAstBinding ? dumpCreationTraces(fileElement) : null;

  List<Attachment> attachments = new ArrayList<>();
  if (stubString != null) {
    attachments.add(new Attachment("stubTree.txt", stubString));
  }
  if (astString != null) {
    attachments.add(new Attachment("ast.txt", astString));
  }
  if (creationTraces != null) {
    attachments.add(new Attachment("creationTraces.txt", creationTraces));
  }

  throw new RuntimeExceptionWithAttachments(message, attachments.toArray(Attachment.EMPTY_ARRAY));
}
 
Example #4
Source File: ProjectImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected ProjectImpl(@Nonnull Application application, @Nonnull ProjectManager manager, @Nonnull String dirPath, boolean isOptimiseTestLoadSpeed, String projectName, boolean noUIThread) {
  super(application, "Project " + (projectName == null ? dirPath : projectName), ExtensionAreaId.PROJECT);
  myApplication = (ApplicationEx)application;
  myDirPath = dirPath;

  putUserData(CREATION_TIME, System.nanoTime());

  if (myApplication.isUnitTestMode()) {
    putUserData(CREATION_TRACE, DebugUtil.currentStackTrace());
  }

  if (!isDefault()) {
    if (noUIThread) {
      getStateStore().setProjectFilePathNoUI(dirPath);
    }
    else {
      getStateStore().setProjectFilePath(dirPath);
    }
  }

  myOptimiseTestLoadSpeed = isOptimiseTestLoadSpeed;

  myManager = manager;

  myName = projectName;
}
 
Example #5
Source File: FoldingUpdate.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void diagnoseIncorrectRange(@Nonnull PsiFile file, @Nonnull Document document, Language language, FoldingBuilder foldingBuilder, FoldingDescriptor descriptor, PsiElement psiElement) {
  String message = "Folding descriptor " +
                   descriptor +
                   " made by " +
                   foldingBuilder +
                   " for " +
                   language +
                   " is outside document range" +
                   ", PSI element: " +
                   psiElement +
                   ", PSI element range: " +
                   psiElement.getTextRange() +
                   "; " +
                   DebugUtil.diagnosePsiDocumentInconsistency(psiElement, document);
  LOG.error(message, ApplicationManager.getApplication().isInternal() ? new Attachment[]{AttachmentFactory.createAttachment(document),
          consulo.logging.attachment.AttachmentFactory.get().create("psiTree.txt", DebugUtil.psiToString(file, false, true))} : Attachment.EMPTY_ARRAY);
}
 
Example #6
Source File: PsiViewerDialog.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
protected Action[] createActions() {
  AbstractAction copyPsi = new AbstractAction("Cop&y PSI") {
    @Override
    public void actionPerformed(ActionEvent e) {
      PsiElement element = parseText(myEditor.getDocument().getText());
      List<PsiElement> allToParse = new ArrayList<PsiElement>();
      if (element instanceof PsiFile) {
        allToParse.addAll(((PsiFile)element).getViewProvider().getAllFiles());
      }
      else if (element != null) {
        allToParse.add(element);
      }
      String data = "";
      for (PsiElement psiElement : allToParse) {
        data += DebugUtil.psiToString(psiElement, !myShowWhiteSpacesBox.isSelected(), true);
      }
      CopyPasteManager.getInstance().setContents(new StringSelection(data));
    }
  };
  return ArrayUtil.mergeArrays(new Action[]{copyPsi}, super.createActions());
}
 
Example #7
Source File: InjectedLanguageUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
static void clearCaches(@Nonnull PsiFile injected, @Nonnull DocumentWindowImpl documentWindow) {
  VirtualFileWindowImpl virtualFile = (VirtualFileWindowImpl)injected.getVirtualFile();
  PsiManagerEx psiManagerEx = (PsiManagerEx)injected.getManager();
  if (psiManagerEx.getProject().isDisposed()) return;

  DebugUtil.performPsiModification("injected clearCaches", () -> psiManagerEx.getFileManager().setViewProvider(virtualFile, null));

  VirtualFile delegate = virtualFile.getDelegate();
  if (!delegate.isValid()) return;

  FileViewProvider viewProvider = psiManagerEx.getFileManager().findCachedViewProvider(delegate);
  if (viewProvider == null) return;

  for (PsiFile hostFile : ((AbstractFileViewProvider)viewProvider).getCachedPsiFiles()) {
    // modification of cachedInjectedDocuments must be under InjectedLanguageManagerImpl.ourInjectionPsiLock
    synchronized (InjectedLanguageManagerImpl.ourInjectionPsiLock) {
      List<DocumentWindow> cachedInjectedDocuments = getCachedInjectedDocuments(hostFile);
      for (int i = cachedInjectedDocuments.size() - 1; i >= 0; i--) {
        DocumentWindow cachedInjectedDocument = cachedInjectedDocuments.get(i);
        if (cachedInjectedDocument == documentWindow) {
          cachedInjectedDocuments.remove(i);
        }
      }
    }
  }
}
 
Example #8
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 #9
Source File: SerializedStubTree.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private String dumpStub() {
  String deserialized;
  try {
    deserialized = "stub: " + DebugUtil.stubTreeToString(getStub(true));
  }
  catch (SerializerNotFoundException e) {
    LOG.error(e);
    deserialized = "error while stub deserialization: " + e.getMessage();
  }
  return deserialized + "\n bytes: " + toHexString(myTreeBytes, myTreeByteLength);
}
 
Example #10
Source File: CompletionServiceImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void stopHere() {
  if (LOG.isTraceEnabled()) {
    LOG.trace("Completion stopped\n" + DebugUtil.currentStackTrace());
  }
  super.stopHere();
  if (myOriginal != null) {
    myOriginal.stopHere();
  }
}
 
Example #11
Source File: ChangesViewManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Inject
public ChangesViewManager(@Nonnull Project project, @Nonnull ChangesViewContentI contentManager) {
  myProject = project;
  myContentManager = contentManager;
  myView = new ChangesListView(project);
  myRepaintAlarm = new Alarm(Alarm.ThreadToUse.SWING_THREAD, project);
  myTsl = new TreeSelectionListener() {
    @Override
    public void valueChanged(TreeSelectionEvent e) {
      if (LOG.isDebugEnabled()) {
        TreePath[] paths = myView.getSelectionPaths();
        String joinedPaths = paths != null ? StringUtil.join(paths, FunctionUtil.string(), ", ") : null;
        String message = "selection changed. selected:  " + joinedPaths;

        if (LOG.isTraceEnabled()) {
          LOG.trace(message + " from: " + DebugUtil.currentStackTrace());
        }
        else {
          LOG.debug(message);
        }
      }
      SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
          changeDetails();
        }
      });
    }
  };
}
 
Example #12
Source File: FileTrees.java    From consulo with Apache License 2.0 5 votes vote down vote up
FileTrees clearStub(@Nonnull String reason) {
  StubTree stubHolder = derefStub();
  if (stubHolder != null) {
    ((PsiFileStubImpl<?>)stubHolder.getRoot()).clearPsi(reason);
  }

  if (myRefToPsi != null) {
    DebugUtil.performPsiModification("clearStub", () -> forEachCachedPsi(psi -> {
      DebugUtil.onInvalidated(psi);
      psi.setSubstrateRef(SubstrateRef.createInvalidRef(psi));
    }));
  }

  return new FileTrees(myFile, null, myTreeElementPointer, null);
}
 
Example #13
Source File: ChangeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static TreeElement decodeInformation(TreeElement element) {
  DebugUtil.startPsiModification(null);
  try {
    return decodeInformation(element, new HashMap<>());
  }
  finally {
    DebugUtil.finishPsiModification();
  }
}
 
Example #14
Source File: PsiPackageBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Override
public String getName() {
  if (DebugUtil.CHECK_INSIDE_ATOMIC_ACTION_ENABLED) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
  }
  if (myQualifiedName.isEmpty()) return null;
  int index = myQualifiedName.lastIndexOf('.');
  if (index < 0) {
    return myQualifiedName;
  }
  else {
    return myQualifiedName.substring(index + 1);
  }
}
 
Example #15
Source File: ChangeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void encodeInformation(TreeElement element, ASTNode original) {
  DebugUtil.startPsiModification(null);
  try {
    encodeInformation(element, original, new HashMap<>());
  }
  finally {
    DebugUtil.finishPsiModification();
  }
}
 
Example #16
Source File: TreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ASTNode findChildBackward(ASTNode parent, IElementType type) {
  if (DebugUtil.CHECK_INSIDE_ATOMIC_ACTION_ENABLED) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
  }
  for (ASTNode element = parent.getLastChildNode(); element != null; element = element.getTreePrev()) {
    if (element.getElementType() == type) return element;
  }
  return null;
}
 
Example #17
Source File: CompositeElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void setPsi(@Nonnull PsiElement psi) {
  PsiElement prev = myWrapper;
  if (prev != null && prev != psi) {
    DebugUtil.onInvalidated(prev);
  }
  myWrapper = psi;
}
 
Example #18
Source File: TreeElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
final void setTreeParent(CompositeElement parent) {
  if (parent == myParent) return;

  PsiFileImpl file = getCachedFile(this);
  if (file != null) {
    file.beforeAstChange();
  }

  myParent = parent;
  if (parent != null && parent.getElementType() != TokenType.DUMMY_HOLDER) {
    DebugUtil.revalidateNode(this);
  }
}
 
Example #19
Source File: TreeElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public final int getStartOffsetInParent() {
  if (myParent == null) return -1;
  int offsetInParent = myStartOffsetInParent;
  if (offsetInParent != -1) return offsetInParent;

  if (DebugUtil.CHECK_INSIDE_ATOMIC_ACTION_ENABLED) {
    assertReadAccessAllowed();
  }

  TreeElement cur = this;
  while (true) {
    TreeElement prev = cur.getTreePrev();
    if (prev == null) break;
    cur = prev;
    offsetInParent = cur.myStartOffsetInParent;
    if (offsetInParent != -1) break;
  }

  if (offsetInParent == -1) {
    cur.myStartOffsetInParent = offsetInParent = 0;
  }

  while (cur != this) {
    TreeElement next = cur.getTreeNext();
    offsetInParent += cur.getTextLength();
    next.myStartOffsetInParent = offsetInParent;
    cur = next;
  }
  return offsetInParent;
}
 
Example #20
Source File: CompositeElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void rawAddChildrenWithoutNotifications(@Nonnull TreeElement first) {
  final TreeElement last = getLastChildNode();
  if (last == null) {
    TreeElement chainLast = rawSetParents(first, this);
    setFirstChildNode(first);
    setLastChildNode(chainLast);
  }
  else {
    last.rawInsertAfterMeWithoutNotifications(first);
  }

  DebugUtil.checkTreeStructure(this);
}
 
Example #21
Source File: BaseParsingTestCase.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
protected PsiFile parseRawCode(@NotNull String code, boolean print) {
    myFile = createPsiFile("dummy", code);
    if (print) {
        System.out.println("» " + this.getClass());
        System.out.println(DebugUtil.psiToString(myFile, true, true));
    }
    return myFile;
}
 
Example #22
Source File: CompositeElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public ASTNode findChildByType(@Nonnull TokenSet typesSet, ASTNode anchor) {
  if (DebugUtil.CHECK_INSIDE_ATOMIC_ACTION_ENABLED) {
    assertReadAccessAllowed();
  }
  return TreeUtil.findSibling(anchor, typesSet);
}
 
Example #23
Source File: CompositeElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public ASTNode findChildByType(@Nonnull IElementType type, ASTNode anchor) {
  if (DebugUtil.CHECK_INSIDE_ATOMIC_ACTION_ENABLED) {
    assertReadAccessAllowed();
  }

  return TreeUtil.findSibling(anchor, type);
}
 
Example #24
Source File: BaseParsingTestCase.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
@NotNull
protected DuneFile parseDuneCode(@NotNull String code, boolean print) {
    myFile = createFile("jbuild", code);
    if (print) {
        System.out.println("» " + this.getClass());
        System.out.println(DebugUtil.psiToString(myFile, true, true));
    }
    return (DuneFile) myFile;
}
 
Example #25
Source File: ORSignatureTest.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
@SuppressWarnings({"SameParameterValue", "ConstantConditions"})
@NotNull
private ORSignature makeSignature(@NotNull Language lang, String sig, boolean debug) {
    PsiFileFactory instance = PsiFileFactory.getInstance(getProject());
    PsiFile psiFile = instance.createFileFromText("Dummy." + lang.getAssociatedFileType().getDefaultExtension(), lang, "let x:" + sig);
    if (debug) {
        System.out.println(DebugUtil.psiToString(psiFile, true, true));
    }
    Collection<PsiSignatureItem> items = PsiTreeUtil.findChildrenOfType(psiFile, PsiSignatureItem.class);
    return new ORSignature(RmlLanguage.INSTANCE, items);
}
 
Example #26
Source File: ORBasePlatformTestCase.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
@NotNull
@SuppressWarnings({"SameParameterValue", "WeakerAccess"})
protected FileBase configureCode(@NotNull String fileName, @NotNull String code, boolean debug) {
    PsiFile file = myFixture.configureByText(fileName, code);
    if (debug) {
        System.out.println("» " + fileName + " " + this.getClass());
        System.out.println(DebugUtil.psiToString(file, true, true));
    }
    return (FileBase) file;
}
 
Example #27
Source File: PsiTestUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void checkFileStructure(PsiFile file) throws IncorrectOperationException {
  String originalTree = DebugUtil.psiTreeToString(file, false);
  PsiFile dummyFile =
    PsiFileFactory.getInstance(file.getProject()).createFileFromText(file.getName(), file.getFileType(), file.getText());
  String reparsedTree = DebugUtil.psiTreeToString(dummyFile, false);
  Assert.assertEquals(reparsedTree, originalTree);
}
 
Example #28
Source File: StubBasedPsiElementBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * @return a not-null child of specified type, taken from stubs (if this element is currently stub-based) or AST (otherwise).
 */
@Nonnull
public <S extends StubElement, Psi extends PsiElement> Psi getRequiredStubOrPsiChild(@Nonnull IStubElementType<S, Psi> elementType) {
  Psi result = getStubOrPsiChild(elementType);
  if (result == null) {
    throw new AssertionError("Missing required child of type " + elementType + "; tree: " + DebugUtil.psiToString(this, false));
  }
  return result;
}
 
Example #29
Source File: StubTextInconsistencyException.java    From consulo with Apache License 2.0 5 votes vote down vote up
private StubTextInconsistencyException(String message, PsiFile file, List<PsiFileStub> fromText, List<PsiFileStub> fromPsi) {
  super(message);
  myStubsFromText = StringUtil.join(fromText, DebugUtil::stubTreeToString, "\n");
  myStubsFromPsi = StringUtil.join(fromPsi, DebugUtil::stubTreeToString, "\n");
  myFileName = file.getName();
  myFileText = file.getText();
}
 
Example #30
Source File: CompositeElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public ASTNode findChildByType(@Nonnull TokenSet types) {
  if (DebugUtil.CHECK_INSIDE_ATOMIC_ACTION_ENABLED) {
    assertReadAccessAllowed();
  }
  for (ASTNode element = getFirstChildNode(); element != null; element = element.getTreeNext()) {
    if (types.contains(element.getElementType())) return element;
  }
  return null;
}