本文整理汇总了Java中com.intellij.psi.PsiDocumentManager.getDocument方法的典型用法代码示例。如果您正苦于以下问题:Java PsiDocumentManager.getDocument方法的具体用法?Java PsiDocumentManager.getDocument怎么用?Java PsiDocumentManager.getDocument使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.psi.PsiDocumentManager
的用法示例。
在下文中一共展示了PsiDocumentManager.getDocument方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onProcessItemViewBinder
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
private void onProcessItemViewBinder(final PsiDirectory dir, final String typeName, final PsiClass itemClass) {
PsiFile file = itemClass.getContainingFile();
final PsiDocumentManager manager = PsiDocumentManager.getInstance(itemClass.getProject());
final Document document = manager.getDocument(file);
if (document == null) {
return;
}
new WriteCommandAction.Simple(itemClass.getProject()) {
@Override
protected void run() throws Throwable {
manager.doPostponedOperationsAndUnblockDocument(document);
document.setText(document.getText()
.replace("MTI_CLASS", typeName)
.replace("MTI_LOWER_NAME", CaseFormat.UPPER_CAMEL.to(LOWER_UNDERSCORE, typeName))
.replace("MTI_NAME", CaseFormat.UPPER_CAMEL.to(LOWER_CAMEL, typeName)));
CodeStyleManager.getInstance(itemClass.getProject()).reformat(itemClass);
}
}.execute();
}
示例2: setFileText
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
/**
* @deprecated to be removed after IDEA 15. Use {@link VfsUtil#saveText(VirtualFile, String)} instead.
*/
public static void setFileText(@Nullable Project project, final VirtualFile virtualFile, final String text) throws IOException {
if (project == null) {
project = ProjectUtil.guessProjectForFile(virtualFile);
}
if (project != null) {
final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
final Document document = psiFile == null? null : psiDocumentManager.getDocument(psiFile);
if (document != null) {
document.setText(text != null ? text : "");
psiDocumentManager.commitDocument(document);
FileDocumentManager.getInstance().saveDocument(document);
return;
}
}
VfsUtil.saveText(virtualFile, text != null ? text : "");
virtualFile.refresh(false, false);
}
示例3: testChangeType
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
public void testChangeType() 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.ID, "action_next"));
assertFalse(resources.hasResourceItem(ResourceType.DIMEN, "action_next"));
final long generation = resources.getModificationCount();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
final int offset = document.getText().indexOf("\"id\" name=\"action_next\" />") + 1;
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
document.replaceString(offset, offset + 2, "dimen");
documentManager.commitDocument(document);
}
});
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
ensureSingleScan();
assertTrue(generation < resources.getModificationCount());
assertFalse(resources.hasResourceItem(ResourceType.ID, "action_next"));
assertTrue(resources.hasResourceItem(ResourceType.DIMEN, "action_next"));
}
});
}
示例4: doTextTest
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
public void doTextTest(@NotNull final Action action, @NotNull String text, @NotNull String textAfter) throws IncorrectOperationException {
final PsiFile file = createFile("A.java", text);
final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
final Document document = manager.getDocument(file);
if (document == null) {
fail("Document is null");
return;
}
replaceAndProcessDocument(action, text, file, document);
assertEquals(textAfter, document.getText());
manager.commitDocument(document);
assertEquals(textAfter, file.getText());
}
示例5: testEditingInDocComment
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
public void testEditingInDocComment() throws Exception {
final Ref<Boolean> gotIt = new Ref<Boolean>(false);
getPsiManager().addPsiTreeChangeListener(new PsiTreeChangeAdapter() {
@Override
public void childReplaced(@NotNull PsiTreeChangeEvent event) {
gotIt.set(true);
}
});
GroovyFile file = GroovyPsiElementFactory.getInstance(myProject).createGroovyFile("/** This is doc comment*/class C{}", true, null);
final PsiDocumentManager docManager = PsiDocumentManager.getInstance(myProject);
final Document doc = docManager.getDocument(file);
assertNotNull(doc);
CommandProcessor.getInstance().executeCommand(myProject,
new Runnable() {
@Override
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
doc.insertString(3, " ");
docManager.commitDocument(doc);
}
});
}
},
"file text set",
this
);
assertTrue(gotIt.get());
}
示例6: testUsageViewHandlesDocumentChange
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
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(getTestRootDisposable(), usageView);
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
Document document = documentManager.getDocument(psiFile);
document.insertString(0, "/* sdfsdfsd */");
documentManager.commitAllDocuments();
int navigationOffset = ((UsageInfo2UsageAdapter)usage).getUsageInfo().getNavigationOffset();
assertEquals(psiFile.getText().indexOf("xxx"), navigationOffset);
}
示例7: testAddArrayItemElements
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
public void testAddArrayItemElements() 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);
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("<item>Question 3</item>");
document.insertString(offset, "<item>Question 2.5</item>");
documentManager.commitDocument(document);
}
});
assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "security_questions"));
ResourceItem array = getOnlyItem(resources, ResourceType.ARRAY, "security_questions");
ResourceValue resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("Question 3", resourceValue.getValue());
assertTrue(resourceValue instanceof ArrayResourceValue);
ArrayResourceValue arv = (ArrayResourceValue)resourceValue;
assertEquals(6, arv.getElementCount());
assertEquals("Question 2", arv.getElement(1));
assertEquals("Question 2.5", arv.getElement(2));
assertEquals("Question 3", arv.getElement(3));
// Shouldn't have done any full file rescans during the above edits
ensureIncremental();
}
示例8: testRemoveValue
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
public void testRemoveValue() 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);
Collection<String> strings = resources.getItemsOfType(ResourceType.STRING);
assertEquals(8, strings.size());
assertTrue(resources.hasResourceItem(ResourceType.STRING, "app_name"));
long generation = resources.getModificationCount();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
final String textToRemove = "<string name=\"app_name\">Animations Demo</string>";
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int offset = document.getText().indexOf(textToRemove);
document.deleteString(offset, offset + textToRemove.length());
documentManager.commitDocument(document);
}
});
assertTrue(generation < resources.getModificationCount());
assertFalse(resources.hasResourceItem(ResourceType.STRING, "app_name"));
// Shouldn't have done any full file rescans during the above edits
ensureIncremental();
}
示例9: doReformat
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
@Override
protected PsiFile doReformat(final Project project, final PsiFile psiFile) {
final String text = psiFile.getText();
final PsiDocumentManager manager = PsiDocumentManager.getInstance(project);
final Document doc = manager.getDocument(psiFile);
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
@Override
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
doc.replaceString(0, doc.getTextLength(), text);
manager.commitDocument(doc);
try {
CodeStyleManager.getInstance(project).reformat(psiFile);
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
}
});
}
}, "", "");
if (doc != null) {
manager.commitDocument(doc);
}
return psiFile;
}
示例10: testBreakNameAttribute
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
public void testBreakNameAttribute() 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.STRING, "app_name"));
final long generation = resources.getModificationCount();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
final int offset = document.getText().indexOf("name=\"app_name\">");
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
document.replaceString(offset + 2, offset + 3, "o"); // name => nome
documentManager.commitDocument(document);
}
});
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
ensureSingleScan();
assertTrue(generation < resources.getModificationCount());
assertFalse(resources.hasResourceItem(ResourceType.STRING, "app_name"));
}
});
}
示例11: writeFragments
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
@SuppressWarnings({"HardCodedStringLiteral"})
private static void writeFragments(final List<PsiFragment> psiFragments,
final PrettyPrintWriter writer,
Project project,
final boolean shouldWriteOffsets) {
final PathMacroManager macroManager = PathMacroManager.getInstance(project);
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
for (PsiFragment fragment : psiFragments) {
final PsiFile psiFile = fragment.getFile();
final VirtualFile virtualFile = psiFile != null ? psiFile.getVirtualFile() : null;
if (virtualFile != null) {
writer.startNode("fragment");
writer.addAttribute("file", macroManager.collapsePath(virtualFile.getUrl()));
if (shouldWriteOffsets) {
final Document document = documentManager.getDocument(psiFile);
LOG.assertTrue(document != null);
int startOffset = fragment.getStartOffset();
final int line = document.getLineNumber(startOffset);
writer.addAttribute("line", String.valueOf(line));
final int lineStartOffset = document.getLineStartOffset(line);
if (StringUtil.isEmptyOrSpaces(document.getText().substring(lineStartOffset, startOffset))) {
startOffset = lineStartOffset;
}
writer.addAttribute("start", String.valueOf(startOffset));
writer.addAttribute("end", String.valueOf(fragment.getEndOffset()));
if (fragment.containsMultipleFragments()) {
final int[][] offsets = fragment.getOffsets();
for (int[] offset : offsets) {
writer.startNode("offset");
writer.addAttribute("start", String.valueOf(offset[0]));
writer.addAttribute("end", String.valueOf(offset[1]));
writer.endNode();
}
}
}
writer.endNode();
}
}
}
示例12: replaceOrDeleteTrailingWhitespaces
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
@NotNull
private static TextRange replaceOrDeleteTrailingWhitespaces(@NotNull final PsiFile pyFile, @NotNull final TextRange whitespaceRange) {
final Project project = pyFile.getProject();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final Document document = documentManager.getDocument(pyFile);
if (document != null) {
final PyCodeStyleSettings customSettings = CodeStyleSettingsManager.getSettings(project).getCustomSettings(PyCodeStyleSettings.class);
final boolean addLineFeed = customSettings.BLANK_LINE_AT_FILE_END || EditorSettingsExternalizable.getInstance().isEnsureNewLineAtEOF();
try {
final String text = addLineFeed ? "\n" : "";
// Do not add extra blank line in empty file
if (!text.isEmpty() && whitespaceRange.getStartOffset() != 0) {
if (!whitespaceRange.isEmpty()) {
document.replaceString(whitespaceRange.getStartOffset(), whitespaceRange.getEndOffset(), text);
}
else {
document.insertString(document.getTextLength(), text);
}
}
else if (!whitespaceRange.isEmpty()) {
document.deleteString(whitespaceRange.getStartOffset(), whitespaceRange.getEndOffset());
}
return TextRange.from(whitespaceRange.getStartOffset(), text.length());
}
finally {
documentManager.commitDocument(document);
}
}
return whitespaceRange;
}
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:31,代码来源:PyTrailingBlankLinesPostFormatProcessor.java
示例13: testSync
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
public void testSync() throws Exception {
// Regression test for https://code.google.com/p/android/issues/detail?id=79629
// Ensure that sync() handles rescanning immediately
VirtualFile file1 = myFixture.copyFileToProject(STRINGS, "res/values/strings.xml");
final PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
final ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
assertTrue(resources.hasResourceItem(ResourceType.STRING, "app_name"));
assertFalse(resources.hasResourceItem(ResourceType.STRING, "app_name2"));
assertTrue(resources.hasResourceItem(ResourceType.STRING, "hello_world"));
final long generation = resources.getModificationCount();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
// The sync() call must be called from the dispatch thread
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
String string = " <string name=\"hello_world\">Hello world!</string>";
final int offset = document.getText().indexOf(string);
assertTrue(offset != -1);
// Simulate an edit event that triggers the incremental updater to
// give up and schedule a subsequent update instead. (This used to be
// the case here, but as of IntelliJ 14.1 it's now delivering more
// accurate events for the below edits, which made the sync-test
// fail because it would already have correct results *before* the
// sync. Therefore, we simply trigger a pending scan (which stops
// subequent incremental events from being processed).
resources.rescan(psiFile1, ResourceFolderType.VALUES);
document.deleteString(offset, offset + string.length());
documentManager.commitDocument(document);
}
});
// The strings file contains definitions for app_name, action_settings and hello_world.
// We've manually deleted the hello_world string. We now check that app_name remains
// in the resource set, and that hello_world is removed. (We check that hello_world
// is there before the sync, and gone after.)
assertTrue(resources.isScanPending(psiFile1));
assertTrue(resources.hasResourceItem(ResourceType.STRING, "app_name"));
assertTrue(resources.hasResourceItem(ResourceType.STRING, "hello_world"));
assertTrue(ApplicationManager.getApplication().isDispatchThread());
resources.sync();
assertTrue(generation < resources.getModificationCount());
assertTrue(resources.hasResourceItem(ResourceType.STRING, "app_name"));
assertFalse(resources.hasResourceItem(ResourceType.STRING, "hello_world"));
assertFalse(resources.isScanPending(psiFile1));
}
});
}
示例14: testEditIdAttributeValue2
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
public void testEditIdAttributeValue2() throws Exception {
// Edit the id attribute value: rather than by making a full value replacement,
// perform a tiny edit on the character content; this takes a different code
// path in the incremental updater
resetScanCounter();
// Edit the id attribute value of a layout item to change the set of available ids
VirtualFile file1 = myFixture.copyFileToProject(LAYOUT1, "res/layout/layout1.xml");
PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
Collection<String> layouts = resources.getItemsOfType(ResourceType.LAYOUT);
assertEquals(1, layouts.size());
assertNotNull(resources.getResourceItem(ResourceType.LAYOUT, "layout1"));
assertTrue(resources.hasResourceItem(ResourceType.ID, "noteArea"));
assertFalse(resources.hasResourceItem(ResourceType.ID, "note2Area"));
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
// Edit value should cause update
long generation = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
final int offset = document.getText().indexOf("noteArea");
document.insertString(offset + 4, "2");
documentManager.commitDocument(document);
}
});
assertTrue(resources.hasResourceItem(ResourceType.ID, "note2Area"));
assertFalse(resources.hasResourceItem(ResourceType.ID, "noteArea"));
assertTrue(resources.getModificationCount() > generation);
// Shouldn't have done any full file rescans during the above edits
ensureIncremental();
}
示例15: createRangeMarker
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
protected RangeMarker createRangeMarker(final PsiElement elt) {
final PsiFile psiFile = elt.getContainingFile();
final PsiDocumentManager instance = PsiDocumentManager.getInstance(elt.getProject());
final Document document = instance.getDocument(psiFile);
return document.createRangeMarker(elt.getTextRange());
}