Search in sources :

Example 1 with PsiModificationTracker

use of com.intellij.psi.util.PsiModificationTracker in project intellij-community by JetBrains.

the class PsiModificationTrackerTest method testNoIncrementOnReadOnlyStatusChange.

public void testNoIncrementOnReadOnlyStatusChange() throws IOException {
    VirtualFile file = addFileToProject("Foo.java", "class Foo {}").getVirtualFile();
    PsiModificationTracker tracker = PsiManager.getInstance(getProject()).getModificationTracker();
    long mc = tracker.getModificationCount();
    WriteAction.run(() -> file.setWritable(false));
    assertEquals(mc, tracker.getModificationCount());
    PlatformTestUtil.tryGcSoftlyReachableObjects();
    assertNull(PsiManagerEx.getInstanceEx(myProject).getFileManager().getCachedPsiFile(file));
    WriteAction.run(() -> file.setWritable(true));
    assertEquals(mc, tracker.getModificationCount());
}
Also used : PsiModificationTracker(com.intellij.psi.util.PsiModificationTracker)

Example 2 with PsiModificationTracker

use of com.intellij.psi.util.PsiModificationTracker in project intellij-community by JetBrains.

the class PsiModificationTrackerTest method testClassShouldNotDisappearWithoutEvents.

public void testClassShouldNotDisappearWithoutEvents() throws Exception {
    PsiModificationTracker tracker = PsiManager.getInstance(getProject()).getModificationTracker();
    long count0 = tracker.getJavaStructureModificationCount();
    final VirtualFile file = addFileToProject("Foo.java", "class Foo {}").getVirtualFile();
    final Document document = FileDocumentManager.getInstance().getDocument(file);
    assertNotNull(document);
    assertNotNull(JavaPsiFacade.getInstance(getProject()).findClass("Foo", GlobalSearchScope.allScope(getProject())));
    long count1 = tracker.getJavaStructureModificationCount();
    assertFalse(count1 == count0);
    WriteCommandAction.runWriteCommandAction(getProject(), () -> document.deleteString(0, document.getTextLength()));
    DocumentCommitThread.getInstance().waitForAllCommits();
    // gc softly-referenced file and AST
    PlatformTestUtil.tryGcSoftlyReachableObjects();
    final PsiManagerEx psiManager = PsiManagerEx.getInstanceEx(getProject());
    assertNull(psiManager.getFileManager().getCachedPsiFile(file));
    assertFalse(count1 == tracker.getJavaStructureModificationCount());
    assertNull(JavaPsiFacade.getInstance(getProject()).findClass("Foo", GlobalSearchScope.allScope(getProject())));
}
Also used : PsiModificationTracker(com.intellij.psi.util.PsiModificationTracker) Document(com.intellij.openapi.editor.Document) PsiManagerEx(com.intellij.psi.impl.PsiManagerEx)

Example 3 with PsiModificationTracker

use of com.intellij.psi.util.PsiModificationTracker in project intellij-community by JetBrains.

the class PsiModificationTrackerTest method testClassShouldNotAppearWithoutEvents_WithoutPsi.

public void testClassShouldNotAppearWithoutEvents_WithoutPsi() throws Exception {
    final GlobalSearchScope allScope = GlobalSearchScope.allScope(getProject());
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
    final PsiManager psiManager = PsiManager.getInstance(getProject());
    final PsiModificationTracker tracker = psiManager.getModificationTracker();
    final VirtualFile file = createTempFile("java", null, "", CharsetToolkit.UTF8_CHARSET);
    final Document document = FileDocumentManager.getInstance().getDocument(file);
    assertNotNull(document);
    assertNull(facade.findClass("Foo", allScope));
    long count1 = tracker.getJavaStructureModificationCount();
    PlatformTestUtil.tryGcSoftlyReachableObjects();
    assertNull(PsiDocumentManager.getInstance(getProject()).getCachedPsiFile(document));
    WriteCommandAction.runWriteCommandAction(getProject(), () -> document.insertString(0, "class Foo {}"));
    DocumentCommitThread.getInstance().waitForAllCommits();
    assertFalse(count1 == tracker.getJavaStructureModificationCount());
    assertTrue(PsiDocumentManager.getInstance(getProject()).isCommitted(document));
    assertNotNull(facade.findClass("Foo", allScope));
    PsiJavaFile psiFile = (PsiJavaFile) psiManager.findFile(file);
    assertSize(1, psiFile.getClasses());
    assertEquals("class Foo {}", psiFile.getText());
    assertEquals("class Foo {}", psiFile.getNode().getText());
}
Also used : PsiModificationTracker(com.intellij.psi.util.PsiModificationTracker) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Document(com.intellij.openapi.editor.Document)

Example 4 with PsiModificationTracker

use of com.intellij.psi.util.PsiModificationTracker in project intellij-community by JetBrains.

the class PsiModificationTrackerTest method testClassShouldNotAppearWithoutEvents_WithPsi.

public void testClassShouldNotAppearWithoutEvents_WithPsi() throws IOException {
    final VirtualFile file = createTempFile("java", null, "", CharsetToolkit.UTF8_CHARSET);
    final Document document = FileDocumentManager.getInstance().getDocument(file);
    assertNotNull(document);
    assertNull(JavaPsiFacade.getInstance(getProject()).findClass("Foo", GlobalSearchScope.allScope(getProject())));
    PsiManager psiManager = PsiManager.getInstance(getProject());
    PsiModificationTracker tracker = psiManager.getModificationTracker();
    long count1 = tracker.getJavaStructureModificationCount();
    PsiJavaFile psiFile = (PsiJavaFile) psiManager.findFile(file);
    WriteCommandAction.runWriteCommandAction(getProject(), () -> document.insertString(0, "class Foo {}"));
    // no PSI changes yet
    assertEquals(count1, tracker.getJavaStructureModificationCount());
    //so the class should not exist
    assertNull(JavaPsiFacade.getInstance(getProject()).findClass("Foo", GlobalSearchScope.allScope(getProject())));
    assertSize(0, psiFile.getClasses());
    assertEquals("", psiManager.findFile(file).getText());
    PlatformTestUtil.tryGcSoftlyReachableObjects();
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    assertFalse(count1 == tracker.getJavaStructureModificationCount());
    assertNotNull(JavaPsiFacade.getInstance(getProject()).findClass("Foo", GlobalSearchScope.allScope(getProject())));
    assertEquals("class Foo {}", psiManager.findFile(file).getText());
    assertEquals("class Foo {}", psiManager.findFile(file).getNode().getText());
    assertSize(1, psiFile.getClasses());
}
Also used : PsiModificationTracker(com.intellij.psi.util.PsiModificationTracker) Document(com.intellij.openapi.editor.Document)

Example 5 with PsiModificationTracker

use of com.intellij.psi.util.PsiModificationTracker in project intellij-community by JetBrains.

the class PsiModificationTrackerTest method testClassShouldNotDisappearWithoutEvents_InCodeBlock.

public void testClassShouldNotDisappearWithoutEvents_InCodeBlock() throws Exception {
    PsiModificationTracker tracker = PsiManager.getInstance(getProject()).getModificationTracker();
    String barStr = "class Bar {}";
    PsiFile file = addFileToProject("Foo.java", "class Foo {{" + barStr + "}}");
    JBIterable<PsiClass> barQuery = SyntaxTraverser.psiTraverser(file).filter(PsiClass.class).filter(o -> "Bar".equals(o.getName()));
    assertNotNull(barQuery.first());
    Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file);
    int index = document.getText().indexOf(barStr);
    long count1 = tracker.getJavaStructureModificationCount();
    //WriteCommandAction.runWriteCommandAction(getProject(), () -> bar.delete());
    WriteCommandAction.runWriteCommandAction(getProject(), () -> document.replaceString(index, index + barStr.length(), ""));
    PsiDocumentManager.getInstance(getProject()).commitDocument(document);
    assertNull(barQuery.first());
    assertFalse(count1 == tracker.getJavaStructureModificationCount());
}
Also used : PsiModificationTracker(com.intellij.psi.util.PsiModificationTracker) Document(com.intellij.openapi.editor.Document)

Aggregations

PsiModificationTracker (com.intellij.psi.util.PsiModificationTracker)18 PsiManagerEx (com.intellij.psi.impl.PsiManagerEx)7 Document (com.intellij.openapi.editor.Document)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 File (java.io.File)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Project (com.intellij.openapi.project.Project)1 PsiModificationTrackerImpl (com.intellij.psi.impl.PsiModificationTrackerImpl)1 PsiEventType (com.intellij.psi.impl.PsiTreeChangeEventImpl.PsiEventType)1