Java Code Examples for com.intellij.openapi.editor.EditorModificationUtil#checkModificationAllowed()

The following examples show how to use com.intellij.openapi.editor.EditorModificationUtil#checkModificationAllowed() . 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
Source File: CodeInsightAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredUIAccess
public void actionPerformedImpl(@Nonnull final Project project, final Editor editor) {
  if (editor == null) return;
  //final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
  final PsiFile psiFile = PsiUtilBase.getPsiFileInEditor(editor, project);
  if (psiFile == null) return;
  final CodeInsightActionHandler handler = getHandler();
  PsiElement elementToMakeWritable = handler.getElementToMakeWritable(psiFile);
  if (elementToMakeWritable != null && !(EditorModificationUtil.checkModificationAllowed(editor) && FileModificationService.getInstance().preparePsiElementsForWrite(elementToMakeWritable))) {
    return;
  }

  CommandProcessor.getInstance().executeCommand(project, () -> {
    final Runnable action = () -> {
      if (!ApplicationManager.getApplication().isHeadlessEnvironment() && !editor.getContentComponent().isShowing()) return;
      handler.invoke(project, editor, psiFile);
    };
    if (handler.startInWriteAction()) {
      ApplicationManager.getApplication().runWriteAction(action);
    }
    else {
      action.run();
    }
  }, getCommandName(), DocCommandGroupId.noneGroupId(editor.getDocument()), editor.getDocument());
}
 
Example 2
Source File: BaseRefactorHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void run() {
  if (!EditorModificationUtil.checkModificationAllowed(editor)) {
    return;
  }
  if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)) {
    return;
  }

  process(chooser.getSelectedElements());
}
 
Example 3
Source File: LookupTypedHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(@Nonnull Editor originalEditor, char charTyped, @Nonnull DataContext dataContext) {
  final Project project = dataContext.getData(CommonDataKeys.PROJECT);
  PsiFile file = project == null ? null : PsiUtilBase.getPsiFileInEditor(originalEditor, project);

  if (file == null) {
    if (myOriginalHandler != null) {
      myOriginalHandler.execute(originalEditor, charTyped, dataContext);
    }
    return;
  }

  if (!EditorModificationUtil.checkModificationAllowed(originalEditor)) {
    return;
  }

  CompletionPhase oldPhase = CompletionServiceImpl.getCompletionPhase();
  if (oldPhase instanceof CompletionPhase.CommittingDocuments && oldPhase.indicator != null) {
    oldPhase.indicator.scheduleRestart();
  }

  Editor editor = TypedHandler.injectedEditorIfCharTypedIsSignificant(charTyped, originalEditor, file);
  if (editor != originalEditor) {
    file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
  }

  if (originalEditor.isInsertMode() && beforeCharTyped(charTyped, project, originalEditor, editor, file)) {
    return;
  }

  if (myOriginalHandler != null) {
    myOriginalHandler.execute(originalEditor, charTyped, dataContext);
  }
}
 
Example 4
Source File: CodeCompletionHandlerBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void invokeCompletion(@Nonnull Project project, @Nonnull Editor editor, int time, boolean hasModifiers, @Nonnull Caret caret) {
  markCaretAsProcessed(caret);

  if (invokedExplicitly) {
    StatisticsUpdate.applyLastCompletionStatisticsUpdate();
  }

  checkNoWriteAccess();

  CompletionAssertions.checkEditorValid(editor);

  int offset = editor.getCaretModel().getOffset();
  if (editor.isViewer() || editor.getDocument().getRangeGuard(offset, offset) != null) {
    editor.getDocument().fireReadOnlyModificationAttempt();
    EditorModificationUtil.checkModificationAllowed(editor);
    return;
  }

  if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)) {
    return;
  }

  CompletionPhase phase = CompletionServiceImpl.getCompletionPhase();
  boolean repeated = phase.indicator != null && phase.indicator.isRepeatedInvocation(completionType, editor);

  final int newTime = phase.newCompletionStarted(time, repeated);
  if (invokedExplicitly) {
    time = newTime;
  }
  final int invocationCount = time;
  if (CompletionServiceImpl.isPhase(CompletionPhase.InsertedSingleItem.class)) {
    CompletionServiceImpl.setCompletionPhase(CompletionPhase.NoCompletion);
  }
  CompletionServiceImpl.assertPhase(CompletionPhase.NoCompletion.getClass(), CompletionPhase.CommittingDocuments.class);

  if (invocationCount > 1 && completionType == CompletionType.BASIC) {
    FeatureUsageTracker.getInstance().triggerFeatureUsed(CodeCompletionFeatures.SECOND_BASIC_COMPLETION);
  }

  long startingTime = System.currentTimeMillis();

  Runnable initCmd = () -> {
    CompletionInitializationContextImpl context =
            withTimeout(calcSyncTimeOut(startingTime), () -> CompletionInitializationUtil.createCompletionInitializationContext(project, editor, caret, invocationCount, completionType));

    boolean hasValidContext = context != null;
    if (!hasValidContext) {
      final PsiFile psiFile = PsiUtilBase.getPsiFileInEditor(caret, project);
      context = new CompletionInitializationContextImpl(editor, caret, psiFile, completionType, invocationCount);
    }

    doComplete(context, hasModifiers, hasValidContext, startingTime);
  };
  try {
    if (autopopup) {
      CommandProcessor.getInstance().runUndoTransparentAction(initCmd);
    }
    else {
      CommandProcessor.getInstance().executeCommand(project, initCmd, null, null, editor.getDocument());
    }
  }
  catch (IndexNotReadyException e) {
    if (invokedExplicitly) {
      DumbService.getInstance(project).showDumbModeNotification("Code completion is not available here while indices are being built");
    }
  }
}