refactor!: deprecate config.keymaps

BREAKING CHANGE: Removed default keymaps. Instead on_attach should be
used. See README.md.
This commit is contained in:
Lewis Russell 2022-01-19 17:04:57 +00:00 committed by Lewis Russell
parent ddffc618ea
commit 58e5d6d85e
6 changed files with 94 additions and 128 deletions

View File

@ -91,28 +91,6 @@ require('gitsigns').setup {
numhl = false, -- Toggle with `:Gitsigns toggle_numhl`
linehl = false, -- Toggle with `:Gitsigns toggle_linehl`
word_diff = false, -- Toggle with `:Gitsigns toggle_word_diff`
keymaps = {
-- Default keymap options
noremap = true,
['n ]c'] = { expr = true, "&diff ? ']c' : '<cmd>Gitsigns next_hunk<CR>'"},
['n [c'] = { expr = true, "&diff ? '[c' : '<cmd>Gitsigns prev_hunk<CR>'"},
['n <leader>hs'] = '<cmd>Gitsigns stage_hunk<CR>',
['v <leader>hs'] = ':Gitsigns stage_hunk<CR>',
['n <leader>hu'] = '<cmd>Gitsigns undo_stage_hunk<CR>',
['n <leader>hr'] = '<cmd>Gitsigns reset_hunk<CR>',
['v <leader>hr'] = ':Gitsigns reset_hunk<CR>',
['n <leader>hR'] = '<cmd>Gitsigns reset_buffer<CR>',
['n <leader>hp'] = '<cmd>Gitsigns preview_hunk<CR>',
['n <leader>hb'] = '<cmd>lua require"gitsigns".blame_line{full=true}<CR>',
['n <leader>hS'] = '<cmd>Gitsigns stage_buffer<CR>',
['n <leader>hU'] = '<cmd>Gitsigns reset_buffer_index<CR>',
-- Text objects
['o ih'] = ':<C-U>Gitsigns select_hunk<CR>',
['x ih'] = ':<C-U>Gitsigns select_hunk<CR>'
},
watch_gitdir = {
interval = 1000,
follow_files = true
@ -149,6 +127,49 @@ require('gitsigns').setup {
For information on configuring neovim via lua please see
[nvim-lua-guide](https://github.com/nanotee/nvim-lua-guide).
### Keymaps
Gitsigns provides an `on_attach` callback which can be used to setup buffer mappings.
Here is a suggested example:
```lua
require('gitsigns').setup{
...
on_attach = function(bufnr)
local gs = package.loaded.gitsigns
local function map(mode, l, r, opts)
opts = opts or {}
opts.buffer = bufnr
vim.keymap.set(mode, l, r, opts)
end
-- Navigation
map('n', ']c', "&diff ? ']c' : '<cmd>Gitsigns next_hunk<CR>'", {expr=true})
map('n', '[c', "&diff ? '[c' : '<cmd>Gitsigns prev_hunk<CR>'", {expr=true})
-- Actions
map({'n', 'v'}, '<leader>hs', gs.stage_hunk)
map({'n', 'v'}, '<leader>hr', gs.reset_hunk)
map('n', '<leader>hS', gs.stage_buffer)
map('n', '<leader>hu', gs.undo_stage_hunk)
map('n', '<leader>hR', gs.reset_buffer)
map('n', '<leader>hp', gs.preview_hunk)
map('n', '<leader>hb', function() gs.blame_line{full=true} end)
map('n', '<leader>tb', gs.toggle_current_line_blame)
map('n', '<leader>hd', gs.diffthis)
map('n', '<leader>hD', function() gs.diffthis('~') end)
map('n', '<leader>td', gs.toggle_deleted)
-- Text object
map({'o', 'x'}, 'ih', ':<C-U>Gitsigns select_hunk<CR>')
end
}
```
Note this requires Neovim v0.7 which introduces `vim.keymap.set`.
## Non-Goals
### Implement every feature in [vim-fugitive](https://github.com/tpope/vim-fugitive)

View File

@ -45,28 +45,6 @@ of the default settings:
numhl = false, -- Toggle with `:Gitsigns toggle_numhl`
linehl = false, -- Toggle with `:Gitsigns toggle_linehl`
word_diff = false, -- Toggle with `:Gitsigns toggle_word_diff`
keymaps = {
-- Default keymap options
noremap = true,
['n ]c'] = { expr = true, "&diff ? ']c' : '<cmd>Gitsigns next_hunk<CR>'"},
['n [c'] = { expr = true, "&diff ? '[c' : '<cmd>Gitsigns prev_hunk<CR>'"},
['n <leader>hs'] = '<cmd>Gitsigns stage_hunk<CR>',
['v <leader>hs'] = ':Gitsigns stage_hunk<CR>',
['n <leader>hu'] = '<cmd>Gitsigns undo_stage_hunk<CR>',
['n <leader>hr'] = '<cmd>Gitsigns reset_hunk<CR>',
['v <leader>hr'] = ':Gitsigns reset_hunk<CR>',
['n <leader>hR'] = '<cmd>Gitsigns reset_buffer<CR>',
['n <leader>hp'] = '<cmd>Gitsigns preview_hunk<CR>',
['n <leader>hb'] = '<cmd>lua require"gitsigns".blame_line{full=true}<CR>',
['n <leader>hS'] = '<cmd>Gitsigns stage_buffer<CR>',
['n <leader>hU'] = '<cmd>Gitsigns reset_buffer_index<CR>',
-- Text objects
['o ih'] = ':<C-U>Gitsigns select_hunk<CR>',
['x ih'] = ':<C-U>Gitsigns select_hunk<CR>'
},
watch_gitdir = {
interval = 1000,
follow_files = true
@ -451,30 +429,11 @@ signs *gitsigns-config-signs*
to `GitGutterAdd`.
keymaps *gitsigns-config-keymaps*
Type: `table`
Default: >
{
-- Default keymap options
noremap = true,
DEPRECATED
config.keymaps is now deprecated. Please define mappings in config.on_attach() instead.
['n ]c'] = { expr = true, "&diff ? ']c' : '<cmd>Gitsigns next_hunk<CR>'"},
['n [c'] = { expr = true, "&diff ? '[c' : '<cmd>Gitsigns prev_hunk<CR>'"},
Type: `table`, Default: `{}`
['n <leader>hs'] = '<cmd>Gitsigns stage_hunk<CR>',
['v <leader>hs'] = ':Gitsigns stage_hunk<CR>',
['n <leader>hu'] = '<cmd>Gitsigns undo_stage_hunk<CR>',
['n <leader>hr'] = '<cmd>Gitsigns reset_hunk<CR>',
['v <leader>hr'] = ':Gitsigns reset_hunk<CR>',
['n <leader>hR'] = '<cmd>Gitsigns reset_buffer<CR>',
['n <leader>hp'] = '<cmd>Gitsigns preview_hunk<CR>',
['n <leader>hb'] = '<cmd>lua require"gitsigns".blame_line{full=true}<CR>',
['n <leader>hS'] = '<cmd>Gitsigns stage_buffer<CR>',
['n <leader>hU'] = '<cmd>Gitsigns reset_buffer_index<CR>',
['o ih'] = ':<C-U>Gitsigns select_hunk<CR>',
['x ih'] = ':<C-U>Gitsigns select_hunk<CR>'
}
<
Keymaps to set up when attaching to a buffer.
Each key in the table defines the mode and key (whitespace delimited)

View File

@ -119,13 +119,18 @@ local function gen_config_doc_deprecated(dep_info, out)
else
out(' DEPRECATED')
end
if type(dep_info) == 'table' and dep_info.new_field then
out('')
local opts_key, field = dep_info.new_field:match('(.*)%.(.*)')
if opts_key and field then
out((' Please instead use the field `%s` in |gitsigns-config-%s|.'):format(field, opts_key))
else
out((' Please instead use |gitsigns-config-%s|.'):format(dep_info.new_field))
if type(dep_info) == 'table' then
if dep_info.message then
out(' '..dep_info.message)
end
if dep_info.new_field then
out('')
local opts_key, field = dep_info.new_field:match('(.*)%.(.*)')
if opts_key and field then
out((' Please instead use the field `%s` in |gitsigns-config-%s|.'):format(field, opts_key))
else
out((' Please instead use |gitsigns-config-%s|.'):format(dep_info.new_field))
end
end
end
out('')
@ -145,7 +150,9 @@ local function gen_config_doc_field(field, out)
if v.deprecated then
gen_config_doc_deprecated(v.deprecated, out)
else
end
if v.description then
local d
if v.default_help ~= nil then
d = v.default_help

View File

@ -23,6 +23,7 @@ local SchemaElem = {Deprecated = {}, }
local M = {Config = {DiffOpts = {}, SignsConfig = {}, watch_gitdir = {}, current_line_blame_formatter_opts = {}, current_line_blame_opts = {}, yadm = {}, }, }
@ -146,28 +147,11 @@ M.schema = {
},
keymaps = {
type = 'table',
default = {
noremap = true,
['n ]c'] = { expr = true, "&diff ? ']c' : '<cmd>Gitsigns next_hunk<CR>'" },
['n [c'] = { expr = true, "&diff ? '[c' : '<cmd>Gitsigns prev_hunk<CR>'" },
['n <leader>hs'] = '<cmd>Gitsigns stage_hunk<CR>',
['v <leader>hs'] = ':Gitsigns stage_hunk<CR>',
['n <leader>hu'] = '<cmd>Gitsigns undo_stage_hunk<CR>',
['n <leader>hr'] = '<cmd>Gitsigns reset_hunk<CR>',
['v <leader>hr'] = ':Gitsigns reset_hunk<CR>',
['n <leader>hR'] = '<cmd>Gitsigns reset_buffer<CR>',
['n <leader>hp'] = '<cmd>Gitsigns preview_hunk<CR>',
['n <leader>hb'] = '<cmd>lua require"gitsigns".blame_line{full=true}<CR>',
['n <leader>hS'] = '<cmd>Gitsigns stage_buffer<CR>',
['n <leader>hU'] = '<cmd>Gitsigns reset_buffer_index<CR>',
['o ih'] = ':<C-U>Gitsigns select_hunk<CR>',
['x ih'] = ':<C-U>Gitsigns select_hunk<CR>',
deprecated = {
message = "config.keymaps is now deprecated. Please define mappings in config.on_attach() instead.",
},
type = 'table',
default = {},
description = [[
Keymaps to set up when attaching to a buffer.
@ -654,11 +638,12 @@ end
local function validate_config(config)
for k, v in pairs(config) do
if M.schema[k] == nil then
local kschema = M.schema[k]
if kschema == nil then
warn("gitsigns: Ignoring invalid configuration field '%s'", k)
else
elseif kschema.type then
vim.validate({
[k] = { v, M.schema[k].type },
[k] = { v, kschema.type },
})
end
end
@ -691,15 +676,15 @@ local function handle_deprecated(cfg)
end
if dep.hard then
if dep.new_field then
if dep.message then
warn(dep.message)
elseif dep.new_field then
warn('%s is now deprecated, please use %s', k, dep.new_field)
else
warn('%s is now deprecated; ignoring', k)
end
end
end
cfg[k] = nil
end
end
end

View File

@ -14,6 +14,7 @@ local record SchemaElem
record Deprecated
new_field: string
message: string
hard: boolean
end
@ -146,28 +147,11 @@ M.schema = {
},
keymaps = {
type = 'table',
default = {
-- Default keymap options
noremap = true,
['n ]c'] = { expr = true, "&diff ? ']c' : '<cmd>Gitsigns next_hunk<CR>'"},
['n [c'] = { expr = true, "&diff ? '[c' : '<cmd>Gitsigns prev_hunk<CR>'"},
['n <leader>hs'] = '<cmd>Gitsigns stage_hunk<CR>',
['v <leader>hs'] = ':Gitsigns stage_hunk<CR>',
['n <leader>hu'] = '<cmd>Gitsigns undo_stage_hunk<CR>',
['n <leader>hr'] = '<cmd>Gitsigns reset_hunk<CR>',
['v <leader>hr'] = ':Gitsigns reset_hunk<CR>',
['n <leader>hR'] = '<cmd>Gitsigns reset_buffer<CR>',
['n <leader>hp'] = '<cmd>Gitsigns preview_hunk<CR>',
['n <leader>hb'] = '<cmd>lua require"gitsigns".blame_line{full=true}<CR>',
['n <leader>hS'] = '<cmd>Gitsigns stage_buffer<CR>',
['n <leader>hU'] = '<cmd>Gitsigns reset_buffer_index<CR>',
['o ih'] = ':<C-U>Gitsigns select_hunk<CR>',
['x ih'] = ':<C-U>Gitsigns select_hunk<CR>'
deprecated = {
message = "config.keymaps is now deprecated. Please define mappings in config.on_attach() instead."
},
type = 'table',
default = {},
description = [[
Keymaps to set up when attaching to a buffer.
@ -654,11 +638,12 @@ end
local function validate_config(config: {string:any})
for k, v in pairs(config) do
if M.schema[k] == nil then
local kschema = M.schema[k]
if kschema == nil then
warn("gitsigns: Ignoring invalid configuration field '%s'", k)
else
elseif kschema.type then
vim.validate {
[k] = { v, M.schema[k].type } as {any};
[k] = { v, kschema.type } as {any};
}
end
end
@ -691,15 +676,15 @@ local function handle_deprecated(cfg: {string:any})
end
if dep.hard then
if dep.new_field then
if dep.message then
warn(dep.message)
elseif dep.new_field then
warn('%s is now deprecated, please use %s', k, dep.new_field)
else
warn('%s is now deprecated; ignoring', k)
end
end
end
cfg[k] = nil
end
end
end

View File

@ -386,6 +386,15 @@ global record vim
list_extend: function({any}, {any}, integer, integer)
list_slice: function<T>({T}, integer, integer): {T}
record keymap
record Options
buffer: boolean|integer
expr: boolean
end
set: function(string|{string}, string, string|function, Options)
end
record log
record levels
WARN: integer