void () { final PyFile file = getTestFile("LoadingTreeRetainsKnownPsiElement.py"); final List<PyClass> classes = file.getTopLevelClasses(); assertEquals(1, classes.size()); final PyClass pyClass = classes.get(0); assertEquals("SomeClass", pyClass.getName()); // Ensure we haven't loaded the tree yet. final PyFileImpl fileImpl = (PyFileImpl)file; assertNull(fileImpl.getTreeElement()); final PsiElement[] children = file.getChildren(); // Load the tree assertNotNull(fileImpl.getTreeElement()); assertEquals(1, children.length); assertSame(pyClass, children[0]); WriteCommandAction.writeCommandAction(myFixture.getProject(), fileImpl).run(() -> { pyClass.setName("RenamedClass"); assertEquals("RenamedClass", pyClass.getName()); }); StubElement fileStub = fileImpl.getStub(); assertNull("There should be no stub if file holds tree element", fileStub); WriteCommandAction.writeCommandAction(myFixture.getProject(), fileImpl).run(() -> ((SingleRootFileViewProvider)fileImpl.getViewProvider()).onContentReload()); assertNull(fileImpl.getTreeElement()); // Test unload succeeded. assertEquals("RenamedClass", fileImpl.getTopLevelClasses().get(0).getName()); }