Files
openide/java/java-tests/testSrc/com/intellij/psi/PsiDocumentManager2Test.java
Peter Gromov c889d83706 take the document content for light vFiles from their bound PSI
to avoid PSI/document mismatches on seemingly committed documents

this is only possible if the bound PSI is unique, so add an assertion for that

towards uniform PSI-doc synchronization for physical and non-physical PSI, which is needed for rename to work in ModelBranch (LAB-51)

GitOrigin-RevId: a20bab456bb242119f476fc8078c2ae6d8a4ce3a
2020-07-16 10:42:45 +00:00

86 lines
3.1 KiB
Java

// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.psi;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
import com.intellij.ide.highlighter.XmlLikeFileType;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.lang.xml.XMLLanguage;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.testFramework.HeavyPlatformTestCase;
import com.intellij.testFramework.LightPlatformTestCase;
import com.intellij.util.LocalTimeCounter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.Objects;
@HeavyPlatformTestCase.WrapInCommand
public class PsiDocumentManager2Test extends LightPlatformTestCase {
public void testUnregisteredFileType() {
final class MyFileType extends XmlLikeFileType {
private MyFileType() {
super(XMLLanguage.INSTANCE);
}
@Override
@NotNull
public String getName() {
return "MY";
}
@Override
@NotNull
public String getDefaultExtension() {
return "my";
}
@Override
@NotNull
public String getDescription() {
return "my own";
}
@Override
@Nullable
public Icon getIcon() {
return null;
}
}
PsiFile file = PsiFileFactory.getInstance(getProject()).createFileFromText("DummyFile.my", new MyFileType(), "<gggg></gggg>", LocalTimeCounter.currentTime(), true);
Document document = Objects.requireNonNull(PsiDocumentManager.getInstance(getProject()).getDocument(file));
assertTrue(document.isWritable());
assertTrue(file.getVirtualFile().getFileType() instanceof MyFileType);
assertTrue(DaemonCodeAnalyzer.getInstance(getProject()).isHighlightingAvailable(file));
}
public void testPsiToDocSyncInNonPhysicalFile() {
PsiJavaFile file = (PsiJavaFile)PsiFileFactory.getInstance(getProject())
.createFileFromText("a.java", JavaLanguage.INSTANCE, "class Foo {}", false, false);
Document document = FileDocumentManager.getInstance().getDocument(file.getViewProvider().getVirtualFile());
assertNotNull(document);
file.getClasses()[0].getModifierList().setModifierProperty(PsiModifier.PUBLIC, true);
assertEquals("public class Foo {}", document.getText());
assertFalse(PsiDocumentManager.getInstance(getProject()).isUncommited(document));
}
public void test_nonPhysical_PSI_matches_document_loaded_after_modifications() {
PsiJavaFile file = (PsiJavaFile)PsiFileFactory.getInstance(getProject())
.createFileFromText("a.java", JavaLanguage.INSTANCE, "class Foo {}\nclass Bar{}", false, false);
file.getClasses()[1].delete();
String expectedText = "class Foo {}\n";
assertEquals(expectedText, file.getText());
Document document = file.getViewProvider().getDocument();
assertTrue(PsiDocumentManager.getInstance(getProject()).isCommitted(document));
assertEquals(expectedText, document.getText());
}
}