// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.psi.impl.smartPointers;

import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.JavaCodeInsightTestCase;
import com.intellij.ide.highlighter.HtmlFileType;
import com.intellij.ide.highlighter.JavaFileType;
import com.intellij.ide.highlighter.XmlFileType;
import com.intellij.lang.FileASTNode;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.diagnostic.DefaultLogger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.EditorModificationUtilEx;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.event.DocumentListener;
import com.intellij.openapi.editor.event.EditorEventMulticaster;
import com.intellij.openapi.editor.ex.DocumentEx;
import com.intellij.openapi.editor.impl.FrozenDocument;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileTypes.PlainTextFileType;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Segment;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.PsiFileImpl;
import com.intellij.psi.impl.source.PsiJavaFileImpl;
import com.intellij.psi.impl.source.tree.FileElement;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.stubs.StubElement;
import com.intellij.psi.stubs.StubTree;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.PsiUtilBase;
import com.intellij.psi.xml.XmlTag;
import com.intellij.testFramework.*;
import com.intellij.util.FileContentUtil;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ref.GCUtil;
import com.intellij.util.ref.GCWatcher;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

@HeavyPlatformTestCase.WrapInCommand
@SkipSlowTestLocally
public class SmartPsiElementPointersTest extends JavaCodeInsightTestCase {
  private VirtualFile myRoot;

  @Override
  protected void setUp() throws Exception {
    super.setUp();

    String root = JavaTestUtil.getJavaTestDataPath() + "/codeEditor/smartPsiElementPointers";
    PsiTestUtil.removeAllRoots(myModule, IdeaTestUtil.getMockJdk17());
    myRoot = createTestProjectStructure( root);
  }

  public void testChangeInDocument() {
    PsiClass aClass = myJavaFacade.findClass("AClass", GlobalSearchScope.allScope(getProject()));
    assertNotNull(aClass);

    SmartPsiElementPointer<PsiClass> pointer = createPointer(aClass);
    Document document = PsiDocumentManager.getInstance(myProject).getDocument(aClass.getContainingFile());

    int offset = aClass.getTextOffset();
    insertString(document, offset, "/**/");
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();

    PsiElement element = pointer.getElement();
    assertNotNull(element);
    assertTrue(element instanceof PsiClass);
    assertTrue(element.isValid());
  }

  private static void insertString(Document document, int offset, String s) {
    ApplicationManager.getApplication().runWriteAction(() -> document.insertString(offset, s));
  }

  // This test is unfair. If pointer is asked for getElement() between commits it'll never restore again anyway.
  //
  public void testChangeInDocumentTwice() {
    PsiClass aClass = myJavaFacade.findClass("AClass",GlobalSearchScope.allScope(getProject()));
    assertNotNull(aClass);

    SmartPsiElementPointer<PsiClass> pointer = createPointer(aClass);
    Document document = PsiDocumentManager.getInstance(myProject).getDocument(aClass.getContainingFile());

    int offset = aClass.getTextOffset();
    insertString(document, offset, "/*");
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();
    insertString(document, offset + 2, "*/");
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();

    PsiElement element = pointer.getElement();
    assertNotNull(element);
    assertTrue(element instanceof PsiClass);
    assertTrue(element.isValid());
  }

  public void testGetElementWhenDocumentModified() {
    PsiClass aClass = myJavaFacade.findClass("AClass",GlobalSearchScope.allScope(getProject()));
    assertNotNull(aClass);

    SmartPsiElementPointer<PsiClass> pointer = createPointer(aClass);
    Document document = PsiDocumentManager.getInstance(myProject).getDocument(aClass.getContainingFile());

    int offset = aClass.getTextOffset();
    insertString(document, offset, "/**/");
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();

    insertString(document, offset, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

    PsiElement element = pointer.getElement();
    assertNotNull(element);
    assertTrue(element instanceof PsiClass);
    assertTrue(element.isValid());
  }

  public void testKeepBeltWhenDocumentModified() {
    PsiClass aClass = myJavaFacade.findClass("AClass",GlobalSearchScope.allScope(getProject()));
    assertNotNull(aClass);

    SmartPsiElementPointer<PsiClass> pointer = createPointer(aClass);
    Document document = PsiDocumentManager.getInstance(myProject).getDocument(aClass.getContainingFile());

    int offset = aClass.getTextOffset();
    insertString(document, offset, "/******/");

    pointer.getElement();

    insertString(document, offset, "/**/");
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();

    PsiElement element = pointer.getElement();
    assertNotNull(element);
    assertTrue(element instanceof PsiClass);
    assertTrue(element.isValid());
  }

  public void testRetrieveOnUncommittedDocument() {
    ApplicationManager.getApplication().runWriteAction(() -> {
      PsiClass aClass = myJavaFacade.findClass("AClass", GlobalSearchScope.allScope(getProject()));
      assertNotNull(aClass);

      PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject);
      Document document = documentManager.getDocument(aClass.getContainingFile());
      document.insertString(0, "/******/");

      SmartPointerEx<PsiIdentifier> pointer = createPointer(aClass.getNameIdentifier());

      document.insertString(0, "/**/");
      documentManager.commitAllDocuments();

      PsiElement element = pointer.getElement();
      assertNotNull(element);
      assertTrue(element.getParent() instanceof PsiClass);
      assertTrue(element.isValid());
    });
  }

  public void testNoAstLoadingWithoutDocumentChanges() {
    PsiClass aClass = myJavaFacade.findClass("Test",GlobalSearchScope.allScope(getProject()));
    assertNotNull(aClass);
    PsiFileImpl file = (PsiFileImpl)aClass.getContainingFile();

    createEditor(file.getVirtualFile());
    assertFalse(file.isContentsLoaded());

    SmartPointerEx<PsiClass> pointer = createPointer(aClass);
    assertFalse(file.isContentsLoaded());

    //noinspection UnusedAssignment
    aClass = null;
    gcPointerCache(pointer);

    assertNotNull(pointer.getElement());
    assertFalse(file.isContentsLoaded());
  }

  private static void gcPointerCache(SmartPsiElementPointer<?>... pointers) {
    GCWatcher.tracking(ContainerUtil.map(pointers, p -> ((SmartPointerEx<?>) p).getCachedElement())).ensureCollected();
    for (SmartPsiElementPointer<?> pointer : pointers) {
      assertNull(((SmartPointerEx<?>)pointer).getCachedElement());
    }
  }

  public void testTextFileClearingDoesNotCrash() {
    configureByText(PlainTextFileType.INSTANCE, "foo bar goo\n");
    SmartPsiElementPointer<PsiElement> pointer = createPointer(myFile.getFirstChild());

    GCWatcher.tracking(myFile.getNode()).ensureCollected();
    assertEquals(myFile.getFirstChild(), pointer.getElement());

    Document document = myFile.getViewProvider().getDocument();
    ApplicationManager.getApplication().runWriteAction(() -> {
      document.deleteString(0, document.getTextLength());

      GCWatcher.tracking(myFile.getNode()).ensureCollected();
      assertEquals(myFile.getFirstChild(), pointer.getElement());

      PsiDocumentManager.getInstance(myProject).commitAllDocuments();
    });

    // Wait for com.intellij.codeInsight.daemon.impl.PsiChangeHandler$Change to release the reference
    GCWatcher.tracking(myFile.getNode()).ensureCollectedWithinTimeout(5_000);

    assertEquals(myFile.getFirstChild(), pointer.getElement());
  }

  public void testChangeInPsi() {
    PsiClass aClass = myJavaFacade.findClass("AClass",GlobalSearchScope.allScope(getProject()));
    assertNotNull(aClass);

    SmartPsiElementPointer<PsiClass> pointer = createPointer(aClass);
    Document document = PsiDocumentManager.getInstance(myProject).getDocument(aClass.getContainingFile());

    int offset = aClass.getTextOffset();
    insertString(document, offset, "/**/");
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();

    PsiElement element = pointer.getElement();
    assertNotNull(element);
    assertTrue(element instanceof PsiClass);
    assertTrue(element.isValid());
  }

  public void testPsiChangesWithLazyPointers() {
    PsiClass aClass = myJavaFacade.findClass("AClass", GlobalSearchScope.allScope(getProject()));
    assertNotNull(aClass);

    final SmartPsiElementPointer<PsiIdentifier> pointer =
      createPointer(aClass.getNameIdentifier());
    final PsiComment javadoc =
      JavaPsiFacade.getElementFactory(aClass.getProject()).createCommentFromText("/** javadoc */", aClass);
    ApplicationManager.getApplication().runWriteAction(() -> {
      aClass.getParent().addBefore(javadoc, aClass);
    });


    final PsiIdentifier elt = pointer.getElement();
    assertNotNull(elt);
    assertSame(elt, aClass.getNameIdentifier());
  }

  public void testTypePointer() {
    PsiClass aClass = myJavaFacade.findClass("AClass",GlobalSearchScope.allScope(getProject()));
    final PsiTypeElement typeElement = myJavaFacade.findClass("Test",GlobalSearchScope.allScope(getProject())).getFields()[0].getTypeElement();

    SmartPsiElementPointer<PsiTypeElement> typePointer = createPointer(typeElement);
    SmartPsiElementPointer<PsiClass> classPointer = createPointer(aClass);

    Document aClassDocument = PsiDocumentManager.getInstance(myProject).getDocument(aClass.getContainingFile());
    Document testDocument = PsiDocumentManager.getInstance(myProject).getDocument(typeElement.getContainingFile());
    assertNotSame(aClassDocument, testDocument);

    insertString(aClassDocument, aClass.getTextOffset(), "/**/");
    insertString(testDocument, typeElement.getTextOffset(), "/**/");
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();

    PsiTypeElement element = typePointer.getElement();
    assertNotNull(element);
    assertTrue(element.isValid());
    assertEquals(classPointer.getElement(), PsiUtil.resolveClassInType(element.getType()));
  }

  public void testCreatePointerInBeforeDocumentChange() {
    final PsiClass aClass = myJavaFacade.findClass("AClass",GlobalSearchScope.allScope(getProject()));
    assertNotNull(aClass);

    Document document = PsiDocumentManager.getInstance(myProject).getDocument(aClass.getContainingFile());

    final SmartPsiElementPointer<?>[] pointer = new SmartPsiElementPointer[1];
    int offset = aClass.getTextOffset();
    DocumentListener listener = new DocumentListener() {
      @Override
      public void beforeDocumentChange(@NotNull DocumentEvent event) {
        pointer[0] = createPointer(aClass);
      }
    };
    EditorEventMulticaster multicaster = EditorFactory.getInstance().getEventMulticaster();
    multicaster.addDocumentListener(listener, getTestRootDisposable());
    try {
      insertString(document, offset, "/******/");
    }
    finally {
      multicaster.removeDocumentListener(listener);
    }

    pointer[0].getElement();

    insertString(document, 0, "/**/");
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();

    PsiElement element = pointer[0].getElement();
    assertNotNull(element);
    assertTrue(element instanceof PsiClass);
    assertTrue(element.isValid());
  }

  public void testCreatePointerWhenNoPsiFile() {
    // to use weak refs
    myPsiManager.runInBatchFilesMode(() -> {
      final PsiClass aClass = myJavaFacade.findClass("AClass",GlobalSearchScope.allScope(getProject()));
      assertNotNull(aClass);

      VirtualFile vFile = myRoot.findChild("AClass.java");
      assertNotNull(vFile);
      PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(myProject);
      Document document = FileDocumentManager.getInstance().getDocument(vFile);

      final SmartPsiElementPointer<PsiClass> pointer = createPointer(aClass);

      System.gc();

      insertString(document, 0, "class Foo{}\n");

      PsiElement element = pointer.getElement();
      assertEquals(aClass, element);

      insertString(document, 0, "/**/");
      psiDocumentManager.commitAllDocuments();

      if (aClass.isValid()) {
        assertNotNull(aClass.getChildren()); // trigger PSI loading
      }

      element = pointer.getElement();
      assertNotNull(element);
      assertTrue(element instanceof PsiClass);
      assertTrue(element.isValid());
      return null;
    });
  }

  public void testReplaceFile() {
    VirtualFile vfile = createChildData(myRoot, "X.java");
    @Language("JAVA")
    String text = "public class X { public int X; }";
    setFileText(vfile, text);

    PsiClass aClass = myJavaFacade.findClass("X", GlobalSearchScope.allScope(getProject()));
    assertNotNull(aClass);
    assertTrue(aClass.isValid());

    SmartPsiElementPointer<PsiClass> classp = createPointer(aClass);
    SmartPsiElementPointer<PsiFile> filep = createPointer(aClass.getContainingFile());

    FileContentUtil.reparseFiles(myProject, Collections.singleton(vfile), true);
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();
    assertFalse(aClass.isValid());

    PsiElement element = classp.getElement();
    assertNotNull(element);
    assertTrue(element instanceof PsiClass);
    assertTrue(element.isValid());
    assertEquals(vfile, element.getContainingFile().getVirtualFile());

    element = filep.getElement();
    assertNotNull(element);
    assertTrue(element instanceof PsiFile);
    assertTrue(element.isValid());
    assertEquals(vfile, element.getContainingFile().getVirtualFile());
  }

  public void testCreatePointerDoesNotLoadPsiTree() {
    VirtualFile vfile = createChildData(myRoot, "X.java");
    @Language("JAVA")
    String text = "public class X { public int X; }";
    setFileText(vfile, text);

    PsiClass aClass = myJavaFacade.findClass("X", GlobalSearchScope.allScope(getProject()));
    assertNotNull(aClass);
    assertTrue(aClass.isValid());

    PsiFileImpl file = (PsiFileImpl)aClass.getContainingFile();

    assertTreeLoaded(file, false);

    SmartPsiElementPointer<PsiClass> p = createPointer(aClass);
    assertNotNull(p);

    assertTreeLoaded(file, false);

    assertInstanceOf(p.getElement(), PsiClass.class);

    assertTreeLoaded(file, false);

    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject);
    Document document = documentManager.getDocument(file);
    insertString(document, 0, "/** asdasd */");
    documentManager.commitAllDocuments();

    // loaded tree
    assertTreeLoaded(file, true);

    assertInstanceOf(p.getElement(), PsiClass.class);

    assertTreeLoaded(file, true);
  }

  private static void assertTreeLoaded(PsiFileImpl file, boolean loaded) {
    FileElement treeElement = file.getTreeElement();
    assertEquals(loaded, treeElement != null);
    StubTree stubTree = file.getStubTree();
    assertEquals(loaded, stubTree == null);
  }

  public void testPointerDisambiguationAfterDupLine() {
    String text = """
      class XXX{ void foo() {\s
       <caret>foo();
      }}""";
    PsiJavaFile file = (PsiJavaFile)configureByText(JavaFileType.INSTANCE, text);
    PsiClass aClass = file.getClasses()[0];
    assertNotNull(aClass);

    PsiReferenceExpression ref1 = PsiTreeUtil.getParentOfType(PsiUtilBase.getElementAtCaret(getEditor()), PsiReferenceExpression.class);
    SmartPsiElementPointer<PsiReferenceExpression> pointer1 = createPointer(ref1);

    ctrlD();
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();

    Set<PsiReferenceExpression> refs = new HashSet<>();
    int offset=0;
    while (true) {
      offset = getEditor().getDocument().getText().indexOf("foo();", offset+1);
      if (offset == -1) break;
      PsiReferenceExpression ref2 = PsiTreeUtil.getParentOfType(getFile().findElementAt(offset), PsiReferenceExpression.class);
      refs.add(ref2);
    }
    refs.remove(ref1);
    assertEquals(1, refs.size());
    PsiReferenceExpression ref2 = refs.iterator().next();
    assertNotSame(ref1, ref2);
    SmartPsiElementPointer<PsiReferenceExpression> pointer2 = createPointer(ref2);
    assertNotSame(pointer1, pointer2);

    PsiElement element1 = pointer1.getElement();
    PsiElement element2 = pointer2.getElement();

    assertNotNull(element1);
    assertNotNull(element2);
    assertNotSame(element1, element2);

    assertFalse(getPointerManager().pointToTheSameElement(pointer1, pointer2));
  }

  public void testPointersRefCount() {
    @Language("JAVA")
    String text = "class X{}";
    PsiFile file = configureByText(JavaFileType.INSTANCE, text);
    PsiClass aClass = ((PsiClassOwner)file).getClasses()[0];
    SmartPointerManagerImpl smartPointerManager = getPointerManager();
    SmartPsiElementPointer<PsiClass> pointer1 = createPointer(aClass);
    SmartPsiElementPointer<PsiClass> pointer2 = createPointer(aClass);
    assertSame(pointer1, pointer2);

    assertNotNull(pointer1.getRange());

    smartPointerManager.removePointer(pointer2);
    assertNotNull(pointer1.getRange());

    smartPointerManager.removePointer(pointer1);
    assertNull(pointer1.getRange());
  }

  private SmartPointerManagerImpl getPointerManager() {
    return (SmartPointerManagerImpl)SmartPointerManager.getInstance(myProject);
  }

  public void testPointersRefCountSaturated() {
    @Language("JAVA")
    String text = "class X{}";
    PsiFile file = configureByText(JavaFileType.INSTANCE, text);
    PsiClass aClass = ((PsiClassOwner)file).getClasses()[0];
    SmartPointerManagerImpl smartPointerManager = getPointerManager();
    SmartPsiElementPointerImpl<PsiClass> pointer1 = (SmartPsiElementPointerImpl<PsiClass>)createPointer(aClass);
    for (int i=0; i<1000; i++) {
      SmartPsiElementPointer<PsiClass> pointer2 = createPointer(aClass);
      assertSame(pointer1, pointer2);
    }

    assertNotNull(pointer1.getRange());
    assertEquals(Byte.MAX_VALUE, pointer1.incrementAndGetReferenceCount(0));

    for (int i=0; i<1100; i++) {
      smartPointerManager.removePointer(pointer1);
      Assert.assertNotNull(pointer1.getRange());
    }
  }

  public void testSmartPointerCreationDoesNotLoadDocument() {
    PsiPackage aPackage = myJavaFacade.findPackage("java.io");
    for (PsiClass aClass : aPackage.getClasses()) {
      PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject);
      PsiFile file = aClass.getContainingFile();
      Document document = documentManager.getCachedDocument(file);
      if (document == null) { //ignore already loaded documents
        createPointer(aClass);
        assertNull(documentManager.getCachedDocument(file));
        //System.out.println("file = " + file);
      }
      else {
        LOG.debug("already loaded file = " + file);
      }
    }
  }

  public void testSmartPointersSurvivePsiFileUnload() {
    final VirtualFile vfile = createChildData(myRoot, "X.txt");
    String xxx = "xxx";
    String text = xxx + " " + xxx + " " + xxx;
    setFileText(vfile, text);
    PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(vfile);
    assertTrue(String.valueOf(psiFile), psiFile instanceof PsiPlainTextFile);
    SmartPointerManagerImpl manager = getPointerManager();
    TextRange range1 = TextRange.from(0, xxx.length());
    SmartPsiFileRange pointer1 = manager.createSmartPsiFileRangePointer(psiFile, range1);
    TextRange range2 = TextRange.from(text.lastIndexOf(xxx), xxx.length());
    SmartPsiFileRange pointer2 = manager.createSmartPsiFileRangePointer(psiFile, range2);
    assertNotNull(FileDocumentManager.getInstance().getCachedDocument(vfile));

    GCWatcher watcher = GCWatcher.tracking(psiFile, FileDocumentManager.getInstance().getDocument(vfile));
    //noinspection UnusedAssignment
    psiFile = null;
    watcher.ensureCollected();

    assertNull(FileDocumentManager.getInstance().getCachedDocument(vfile));
    assertEquals(pointer1.getRange(), range1);
    WriteCommandAction.runWriteCommandAction(getProject(), () -> insertString(FileDocumentManager.getInstance().getDocument(vfile), 0, " "));
    assertEquals(range1.shiftRight(1), pointer1.getRange());
    assertEquals(range2.shiftRight(1), pointer2.getRange());
  }

  public void testInXml() {
    @Language("HTML")
    String text = """
      <!doctype html>
      <html>
          <fieldset></fieldset>
          <select></select>

          <caret>
      </html>""";
    final PsiFile file = configureByText(HtmlFileType.INSTANCE,
                                         text
    );

    final XmlTag fieldSet = PsiTreeUtil.getParentOfType(file.findElementAt(file.getText().indexOf("fieldset")), XmlTag.class);
    assertNotNull(fieldSet);
    assertEquals("fieldset", fieldSet.getName());

    final XmlTag select = PsiTreeUtil.getParentOfType(file.findElementAt(file.getText().indexOf("select")), XmlTag.class);
    assertNotNull(select);
    assertEquals("select", select.getName());

    final SmartPsiElementPointer<XmlTag> fieldSetPointer = createPointer(fieldSet);
    final SmartPsiElementPointer<XmlTag> selectPointer = createPointer(select);

    WriteCommandAction.runWriteCommandAction(getProject(), () -> insertString(getEditor().getDocument(), getEditor().getCaretModel().getOffset(), "<a></a>"));

    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();

    final XmlTag newFieldSet = fieldSetPointer.getElement();
    assertNotNull(newFieldSet);
    assertEquals("fieldset", newFieldSet.getName());

    final XmlTag newSelect = selectPointer.getElement();
    assertNotNull(newSelect);
    assertEquals("select", newSelect.getName());
  }

  public void testInXml2() {
    String text = """
      <html>
          <ul class="dropdown-menu">
              <li><a href="#">One more separated link</a></li>
          </ul>
      <caret>
      </html>""";
    final PsiFile file = configureByText(XmlFileType.INSTANCE,
                                         text
    );

    final XmlTag ul = PsiTreeUtil.getParentOfType(file.findElementAt(file.getText().indexOf("ul")), XmlTag.class);
    assertNotNull(ul);
    assertEquals("ul", ul.getName());
    assertEquals("dropdown-menu", ul.getAttributeValue("class"));

    SmartPsiElementPointer<XmlTag> ulPointer = createPointer(ul);

    WriteCommandAction.runWriteCommandAction(getProject(), () -> {
      int offset = getEditor().getCaretModel().getOffset();
      insertString(getEditor().getDocument(), offset, """
            <ul class="nav navbar-nav navbar-right">
            </ul>
        """);
    });

    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();

    final XmlTag newUl = ulPointer.getElement();
    assertNotNull(newUl);
    assertEquals("ul", newUl.getName());
    assertEquals("dropdown-menu", newUl.getAttributeValue("class"));
  }

  public void testInsertImport() {
    @Language("JAVA")
    String text = "class S {\n" +
                  "}";
    final PsiFile file = configureByText(JavaFileType.INSTANCE,
                                         text);

    PsiClass aClass = ((PsiJavaFile)file).getClasses()[0];

    WriteCommandAction.runWriteCommandAction(getProject(), () -> insertString(getEditor().getDocument(), 0, "import java.util.Map;\n"));

    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();

    PsiClass aClass2 = ((PsiJavaFile)file).getClasses()[0];
    assertSame(aClass, aClass2);
  }

  public void testEqualPointerRangesWhenCreatedFromStubAndAST() {
    @Language("JAVA")
    String text = "class S {\n" +
                  "}";
    final PsiFile file = configureByText(JavaFileType.INSTANCE,
                                         text);

    PsiClass aClass = ((PsiJavaFile)file).getClasses()[0];
    assertNotNull(((PsiFileImpl)file).getStubTree());

    final SmartPointerManager manager = getPointerManager();
    final SmartPsiElementPointer<PsiClass> pointer1 = createPointer(aClass);
    Segment range1 = pointer1.getRange();
    manager.removePointer(pointer1);

    final FileASTNode node = file.getNode();
    final SmartPsiElementPointer<PsiClass> pointer2 = createPointer(aClass);
    assertEquals(range1, pointer2.getRange());
    assertNotNull(node);
  }

  public void testEqualPointersWhenCreatedFromStubAndAST() {
    PsiJavaFile file = (PsiJavaFile)myJavaFacade.findClass("AClass", GlobalSearchScope.allScope(getProject())).getContainingFile();

    int hash1 = file.getClasses()[0].hashCode();
    final SmartPsiElementPointer<PsiClass> pointer1 = createPointer(file.getClasses()[0]);
    assertNotNull(((PsiFileImpl)file).getStubTree());

    GCWatcher.tracking(((PsiFileImpl)file).getStubTree(), file.getClasses()[0]).ensureCollected();

    final FileASTNode node = file.getNode();
    final SmartPsiElementPointer<PsiClass> pointer2 = createPointer(file.getClasses()[0]);
    assertFalse(hash1 == file.getClasses()[0].hashCode());
    assertEquals(pointer1, pointer2);
    assertEquals(pointer1.getRange(), pointer2.getRange());
    assertNotNull(node);
  }

  public void testConvergingRanges() {
    configureByText(PlainTextFileType.INSTANCE, "aba");
    final Document document = myFile.getViewProvider().getDocument();
    assertNotNull(document);

    SmartPsiFileRange range1 = getPointerManager().createSmartPsiFileRangePointer(myFile, TextRange.create(0, 2));
    SmartPsiFileRange range2 = getPointerManager().createSmartPsiFileRangePointer(myFile, TextRange.create(1, 3));

    ApplicationManager.getApplication().runWriteAction(() -> {
      document.deleteString(0, 1);
      document.deleteString(1, 2);
    });

    assertEquals(TextRange.create(0, 1), range1.getRange());
    assertEquals(TextRange.create(0, 1), range2.getRange());

    PsiDocumentManager.getInstance(myProject).commitAllDocuments();
    assertEquals(TextRange.create(0, 1), range1.getRange());
    assertEquals(TextRange.create(0, 1), range2.getRange());

    insertString(document, 0, "a");
    assertEquals(TextRange.create(1, 2), range1.getRange());
    assertEquals(TextRange.create(1, 2), range2.getRange());

    PsiDocumentManager.getInstance(myProject).commitAllDocuments();
    assertEquals(TextRange.create(1, 2), range1.getRange());
    assertEquals(TextRange.create(1, 2), range2.getRange());
  }

  public void testMoveText() {
    @Language("JAVA")
    String text = "class C1{}\nclass C2 {}";
    PsiJavaFile file = (PsiJavaFile)configureByText(JavaFileType.INSTANCE, text);
    DocumentEx document = (DocumentEx)file.getViewProvider().getDocument();

    SmartPsiElementPointer<PsiClass> pointer1 =
      createPointer(file.getClasses()[0]);
    SmartPsiElementPointer<PsiClass> pointer2 =
      createPointer(file.getClasses()[1]);
    assertEquals("C1", pointer1.getElement().getName());
    assertEquals("C2", pointer2.getElement().getName());

    gcPointerCache(pointer1, pointer2);

    TextRange range = file.getClasses()[1].getTextRange();
    ApplicationManager.getApplication().runWriteAction(() -> document.moveText(range.getStartOffset(), range.getEndOffset(), 0));

    PsiDocumentManager.getInstance(myProject).commitAllDocuments();

    assertEquals("C1", pointer1.getElement().getName());
    assertEquals("C2", pointer2.getElement().getName());
  }

  public void testNonPhysicalFile() {
    PsiJavaFile file = (PsiJavaFile)myJavaFacade.findClass("AClass", GlobalSearchScope.allScope(getProject())).getContainingFile().copy();
    SmartPsiFileRange pointer = getPointerManager().createSmartPsiFileRangePointer(file, TextRange.create(1, 2));

    insertString(file.getViewProvider().getDocument(), 0, " ");

    assertEquals(TextRange.create(2, 3), pointer.getRange());
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();
    assertEquals(TextRange.create(2, 3), pointer.getRange());
  }

  public void testUpdateAfterInsertingIdenticalText() {
    String text = """
      class Foo {
          void m() {
          }
      <caret>}
      """;
    PsiJavaFile file = (PsiJavaFile)configureByText(JavaFileType.INSTANCE, text);
    PsiMethod method = file.getClasses()[0].getMethods()[0];
    TextRange originalRange = method.getTextRange();
    SmartPsiElementPointer<PsiMethod> pointer = createPointer(method);

    ApplicationManager.getApplication().runWriteAction(() -> EditorModificationUtilEx.insertStringAtCaret(myEditor, """
          void m() {
          }
      """));

    PsiDocumentManager.getInstance(myProject).commitDocument(myEditor.getDocument());
    PsiElement element = pointer.getElement();
    assertNotNull(element);
    TextRange newRange = element.getTextRange();
    assertEquals(originalRange, newRange);
  }

  public void testAnchorInfoSurvivesPsiChange() {
    @Language("JAVA")
    String text = "class C1{}\nclass C2 {}";
    PsiJavaFile file = (PsiJavaFile)configureByText(JavaFileType.INSTANCE, text);

    SmartPsiElementPointer<PsiClass> pointer = createPointer(file.getClasses()[1]);
    gcPointerCache(pointer);

    ApplicationManager.getApplication().runWriteAction(() -> file.getClasses()[0].delete());

    assertNotNull(pointer.getElement());
  }

  public void testPointerToEmptyElement() {
    @Language("JAVA")
    String text = """
      class Foo {
        Test<String> test = new Test<>();
      }""";
    PsiFile file = configureByText(JavaFileType.INSTANCE, text);
    PsiJavaCodeReferenceElement ref = PsiTreeUtil.findElementOfClassAtOffset(file, file.getText().indexOf("<>"), PsiJavaCodeReferenceElement.class, false);
    SmartPointerEx<PsiTypeElement> pointer = createPointer(ref.getParameterList().getTypeParameterElements()[0]);
    //noinspection UnusedAssignment
    ref = null;

    gcPointerCache(pointer);

    assertInstanceOf(pointer.getElement(), PsiTypeElement.class);
  }

  public void testPointerToEmptyElement2() {
    @Language("JAVA")
    String text = """
      class Foo {
        void foo() {}
      }""";
    PsiFile file = configureByText(JavaFileType.INSTANCE, text);
    PsiMethod method = PsiTreeUtil.findElementOfClassAtOffset(file, file.getText().indexOf("void"), PsiMethod.class, false);
    SmartPointerEx<PsiModifierList> pointer1 = createPointer(method.getModifierList());
    SmartPointerEx<PsiTypeParameterList> pointer2 = createPointer(method.getTypeParameterList());
    //noinspection UnusedAssignment
    method = null;

    gcPointerCache(pointer1, pointer2);

    assertInstanceOf(pointer1.getElement(), PsiModifierList.class);
    assertInstanceOf(pointer2.getElement(), PsiTypeParameterList.class);
  }

  public void testPointerToReferenceSurvivesRename() {
    @Language("JAVA")
    String text = "class Foo extends Bar {}";
    PsiFile file = configureByText(JavaFileType.INSTANCE, text);
    PsiJavaCodeReferenceElement ref = PsiTreeUtil.findElementOfClassAtOffset(file, file.getText().indexOf("Bar"), PsiJavaCodeReferenceElement.class, false);
    SmartPointerEx<PsiJavaCodeReferenceElement> pointer = createPointer(ref);
    //noinspection UnusedAssignment
    ref = null;

    gcPointerCache(pointer);

    ref = PsiTreeUtil.findElementOfClassAtOffset(file, file.getText().indexOf("Bar"), PsiJavaCodeReferenceElement.class, false);
    final PsiJavaCodeReferenceElement finalRef = ref;
    ApplicationManager.getApplication().runWriteAction(() -> {
      finalRef.handleElementRename("BarImpl");
    });

    assertNotNull(pointer.getElement());
  }

  public void testNonAnchoredStubbedElement() {
    @Language("JAVA")
    String text = "class Foo { { @NotNull String foo; } }";
    PsiFile file = configureByText(JavaFileType.INSTANCE, text);
    StubTree stubTree = ((PsiFileImpl)file).getStubTree();
    assertNotNull(stubTree);
