ronantakizawa/codereview-bench huggingface dataset:
Repository: neovim/neovim
File: src/nvim/lua/executor.c (C)

Code before:
  return 0;
}

int nlua_in_fast_event(lua_State *lstate)
{
  lua_pushboolean(lstate, in_fast_callback > 0);
  return 1;
}

static bool viml_func_is_fast(const char *name)
{
  const EvalFuncDef *const fdef = find_internal_func(name);
  if (fdef) {
    return fdef->fast;
  }
  // Not a Vimscript function
  return false;
}

int nlua_call(lua_State *lstate)
{
  Error err = ERROR_INIT;
  size_t name_len;
  const char *name = luaL_checklstring(lstate, 1, &name_len);
  if (!nlua_is_deferred_safe() && !viml_func_is_fast(name)) {
    size_t length = strlen(name) + sizeof("Vimscript function \"\"") + 1;
    char *str = xmalloc(length);
    vim_snprintf(str, length, "Vimscript function \"%s\"", name);
    int ret = luaL_error(lstate, e_fast_api_disabled, str);
    xfree(str);
    return ret;
  }

  int nargs = lua_gettop(lstate) - 1;
  if (nargs > MAX_FUNC_ARGS) {
    return luaL_error(lstate, "Function called with too many arguments");
  }

  typval_T vim_args[MAX_FUNC_ARGS + 1];
  int i = 0;  // also used for freeing the variables
  for (; i < nargs; i++) {
    lua_pushvalue(lstate, i + 2);
    if (!nlua_pop_typval(lstate, &vim_args[i])) {
      api_set_error(&err, kErrorTypeException,
                    "error converting argument %d", i + 1);
      goto free_vim_args;
    }
  }

  // TODO(bfredl): this should be simplified in error handling refactor
  force_abort = false;

Review comment (suggestion):
`sizeof(...)` already includes the NUL
```suggestion
    size_t length = strlen(name) + sizeof("Vimscript function \"\"");
```

Code after:
      .v_type = VAR_STRING,
      .vval.v_string = "lua_debug> ",
    },
    {
      .v_type = VAR_UNKNOWN,
    },
  };
  while (true) {
    lua_settop(lstate, 0);
    typval_T input;
    get_user_input(input_args, &input, false, false);
    msg_putchar('\n');  // Avoid outputting on input line.
    if (input.v_type != VAR_STRING
        || input.vval.v_string == NULL
        || *input.vval.v_string == NUL
        || strcmp(input.vval.v_string, "cont") == 0) {
      tv_clear(&input);
      return 0;
    }
    if (luaL_loadbuffer(lstate, input.vval.v_string,
                        strlen(input.vval.v_string), "=(debug command)")) {
      nlua_error(lstate, _("E5115: Loading Lua debug string: %.*s"));
    } else if (nlua_pcall(lstate, 0, 0)) {
      nlua_error(lstate, _("E5116: Calling Lua debug string: %.*s"));
    }
    tv_clear(&input);
  }
  return 0;
}

int nlua_in_fast_event(lua_State *lstate)
{
  lua_pushboolean(lstate, in_fast_callback > 0);
  return 1;
}

static bool viml_func_is_fast(const char *name)
{
  const EvalFuncDef *const fdef = find_internal_func(name);
  if (fdef) {
    return fdef->fast;
  }
  // Not a Vimscript function
  return false;
}

int nlua_call(lua_State *lstate)
{
  Error err = ERROR_INIT;
  size_t name_len;
  const char *name = luaL_checklstring(lstate, 1, &name_len);
Repository: neovim/neovim
File: runtime/lua/vim/_stringbuffer.lua (Lua)

Code before:
-- Basic shim for LuaJIT's stringbuffer.
-- Note this does not implement the full API.
local M = {}

local has_strbuffer, strbuffer = pcall(require, 'string.buffer')

if has_strbuffer then
  M.new = strbuffer.new

  -- Lua 5.1 does not have __len metamethod so we need to provdie a len()
  -- function to use instead.

  --- @param buf vim._stringbuffer
  function M.len(buf)
    return #buf
  end

  return M
end

--- @class vim._stringbuffer
--- @field private buffer string[]
--- @field private skip_ptr integer
--- @field package len integer
local StrBuffer = {}
StrBuffer.__index = StrBuffer

--- @return string
function StrBuffer:tostring()
  if self.skip_ptr == 0 and #self.buffer <= 1 then
    return self.buffer[1] or ''
  end
  local ret = table.concat(self.buffer):sub(self.skip_ptr + 1)
  self.skip_ptr = 0
  self.buffer = { ret }

Review comment (suggestion):
Nit:
```suggestion
  -- Lua 5.1 does not have __len metamethod so we need to provide a len()
```

Code after:
-- Basic shim for LuaJIT's stringbuffer.
-- Note this does not implement the full API.
-- This is intentionally internal-only. If we want to expose it, we should
-- reimplement this a userdata and ship it as `string.buffer`
-- (minus the FFI stuff) for Lua 5.1.
local M = {}

local has_strbuffer, strbuffer = pcall(require, 'string.buffer')

if has_strbuffer then
  M.new = strbuffer.new

  -- Lua 5.1 does not have __len metamethod so we need to provide a len()
  -- function to use instead.

  --- @param buf vim._stringbuffer
  --- @return integer
  function M.len(buf)
    return #buf
  end

  return M
end

--- @class vim._stringbuffer
--- @field private buf string[]
--- @field package len integer absolute length of the `buf`
--- @field package skip_ptr integer
local StrBuffer = {}
StrBuffer.__index = StrBuffer

--- @return string
function StrBuffer:tostring()
  if #self.buf > 1 then
    self.buf = { table.concat(self.buf) }
Repository: neovim/neovim
File: runtime/lua/vim/_stringbuffer.lua (Lua)

Code before:
-- Basic shim for LuaJIT's stringbuffer.
-- Note this does not implement the full API.
local M = {}

local has_strbuffer, strbuffer = pcall(require, 'string.buffer')

if has_strbuffer then
  M.new = strbuffer.new

  -- Lua 5.1 does not have __len metamethod so we need to provdie a len()
  -- function to use instead.

  --- @param buf vim._stringbuffer
  function M.len(buf)
    return #buf
  end

  return M
end

--- @class vim._stringbuffer
--- @field private buffer string[]
--- @field private skip_ptr integer
--- @field package len integer
local StrBuffer = {}
StrBuffer.__index = StrBuffer

--- @return string
function StrBuffer:tostring()
  if self.skip_ptr == 0 and #self.buffer <= 1 then
    return self.buffer[1] or ''
  end
  local ret = table.concat(self.buffer):sub(self.skip_ptr + 1)
  self.skip_ptr = 0
  self.buffer = { ret }
  return ret
end

StrBuffer.__tostring = StrBuffer.tostring

--- @param chunk string

Review comment (suggestion):
I had not realized that Lua hoisted types such that you can reference this type even if you define it below. Neat.

Code after:
-- Basic shim for LuaJIT's stringbuffer.
-- Note this does not implement the full API.
-- This is intentionally internal-only. If we want to expose it, we should
-- reimplement this a userdata and ship it as `string.buffer`
-- (minus the FFI stuff) for Lua 5.1.
local M = {}

local has_strbuffer, strbuffer = pcall(require, 'string.buffer')

if has_strbuffer then
  M.new = strbuffer.new

  -- Lua 5.1 does not have __len metamethod so we need to provide a len()
  -- function to use instead.

  --- @param buf vim._stringbuffer
  --- @return integer
  function M.len(buf)
    return #buf
  end

  return M
end

--- @class vim._stringbuffer
--- @field private buf string[]
--- @field package len integer absolute length of the `buf`
--- @field package skip_ptr integer
local StrBuffer = {}
StrBuffer.__index = StrBuffer

--- @return string
function StrBuffer:tostring()
  if #self.buf > 1 then
    self.buf = { table.concat(self.buf) }
  end

  -- assert(self.len == #(self.buf[1] or ''), 'len mismatch')

  if self.skip_ptr > 0 then
    if self.buf[1] then
Repository: neovim/neovim
File: runtime/lua/vim/_stringbuffer.lua (Lua)

Code before:
  M.new = strbuffer.new

  -- Lua 5.1 does not have __len metamethod so we need to provdie a len()
  -- function to use instead.

  --- @param buf vim._stringbuffer
  function M.len(buf)
    return #buf
  end

  return M
end

--- @class vim._stringbuffer
--- @field private buffer string[]
--- @field private skip_ptr integer
--- @field package len integer
local StrBuffer = {}
StrBuffer.__index = StrBuffer

--- @return string
function StrBuffer:tostring()
  if self.skip_ptr == 0 and #self.buffer <= 1 then
    return self.buffer[1] or ''
  end
  local ret = table.concat(self.buffer):sub(self.skip_ptr + 1)
  self.skip_ptr = 0
  self.buffer = { ret }
  return ret
end

StrBuffer.__tostring = StrBuffer.tostring

--- @param chunk string
function StrBuffer:put(chunk)
  local s = tostring(chunk)
  self.buffer[#self.buffer + 1] = s
  self.len = self.len + #s
  return self
end

--- @param n integer
--- @return string
function StrBuffer:get(n)
  local r = self:tostring():sub(1, n)
  self:skip(n)
  return r
end

--- @param n integer
function StrBuffer:skip(n)

Review comment (suggestion):
This implementation will be called with `print()` right? If so, I think it's quite unexpected for it to modify internal state.

Code after:
local has_strbuffer, strbuffer = pcall(require, 'string.buffer')

if has_strbuffer then
  M.new = strbuffer.new

  -- Lua 5.1 does not have __len metamethod so we need to provide a len()
  -- function to use instead.

  --- @param buf vim._stringbuffer
  --- @return integer
  function M.len(buf)
    return #buf
  end

  return M
end

--- @class vim._stringbuffer
--- @field private buf string[]
--- @field package len integer absolute length of the `buf`
--- @field package skip_ptr integer
local StrBuffer = {}
StrBuffer.__index = StrBuffer

--- @return string
function StrBuffer:tostring()
  if #self.buf > 1 then
    self.buf = { table.concat(self.buf) }
  end

  -- assert(self.len == #(self.buf[1] or ''), 'len mismatch')

  if self.skip_ptr > 0 then
    if self.buf[1] then
      self.buf[1] = self.buf[1]:sub(self.skip_ptr + 1)
      self.len = self.len - self.skip_ptr
    end
    self.skip_ptr = 0
  end

  -- assert(self.len == #(self.buf[1] or ''), 'len mismatch')

  return self.buf[1] or ''
end

StrBuffer.__tostring = StrBuffer.tostring

--- @private
--- Efficiently peak at the first `n` characters of the buffer.
--- @param n integer
--- @return string
Repository: neovim/neovim
File: runtime/lua/vim/_stringbuffer.lua (Lua)

Code before:
-- Basic shim for LuaJIT's stringbuffer.
-- Note this does not implement the full API.
local M = {}

local has_strbuffer, strbuffer = pcall(require, 'string.buffer')

if has_strbuffer then
  M.new = strbuffer.new

  -- Lua 5.1 does not have __len metamethod so we need to provide a len()
  -- function to use instead.

  --- @param buf vim._stringbuffer
  function M.len(buf)
    return #buf
  end

  return M
end

--- @class vim._stringbuffer
--- @field private buf string[]
--- @field package len integer absolute length of the `buf`
--- @field package skip_ptr integer
local StrBuffer = {}
StrBuffer.__index = StrBuffer


Review comment (suggestion):
```suggestion
-- Note this does not implement the full API.
-- This is intentionally internal-only. If we want to expose it, we should ship
-- it as `string.buffer` (minus the FFI stuff) for Lua 5.1.
```


Code after:
-- Basic shim for LuaJIT's stringbuffer.
-- Note this does not implement the full API.
-- This is intentionally internal-only. If we want to expose it, we should
-- reimplement this a userdata and ship it as `string.buffer`
-- (minus the FFI stuff) for Lua 5.1.
local M = {}

local has_strbuffer, strbuffer = pcall(require, 'string.buffer')

if has_strbuffer then
  M.new = strbuffer.new

  -- Lua 5.1 does not have __len metamethod so we need to provide a len()
  -- function to use instead.

  --- @param buf vim._stringbuffer
  --- @return integer
  function M.len(buf)
    return #buf
  end

  return M
end

--- @class vim._stringbuffer
--- @field private buf string[]
--- @field package len integer absolute length of the `buf`
Repository: neovim/neovim
File: test/functional/plugin/lsp/inlay_hint_spec.lua (Lua)

Code before:
          inlayHintProvider = true,
        },
        handlers = {
          ['textDocument/inlayHint'] = function(_, _, callback)
            callback(nil, vim.json.decode(response))
          end,
        },
      })

      vim.api.nvim_win_set_buf(0, bufnr)

      return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
    end)
    insert(text)
  end)

  it('renders hints with same position in received order', function()
    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
    screen:expect({ grid = grid_with_inlay_hints })
    exec_lua(function()
      vim.lsp.stop_client(client_id)
    end)
    screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
  end)

  it('refresh hints after been requested', function()
    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
    screen:expect({ grid = grid_with_inlay_hints })
    feed('kibefore <Esc>')
    screen:expect([[
  before^ {1:01234}test text                             |
                                                    |*2
]])
    exec_lua(function()
      vim.lsp.inlay_hint.on_refresh(
        nil,
        nil,
        { method = 'workspace/inlayHint/refresh', client_id = client_id }
      )
    end)
    screen:expect([[
  {1:01234}before^ test text                             |
                                                    |*2
]])
  end)

  after_each(function()
    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
  end)
end)


Review comment (suggestion):
```suggestion
  it('refreshes hints after refresh request', function()
```

Code after:
          inlayHintProvider = true,
        },
        handlers = {
          ['textDocument/inlayHint'] = function(_, _, callback)
            callback(nil, vim.json.decode(response))
          end,
        },
      })

      vim.api.nvim_win_set_buf(0, bufnr)

      return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
    end)
    insert(text)
  end)

  it('renders hints with same position in received order', function()
    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
    screen:expect({ grid = grid_with_inlay_hints })
    exec_lua(function()
      vim.lsp.stop_client(client_id)
    end)
    screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
  end)

  it('refreshes hints on request', function()
    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
    screen:expect({ grid = grid_with_inlay_hints })
    feed('kibefore <Esc>')
    screen:expect([[
      before^ {1:01234}test text                             |
                                                        |*2
    ]])
    exec_lua(function()
      vim.lsp.inlay_hint.on_refresh(
        nil,
        nil,
        { method = 'workspace/inlayHint/refresh', client_id = client_id }
      )
    end)
    screen:expect([[
      {1:01234}before^ test text                             |
                                                        |*2
    ]])
  end)

  after_each(function()
    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
  end)
end)
Repository: neovim/neovim
File: test/functional/plugin/lsp/inlay_hint_spec.lua (Lua)

Code before:
      })

      vim.api.nvim_win_set_buf(0, bufnr)

      return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
    end)
    insert(text)
  end)

  it('renders hints with same position in received order', function()
    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
    screen:expect({ grid = grid_with_inlay_hints })
    exec_lua(function()
      vim.lsp.stop_client(client_id)
    end)
    screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
  end)

  it('refreshes hints after been requested', function()
    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
    screen:expect({ grid = grid_with_inlay_hints })
    feed('kibefore <Esc>')
    screen:expect([[
  before^ {1:01234}test text                             |
                                                    |*2
]])
    exec_lua(function()
      vim.lsp.inlay_hint.on_refresh(
        nil,
        nil,
        { method = 'workspace/inlayHint/refresh', client_id = client_id }
      )
    end)
    screen:expect([[
  {1:01234}before^ test text                             |
                                                    |*2
]])
  end)

  after_each(function()
    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
  end)
end)


Review comment (suggestion):
```suggestion
      before^ {1:01234}test text                             |
                                                        |*2
    ]])
```

Code after:
      })

      vim.api.nvim_win_set_buf(0, bufnr)

      return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
    end)
    insert(text)
  end)

  it('renders hints with same position in received order', function()
    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
    screen:expect({ grid = grid_with_inlay_hints })
    exec_lua(function()
      vim.lsp.stop_client(client_id)
    end)
    screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
  end)

  it('refreshes hints on request', function()
    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
    screen:expect({ grid = grid_with_inlay_hints })
    feed('kibefore <Esc>')
    screen:expect([[
      before^ {1:01234}test text                             |
                                                        |*2
    ]])
    exec_lua(function()
      vim.lsp.inlay_hint.on_refresh(
        nil,
        nil,
        { method = 'workspace/inlayHint/refresh', client_id = client_id }
      )
    end)
    screen:expect([[
      {1:01234}before^ test text                             |
                                                        |*2
    ]])
  end)

  after_each(function()
    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
  end)
end)
Repository: neovim/neovim
File: test/functional/plugin/lsp/inlay_hint_spec.lua (Lua)

Code before:
    screen:expect({ grid = grid_with_inlay_hints })
    exec_lua(function()
      vim.lsp.stop_client(client_id)
    end)
    screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
  end)

  it('refreshes hints after been requested', function()
    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
    screen:expect({ grid = grid_with_inlay_hints })
    feed('kibefore <Esc>')
    screen:expect([[
  before^ {1:01234}test text                             |
                                                    |*2
]])
    exec_lua(function()
      vim.lsp.inlay_hint.on_refresh(
        nil,
        nil,
        { method = 'workspace/inlayHint/refresh', client_id = client_id }
      )
    end)
    screen:expect([[
  {1:01234}before^ test text                             |
                                                    |*2
]])
  end)

  after_each(function()
    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
  end)
end)


Review comment (suggestion):
```suggestion
    screen:expect([[
      {1:01234}before^ test text                             |
                                                        |*2
    ]])
```

Code after:
    screen:expect({ grid = grid_with_inlay_hints })
    exec_lua(function()
      vim.lsp.stop_client(client_id)
    end)
    screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
  end)

  it('refreshes hints on request', function()
    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
    screen:expect({ grid = grid_with_inlay_hints })
    feed('kibefore <Esc>')
    screen:expect([[
      before^ {1:01234}test text                             |
                                                        |*2
    ]])
    exec_lua(function()
      vim.lsp.inlay_hint.on_refresh(
        nil,
        nil,
        { method = 'workspace/inlayHint/refresh', client_id = client_id }
      )
    end)
    screen:expect([[
      {1:01234}before^ test text                             |
                                                        |*2
    ]])
  end)

  after_each(function()
    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
  end)
end)
Repository: neovim/neovim
File: test/functional/plugin/lsp/inlay_hint_spec.lua (Lua)

Code before:
          inlayHintProvider = true,
        },
        handlers = {
          ['textDocument/inlayHint'] = function(_, _, callback)
            callback(nil, vim.json.decode(response))
          end,
        },
      })

      vim.api.nvim_win_set_buf(0, bufnr)

      return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
    end)
    insert(text)
  end)

  it('renders hints with same position in received order', function()
    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
    screen:expect({ grid = grid_with_inlay_hints })
    exec_lua(function()
      vim.lsp.stop_client(client_id)
    end)
    screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
  end)

  it('refreshes hints after been requested', function()
    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
    screen:expect({ grid = grid_with_inlay_hints })
    feed('kibefore <Esc>')
    screen:expect([[
  before^ {1:01234}test text                             |
                                                    |*2
]])
    exec_lua(function()
      vim.lsp.inlay_hint.on_refresh(
        nil,
        nil,
        { method = 'workspace/inlayHint/refresh', client_id = client_id }
      )
    end)
    screen:expect([[
  {1:01234}before^ test text                             |
                                                    |*2
]])
  end)

  after_each(function()
    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
  end)
end)


Review comment (suggestion):
```suggestion
  it('refreshes hints on request', function()
```

Code after:
          inlayHintProvider = true,
        },
        handlers = {
          ['textDocument/inlayHint'] = function(_, _, callback)
            callback(nil, vim.json.decode(response))
          end,
        },
      })

      vim.api.nvim_win_set_buf(0, bufnr)

      return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
    end)
    insert(text)
  end)

  it('renders hints with same position in received order', function()
    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
    screen:expect({ grid = grid_with_inlay_hints })
    exec_lua(function()
      vim.lsp.stop_client(client_id)
    end)
    screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
  end)

  it('refreshes hints on request', function()
    exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
    screen:expect({ grid = grid_with_inlay_hints })
    feed('kibefore <Esc>')
    screen:expect([[
      before^ {1:01234}test text                             |
                                                        |*2
    ]])
    exec_lua(function()
      vim.lsp.inlay_hint.on_refresh(
        nil,
        nil,
        { method = 'workspace/inlayHint/refresh', client_id = client_id }
      )
    end)
    screen:expect([[
      {1:01234}before^ test text                             |
                                                        |*2
    ]])
  end)

  after_each(function()
    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
  end)
end)