override fun autoIndentRange()

in src/main/java/com/maddyhome/idea/vim/group/ChangeGroup.kt [153:204]


  override fun autoIndentRange(
    editor: VimEditor,
    caret: VimCaret,
    context: ExecutionContext,
    range: TextRange,
  ) {
    val startOffset = editor.getLineStartForOffset(range.startOffset)
    val endOffset = editor.getLineEndForOffset(range.endOffset)
    val ijEditor = (editor as IjVimEditor).editor

    // FIXME: Here we do selection, and it is not a good idea, because it updates primary selection in Linux
    // FIXME: I'll leave here a dirty fix that restores primary selection, but it would be better to rewrite this method
    var copiedText: IjVimCopiedText? = null
    try {
      if (injector.registerGroup.isPrimaryRegisterSupported()) {
        copiedText = injector.clipboardManager.getPrimaryContent(editor, context) as IjVimCopiedText
      }
    } catch (e: Exception) {
      // FIXME: [isPrimaryRegisterSupported()] is not implemented perfectly, so there might be thrown an exception after trying to access the primary selection
      logger.warn("False positive X11 primary selection support")
    }
    ijEditor.selectionModel.vimSetSystemSelectionSilently(startOffset, endOffset)
    val project = ijEditor.project
    val actionExecution = {
      val joinLinesAction = injector.nativeActionManager.indentLines
      if (joinLinesAction != null) {
        injector.actionExecutor.executeAction(editor, joinLinesAction, context)
      }
    }
    val afterAction = {
      val firstLine = editor.offsetToBufferPosition(
        min(startOffset.toDouble(), endOffset.toDouble()).toInt()
      ).line
      val newOffset = injector.motion.moveCaretToLineStartSkipLeading(editor, firstLine)
      caret.moveToOffset(newOffset)
      restoreCursor(editor, caret, (caret as IjVimCaret).caret.logicalPosition.line)
    }
    if (project != null) {
      AsyncActionExecutionService.getInstance(project)
        .withExecutionAfterAction(IdeActions.ACTION_EDITOR_AUTO_INDENT_LINES, actionExecution, afterAction)
    } else {
      actionExecution.invoke()
      afterAction.invoke()
    }
    try {
      if (copiedText != null) {
        injector.clipboardManager.setPrimaryContent(editor, context, copiedText)
      }
    } catch (e: Exception) {
      // FIXME: [isPrimaryRegisterSupported()] is not implemented perfectly, so there might be thrown an exception after trying to access the primary selection
    }
  }