private void queueElement(
      @NotNull PsiElement child,
      final boolean whitespaceOptimizationAllowed,
      @NotNull PsiTreeChangeEvent event) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    PsiFile file = event.getFile();
    if (file == null) file = child.getContainingFile();
    if (file == null) {
      myFileStatusMap.markAllFilesDirty(child);
      return;
    }

    if (!child.isValid()) return;

    PsiDocumentManagerImpl pdm = (PsiDocumentManagerImpl) PsiDocumentManager.getInstance(myProject);
    Document document = pdm.getCachedDocument(file);
    if (document != null) {
      if (pdm.getSynchronizer().getTransaction(document) == null) {
        // content reload, language level change or some other big change
        myFileStatusMap.markAllFilesDirty(child);
        return;
      }

      List<Pair<PsiElement, Boolean>> toUpdate = changedElements.get(document);
      if (toUpdate == null) {
        toUpdate = new SmartList<>();
        changedElements.put(document, toUpdate);
      }
      toUpdate.add(Pair.create(child, whitespaceOptimizationAllowed));
    }
  }
Ejemplo n.º 2
0
 private static LineRange expandLineRangeToCoverPsiElements(
     final LineRange range, Editor editor, final PsiFile file) {
   Pair<PsiElement, PsiElement> psiRange = getElementRange(editor, file, range);
   if (psiRange == null) return null;
   final PsiElement parent =
       PsiTreeUtil.findCommonParent(psiRange.getFirst(), psiRange.getSecond());
   Pair<PsiElement, PsiElement> elementRange =
       getElementRange(parent, psiRange.getFirst(), psiRange.getSecond());
   if (elementRange == null) return null;
   int endOffset = elementRange.getSecond().getTextRange().getEndOffset();
   Document document = editor.getDocument();
   if (endOffset > document.getTextLength()) {
     LOG.assertTrue(!PsiDocumentManager.getInstance(file.getProject()).isUncommited(document));
     LOG.assertTrue(PsiDocumentManagerImpl.checkConsistency(file, document));
   }
   int endLine;
   if (endOffset == document.getTextLength()) {
     endLine = document.getLineCount();
   } else {
     endLine = editor.offsetToLogicalPosition(endOffset).line + 1;
     endLine = Math.min(endLine, document.getLineCount());
   }
   int startLine =
       Math.min(
           range.startLine,
           editor.offsetToLogicalPosition(elementRange.getFirst().getTextOffset()).line);
   endLine = Math.max(endLine, range.endLine);
   return new LineRange(startLine, endLine);
 }
 @TestOnly
 public void checkAllTreesEqual() {
   Collection<PsiFile> roots = myRoots.values();
   PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getManager().getProject());
   documentManager.commitAllDocuments();
   for (PsiFile root : roots) {
     Document document = documentManager.getDocument(root);
     PsiDocumentManagerImpl.checkConsistency(root, document);
     assert root.getText().equals(document.getText());
   }
 }
  public void testDocumentGced() throws Exception {
    VirtualFile vFile = createFile();
    PsiDocumentManagerImpl documentManager = getPsiDocumentManager();
    long id = System.identityHashCode(documentManager.getDocument(getPsiManager().findFile(vFile)));

    documentManager.commitAllDocuments();
    UIUtil.dispatchAllInvocationEvents();
    UIUtil.dispatchAllInvocationEvents();
    assertEmpty(documentManager.getUncommittedDocuments());

    LeakHunter.checkLeak(documentManager, DocumentImpl.class);
    LeakHunter.checkLeak(
        documentManager,
        PsiFileImpl.class,
        new Processor<PsiFileImpl>() {
          @Override
          public boolean process(PsiFileImpl psiFile) {
            return psiFile.getViewProvider().getVirtualFile().getFileSystem()
                instanceof LocalFileSystem;
          }
        });
    // Class.forName("com.intellij.util.ProfilingUtil").getDeclaredMethod("forceCaptureMemorySnapshot").invoke(null);

    Reference<Document> reference = vFile.getUserData(FileDocumentManagerImpl.DOCUMENT_KEY);
    assertNotNull(reference);
    for (int i = 0; i < 1000; i++) {
      UIUtil.dispatchAllInvocationEvents();
      if (reference.get() == null) break;
      System.gc();
    }
    assertNull(documentManager.getCachedDocument(getPsiManager().findFile(vFile)));

    Document newDoc = documentManager.getDocument(getPsiManager().findFile(vFile));
    assertTrue(id != System.identityHashCode(newDoc));
  }
 private RangeMarkerEx createMarker(PsiFile psiFile, final int start, final int end) {
   document = documentManager.getDocument(psiFile);
   return (RangeMarkerEx) document.createRangeMarker(start, end);
 }
 @Override
 protected void setUp() throws Exception {
   super.setUp();
   documentManager = (PsiDocumentManagerImpl) PsiDocumentManager.getInstance(getProject());
   synchronizer = documentManager.getSynchronizer();
 }
  public void testDocumentFromAlienProjectDoesNotEndsUpInMyUncommittedList() throws Exception {
    PsiFile file = getPsiManager().findFile(createFile());

    final Document document = getPsiDocumentManager().getDocument(file);

    File temp = createTempDirectory();
    final Project alienProject =
        createProject(new File(temp, "alien.ipr"), DebugUtil.currentStackTrace());
    boolean succ2 = ProjectManagerEx.getInstanceEx().openProject(alienProject);
    assertTrue(succ2);

    try {
      PsiManager alienManager = PsiManager.getInstance(alienProject);
      final String alienText = "alien";

      LightVirtualFile alienVirt = new LightVirtualFile("foo.java", alienText);
      final PsiFile alienFile = alienManager.findFile(alienVirt);
      final PsiDocumentManagerImpl alienDocManager =
          (PsiDocumentManagerImpl) PsiDocumentManager.getInstance(alienProject);
      final Document alienDocument = alienDocManager.getDocument(alienFile);
      // alienDocument.putUserData(CACHED_VIEW_PROVIDER, new MockFileViewProvider(alienFile));
      assertEquals(0, alienDocManager.getUncommittedDocuments().length);
      assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);

      ApplicationManager.getApplication()
          .runWriteAction(
              new Runnable() {
                public void run() {
                  getPsiDocumentManager()
                      .documentChanged(
                          new DocumentEventImpl(
                              alienDocument,
                              0,
                              "",
                              "",
                              alienDocument.getModificationStamp(),
                              false));
                  assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);
                  assertEquals(0, alienDocManager.getUncommittedDocuments().length);

                  alienDocManager.documentChanged(
                      new DocumentEventImpl(
                          alienDocument, 0, "", "", alienDocument.getModificationStamp(), false));
                  assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);
                  assertEquals(1, alienDocManager.getUncommittedDocuments().length);

                  getPsiDocumentManager()
                      .documentChanged(
                          new DocumentEventImpl(
                              document, 0, "", "", document.getModificationStamp(), false));
                  assertEquals(1, getPsiDocumentManager().getUncommittedDocuments().length);
                  assertEquals(1, alienDocManager.getUncommittedDocuments().length);

                  alienDocManager.documentChanged(
                      new DocumentEventImpl(
                          document, 0, "", "", document.getModificationStamp(), false));
                  assertEquals(1, getPsiDocumentManager().getUncommittedDocuments().length);
                  assertEquals(1, alienDocManager.getUncommittedDocuments().length);
                }
              });
    } finally {
      ProjectUtil.closeAndDispose(alienProject);
    }
  }