use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class ResourceFolderRepositoryTest method testEditPluralItems.
public void testEditPluralItems() throws Exception {
resetScanCounter();
VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
// Test that our tools:quantity works correctly for getResourceValue()
assertTrue(resources.hasResourceItem(ResourceType.PLURALS, "my_plural"));
ResourceItem plural = getOnlyItem(resources, ResourceType.PLURALS, "my_plural");
ResourceValue resourceValue = plural.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("@string/hello_two", resourceValue.getValue());
// TODO: It would be nice to avoid updating the generation if you
// edit a different item than the one being picked (default or via
// tools:quantity) but for now we're not worrying about that optimization
long generation = resources.getModificationCount();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
final int offset = document.getText().indexOf("@string/hello_two");
document.replaceString(offset + 9, offset + 10, "a");
documentManager.commitDocument(document);
}
});
// First edit won't be incremental (file -> Psi).
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
plural = getOnlyItem(resources, ResourceType.PLURALS, "my_plural");
resourceValue = plural.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("@string/hallo_two", resourceValue.getValue());
assertTrue(generation < resources.getModificationCount());
resetScanCounter();
// However, the second edit can then be incremental.
long generation2 = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int offset = document.getText().indexOf("@string/hallo_two");
document.replaceString(offset + 9, offset + 10, "i");
documentManager.commitDocument(document);
}
});
plural = getOnlyItem(resources, ResourceType.PLURALS, "my_plural");
resourceValue = plural.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("@string/hillo_two", resourceValue.getValue());
assertTrue(generation2 < resources.getModificationCount());
// Shouldn't have done any full file rescans during the above edits
ensureIncremental();
}
use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class ResourceFolderRepositoryTest method testEditStyleParent.
public void testEditStyleParent() throws Exception {
resetScanCounter();
VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
final ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkTheme"));
// Change style parent
final long generation = resources.getModificationCount();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
// First edit won't be incremental (file -> Psi).
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
final int offset = document.getText().indexOf("android:Theme.Holo");
document.replaceString(offset, offset + "android:Theme.Holo".length(), "android:Theme.Light");
documentManager.commitDocument(document);
}
});
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
ensureSingleScan();
assertTrue(generation < resources.getModificationCount());
ResourceItem style = getOnlyItem(resources, ResourceType.STYLE, "DarkTheme");
ResourceValue resourceValue = style.getResourceValue(false);
assertNotNull(resourceValue);
assertTrue(resourceValue instanceof StyleResourceValue);
StyleResourceValue srv = (StyleResourceValue) resourceValue;
assertEquals("android:Theme.Light", srv.getParentStyle());
ResourceValue actionBarStyle = srv.getItem("actionBarStyle", true);
assertNotNull(actionBarStyle);
assertEquals("@style/DarkActionBar", actionBarStyle.getValue());
resetScanCounter();
}
});
// Even on the second edit we don't expect editing the style parent to be incremental.
final long generation2 = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
final int offset = document.getText().indexOf("android:Theme.Light");
document.replaceString(offset, offset + "android:Theme.Light".length(), "android:Theme.Material");
documentManager.commitDocument(document);
}
});
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
ensureSingleScan();
assertTrue(generation2 < resources.getModificationCount());
ResourceItem style = getOnlyItem(resources, ResourceType.STYLE, "DarkTheme");
ResourceValue resourceValue = style.getResourceValue(false);
assertNotNull(resourceValue);
assertTrue(resourceValue instanceof StyleResourceValue);
StyleResourceValue srv = (StyleResourceValue) resourceValue;
assertEquals("android:Theme.Material", srv.getParentStyle());
ResourceValue actionBarStyle = srv.getItem("actionBarStyle", true);
assertNotNull(actionBarStyle);
assertEquals("@style/DarkActionBar", actionBarStyle.getValue());
}
});
}
use of com.intellij.psi.PsiDocumentManager in project intellij-plugins by JetBrains.
the class AddTestSectionAction method invoke.
@Override
public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
if (file instanceof YAMLFile && file.isValid()) {
final YAMLFile yamlFile = (YAMLFile) file;
ApplicationManager.getApplication().runWriteAction(() -> {
PsiDocumentManager manager = PsiDocumentManager.getInstance(project);
Document document = manager.getDocument(yamlFile);
int length = document.getTextLength();
document.insertString(length, "\n" + "test:");
manager.commitDocument(document);
});
}
}
use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.
the class UsageViewTest method testUsageViewHandlesDocumentChange.
public void testUsageViewHandlesDocumentChange() throws Exception {
PsiFile psiFile = myFixture.addFileToProject("X.java", "public class X{ int xxx; } //comment");
Usage usage = createUsage(psiFile, psiFile.getText().indexOf("xxx"));
UsageView usageView = UsageViewManager.getInstance(getProject()).createUsageView(UsageTarget.EMPTY_ARRAY, new Usage[] { usage }, new UsageViewPresentation(), null);
Disposer.register(myFixture.getTestRootDisposable(), usageView);
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
Document document = documentManager.getDocument(psiFile);
WriteCommandAction.runWriteCommandAction(getProject(), () -> document.insertString(0, "/* sdfsdfsd */"));
documentManager.commitAllDocuments();
int navigationOffset = ((UsageInfo2UsageAdapter) usage).getUsageInfo().getNavigationOffset();
assertEquals(psiFile.getText().indexOf("xxx"), navigationOffset);
}
use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.
the class DetectAndAdjustIndentOptionsTask method scheduleInBackgroundForCommittedDocument.
public void scheduleInBackgroundForCommittedDocument() {
if (myProject.isDisposed())
return;
if (ApplicationManager.getApplication().isUnitTestMode()) {
PsiDocumentManager.getInstance(myProject).commitDocument(myDocument);
Continuation continuation = performInReadAction(new DumbProgressIndicator());
if (continuation != null) {
continuation.getAction().run();
}
} else {
PsiDocumentManager manager = PsiDocumentManager.getInstance(myProject);
manager.performForCommittedDocument(myDocument, () -> ProgressIndicatorUtils.scheduleWithWriteActionPriority(myExecutor, this));
}
}
Aggregations