Java 类com.intellij.psi.impl.PsiDocumentManagerImpl 实例源码

项目:intellij-ce-playground    文件:StatementMover.java   
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);
}
项目:tools-idea    文件:StatementMover.java   
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);
}
项目:tools-idea    文件:RangeMarkerTest.java   
public void testDocSynchronizerPrefersLineBoundaryChanges() throws Exception {
  RangeMarker marker = createMarker("import java.awt.List;\n" +
                                    "[import java.util.ArrayList;\n]" +
                                    "import java.util.HashMap;\n" +
                                    "import java.util.Map;");
  PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer();
  Document document = marker.getDocument();
  synchronizer.startTransaction(getProject(), document, null);

  String newText = StringUtil.replaceSubstring(document.getText(), TextRange.create(marker), "");
  synchronizer.replaceString(document, 0, document.getTextLength(), newText);

  final List<DocumentEvent> events = new ArrayList<DocumentEvent>();
  document.addDocumentListener(new DocumentAdapter() {
    @Override
    public void documentChanged(DocumentEvent e) {
      events.add(e);
    }
  });
  synchronizer.doCommitTransaction(document);

  assertEquals(newText, document.getText());
  DocumentEvent event = assertOneElement(events);
  assertEquals("DocumentEventImpl[myOffset=22, myOldLength=28, myNewLength=0, myOldString='import java.util.ArrayList;\n', myNewString=''].", event.toString());
}
项目:tools-idea    文件:RangeMarkerTest.java   
public void testPsi2DocReplaceAfterAdd() throws Exception {
  StringBuilder buffer = new StringBuilder("0123456789");
  RangeMarker marker = createMarker("0123456789", 2, 5);
  PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer();
  synchronizer.startTransaction(getProject(), marker.getDocument(), null);

  synchronizer.insertString(marker.getDocument(), 1, "a");
  buffer.insert(1, "a");

  synchronizer.replaceString(marker.getDocument(), 3, 4, "a");
  buffer.replace(3, 4, "a");

  synchronizer.doCommitTransaction(marker.getDocument());

  assertEquals(buffer.toString(), marker.getDocument().getText());

  assertValidMarker(marker, 3, 6);
}
项目:tools-idea    文件:RangeMarkerTest.java   
public void testPsi2DocMergeReplaceAfterAdd() throws Exception {
  StringBuilder buffer = new StringBuilder("0123456789");
  RangeMarker marker = createMarker("0123456789", 2, 5);
  PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer();
  synchronizer.startTransaction(getProject(), marker.getDocument(), null);

  synchronizer.insertString(marker.getDocument(), 1, "a");
  buffer.insert(1, "a");

  synchronizer.replaceString(marker.getDocument(), 3, 4, "a");
  buffer.replace(3, 4, "a");

  synchronizer.replaceString(marker.getDocument(), 3, 5, "bb");
  buffer.replace(3, 5, "bb");
  final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(marker.getDocument());
  final Set<Pair<PsiToDocumentSynchronizer.MutableTextRange, StringBuffer>> affectedFragments = transaction.getAffectedFragments();
  assertEquals(affectedFragments.size(), 2);

  synchronizer.doCommitTransaction(marker.getDocument());

  assertEquals(buffer.toString(), marker.getDocument().getText());

  assertValidMarker(marker, 3, 6);
}
项目:tools-idea    文件:RangeMarkerTest.java   
public void testPsi2DocMergeReplaceWithMultipleAdditions() throws Exception {
  StringBuilder buffer = new StringBuilder("0123456789");
  RangeMarker marker = createMarker("0123456789", 2, 5);
  PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer();
  synchronizer.startTransaction(getProject(), marker.getDocument(), null);

  synchronizer.replaceString(marker.getDocument(), 0, 10, "0");
  buffer.replace(0, 10, "0");

  for (int i = 1; i < 10; i++) {
    synchronizer.insertString(marker.getDocument(), i, "" + i);
    buffer.insert(i, "" + i);
  }
  final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(marker.getDocument());
  final Set<Pair<PsiToDocumentSynchronizer.MutableTextRange, StringBuffer>> affectedFragments = transaction.getAffectedFragments();
  assertEquals(1, affectedFragments.size());

  synchronizer.doCommitTransaction(marker.getDocument());

  assertEquals(buffer.toString(), marker.getDocument().getText());

  assertValidMarker(marker, 2, 5);
}
项目:tools-idea    文件:RangeMarkerTest.java   
public void testPsi2DocMergeMultipleAdditionsWithReplace() throws Exception {
  StringBuilder buffer = new StringBuilder("0123456789");
  RangeMarker marker = createMarker("0123456789", 2, 5);
  PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer();
  synchronizer.startTransaction(getProject(), marker.getDocument(), null);
  final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(marker.getDocument());
  final Set<Pair<PsiToDocumentSynchronizer.MutableTextRange, StringBuffer>> affectedFragments = transaction.getAffectedFragments();


  for (int i = 0; i < 10; i++) {
    synchronizer.insertString(marker.getDocument(), i, "" + i);
    buffer.insert(i, "" + i);
  }

  assertEquals(1, affectedFragments.size());
  synchronizer.replaceString(marker.getDocument(), 0, 20, "0123456789");
  buffer.replace(0, 20, "0123456789");

  assertEquals(1, affectedFragments.size());

  synchronizer.doCommitTransaction(marker.getDocument());

  assertEquals(buffer.toString(), marker.getDocument().getText());

  assertValidMarker(marker, 2, 5);
}
项目:tools-idea    文件:RangeMarkerTest.java   
public void testPsi2DocSurround() throws Exception {
  StringBuilder buffer = new StringBuilder("0123456789");
  RangeMarker marker = createMarker("0123456789", 2, 5);
  PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer();
  synchronizer.startTransaction(getProject(), marker.getDocument(), null);

  synchronizer.replaceString(marker.getDocument(), 3, 5, "3a4");
  buffer.replace(3, 5, "3a4");

  synchronizer.insertString(marker.getDocument(), 3, "b");
  buffer.insert(3, "b");

  synchronizer.insertString(marker.getDocument(), 7, "d");
  buffer.insert(7, "d");

  final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(marker.getDocument());
  final Set<Pair<PsiToDocumentSynchronizer.MutableTextRange, StringBuffer>> affectedFragments = transaction.getAffectedFragments();
  assertEquals(3, affectedFragments.size());

  synchronizer.doCommitTransaction(marker.getDocument());

  assertEquals(buffer.toString(), marker.getDocument().getText());

  assertValidMarker(marker, 2, 7);
}
项目:tools-idea    文件:RangeMarkerTest.java   
public void testPsi2DocForwardRangesChanges() throws Exception {
  StringBuilder buffer = new StringBuilder("0123456789");
  RangeMarker marker = createMarker("0123456789", 2, 5);
  PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer();
  Document document = marker.getDocument();
  synchronizer.startTransaction(getProject(), document, null);

  synchronizer.replaceString(document, 4, 5, "3a4");
  buffer.replace(4, 5, "3a4");

  synchronizer.insertString(document, 7, "b");
  buffer.insert(7, "b");

  synchronizer.insertString(document, 1, "b");
  buffer.insert(1, "b");

  synchronizer.doCommitTransaction(document);

  assertEquals(buffer.toString(), document.getText());

  assertValidMarker(marker, 3, 8);
}
项目:tools-idea    文件:MultiHostRegistrarImpl.java   
private static boolean cacheEverything(@NotNull Place place,
                                       @NotNull DocumentWindowImpl documentWindow,
                                       @NotNull InjectedFileViewProvider viewProvider,
                                       @NotNull PsiFile psiFile,
                                       @NotNull SmartPsiElementPointer<PsiLanguageInjectionHost> pointer) {
  FileDocumentManagerImpl.registerDocument(documentWindow, viewProvider.getVirtualFile());

  viewProvider.forceCachedPsi(psiFile);

  psiFile.putUserData(FileContextUtil.INJECTED_IN_ELEMENT, pointer);
  PsiDocumentManagerImpl.cachePsi(documentWindow, psiFile);

  keepTreeFromChameleoningBack(psiFile);

  return viewProvider.setShreds(place, psiFile.getProject());
}
项目:consulo-java    文件:StatementMover.java   
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);
}
项目:intellij-ce-playground    文件:LightPlatformTestCase.java   
public static PsiDocumentManagerImpl clearUncommittedDocuments(@NotNull Project project) {
  PsiDocumentManagerImpl documentManager = (PsiDocumentManagerImpl)PsiDocumentManager.getInstance(project);
  documentManager.clearUncommittedDocuments();

  ProjectManagerImpl projectManager = (ProjectManagerImpl)ProjectManager.getInstance();
  if (projectManager.isDefaultProjectInitialized()) {
    Project defaultProject = projectManager.getDefaultProject();
    ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(defaultProject)).clearUncommittedDocuments();
  }
  return documentManager;
}
项目:intellij-ce-playground    文件:FormattingDocumentModelImpl.java   
@Nullable
public static Document getDocumentToBeUsedFor(final PsiFile file) {
  final Project project = file.getProject();
  final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
  if (document == null) return null;
  if (PsiDocumentManager.getInstance(project).isUncommited(document)) return null;
  PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(project)).getSynchronizer();
  if (synchronizer.isDocumentAffectedByTransactions(document)) return null;

  return document;
}
项目:tools-idea    文件:PsiDocumentManagerImplTest.java   
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));
}
项目:tools-idea    文件:LightPlatformTestCase.java   
public static PsiDocumentManagerImpl clearUncommittedDocuments(@NotNull Project project) {
  PsiDocumentManagerImpl documentManager = (PsiDocumentManagerImpl)PsiDocumentManager.getInstance(project);
  documentManager.clearUncommittedDocuments();

  ProjectManagerImpl projectManager = (ProjectManagerImpl)ProjectManager.getInstance();
  if (projectManager.isDefaultProjectInitialized()) {
    Project defaultProject = projectManager.getDefaultProject();
    ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(defaultProject)).clearUncommittedDocuments();
  }
  return documentManager;
}
项目:tools-idea    文件:RangeMarkerTest.java   
public void testPsi2Doc1() throws Exception {
  StringBuilder buffer = new StringBuilder("0123456789");
  RangeMarker marker = createMarker("0123456789", 2, 5);
  PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer();
  synchronizer.startTransaction(getProject(), marker.getDocument(), null);

  synchronizer.insertString(marker.getDocument(), 3, "a");
  buffer.insert(3, "a");

  synchronizer.doCommitTransaction(marker.getDocument());

  assertEquals(buffer.toString(), marker.getDocument().getText());

  assertValidMarker(marker, 2, 6);
}
项目:tools-idea    文件:FormattingDocumentModelImpl.java   
@Nullable
public static Document getDocumentToBeUsedFor(final PsiFile file) {
  final Project project = file.getProject();
  final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
  if (document == null) return null;
  if (PsiDocumentManager.getInstance(project).isUncommited(document)) return null;
  PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(file.getProject())).getSynchronizer();
  if (synchronizer.isDocumentAffectedByTransactions(document)) return null;

  return document;
}
项目:consulo-csharp    文件:CSharpStatementMover.java   
@RequiredReadAction
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);
}
项目:consulo    文件:LightPlatformTestCase.java   
public static PsiDocumentManagerImpl clearUncommittedDocuments(@Nonnull Project project) {
  PsiDocumentManagerImpl documentManager = (PsiDocumentManagerImpl)PsiDocumentManager.getInstance(project);
  documentManager.clearUncommittedDocuments();

  ProjectManagerImpl projectManager = (ProjectManagerImpl)ProjectManager.getInstance();
  if (projectManager.isDefaultProjectInitialized()) {
    Project defaultProject = projectManager.getDefaultProject();
    ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(defaultProject)).clearUncommittedDocuments();
  }
  return documentManager;
}
项目:consulo    文件:RangeMarkerTest.java   
@RequiredDispatchThread
@Override
protected void setUp() throws Exception {
  super.setUp();
  documentManager = (PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject());
  synchronizer = documentManager.getSynchronizer();
}
项目:consulo    文件:PsiChangeHandler.java   
private void queueElement(@Nonnull PsiElement child, final boolean whitespaceOptimizationAllowed, @Nonnull 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));
  }
}
项目:consulo    文件:PsiEventWrapperAspect.java   
private static void promoteNonPhysicalChangesToDocument(ASTNode rootElement, PsiFile file) {
  if (file instanceof DummyHolder) return;
  if (((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(file.getProject())).isCommitInProgress()) return;

  VirtualFile vFile = file.getViewProvider().getVirtualFile();
  if (vFile instanceof LightVirtualFile && !(vFile instanceof VirtualFileWindow)) {
    Document document = FileDocumentManager.getInstance().getCachedDocument(vFile);
    if (document != null) {
      CharSequence text = rootElement.getChars();
      PsiToDocumentSynchronizer.performAtomically(file, () -> document.replaceString(0, document.getTextLength(), text));
    }
  }
}
项目:consulo    文件:FormattingDocumentModelImpl.java   
@Nullable
public static Document getDocumentToBeUsedFor(final PsiFile file) {
  final Project project = file.getProject();
  final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
  if (document == null) return null;
  if (PsiDocumentManager.getInstance(project).isUncommited(document)) return null;
  PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(file.getProject())).getSynchronizer();
  if (synchronizer.isDocumentAffectedByTransactions(document)) return null;

  return document;
}
项目:consulo-java    文件:PsiDocumentManagerImplTest.java   
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 =null; //TODO [VISTLAL] 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));
}
项目:intellij-ce-playground    文件:RangeMarkerTest.java   
@Override
protected void setUp() throws Exception {
  super.setUp();
  documentManager = (PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject());
  synchronizer = documentManager.getSynchronizer();
}
项目:tools-idea    文件:PsiDocumentManagerImplTest.java   
private PsiDocumentManagerImpl getPsiDocumentManager() {
  return (PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject());
}
项目:tools-idea    文件:PsiDocumentManagerImplTest.java   
public void testDocumentFromAlienProjectDoesNotEndUpInMyUncommittedList() 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() {
      @Override
      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);
  }
}
项目:tools-idea    文件:PsiDocumentManagerImplTest.java   
public void testDocumentFromAlienProjectGetsCommittedInBackground() throws Exception {
  LightVirtualFile virtualFile = createFile();
  PsiFile file = getPsiManager().findFile(virtualFile);

  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 PsiFile alienFile = alienManager.findFile(virtualFile);
    assertNotNull(alienFile);
    final PsiDocumentManagerImpl alienDocManager = (PsiDocumentManagerImpl)PsiDocumentManager.getInstance(alienProject);
    final Document alienDocument = alienDocManager.getDocument(alienFile);
    assertSame(document, alienDocument);
    assertEquals(0, alienDocManager.getUncommittedDocuments().length);
    assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);

    ApplicationManager.getApplication().runWriteAction(new Runnable() {
      @Override
      public void run() {
        document.setText("xxx");
        assertOrderedEquals(getPsiDocumentManager().getUncommittedDocuments(), document);
        assertOrderedEquals(alienDocManager.getUncommittedDocuments(), alienDocument);
      }
    });
    assertEquals("xxx", document.getText());
    assertEquals("xxx", alienDocument.getText());

    while (!getPsiDocumentManager().isCommitted(document)) {
      UIUtil.dispatchAllInvocationEvents();
    }
    long start = System.currentTimeMillis();
    while (!alienDocManager.isCommitted(alienDocument) && System.currentTimeMillis()-start < 20000) {
      UIUtil.dispatchAllInvocationEvents();
    }
    assertTrue("Still not committed: "+alienDocument, alienDocManager.isCommitted(alienDocument));
  }
  finally {
    ProjectUtil.closeAndDispose(alienProject);
  }
}
项目:tools-idea    文件:DaemonCodeAnalyzerImpl.java   
@NotNull
private Runnable createUpdateRunnable() {
  return new Runnable() {
    @Override
    public void run() {
      if (myDisposed || !myProject.isInitialized()) return;
      if (PowerSaveMode.isEnabled()) return;
      Editor activeEditor = FileEditorManager.getInstance(myProject).getSelectedTextEditor();

      final PsiDocumentManagerImpl documentManager = (PsiDocumentManagerImpl)PsiDocumentManager.getInstance(myProject);
      Runnable runnable = new Runnable() {
        @Override
        public void run() {
          PassExecutorService.log(getUpdateProgress(), null, "Update Runnable. myUpdateByTimerEnabled:",
                                  myUpdateByTimerEnabled, " something disposed:",
                                  PowerSaveMode.isEnabled() || myDisposed || !myProject.isInitialized(), " activeEditors:",
                                  myProject.isDisposed() ? null : getSelectedEditors());
          if (!myUpdateByTimerEnabled) return;
          if (myDisposed) return;
          ApplicationManager.getApplication().assertIsDispatchThread();

          final Collection<FileEditor> activeEditors = getSelectedEditors();
          if (activeEditors.isEmpty()) return;

          ApplicationManager.getApplication().assertIsDispatchThread();
          if (ApplicationManager.getApplication().isWriteAccessAllowed()) {
            // makes no sense to start from within write action, will cancel anyway
            // we'll restart when write action finish
            return;
          }
          if (documentManager.hasUncommitedDocuments()) {
            documentManager.cancelAndRunWhenAllCommitted("restart daemon when all committed", this);
            return;
          }

          Map<FileEditor, HighlightingPass[]> passes = new THashMap<FileEditor, HighlightingPass[]>(activeEditors.size());
          for (FileEditor fileEditor : activeEditors) {
            BackgroundEditorHighlighter highlighter = fileEditor.getBackgroundHighlighter();
            if (highlighter != null) {
              HighlightingPass[] highlightingPasses = highlighter.createPassesForEditor();
              passes.put(fileEditor, highlightingPasses);
            }
          }
          // cancel all after calling createPasses() since there are perverts {@link com.intellij.util.xml.ui.DomUIFactoryImpl} who are changing PSI there
          cancelUpdateProgress(true, "Cancel by alarm");
          myAlarm.cancelAllRequests();
          DaemonProgressIndicator progress = createUpdateProgress();
          myPassExecutorService.submitPasses(passes, progress, Job.DEFAULT_PRIORITY);
        }
      };


      if (activeEditor == null) {
        runnable.run();
      }
      else {
        documentManager.cancelAndRunWhenAllCommitted("start daemon when all committed", runnable);
      }
    }
  };
}
项目:consulo    文件:LightPlatformTestCase.java   
public static void doTearDown(@Nonnull final Project project, ApplicationStarter application, boolean checkForEditors) throws Exception {
  CodeStyleSettingsManager.getInstance(project).dropTemporarySettings();
  checkAllTimersAreDisposed();
  UsefulTestCase.doPostponedFormatting(project);

  LookupManager lookupManager = LookupManager.getInstance(project);
  if (lookupManager != null) {
    lookupManager.hideActiveLookup();
  }
  ((StartupManagerImpl)StartupManager.getInstance(project)).prepareForNextTest();
  InspectionProfileManager.getInstance().deleteProfile(PROFILE);
  assertNotNull("Application components damaged", ProjectManager.getInstance());

  new WriteCommandAction.Simple(project) {
    @Override
    @RequiredWriteAction
    protected void run() throws Throwable {
      if (ourSourceRoot != null) {
        try {
          final VirtualFile[] children = ourSourceRoot.getChildren();
          for (VirtualFile child : children) {
            child.delete(this);
          }
        }
        catch (IOException e) {
          //noinspection CallToPrintStackTrace
          e.printStackTrace();
        }
      }
      EncodingManager encodingManager = EncodingManager.getInstance();
      if (encodingManager instanceof EncodingManagerImpl) ((EncodingManagerImpl)encodingManager).clearDocumentQueue();

      FileDocumentManager manager = FileDocumentManager.getInstance();

      ApplicationManager.getApplication().runWriteAction(EmptyRunnable.getInstance()); // Flush postponed formatting if any.
      manager.saveAllDocuments();
      if (manager instanceof FileDocumentManagerImpl) {
        ((FileDocumentManagerImpl)manager).dropAllUnsavedDocuments();
      }
    }
  }.execute().throwException();

  assertFalse(PsiManager.getInstance(project).isDisposed());
  if (!ourAssertionsInTestDetected) {
    if (IdeaLogger.ourErrorsOccurred != null) {
      throw IdeaLogger.ourErrorsOccurred;
    }
  }
  PsiDocumentManagerImpl documentManager = clearUncommittedDocuments(project);
  ((HintManagerImpl)HintManager.getInstance()).cleanup();

  UIUtil.invokeAndWaitIfNeeded(new Runnable() {
    @Override
    public void run() {
      ((UndoManagerImpl)UndoManager.getGlobalInstance()).dropHistoryInTests();
      ((UndoManagerImpl)UndoManager.getInstance(project)).dropHistoryInTests();

      UIUtil.dispatchAllInvocationEvents();
    }
  });

  TemplateDataLanguageMappings.getInstance(project).cleanupForNextTest();

  ProjectManagerEx.getInstanceEx().closeTestProject(project);
  //application.setDataProvider(null);
  ourTestCase = null;
  ((PsiManagerImpl)PsiManager.getInstance(project)).cleanupForNextTest();

  CompletionProgressIndicator.cleanupForNextTest();

  if (checkForEditors) {
    checkEditorsReleased();
  }
  if (isLight(project)) {
    // mark temporarily as disposed so that rogue component trying to access it will fail
    ((ProjectImpl)project).setTemporarilyDisposed(true);
    documentManager.clearUncommittedDocuments();
  }
}
项目:consulo-java    文件:PsiDocumentManagerImplTest.java   
private PsiDocumentManagerImpl getPsiDocumentManager()
{
    return (PsiDocumentManagerImpl) PsiDocumentManager.getInstance(getProject());
}
项目:consulo-java    文件:PsiDocumentManagerImplTest.java   
public void testDocumentFromAlienProjectDoesNotEndUpInMyUncommittedList() 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()
        {
            @Override
            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);
    }
}
项目:consulo-java    文件:PsiDocumentManagerImplTest.java   
public void testDocumentFromAlienProjectGetsCommittedInBackground() throws Exception
{
    LightVirtualFile virtualFile = createFile();
    PsiFile file = getPsiManager().findFile(virtualFile);

    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 PsiFile alienFile = alienManager.findFile(virtualFile);
        assertNotNull(alienFile);
        final PsiDocumentManagerImpl alienDocManager = (PsiDocumentManagerImpl) PsiDocumentManager.getInstance(alienProject);
        final Document alienDocument = alienDocManager.getDocument(alienFile);
        assertSame(document, alienDocument);
        assertEquals(0, alienDocManager.getUncommittedDocuments().length);
        assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);

        ApplicationManager.getApplication().runWriteAction(new Runnable()
        {
            @Override
            public void run()
            {
                document.setText("xxx");
                assertOrderedEquals(getPsiDocumentManager().getUncommittedDocuments(), document);
                assertOrderedEquals(alienDocManager.getUncommittedDocuments(), alienDocument);
            }
        });
        assertEquals("xxx", document.getText());
        assertEquals("xxx", alienDocument.getText());

        while(!getPsiDocumentManager().isCommitted(document))
        {
            UIUtil.dispatchAllInvocationEvents();
        }
        long start = System.currentTimeMillis();
        while(!alienDocManager.isCommitted(alienDocument) && System.currentTimeMillis() - start < 20000)
        {
            UIUtil.dispatchAllInvocationEvents();
        }
        assertTrue("Still not committed: " + alienDocument, alienDocManager.isCommitted(alienDocument));
    }
    finally
    {
        ProjectUtil.closeAndDispose(alienProject);
    }
}