Repository: neovim/neovim
File: src/nvim/api/command.c (C)

Code before:
    // If the autocommands did something and didn't cause an error, try
    // finding the command again.
    p = (ret && !aborting()) ? find_ex_command(&ea, NULL) : ea.cmd;
  }

  VALIDATE((p != NULL && ea.cmdidx != CMD_SIZE), "Command not found: %s", cmdname, {
    goto end;
  });
  VALIDATE(!is_cmd_ni(ea.cmdidx), "Command not implemented: %s", cmdname, {
    goto end;
  });
  const char *fullname = IS_USER_CMDIDX(ea.cmdidx)
                         ? get_user_command_name(ea.useridx, ea.cmdidx)
                         : get_command_name(NULL, ea.cmdidx);
  VALIDATE(strncmp(fullname, cmdname, strlen(cmdname)) == 0, "Invalid command: \"%s\"", cmdname, {
    goto end;
  });

  // Get the command flags so that we can know what type of arguments the command uses.
  // Not required for a user command since `find_ex_command` already deals with it in that case.
  if (!IS_USER_CMDIDX(ea.cmdidx)) {
    ea.argt = get_cmd_argt(ea.cmdidx);
  }

  bool count_from_first_arg = false;
  // Parse command arguments since it's needed to get the command address type.
  if (HAS_KEY(cmd, cmd, args)) {
    if (cmd->args.size == 1 && (ea.argt & EX_COUNT) && !(ea.argt & EX_EXTRA)) {
      Object first_arg = cmd->args.items[0];
      bool is_numeric = false;
      int64_t count_value = 0;

      if (first_arg.type == kObjectTypeInteger) {
        is_numeric = true;
        count_value = first_arg.data.integer;
      } else if (first_arg.type == kObjectTypeString) {
        char *endptr;
        long val = strtol(first_arg.data.string.data, &endptr, 10);
        if (*endptr == '\0' && first_arg.data.string.size > 0) {
          is_numeric = true;
          count_value = val;
        }
      }

      if (is_numeric && count_value >= 0) {
        count_from_first_arg = true;
        ea.addr_count = 1;
        ea.line1 = ea.line2 = (linenr_T)count_value;
        args = arena_array(arena, 0);
      }
    }

Review comment (suggestion):
a hint comment will be helpful here to show an example of what this means
```suggestion
  bool count_from_first_arg = false;  // "..."
```

Code after:
    // If the autocommands did something and didn't cause an error, try
    // finding the command again.
    p = (ret && !aborting()) ? find_ex_command(&ea, NULL) : ea.cmd;
  }

  VALIDATE((p != NULL && ea.cmdidx != CMD_SIZE), "Command not found: %s", cmdname, {
    goto end;
  });
  VALIDATE(!is_cmd_ni(ea.cmdidx), "Command not implemented: %s", cmdname, {
    goto end;
  });
  const char *fullname = IS_USER_CMDIDX(ea.cmdidx)
                         ? get_user_command_name(ea.useridx, ea.cmdidx)
                         : get_command_name(NULL, ea.cmdidx);
  VALIDATE(strncmp(fullname, cmdname, strlen(cmdname)) == 0, "Invalid command: \"%s\"", cmdname, {
    goto end;
  });

  // Get the command flags so that we can know what type of arguments the command uses.
  // Not required for a user command since `find_ex_command` already deals with it in that case.
  if (!IS_USER_CMDIDX(ea.cmdidx)) {
    ea.argt = get_cmd_argt(ea.cmdidx);
  }

  // Track whether the first argument was interpreted as count to avoid conflicts
  bool count_from_first_arg = false;
  // Parse command arguments since it's needed to get the command address type.
  if (HAS_KEY(cmd, cmd, args)) {
    // Special handling: for commands that support count but not regular arguments,
    // if a single numeric argument is provided, interpret it as count
    if (cmd->args.size == 1 && (ea.argt & EX_COUNT) && !(ea.argt & EX_EXTRA)) {
      Object first_arg = cmd->args.items[0];
      bool is_numeric = false;
      int64_t count_value = 0;

      if (first_arg.type == kObjectTypeInteger) {
        is_numeric = true;
        count_value = first_arg.data.integer;
      } else if (first_arg.type == kObjectTypeString) {
        // Try to parse string as a number Example: vim.api.nvim_cmd({cmd = 'copen', args = {'10'}}, {})
        char *endptr;
        long val = strtol(first_arg.data.string.data, &endptr, 10);
        // Check if entire string was consumed (valid number) and string is not empty
        if (*endptr == '\0' && first_arg.data.string.size > 0) {
          is_numeric = true;
          count_value = val;
        }
      }

      if (is_numeric && count_value >= 0) {
        // Interpret the argument as count