ronantakizawa/codereview-bench huggingface dataset:
Repository: neovim/neovim
File: runtime/lua/vim/treesitter/query.lua (Lua)

Code before:
  if opts and opts.clear then
    vim.treesitter._query_linter.clear(buf)
  else
    vim.treesitter._query_linter.lint(buf, opts)
  end
end

--- Omnifunc for completing node names and predicates in treesitter queries.
---
--- Use via
---
--- ```lua
--- vim.bo.omnifunc = 'v:lua.vim.treesitter.query.omnifunc'
--- ```
---
--- @param findstart 0|1
--- @param base string
function M.omnifunc(findstart, base)
  return vim.treesitter._query_linter.omnifunc(findstart, base)
end

--- Returns available treesitter languages.
function M._complete()
  local parsers = vim.api.nvim_get_runtime_file('parser/*', true)
  local parser_names_set = {} ---@type table<string, boolean>
  for _, parser in ipairs(parsers) do
    local parser_name = vim.fn.fnamemodify(parser, ':t:r')
    parser_names_set[parser_name] = true
  end
  return vim.tbl_keys(parser_names_set)
end

--- Opens a live editor to query the buffer you started from.
---
--- Can also be shown with `:EditQuery`. [:EditQuery]()
---
--- `:EditQuery <tab>` completes injected language names.
---
--- If you move the cursor to a capture name ("@foo"), text matching the capture is highlighted in
--- the source buffer. The query editor is a scratch buffer, use `:write` to save it. You can find
--- example queries at `$VIMRUNTIME/queries/`.
---
--- @param lang? string language to open the query editor for. If omitted, inferred from the current buffer's filetype.
function M.edit(lang)
  assert(vim.treesitter.dev.edit_query(lang))
end

return M


Review comment (suggestion):
```suggestion
--- Can also be shown with `:EditQuery`. [:EditQuery]()
--- `:EditQuery <tab>` completes injected language names.
```

Code after:
  if opts and opts.clear then
    vim.treesitter._query_linter.clear(buf)
  else
    vim.treesitter._query_linter.lint(buf, opts)
  end
end

--- Omnifunc for completing node names and predicates in treesitter queries.
---
--- Use via
---
--- ```lua
--- vim.bo.omnifunc = 'v:lua.vim.treesitter.query.omnifunc'
--- ```
---
--- @param findstart 0|1
--- @param base string
function M.omnifunc(findstart, base)
  return vim.treesitter._query_linter.omnifunc(findstart, base)
end

--- Opens a live editor to query the buffer you started from.
---
--- Can also be shown with `:EditQuery`. [:EditQuery]()
---
--- `:EditQuery <tab>` completes the treesitter parser names in the runtime path.
---
--- If you move the cursor to a capture name ("@foo"), text matching the capture is highlighted in
--- the source buffer. The query editor is a scratch buffer, use `:write` to save it. You can find
--- example queries at `$VIMRUNTIME/queries/`.
---
--- @param lang? string language to open the query editor for. If omitted, inferred from the current buffer's filetype.
function M.edit(lang)
  assert(vim.treesitter.dev.edit_query(lang))
end

return M
Repository: neovim/neovim
File: runtime/lua/vim/pack.lua (Lua)

Code before:
  --- @type integer
  local cancel_au_id
  local function on_cancel(data)
    if tonumber(data.match) ~= win_id then
      return
    end
    pcall(api.nvim_del_autocmd, cancel_au_id)
    delete_buffer()
  end
  cancel_au_id = api.nvim_create_autocmd('WinClosed', { nested = true, callback = on_cancel })

  -- Set buffer-local options last (so that user autocmmands could override)
  vim.bo[bufnr].modified = false
  vim.bo[bufnr].modifiable = false
  vim.bo[bufnr].buftype = 'acwrite'
  vim.bo[bufnr].filetype = 'nvim-pack'

  -- Attach in-process LSP for more capabilities
  vim.lsp.buf_attach_client(bufnr, require('vim.pack._lsp').client_id)
end

--- Get map of plugin names that need update: all plugin sections present in
--- confirmation buffer under "# Update" section.
--- @param bufnr integer
--- @return table<string,boolean>
local function get_confirmed_update_plugins(bufnr)
  local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
  --- @type table<string,boolean>, boolean
  local res, is_in_update = {}, false
  for _, l in ipairs(lines) do
    local name = l:match('^## (.+)$')
    if name and is_in_update then
      res[name] = true
    end

    local group = l:match('^# (%S+)')
    if group then
      is_in_update = group == 'Update'
      if not is_in_update then
        break
      end
    end
  end
  return res
end

--- @class vim.pack.keyset.update
--- @inlinedoc
--- @field force? boolean Whether to skip confirmation and make updates immediately. Default `false`.

--- Update plugins

Review comment (suggestion):
the doc seems to imply this name:


```suggestion
local function get_confirm_update_plugins(bufnr)
```

Code after:
      if not ok then
        p.info.err = err
      end
    end
    if p.info.err ~= '' then
      errors[#errors + 1] = ('`%s`:\n%s'):format(p.spec.name, p.info.err)
    end
  end

  if #errors > 0 then
    local error_str = table.concat(errors, '\n\n')
    error(('vim.pack:\n\n%s'):format(error_str))
  end
end

--- @param p vim.pack.Plug
--- @return string
local function compute_feedback_lines_single(p)
  if p.info.err ~= '' then
    return ('## %s\n\n %s'):format(p.spec.name, p.info.err:gsub('\n', '\n  '))
  end

  local parts = { '## ' .. p.spec.name .. '\n' }
  local version_suffix = p.info.version_str == '' and '' or (' (%s)'):format(p.info.version_str)

  if p.info.sha_head == p.info.sha_target then
    parts[#parts + 1] = table.concat({
      'Path:   ' .. p.path,
      'Source: ' .. p.spec.src,
      'State:  ' .. p.info.sha_target .. version_suffix,
    }, '\n')

    if p.info.update_details ~= '' then
      local details = p.info.update_details:gsub('\n', '\n• ')
      parts[#parts + 1] = '\n\nAvailable newer versions:\n• ' .. details
    end
  else
    parts[#parts + 1] = table.concat({
      'Path:         ' .. p.path,
      'Source:       ' .. p.spec.src,
      'State before: ' .. p.info.sha_head,
      'State after:  ' .. p.info.sha_target .. version_suffix,
      '',
      'Pending updates:',
      p.info.update_details,
    }, '\n')
  end

  return table.concat(parts, '')
end
Repository: neovim/neovim
File: runtime/ftplugin/nvim-pack.lua (Lua)

Code before:
  elseif l:match('^> ') then
    -- Added change with possibly "breaking message"
    hi_range(i, 0, l:len(), 'Added')
    local col = l:match('│() %S+!:') or l:match('│() %S+%b()!:') or l:len()
    hi_range(i, col, l:len(), 'DiagnosticWarn', priority + 1)
  elseif l:match('^< ') then
    -- Removed change
    hi_range(i, 0, l:len(), 'Removed')
  elseif l:match('^• ') then
    -- Available newer tags
    hi_range(i, 4, l:len(), 'DiagnosticHint')
  end
end

-- Mappings
local jump = function(direction)
  local lnum = vim.api.nvim_win_get_cursor(0)[1]
  local buf_lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
  local from, to, by = lnum + 1, #buf_lines, 1
  if direction == 'prev' then
    from, to, by = lnum - 1, 1, -1
  end

  for i = from, to, by do
    if buf_lines[i]:match('^## ') then
      vim.api.nvim_win_set_cursor(0, { i, 0 })
      return
    end
  end
end

vim.keymap.set({ 'n', 'x' }, '[[', function()
  for _ = 1, vim.v.count1 do
    jump('prev')
  end
end, { buffer = 0, desc = 'Previous plugin' })
vim.keymap.set({ 'n', 'x' }, ']]', function()
  for _ = 1, vim.v.count1 do
    jump('next')
  end
end, { buffer = 0, desc = 'Next plugin' })


Review comment (suggestion):
Does `vim.fn.search()` achieve this with less code? Moving to a place in a buffer is very common. If `search()` is missing something then we should fix it.

Code after:
  elseif l:match('^> ') then
    -- Added change with possibly "breaking message"
    hi_range(i, 0, l:len(), 'Added')
    local col = l:match('│() %S+!:') or l:match('│() %S+%b()!:') or l:len()
    hi_range(i, col, l:len(), 'DiagnosticWarn', priority + 1)
  elseif l:match('^< ') then
    -- Removed change
    hi_range(i, 0, l:len(), 'Removed')
  elseif l:match('^• ') then
    -- Available newer tags
    hi_range(i, 4, l:len(), 'DiagnosticHint')
  end
end

-- Mappings
local map_section_jump = function(lhs, search_flags, desc)
  vim.keymap.set({ 'n', 'x' }, lhs, function()
    for _ = 1, vim.v.count1 do
      vim.fn.search('^## ', search_flags)
    end
  end, { buffer = 0, desc = desc })
end

map_section_jump('[[', 'bsW', 'Previous plugin')
map_section_jump(']]', 'sW', 'Next plugin')
Repository: neovim/neovim
File: runtime/lua/vim/filetype.lua (Lua)

Code before:
  cer = 'pem',
  crt = 'pem',
  csr = 'pem',
  plx = 'perl',
  prisma = 'prisma',
  psgi = 'perl',
  al = 'perl',
  ctp = 'php',
  php = 'php',
  phpt = 'php',
  php0 = 'php',
  php1 = 'php',
  php2 = 'php',
  php3 = 'php',
  php4 = 'php',
  php5 = 'php',
  php6 = 'php',
  php7 = 'php',
  php8 = 'php',
  php9 = 'php',
  phtml = 'php',
  theme = 'php',
  pike = 'pike',
  pmod = 'pike',
  rcp = 'pilrc',
  PL = detect.pl,
  pkl = 'pkl',
  pli = 'pli',
  pl1 = 'pli',
  p36 = 'plm',
  plm = 'plm',
  pac = 'plm',
  plp = 'plp',
  pls = 'plsql',
  plsql = 'plsql',
  po = 'po',
  pot = 'po',
  pod = 'pod',
  filter = 'poefilter',
  pk = 'poke',
  pony = 'pony',
  ps = 'postscr',
  epsi = 'postscr',
  afm = 'postscr',
  epsf = 'postscr',
  eps = 'postscr',
  pfa = 'postscr',
  ai = 'postscr',
  pov = 'pov',
  ppd = 'ppd',
  it = 'ppwiz',

Review comment (suggestion):
```suggestion
  pkl = 'pkl',
  PL = detect.pl,
```

Code after:
  cer = 'pem',
  crt = 'pem',
  csr = 'pem',
  plx = 'perl',
  prisma = 'prisma',
  psgi = 'perl',
  al = 'perl',
  ctp = 'php',
  php = 'php',
  phpt = 'php',
  php0 = 'php',
  php1 = 'php',
  php2 = 'php',
  php3 = 'php',
  php4 = 'php',
  php5 = 'php',
  php6 = 'php',
  php7 = 'php',
  php8 = 'php',
  php9 = 'php',
  phtml = 'php',
  theme = 'php',
  pike = 'pike',
  pmod = 'pike',
  rcp = 'pilrc',
  pkl = 'pkl',
  PL = detect.pl,
  pli = 'pli',
  pl1 = 'pli',
  p36 = 'plm',
  plm = 'plm',
  pac = 'plm',
  plp = 'plp',
  pls = 'plsql',
  plsql = 'plsql',
  po = 'po',
  pot = 'po',
  pod = 'pod',
  filter = 'poefilter',
  pk = 'poke',
  pony = 'pony',
  ps = 'postscr',
  epsi = 'postscr',
  afm = 'postscr',
  epsf = 'postscr',
  eps = 'postscr',
  pfa = 'postscr',
  ai = 'postscr',
  pov = 'pov',
  ppd = 'ppd',
  it = 'ppwiz',
Repository: neovim/neovim
File: src/nvim/ex_docmd.c (C)

Code before:
      } else {
        shift_cmd_args(eap);
      }
    }

    if (n <= 0 && (eap->argt & EX_ZEROR) == 0) {
      if (errormsg != NULL) {
        *errormsg = _(e_zerocount);
      }
      return FAIL;
    }
    set_cmd_count(eap, n, validate);
  }

  return OK;
}

/// Check if command is not implemented
bool is_cmd_ni(cmdidx_T cmdidx)
{
  return !IS_USER_CMDIDX(cmdidx) && (cmdnames[cmdidx].cmd_func == ex_ni
                                     || cmdnames[cmdidx].cmd_func == ex_script_ni);
}

// Find the command name after skipping range specifiers.
static char *find_command_after_range(exarg_T *eap)
{
  char *cmd = eap->cmd;
  eap->cmd = skip_range(eap->cmd, NULL);
  if (*eap->cmd == '*') {
    eap->cmd = skipwhite(eap->cmd + 1);
  }
  char *p = find_ex_command(eap, NULL);
  eap->cmd = cmd;  // Restore original position for address parsing
  return p;
}

// Set the forceit flag based on the presence of '!' after the command.
static void set_forceit_flag(exarg_T *eap, char **p)
{
  if (**p == '!' && eap->cmdidx != CMD_substitute
      && eap->cmdidx != CMD_smagic && eap->cmdidx != CMD_snomagic) {
    (*p)++;
    eap->forceit = true;
  } else {
    eap->forceit = false;
  }
}

/// Parse command line and return information about the first command.
/// If parsing is done successfully, need to free cmod_filter_pat and cmod_filter_regmatch.regprog

Review comment (suggestion):
```suggestion
static char *find_excmd_after_range(exarg_T *eap)
```

Code after:
      } else {
        shift_cmd_args(eap);
      }
    }

    if (n <= 0 && (eap->argt & EX_ZEROR) == 0) {
      if (errormsg != NULL) {
        *errormsg = _(e_zerocount);
      }
      return FAIL;
    }
    set_cmd_count(eap, n, validate);
  }

  return OK;
}

/// Check if command is not implemented
bool is_cmd_ni(cmdidx_T cmdidx)
{
  return !IS_USER_CMDIDX(cmdidx) && (cmdnames[cmdidx].cmd_func == ex_ni
                                     || cmdnames[cmdidx].cmd_func == ex_script_ni);
}

// Find the command name after skipping range specifiers.
static char *find_excmd_after_range(exarg_T *eap)
{
  // Save location after command modifiers.
  char *cmd = eap->cmd;
  eap->cmd = skip_range(eap->cmd, NULL);
  if (*eap->cmd == '*') {
    eap->cmd = skipwhite(eap->cmd + 1);
  }
  char *p = find_ex_command(eap, NULL);
  eap->cmd = cmd;  // Restore original position for address parsing
  return p;
}

// Set the forceit flag based on the presence of '!' after the command.
static bool parse_bang(const exarg_T *eap, char **p)
{
  if (**p == '!'
      && eap->cmdidx != CMD_substitute
      && eap->cmdidx != CMD_smagic
      && eap->cmdidx != CMD_snomagic) {
    (*p)++;
    return true;
  }
  return false;
}
Repository: neovim/neovim
File: src/nvim/ex_docmd.c (C)

Code before:
    }

    if (n <= 0 && (eap->argt & EX_ZEROR) == 0) {
      if (errormsg != NULL) {
        *errormsg = _(e_zerocount);
      }
      return FAIL;
    }
    set_cmd_count(eap, n, validate);
  }

  return OK;
}

/// Check if command is not implemented
bool is_cmd_ni(cmdidx_T cmdidx)
{
  return !IS_USER_CMDIDX(cmdidx) && (cmdnames[cmdidx].cmd_func == ex_ni
                                     || cmdnames[cmdidx].cmd_func == ex_script_ni);
}

// Find the command name after skipping range specifiers.
static char *find_command_after_range(exarg_T *eap)
{
  char *cmd = eap->cmd;
  eap->cmd = skip_range(eap->cmd, NULL);
  if (*eap->cmd == '*') {
    eap->cmd = skipwhite(eap->cmd + 1);
  }
  char *p = find_ex_command(eap, NULL);
  eap->cmd = cmd;  // Restore original position for address parsing
  return p;
}

// Set the forceit flag based on the presence of '!' after the command.
static void set_forceit_flag(exarg_T *eap, char **p)
{
  if (**p == '!' && eap->cmdidx != CMD_substitute
      && eap->cmdidx != CMD_smagic && eap->cmdidx != CMD_snomagic) {
    (*p)++;
    eap->forceit = true;
  } else {
    eap->forceit = false;
  }
}

/// Parse command line and return information about the first command.
/// If parsing is done successfully, need to free cmod_filter_pat and cmod_filter_regmatch.regprog
/// after calling, usually done using undo_cmdmod() or execute_cmd().
///
/// @param cmdline Command line string

Review comment (suggestion):
do not drop useful information. this comment from the old code was a useful hint because it's not obvious that this has anything to do with "modifiers"
 
```suggestion
  // Save location after command modifiers.
  char *cmd = eap->cmd;
```

Code after:
    }

    if (n <= 0 && (eap->argt & EX_ZEROR) == 0) {
      if (errormsg != NULL) {
        *errormsg = _(e_zerocount);
      }
      return FAIL;
    }
    set_cmd_count(eap, n, validate);
  }

  return OK;
}

/// Check if command is not implemented
bool is_cmd_ni(cmdidx_T cmdidx)
{
  return !IS_USER_CMDIDX(cmdidx) && (cmdnames[cmdidx].cmd_func == ex_ni
                                     || cmdnames[cmdidx].cmd_func == ex_script_ni);
}

// Find the command name after skipping range specifiers.
static char *find_excmd_after_range(exarg_T *eap)
{
  // Save location after command modifiers.
  char *cmd = eap->cmd;
  eap->cmd = skip_range(eap->cmd, NULL);
  if (*eap->cmd == '*') {
    eap->cmd = skipwhite(eap->cmd + 1);
  }
  char *p = find_ex_command(eap, NULL);
  eap->cmd = cmd;  // Restore original position for address parsing
  return p;
}

// Set the forceit flag based on the presence of '!' after the command.
static bool parse_bang(const exarg_T *eap, char **p)
{
  if (**p == '!'
      && eap->cmdidx != CMD_substitute
      && eap->cmdidx != CMD_smagic
      && eap->cmdidx != CMD_snomagic) {
    (*p)++;
    return true;
  }
  return false;
}

/// Parse command line and return information about the first command.
/// If parsing is done successfully, need to free cmod_filter_pat and cmod_filter_regmatch.regprog
/// after calling, usually done using undo_cmdmod() or execute_cmd().
Repository: neovim/neovim
File: test/functional/lua/ssh_spec.lua (Lua)

Code before:
local t = require('test.testutil')
local parser = require('vim._ssh.parser')
local eq = t.eq

describe('SSH parser', function()
  it('parses SSH configuration strings', function()
    local config = [[
      Host *
        ConnectTimeout 10
        ServerAliveInterval 60
        ServerAliveCountMax 3
        # Use a specific key for any host not otherwise specified
        # IdentityFile ~/.ssh/id_rsa

      Host dev
        HostName dev.example.com
        User devuser
        Port 2222
        IdentityFile ~/.ssh/id_rsa_dev

      Host prod test
        HostName 198.51.100.10
        User admin
        Port 22
        IdentityFile ~/.ssh/id_rsa_prod
        ForwardAgent yes

      Host test
        IdentitiesOnly yes

      Match host foo host gh

Review comment (bug):
also need `it('failure modes')` which tests bad or unexpected input, i.e it should use `pcall_err` or similar to assert what happens when parsing fails.


that is almost always required when adding new features.

https://github.com/cyjake/ssh-config/tree/master/test/unit may have some ideas.

Code after:
local t = require('test.testutil')
local parser = require('vim.net._ssh')
local eq = t.eq

describe('SSH parser', function()
  it('parses SSH configuration strings', function()
    local config = [[
      Host *
        ConnectTimeout 10
        ServerAliveInterval 60
        ServerAliveCountMax 3
        # Use a specific key for any host not otherwise specified
        # IdentityFile ~/.ssh/id_rsa

      Host=dev
        HostName=dev.example.com
        User=devuser
        Port=2222
        IdentityFile=~/.ssh/id_rsa_dev

      Host prod test
        HostName 198.51.100.10
        User admin
        Port 22
        IdentityFile ~/.ssh/id_rsa_prod
        ForwardAgent yes

      Host test
        IdentitiesOnly yes

      Host "quoted string"
Repository: neovim/neovim
File: test/functional/lua/ssh_spec.lua (Lua)

Code before:
local t = require('test.testutil')
local parser = require('vim._ssh.parser')
local eq = t.eq

describe('SSH parser', function()
  it('parses SSH configuration strings', function()
    local config = [[
      Host *
        ConnectTimeout 10
        ServerAliveInterval 60
        ServerAliveCountMax 3
        # Use a specific key for any host not otherwise specified
        # IdentityFile ~/.ssh/id_rsa

      Host dev
        HostName dev.example.com
        User devuser
        Port 2222
        IdentityFile ~/.ssh/id_rsa_dev

      Host prod test
        HostName 198.51.100.10
        User admin
        Port 22
        IdentityFile ~/.ssh/id_rsa_prod
        ForwardAgent yes


Review comment (suggestion):
`runtime/lua/vim/net/_ssh.lua` seems like a good place for this, though it's private for now. 

do not create lots of granular modules. just put everything in `_ssh.lua` , until it's clear that separate modules are needed.

Code after:
local t = require('test.testutil')
local parser = require('vim.net._ssh')
local eq = t.eq

describe('SSH parser', function()
  it('parses SSH configuration strings', function()
    local config = [[
      Host *
        ConnectTimeout 10
        ServerAliveInterval 60
        ServerAliveCountMax 3
        # Use a specific key for any host not otherwise specified
        # IdentityFile ~/.ssh/id_rsa

      Host=dev
        HostName=dev.example.com
        User=devuser
        Port=2222
        IdentityFile=~/.ssh/id_rsa_dev

      Host prod test
        HostName 198.51.100.10
        User admin
        Port 22
        IdentityFile ~/.ssh/id_rsa_prod
        ForwardAgent yes
Repository: neovim/neovim
File: runtime/lua/vim/net/_ssh.lua (Lua)

Code before:
      param = param .. chr
      chr = consume()
    end
    return param
  end

  local function parse_separator()
    local sep = parse_spaces()
    if chr == '=' then
      sep = sep .. chr
      chr = consume()
    end
    return sep .. parse_spaces()
  end

  local function parse_value()
    local val = {}
    local quoted, escaped = false, false

    while chr and not chr:match(line_break_pattern) do
      if escaped then
        if chr == '"' then
          table.insert(val, chr)
        else
          table.insert(val, '\\' .. chr)
        end
        escaped = false
      elseif chr == '"' and (val == {} or quoted) then
        quoted = not quoted
      elseif chr == '\\' then
        escaped = true
      elseif chr == '#' and not quoted then
        break
      else
        table.insert(val, chr)
      end
      chr = consume()
    end

    if quoted or escaped then
      error('Unexpected line break at line ' .. line)
    end

    return vim.trim(table.concat(val))
  end

  local function parse_comment()
    while chr and not chr:match(line_break_pattern) do
      chr = consume()
    end
  end

Review comment (suggestion):
```suggestion
        table.insert(val, chr == '"' and chr or '\\' .. chr)
```

Code after:
      param = param .. chr
      chr = consume()
    end
    return param
  end

  local function parse_separator()
    local sep = parse_spaces()
    if chr == '=' then
      sep = sep .. chr
      chr = consume()
    end
    return sep .. parse_spaces()
  end

  local function parse_value()
    local val = {}
    local quoted, escaped = false, false

    while chr and not chr:match(line_break_pattern) do
      if escaped then
        table.insert(val, chr == '"' and chr or '\\' .. chr)
        escaped = false
      elseif chr == '"' and (val == {} or quoted) then
        quoted = not quoted
      elseif chr == '\\' then
        escaped = true
      elseif chr == '#' and not quoted then
        break
      else
        table.insert(val, chr)
      end
      chr = consume()
    end

    if quoted or escaped then
      error('Unexpected line break at line ' .. line)
    end

    return vim.trim(table.concat(val))
  end

  local function parse_comment()
    while chr and not chr:match(line_break_pattern) do
      chr = consume()
    end
  end

  ---@return string[]
  local function parse_multiple_values()
    local results = {}