Automate updating the help file

This commit is contained in:
Lewis Russell 2020-12-20 23:45:36 +00:00
parent 2943481161
commit 855fcac179
4 changed files with 274 additions and 96 deletions

View File

@ -127,15 +127,15 @@ detach_all() *gitsigns.detach_all()*
CONFIGURATION *gitsigns-config*
signs *gitsigns-config-signs*
Type: `dictionary`, Default:
Type: `table`, Default:
>
{
add = {hl = 'DiffAdd' , text = '│', numhl='GitSignsAddNr'},
change = {hl = 'DiffChange', text = '│', numhl='GitSignsChangeNr'},
delete = {hl = 'DiffDelete', text = '_', numhl='GitSignsDeleteNr'},
topdelete = {hl = 'DiffDelete', text = '‾', numhl='GitSignsDeleteNr'},
changedelete = {hl = 'DiffChange', text = '~', numhl='GitSignsChangeNr'},
}
{
add = {hl = 'DiffAdd' , text = '│', numhl='GitSignsAddNr'},
change = {hl = 'DiffChange', text = '│', numhl='GitSignsChangeNr'},
delete = {hl = 'DiffDelete', text = '_', numhl='GitSignsDeleteNr'},
topdelete = {hl = 'DiffDelete', text = '‾', numhl='GitSignsDeleteNr'},
changedelete = {hl = 'DiffChange', text = '~', numhl='GitSignsChangeNr'},
}
<
Configuration for signs:
• `hl` specifies the highlight group to use for the sign.
@ -146,7 +146,7 @@ signs *gitsigns-config-signs*
lines.
keymaps *gitsigns-config-keymaps*
Type: `dicitonary`, Default:
Type: `table`, Default:
>
{
-- Default keymap options
@ -173,8 +173,10 @@ keymaps *gitsigns-config-keymaps*
the top level of the table to define default options for all mappings.
watch_index *gitsigns-config-watch_index*
Type: `boolean`, Default: `true`
Type: `table`, Default:
>
{ interval = 1000 }
<
When opening a file, a libuv watcher is placed on the respective
`.git/index` file to detect when changes happen to use as a trigger to
update signs.
@ -193,17 +195,17 @@ numhl *gitsigns-config-numhl*
the highlight group does not exist, then it is automatically defined
and linked to the corresponding highlight group in `signs.*.hl`.
diff_algorithm *gitsigns-config-diff_algorithm*
Type: `string`, Default: taken from 'diffopt'
Diff algorithm to pass to `git diff` .
count_chars *gitsigns-config-count_chars*
Type: `boolean`, Default:
Type: `table`, Default:
>
{ [1] = '1', [2] = '2', [3] = '3', [4] = '4', [5] = '5',
[6] = '6', [7] = '7', [8] = '8', [9] = '9', ['+'] = '>' }
{ "1", "2", "3", "4", "5", "6", "7", "8", "9",
["+"] = ">"
}
<
The count characters used when `signs.*.show_count` is enabled. The
`+` entry is used as a fallback. With the default, any count outside
@ -216,14 +218,14 @@ count_chars *gitsigns-config-count_chars*
status_formatter *gitsigns-config-status_formatter*
Type: `function`, Default:
>
function(status)
local added, changed, removed = status.added, status.changed, status.removed
local status_txt = {}
if added > 0 then table.insert(status_txt, '+'..added ) end
if changed > 0 then table.insert(status_txt, '~'..changed) end
if removed > 0 then table.insert(status_txt, '-'..removed) end
return table.concat(status_txt, ' ')
end
function(status)
local added, changed, removed = status.added, status.changed, status.removed
local status_txt = {}
if added > 0 then table.insert(status_txt, '+'..added ) end
if changed > 0 then table.insert(status_txt, '~'..changed) end
if removed > 0 then table.insert(status_txt, '-'..removed) end
return table.concat(status_txt, ' ')
end
<
Function used to format `b:gitsigns_status`.

108
gen_help.lua Normal file
View File

@ -0,0 +1,108 @@
-- Simple script to update the help doc by reading the config schema.
inspect = require('inspect')
config = require('lua/gitsigns/config')
local function is_simple_type(t)
return t == 'number' or t == 'string' or t == 'boolean'
end
local function startswith(str, start)
return str.sub(str, 1, string.len(start)) == start
end
local function out(line)
io.write(line or '', '\n')
end
local function read_file(path)
local f = assert(io.open(path, 'r'))
local t = f:read("*all")
f:close()
return t
end
-- To makw sure the output is consistent between runs (to minimise diffs), we
-- need to iterate through the schema keys in a deterministic way. To do this we
-- do a simple scan over the file the schema is defined in and collect the keys
-- in the order they are defined.
local function get_ordered_schema_keys()
local c = read_file('lua/gitsigns/config.lua')
local ci = c:gmatch("[^\n\r]+")
for l in ci do
if startswith(l, 'local schema = {') then
break
end
end
local keys = {}
for l in ci do
if startswith(l, '}') then
break
end
if l:find('^ (%w+).*') then
local lc = l:gsub('^%s*([%w_]+).*', '%1')
table.insert(keys, lc)
end
end
return keys
end
local function gen_config_doc()
for _, k in ipairs(get_ordered_schema_keys()) do
local v = config.schema[k]
local t = ('*gitsigns-config-%s*'):format(k)
out(('%-30s%48s'):format(k, t))
if v.default_help ~= nil or is_simple_type(v.type) then
local d = v.default_help or ('`%s`'):format(inspect(v.default))
out((' Type: `%s`, Default: %s'):format(v.type, d))
out()
else
out((' Type: `%s`, Default:'):format(v.type))
out('>')
local d = v.default
if type(d) == 'table' then
d = inspect(d):gsub('\n([^\n\r])', '\n %1')
end
out(' '..d:gsub('\n([^\n\r])', '\n %1'))
out('<')
end
out(v.description:gsub(' +$', ''))
end
end
local function main()
local i = read_file('doc/gitsigns.txt'):gmatch("([^\n]*)\n?")
io.output("doc/gitsigns.txt")
-- Output doc upto config
for l in i do
out(l)
if startswith(l, 'CONFIGURATION') then
out()
break
end
end
-- Output new doc
gen_config_doc()
-- Skip over old doc
for l in i do
if startswith(l, '===================') then
out(l)
break
end
end
-- Output remaining config
for l in i do
out(l)
end
end
main()

View File

@ -18,7 +18,7 @@ local debounce_trailing = gs_debounce.debounce_trailing
local gs_popup = require('gitsigns/popup')
local sign_define = require('gitsigns/signs').sign_define
local apply_config = require('gitsigns/config')
local process_config = require('gitsigns/config').process
local mk_repeatable = require('gitsigns/repeat').mk_repeatable
local apply_mappings = require('gitsigns/mappings')
@ -628,7 +628,7 @@ local attach = throttle_leading(100, async('attach', function()
end))
local function setup(cfg)
config = apply_config(cfg)
config = process_config(cfg)
-- TODO: Attach to all open buffers

View File

@ -3,55 +3,27 @@ local schema = {
signs = {
type = 'table',
deep_extend = true,
default = {
default = [[{
add = {hl = 'DiffAdd' , text = '', numhl='GitSignsAddNr'},
change = {hl = 'DiffChange', text = '', numhl='GitSignsChangeNr'},
delete = {hl = 'DiffDelete', text = '_', numhl='GitSignsDeleteNr'},
topdelete = {hl = 'DiffDelete', text = '', numhl='GitSignsDeleteNr'},
changedelete = {hl = 'DiffChange', text = '~', numhl='GitSignsChangeNr'},
}
},
numhl = {
type = 'boolean',
default = false
},
watch_index = {
type = 'table',
default = {
interval = 1000
}
},
debug_mode = {
type = 'boolean',
default = false
},
sign_priority = {
type = 'number',
default = 6
},
diff_algorithm = {
type = 'string',
-- Get algorithm from 'diffopt'
default = function()
local algo = 'myers'
for o in vim.gsplit(vim.o.diffopt, ',') do
if vim.startswith(o, 'algorithm:') then
algo = string.sub(o, 11)
end
end
return algo
end
}]],
description = [[
Configuration for signs:
`hl` specifies the highlight group to use for the sign.
`text` specifies the character to use for the sign.
`numhl` specifies the highlight group to use for the number column (see
|gitsigns-config.numhl|).
`show_count` to enable showing count of hunk, e.g. number of deleted
lines.
]]
},
keymaps = {
type = 'table',
default = {
default = [[{
-- Default keymap options
noremap = true,
buffer = true,
@ -64,19 +36,67 @@ local schema = {
['n <leader>hr'] = '<cmd>lua require"gitsigns".reset_hunk()<CR>',
['n <leader>hp'] = '<cmd>lua require"gitsigns".preview_hunk()<CR>',
['n <leader>hb'] = '<cmd>lua require"gitsigns".blame_line()<CR>',
}
}]],
description = [[
Keymaps to set up when attaching to a buffer.
Each key in the table defines the mode and key (whitespace delimited)
for the mapping and the value defines what the key maps to. The value
can be a table which can contain keys matching the options defined in
|map-arguments| which are: `expr`, `noremap`, `nowait`, `script`,
`silent`, `unique` and `buffer`. These options can also be used in
the top level of the table to define default options for all mappings.
]]
},
status_formatter = {
type = 'function',
default = function(status)
local added, changed, removed = status.added, status.changed, status.removed
local status_txt = {}
if added > 0 then table.insert(status_txt, '+'..added ) end
if changed > 0 then table.insert(status_txt, '~'..changed) end
if removed > 0 then table.insert(status_txt, '-'..removed) end
return table.concat(status_txt, ' ')
end
watch_index = {
type = 'table',
default = [[{ interval = 1000 }]],
description = [[
When opening a file, a libuv watcher is placed on the respective
`.git/index` file to detect when changes happen to use as a trigger to
update signs.
]]
},
sign_priority = {
type = 'number',
default = 6,
description = [[
Priority to use for signs.
]]
},
numhl = {
type = 'boolean',
default = false,
description = [[
Enable/disable line number highlights.
When enabled the highlights defined in `signs.*.numhl` are used. If
the highlight group does not exist, then it is automatically defined
and linked to the corresponding highlight group in `signs.*.hl`.
]]
},
diff_algorithm = {
type = 'string',
-- Get algorithm from 'diffopt'
default = [[function()
local algo = 'myers'
for o in vim.gsplit(vim.o.diffopt, ',') do
if vim.startswith(o, 'algorithm:') then
algo = string.sub(o, 11)
end
end
print('dwqdqwwqdqw: '..algo)
return algo
end]],
default_help = "taken from 'diffopt'",
description = [[
Diff algorithm to pass to `git diff` .
]]
},
count_chars = {
@ -92,7 +112,39 @@ local schema = {
[8] = '8', -- '₈',
[9] = '9', -- '₉',
['+'] = '>', -- '₊',
}
},
description = [[
The count characters used when `signs.*.show_count` is enabled. The
`+` entry is used as a fallback. With the default, any count outside
of 1-9 uses the `>` character in the sign.
Possible use cases for this field:
to specify unicode characters for the counts instead of 1-9.
to define characters to be used for counts greater than 9.
]]
},
status_formatter = {
type = 'function',
default = [[function(status)
local added, changed, removed = status.added, status.changed, status.removed
local status_txt = {}
if added > 0 then table.insert(status_txt, '+'..added ) end
if changed > 0 then table.insert(status_txt, '~'..changed) end
if removed > 0 then table.insert(status_txt, '-'..removed) end
return table.concat(status_txt, ' ')
end]],
description = [[
Function used to format `b:gitsigns_status`.
]]
},
debug_mode = {
type = 'boolean',
default = false,
description = [[
Print diagnostic messages.
]]
}
}
@ -108,27 +160,43 @@ local function validate_config(config)
end
end
return function(user_config)
user_config = user_config or {}
validate_config(user_config)
local config = {}
for k, v in pairs(schema) do
if user_config[k] ~= nil then
if v.deep_extend then
config[k] = vim.tbl_deep_extend('force', v.default, user_config[k])
else
config[k] = user_config[k]
end
local function resolve_default(schema_elem)
local v = schema_elem
if type(v.default) == 'string' and vim.startswith(v.default, 'function(') then
local d = loadstring('return '..v.default)()
if v.type == 'function' then
return d
else
if type(v.default) == 'function' and v.type ~= 'function' then
config[k] = v.default()
return d()
end
elseif type(v.default) == 'string' and vim.startswith(v.default, '{') then
return loadstring('return '..v.default)()
else
return v.default
end
end
return {
process = function(user_config)
user_config = user_config or {}
validate_config(user_config)
local config = {}
for k, v in pairs(schema) do
if user_config[k] ~= nil then
if v.deep_extend then
local d = resolve_default(v)
config[k] = vim.tbl_deep_extend('force', d, user_config[k])
else
config[k] = user_config[k]
end
else
config[k] = v.default
config[k] = resolve_default(v)
end
end
end
return config
end
return config
end,
schema = schema
}