本文整理汇总了Java中com.intellij.psi.PsiDocumentManager.commitDocument方法的典型用法代码示例。如果您正苦于以下问题:Java PsiDocumentManager.commitDocument方法的具体用法?Java PsiDocumentManager.commitDocument怎么用?Java PsiDocumentManager.commitDocument使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.psi.PsiDocumentManager
的用法示例。
在下文中一共展示了PsiDocumentManager.commitDocument方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: applyPlugin
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
/**
* 插入插件的表达式
* apply plugin: 'fastdex.app'
*
* @param project
* @param psiFile
* @param pluginId
*/
public static void applyPlugin(Project project, GroovyFile psiFile, String pluginId) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
GrStatement grStatement = factory.createExpressionFromText(String.format("apply plugin: \'%s\'",
new Object[]{pluginId}), null);
GrExpression expression = GroovyFileUil.getLastPlugin(psiFile);
if (expression != null && expression.getParent() != null) {
psiFile.addAfter(grStatement, expression.getParent());
// 换行
psiFile.addAfter(factory.createLineTerminator("\n"), expression.getParent());
}
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
Document document = documentManager.getDocument(psiFile);
if (document != null) {
documentManager.commitDocument(document);
}
}
示例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: addOrReplaceSelectionForEditor
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
protected final void addOrReplaceSelectionForEditor(final String selection, Editor editor) {
final Project project = searchContext.getProject();
UIUtil.setContent(editor, selection, 0, -1, project);
final Document document = editor.getDocument();
editor.getSelectionModel().setSelection(0, document.getTextLength());
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitDocument(document);
final PsiFile file = documentManager.getPsiFile(document);
if (file == null) return;
new WriteCommandAction(project, file) {
@Override protected void run(@NotNull Result result) throws Throwable {
CodeStyleManager.getInstance(project).adjustLineIndent(file, new TextRange(0, document.getTextLength()));
}
}.execute();
}
示例4: forcePsiPostprocessAndRestoreElement
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
public static <T extends PsiElement> T forcePsiPostprocessAndRestoreElement(@NotNull T element,
boolean useFileLanguage) {
final PsiFile psiFile = element.getContainingFile();
final Document document = psiFile.getViewProvider().getDocument();
//if (document == null) return element;
final Language language = useFileLanguage ? psiFile.getLanguage() : PsiUtilCore.getDialect(element);
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(psiFile.getProject());
final RangeMarker rangeMarker = document.createRangeMarker(element.getTextRange());
documentManager.doPostponedOperationsAndUnblockDocument(document);
documentManager.commitDocument(document);
T elementInRange = findElementInRange(psiFile, rangeMarker.getStartOffset(), rangeMarker.getEndOffset(),
(Class<? extends T>)element.getClass(),
language);
rangeMarker.dispose();
return elementInRange;
}
示例5: insert
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
private static void insert(final String fqn, final PsiElement element, final Editor editor, final QualifiedNameProvider provider) {
final Project project = editor.getProject();
if (project == null) return;
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitDocument(editor.getDocument());
final PsiFile file = documentManager.getPsiFile(editor.getDocument());
if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
@Override
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
Document document = editor.getDocument();
documentManager.doPostponedOperationsAndUnblockDocument(document);
documentManager.commitDocument(document);
EditorModificationUtil.deleteSelectedText(editor);
provider.insertQualifiedName(fqn, element, editor, project);
}
});
}
}, IdeBundle.message("command.pasting.reference"), null);
}
示例6: runWriteActionRearrangement
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
/**
* must be called from within an IDEA write-action thread.
*
* @param project
* @param document
* @param psiFile
*/
void runWriteActionRearrangement(final Project project,
final Document document,
final PsiFile psiFile,
final RearrangerSettings settings)
{
/**
* Per instructions from IntelliJ, we have to commit any changes to the document to the Psi
* tree.
*/
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitDocument(document);
final WaitableBoolean wb = new WaitableBoolean();
if (psiFile != null &&
isFileWritable(psiFile) &&
psiFile.getName().endsWith(".java"))
{
logger.debug("schedule rearranger task");
final Runnable task = new rearrangerTask(project, psiFile, settings, document, wb);
CommandProcessor.getInstance().executeCommand(project, task, "Rearrange", null);
}
try
{
logger.debug("wait for rearranger task to complete.");
wb.whenTrue();
}
catch (InterruptedException e)
{
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
logger.debug("end execute");
}
示例7: buildLiveRearrangerData
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
/**
* must be called from within an IDEA read-action thread.
*
* @param project
* @param document
* @param psiFile
*/
void buildLiveRearrangerData(final Project project,
final Document document,
final PsiFile psiFile,
final int cursorOffset)
{
/**
* Per instructions from IntelliJ, we have to commit any changes to the document to the Psi
* tree.
*/
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitDocument(document);
final RearrangerSettings settings = new RearrangerSettings(); // use default settings with no rules
settings.setAskBeforeRearranging(true);
final Runnable task = new Runnable()
{
public void run()
{
logger.debug("liveRearrangeDocument task started");
liveRearrangeDocument(project, psiFile, settings, document, cursorOffset);
}
};
Thread t = new Thread(new Runnable()
{
public void run()
{
logger.debug("started thread " + Thread.currentThread().getName());
final Application application = ApplicationManager.getApplication();
application.runReadAction(new Runnable() {
public void run() {
logger.debug("enter application.runReadAction() on thread " + Thread.currentThread().getName());
task.run();
logger.debug("exit application.runReadAction() on thread " + Thread.currentThread().getName());
}});
}
}, "Live Rearranger parser");
t.start();
logger.debug("exit buildLiveRearrangerData on thread " + Thread.currentThread().getName());
}
示例8: 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());
}
示例9: refresh
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
public void refresh() {
if (myDocument != null) {
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(myProject);
psiDocumentManager.commitDocument(myDocument);
}
disposeGroups();
myGroups = new GradleEditorModelParserFacade().parse(myVirtualFile, myProject);
myComponent.setData(myGroups);
}
示例10: 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;
}
示例11: actionPerformed
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
@Override
public void actionPerformed(AnActionEvent event) {
final Project project = CommonDataKeys.PROJECT.getData(event.getDataContext());
if (project == null) {
// no active project
LOG.debug("No project");
return;
}
Editor editor = CommonDataKeys.EDITOR.getData(event.getDataContext());
if (editor == null) {
FileEditorManager fem = FileEditorManager.getInstance(project);
editor = fem.getSelectedTextEditor();
}
if (editor == null) {
// no editor available
LOG.debug("No editor");
return;
}
// do we have an xml file?
final PsiDocumentManager pdm = PsiDocumentManager.getInstance(project);
final PsiFile psiFile = pdm.getPsiFile(editor.getDocument());
if (!(psiFile instanceof XmlFile)) {
// not xml
LOG.debug("No XML-File: " + psiFile);
return;
}
// make sure PSI is in sync with document
pdm.commitDocument(editor.getDocument());
execute(editor);
}
示例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: replaceByTagContent
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
private static void replaceByTagContent(Project project, XmlTag tagToReplace, XmlTag tagToInline)
throws AndroidRefactoringErrorException {
final ASTNode node = tagToInline.getNode();
if (node == null) {
throw new AndroidRefactoringErrorException();
}
final ASTNode startTagEnd = XmlChildRole.START_TAG_END_FINDER.findChild(node);
final ASTNode closingTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(node);
if (startTagEnd == null || closingTagStart == null) {
throw new AndroidRefactoringErrorException();
}
final int contentStart = startTagEnd.getTextRange().getEndOffset();
final int contentEnd = closingTagStart.getTextRange().getStartOffset();
if (contentStart < 0 || contentEnd < 0 || contentStart >= contentEnd) {
throw new AndroidRefactoringErrorException();
}
final PsiFile file = tagToInline.getContainingFile();
if(file == null) {
throw new AndroidRefactoringErrorException();
}
final String textToInline = file.getText().
substring(contentStart, contentEnd).trim();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final Document document = documentManager.getDocument(tagToReplace.getContainingFile());
if (document == null) {
throw new AndroidRefactoringErrorException();
}
final TextRange range = tagToReplace.getTextRange();
document.replaceString(range.getStartOffset(), range.getEndOffset(), textToInline);
documentManager.commitDocument(document);
}
示例14: createProperty
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
public static void createProperty(final Project project,
final Collection<PropertiesFile> propertiesFiles,
final String key,
final String value) throws IncorrectOperationException {
for (PropertiesFile file : propertiesFiles) {
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitDocument(documentManager.getDocument(file.getContainingFile()));
IProperty existingProperty = file.findPropertyByKey(key);
if (existingProperty == null) {
file.addProperty(key, value);
}
}
}
示例15: buildLiveRearrangerData
import com.intellij.psi.PsiDocumentManager; //导入方法依赖的package包/类
/**
* must be called from within an IDEA read-action thread.
*
* @param project
* @param document
* @param psiFile
*/
void buildLiveRearrangerData(final Project project,
final Document document,
final PsiFile psiFile,
final int cursorOffset)
{
/**
* Per instructions from IntelliJ, we have to commit any changes to the document to the Psi
* tree.
*/
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitDocument(document);
final RearrangerSettings settings = new RearrangerSettings(); // use default settings with no rules
settings.setAskBeforeRearranging(true);
final Runnable task = new Runnable()
{
public void run()
{
logger.debug("liveRearrangeDocument task started");
liveRearrangeDocument(project, psiFile, settings, document, cursorOffset);
}
};
Thread t = new Thread(new Runnable()
{
public void run()
{
logger.debug("started thread " + Thread.currentThread().getName());
final Application application = ApplicationManager.getApplication();
application.runReadAction(new Runnable() {
public void run() {
logger.debug("enter application.runReadAction() on thread " + Thread.currentThread().getName());
task.run();
logger.debug("exit application.runReadAction() on thread " + Thread.currentThread().getName());
}});
}
}, "Live Rearranger parser");
t.start();
logger.debug("exit buildLiveRearrangerData on thread " + Thread.currentThread().getName());
}