Java Code Examples for com.intellij.psi.PsiDocumentManager#commitAllDocuments()

The following examples show how to use com.intellij.psi.PsiDocumentManager#commitAllDocuments() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private void formatInternal(PsiFile file, Collection<TextRange> ranges) {
  ApplicationManager.getApplication().assertWriteAccessAllowed();
  PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
  documentManager.commitAllDocuments();
  CheckUtil.checkWritable(file);

  Document document = documentManager.getDocument(file);

  if (document == null) {
    return;
  }
  // If there are postponed PSI changes (e.g., during a refactoring), just abort.
  // If we apply them now, then the incoming text ranges may no longer be valid.
  if (documentManager.isDocumentBlockedByPsi(document)) {
    return;
  }

  format(file, document, ranges);
}
 
Example 2
private void formatInternal(PsiFile file, Collection<TextRange> ranges) {
  ApplicationManager.getApplication().assertWriteAccessAllowed();
  PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
  documentManager.commitAllDocuments();
  CheckUtil.checkWritable(file);

  Document document = documentManager.getDocument(file);

  if (document == null) {
    return;
  }
  // If there are postponed PSI changes (e.g., during a refactoring), just abort.
  // If we apply them now, then the incoming text ranges may no longer be valid.
  if (documentManager.isDocumentBlockedByPsi(document)) {
    return;
  }

  format(document, ranges);
}
 
Example 3
private static void iterateOverCarets(@Nonnull final Project project,
                                      @Nonnull final Editor hostEditor,
                                      @Nonnull final MultiCaretCodeInsightActionHandler handler) {
  PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
  final PsiFile psiFile = documentManager.getCachedPsiFile(hostEditor.getDocument());
  documentManager.commitAllDocuments();

  hostEditor.getCaretModel().runForEachCaret(new CaretAction() {
    @Override
    public void perform(Caret caret) {
      Editor editor = hostEditor;
      if (psiFile != null) {
        Caret injectedCaret = InjectedLanguageUtil.getCaretForInjectedLanguageNoCommit(caret, psiFile);
        if (injectedCaret != null) {
          caret = injectedCaret;
          editor = caret.getEditor();
        }
      }
      final PsiFile file = PsiUtilBase.getPsiFileInEditor(caret, project);
      if (file != null) {
        handler.invoke(project, editor, caret, file);
      }
    }
  });
}
 
Example 4
Source Project: consulo   File: PasteHandler.java    License: Apache License 2.0 6 votes vote down vote up
static void indentBlock(Project project, Editor editor, final int startOffset, final int endOffset, int originalCaretCol) {
  final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
  documentManager.commitAllDocuments();
  final Document document = editor.getDocument();
  PsiFile file = documentManager.getPsiFile(document);
  if (file == null) {
    return;
  }

  if (LanguageFormatting.INSTANCE.forContext(file) != null) {
    indentBlockWithFormatter(project, document, startOffset, endOffset, file);
  }
  else {
    indentPlainTextBlock(document, startOffset, endOffset, originalCaretCol);
  }
}
 
Example 5
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
  final Project project = event.getProject();
  if (project == null) {
    return;
  }

  PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
  if (psiDocumentManager.hasUncommitedDocuments()) {
    psiDocumentManager.commitAllDocuments();
  }

  final DataContext dataContext = event.getDataContext();
  final Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);

  if (null != editor) {
    final PsiFile psiFile = PsiUtilBase.getPsiFileInEditor(editor, project);
    if (null != psiFile) {
      final PsiClass targetClass = getTargetClass(editor, psiFile);
      if (null != targetClass) {
        process(project, psiFile, targetClass);
      }
    }
  } else {
    final VirtualFile[] files = PlatformDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
    if (null != files) {
      for (VirtualFile file : files) {
        if (file.isDirectory()) {
          processDirectory(project, file);
        } else {
          processFile(project, file);
        }
      }
    }
  }
}
 
Example 6
Source Project: consulo   File: UsageViewTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testUsageViewHandlesDocumentChange() throws Exception {
  PsiFile psiFile = createFile("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);
}
 
Example 7
Source Project: consulo   File: UsageViewTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testTextUsageInfoHandlesDocumentChange() throws Exception {
  PsiFile psiFile = createFile("X.java", "public class X{ int xxx; } //comment");
  Usage usage = new UsageInfo2UsageAdapter(new UsageInfo(psiFile, psiFile.getText().indexOf("xxx"), StringUtil.indexOfSubstringEnd(psiFile.getText(),"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);
}
 
Example 8
Source Project: consulo   File: BaseCodeInsightAction.java    License: Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
public static Editor getInjectedEditor(@Nonnull Project project, final Editor editor, boolean commit) {
  Editor injectedEditor = editor;
  if (editor != null) {
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    PsiFile psiFile = documentManager.getCachedPsiFile(editor.getDocument());
    if (psiFile != null) {
      if (commit) documentManager.commitAllDocuments();
      injectedEditor = InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(editor, psiFile);
    }
  }
  return injectedEditor;
}