override fun processCommand()

in vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/commands/HistoryCommand.kt [32:95]


  override fun processCommand(
    editor: VimEditor,
    context: ExecutionContext,
    operatorArguments: OperatorArguments,
  ): ExecutionResult {
    if (modifier == CommandModifier.BANG) {
      throw exExceptionMessage("E477")
    }

    if (injector.options(editor).history == 0) {
      injector.messages.showStatusBarMessage(editor, injector.messages.message("command.history.option.is.zero"))
      return ExecutionResult.Success
    }

    // Strip any trailing whitespace. There won't be leading whitespace
    val arg = argument.trim()

    // The argument is an optional type, followed by optional whitespace, followed by an optional range. First, let's
    // figure out what the type is, and get the offset of the next part to parse
    val (type, offset) = parseType(arg)

    var first: Int
    var last: Int

    // The rest is either empty, a from value, a from/to pair, or trailing characters
    val rest = arg.substring(offset)
    val regex = """^\s*((?<first>-?\d+)(\s*,\s*(?<last>-?\d+))?)(?<trailing>.*)""".toRegex()
    val match = regex.matchEntire(rest)
    if (match == null && rest.isNotEmpty() && rest.isNotBlank()) {
      throw exExceptionMessage("E488", rest.trim())
    }
    else {
      val trailing = match?.groups?.get("trailing")?.value ?: ""
      if (trailing.isNotBlank()) {
        throw exExceptionMessage("E488", trailing.trim())
      }

      first = match?.groups?.get("first")?.value?.toIntOrNull() ?: 0
      last = match?.groups?.get("last")?.value?.toIntOrNull() ?: 0
    }

    val text = buildString {
      // Order is important here
      if (type == CMD || type == ALL) {
        outputHistory(CMD, VimHistory.Type.Command, first, last)
      }
      if (type == ALL) appendLine()
      if (type == SEARCH || type == ALL) {
        outputHistory(SEARCH, VimHistory.Type.Search, first, last)
      }
      if (type == ALL) appendLine()
      if (type == EXPRESSION || type == ALL) {
        outputHistory(EXPRESSION, VimHistory.Type.Expression, first, last)
      }
      if (type == ALL) appendLine()
      if (type == INPUT || type == ALL) {
        outputHistory(INPUT, VimHistory.Type.Input, first, last)
      }
      if (type == ALL) appendLine()
    }

    injector.outputPanel.output(editor, context, text)
    return ExecutionResult.Success
  }