forked from lewis6991/gitsigns.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_help.lua
executable file
·135 lines (115 loc) · 3.11 KB
/
gen_help.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/sh
_=[[
exec lua "$0" "$@"
]]
-- Simple script to update the help doc by reading the config schema.
local inspect = require('inspect')
local config = require('lua/gitsigns/config')
function table.slice(tbl, first, last, step)
local sliced = {}
for i = first or 1, last or #tbl, step or 1 do
sliced[#sliced+1] = tbl[i]
end
return sliced
end
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
local function read_file_lines(path)
local lines = {}
for l in read_file(path):gmatch("([^\n]*)\n?") do
table.insert(lines, l)
end
return lines
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 smple 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, 'M.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')
elseif type(d) == 'function' then
local info = debug.getinfo(d)
local start, last = info.linedefined, info.lastlinedefined
local source = info.source:sub(2) -- Remove leading @
local func = table.slice(read_file_lines(source), start, last)
d = table.concat(func, '\n ')
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()
-- helo