Repository: neovim/neovim
File: runtime/lua/vim/_defaults.lua (Lua)

Code before:

    vim.keymap.set({ 'n', 'x' }, 'gra', function()
      vim.lsp.buf.code_action()
    end, { desc = 'vim.lsp.buf.code_action()' })

    vim.keymap.set('n', 'grr', function()
      vim.lsp.buf.references()
    end, { desc = 'vim.lsp.buf.references()' })

    vim.keymap.set('n', 'gri', function()
      vim.lsp.buf.implementation()
    end, { desc = 'vim.lsp.buf.implementation()' })

    vim.keymap.set('n', 'grt', function()
      vim.lsp.buf.type_definition()
    end, { desc = 'vim.lsp.buf.type_definition()' })

    vim.keymap.set('x', 'an', function()
      vim.lsp.buf.selection_range(vim.v.count1)
    end, { desc = 'vim.lsp.buf.selection_range(vim.v.count1)' })

    vim.keymap.set('x', 'in', function()
      vim.lsp.buf.selection_range(-vim.v.count1)
    end, { desc = 'vim.lsp.buf.selection_range(-vim.v.count1)' })

    vim.keymap.set('o', 'an', function()
      local done = false
      vim.lsp.buf.selection_range(-vim.v.count1, function()
        done = true
      end)
      local timeout_ms = 1000
      local did_finish = vim.wait(timeout_ms, function()
        return done
      end)
      if not did_finish then
        vim.lsp.log.error('Timeout error when executing operator function for selection_range')
      end
    end, { desc = 'vim.lsp.buf.selection_range(vim.v.count1, on_response)' })

    vim.keymap.set('o', 'in', function()
      local done = false
      vim.lsp.buf.selection_range(-vim.v.count1, function()
        done = true
      end)
      local timeout_ms = 1000
      local did_finish = vim.wait(timeout_ms, function()
        return done
      end)
      if not did_finish then
        vim.lsp.log.error('Timeout error when executing operator function for selection_range')
      end

Review comment (suggestion):
the mappings are identical except for the `vim.v.count1` param. just define a `local function` and re-use it

Code after:

    vim.keymap.set({ 'n', 'x' }, 'gra', function()
      vim.lsp.buf.code_action()
    end, { desc = 'vim.lsp.buf.code_action()' })

    vim.keymap.set('n', 'grr', function()
      vim.lsp.buf.references()
    end, { desc = 'vim.lsp.buf.references()' })

    vim.keymap.set('n', 'gri', function()
      vim.lsp.buf.implementation()
    end, { desc = 'vim.lsp.buf.implementation()' })

    vim.keymap.set('n', 'grt', function()
      vim.lsp.buf.type_definition()
    end, { desc = 'vim.lsp.buf.type_definition()' })

    vim.keymap.set('x', 'an', function()
      vim.lsp.buf.selection_range(vim.v.count1)
    end, { desc = 'vim.lsp.buf.selection_range(vim.v.count1)' })

    vim.keymap.set('x', 'in', function()
      vim.lsp.buf.selection_range(-vim.v.count1)
    end, { desc = 'vim.lsp.buf.selection_range(-vim.v.count1)' })

    vim.keymap.set('o', 'an', function()
      vim.lsp.buf.selection_range(vim.v.count1, 1000)
    end, { desc = 'vim.lsp.buf.selection_range(vim.v.count1, timeout_ms)' })

    vim.keymap.set('o', 'in', function()
      vim.lsp.buf.selection_range(-vim.v.count1, 1000)
    end, { desc = 'vim.lsp.buf.selection_range(-vim.v.count1, timeout_ms)' })

    vim.keymap.set('n', 'gO', function()
      vim.lsp.buf.document_symbol()
    end, { desc = 'vim.lsp.buf.document_symbol()' })

    vim.keymap.set({ 'i', 's' }, '<C-S>', function()
      vim.lsp.buf.signature_help()
    end, { desc = 'vim.lsp.buf.signature_help()' })
  end

  do
    ---@param direction vim.snippet.Direction
    ---@param key string
    local function set_snippet_jump(direction, key)
      vim.keymap.set({ 'i', 's' }, key, function()
        if vim.snippet.active({ direction = direction }) then
          return string.format('<Cmd>lua vim.snippet.jump(%d)<CR>', direction)
        else
          return key