Skip to content

Commit

Permalink
refactor!: deprecate config.keymaps
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Removed default keymaps. Instead on_attach should be
used. See README.md.
  • Loading branch information
lewis6991 committed Jan 22, 2022
1 parent ddffc61 commit 58e5d6d
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 130 deletions.
65 changes: 43 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
51 changes: 5 additions & 46 deletions doc/gitsigns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -451,30 +429,11 @@ signs *gitsigns-config-signs*
to `GitGutterAdd`.

keymaps *gitsigns-config-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
config.keymaps is now deprecated. Please define mappings in config.on_attach() instead.

Type: `table`, Default: `{}`

Keymaps to set up when attaching to a buffer.

Each key in the table defines the mode and key (whitespace delimited)
Expand Down
23 changes: 15 additions & 8 deletions gen_help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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('')
Expand All @@ -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
Expand Down
39 changes: 12 additions & 27 deletions lua/gitsigns/config.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 12 additions & 27 deletions teal/gitsigns/config.tl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local record SchemaElem

record Deprecated
new_field: string
message: string
hard: boolean
end

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions types/types.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit 58e5d6d

@wookayin
Copy link
Contributor

@wookayin wookayin commented on 58e5d6d Jan 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why deprecated? Actually, "removed". I wish there were some context/information about this change given. (Happy to move this to an issue if it's going to be a long discussion.). -> UPDATE: Already discussed in #449.

@lewis6991
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config.keymaps is deprecated as a configuration field, and the default value of config.keymaps has been removed. The config.keymaps field still exists and can still be used to set mappings.

In the next release (0.5) config.keymaps will be hard-deprecated, meaning it will issue warnings if you use it, then in the release after that (0.6) it will be removed entirely.

Please sign in to comment.