From b7f6c52db540ab8198629e6595eb08d8daac8293 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Mon, 10 Jul 2023 14:10:04 +0100 Subject: [PATCH] chore: formatting --- etc/doc_template.txt | 1 + gen_help.lua | 125 +++++++++++++++++++++++-------------------- 2 files changed, 68 insertions(+), 58 deletions(-) diff --git a/etc/doc_template.txt b/etc/doc_template.txt index 462151f1..e8d78606 100644 --- a/etc/doc_template.txt +++ b/etc/doc_template.txt @@ -50,6 +50,7 @@ FUNCTIONS *gitsigns-functions* Note functions with the {async} attribute are run asynchronously and are non-blocking (return immediately). + {{FUNCTIONS}} ============================================================================== diff --git a/gen_help.lua b/gen_help.lua index 01ccd90b..c5c8edc7 100755 --- a/gen_help.lua +++ b/gen_help.lua @@ -14,7 +14,7 @@ local config = require('lua.gitsigns.config') --- @return string local function read_file(path) local f = assert(io.open(path, 'r')) - local t = f:read("*all") + local t = f:read('*all') f:close() return t end @@ -27,7 +27,7 @@ end local function get_ordered_schema_keys() local c = read_file('lua/gitsigns/config.lua') - local ci = c:gmatch("[^\n\r]+") + local ci = c:gmatch('[^\n\r]+') for l in ci do if startswith(l, 'M.schema = {') then @@ -49,6 +49,8 @@ local function get_ordered_schema_keys() return keys end +--- @param dep_info boolean|{new_field: string, message: string, hard: boolean} +--- @param out fun(_: string?) local function gen_config_doc_deprecated(dep_info, out) if type(dep_info) == 'table' and dep_info.hard then out(' HARD-DEPRECATED') @@ -57,13 +59,15 @@ local function gen_config_doc_deprecated(dep_info, out) end if type(dep_info) == 'table' then if dep_info.message then - out(' '..dep_info.message) + 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)) + 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 @@ -72,6 +76,8 @@ local function gen_config_doc_deprecated(dep_info, out) out('') end +--- @param field string +--- @param out fun(_: string?) local function gen_config_doc_field(field, out) local v = config.schema[field] @@ -84,8 +90,9 @@ local function gen_config_doc_field(field, out) out(('%78s'):format(t)) end - if v.deprecated then - gen_config_doc_deprecated(v.deprecated, out) + local deprecated = v.deprecated + if deprecated then + gen_config_doc_deprecated(deprecated, out) end if v.description then @@ -101,8 +108,9 @@ local function gen_config_doc_field(field, out) if v.type == 'table' and v.deep_extend then return 'table[extended]' end - if type(v.type) == 'table' then - v.type = table.concat(v.type, '|') + local ty = v.type + if type(ty) == 'table' then + v.type = table.concat(ty, '|') end return v.type end)() @@ -110,7 +118,7 @@ local function gen_config_doc_field(field, out) if d:find('\n') then out((' Type: `%s`'):format(vtype)) out(' Default: >') - out(' '..d:gsub('\n([^\n\r])', '\n %1')) + out(' ' .. d:gsub('\n([^\n\r])', '\n %1')) out('<') else out((' Type: `%s`, Default: %s'):format(vtype, d)) @@ -124,9 +132,11 @@ end --- @return string local function gen_config_doc() local res = {} ---@type string[] + local function out(line) - res[#res+1] = line or '' + res[#res + 1] = line or '' end + for _, k in ipairs(get_ordered_schema_keys()) do gen_config_doc_field(k, out) end @@ -138,21 +148,20 @@ end local function parse_func_header(line) local func = line:match('%w+%.([%w_]+)') if not func then - error('Unable to parse: '..line) + error('Unable to parse: ' .. line) end - local args_raw = - line:match('function%((.*)%)') or -- M.name = function(args) - line:match('function%s+%w+%.[%w_]+%((.*)%)') -- function M.name(args) - local args = {} - for k in string.gmatch(args_raw, "([%w_]+)") do + local args_raw = line:match('function%((.*)%)') -- M.name = function(args) + or line:match('function%s+%w+%.[%w_]+%((.*)%)') -- function M.name(args) + local args = {} --- @type string[] + for k in string.gmatch(args_raw, '([%w_]+)') do if k:sub(1, 1) ~= '_' then - args[#args+1] = string.format('{%s}', k) + args[#args + 1] = string.format('{%s}', k) end end return string.format( '%-40s%38s', string.format('%s(%s)', func, table.concat(args, ', ')), - '*gitsigns.'..func..'()*' + '*gitsigns.' .. func .. '()*' ) end @@ -161,8 +170,8 @@ end --- @return string? type --- @return string? description local function parse_param(x) - local name, ty, des = x:match('([^ ]+) +([^ ]+) *(.*)') - return name, ty, des + local name, ty, des = x:match('([^ ]+) +([^ ]+) *(.*)') + return name, ty, des end --- @param x string[] @@ -178,7 +187,7 @@ local function trim_lines(x) local r = {} --- @type string[] for _, e in ipairs(x) do - r[#r+1] = e:sub(min_pad + 1) + r[#r + 1] = e:sub(min_pad + 1) end return r @@ -196,7 +205,7 @@ local function render_param_or_return(name, ty, desc, name_pad) if name == ':' then name_str = '' else - local nf = '%-'..tostring(name_pad)..'s' + local nf = '%-' .. tostring(name_pad) .. 's' name_str = nf:format(string.format('{%s} ', name)) end @@ -206,12 +215,12 @@ local function render_param_or_return(name, ty, desc, name_pad) local r = {} --- @type string[] - local desc1 = desc[1] == '' and '' or ' '..desc[1] - r[#r+1] = string.format(' %s(%s): %s', name_str, ty, desc1) + local desc1 = desc[1] == '' and '' or ' ' .. desc[1] + r[#r + 1] = string.format(' %s(%s): %s', name_str, ty, desc1) local remain_desc = trim_lines(vim.list_slice(desc, 2)) for _, d in ipairs(remain_desc) do - r[#r+1] = ' '..string.rep(' ', name_pad)..d + r[#r + 1] = ' ' .. string.rep(' ', name_pad) .. d end return r @@ -225,7 +234,7 @@ local function pad(x, amount) local r = {} --- @type string[] for _, e in ipairs(x) do - r[#r+1] = pad_str..e + r[#r + 1] = pad_str .. e end return r end @@ -245,13 +254,13 @@ local function process_doc_comment(state, doc_comment, desc, params, returns) if emmy_type == 'param' then local name, ty, pdesc = parse_param(emmy_str) - params[#params+1] = {name, ty, { pdesc }} + params[#params + 1] = { name, ty, { pdesc } } return 'in_param' end if emmy_type == 'return' then local ty, name, rdesc = parse_param(emmy_str) - returns[#returns+1] = {name, ty, { rdesc }} + returns[#returns + 1] = { name, ty, { rdesc } } return 'in_return' end @@ -259,17 +268,17 @@ local function process_doc_comment(state, doc_comment, desc, params, returns) -- Consume any remaining doc document lines as the description for the -- last parameter local lastdes = params[#params][3] - lastdes[#lastdes+1] = doc_comment + lastdes[#lastdes + 1] = doc_comment elseif state == 'in_return' then -- Consume any remaining doc document lines as the description for the -- last return local lastdes = returns[#returns][3] - lastdes[#lastdes+1] = doc_comment + lastdes[#lastdes + 1] = doc_comment else if doc_comment ~= '' and doc_comment ~= '<' then - doc_comment = string.rep(' ', 16)..doc_comment + doc_comment = string.rep(' ', 16) .. doc_comment end - desc[#desc+1] = doc_comment + desc[#desc + 1] = doc_comment end return state @@ -295,7 +304,7 @@ local function render_block(header, block, params, returns) ) if #params > 0 then - local param_block = {'Parameters: ~'} + local param_block = { 'Parameters: ~' } local name_pad = 0 for _, v in ipairs(params) do @@ -312,8 +321,8 @@ local function render_block(header, block, params, returns) end if #returns > 0 then - res[#res+1] = '' - local param_block = {'Returns: ~'} + res[#res + 1] = '' + local param_block = { 'Returns: ~' } for _, v in ipairs(returns) do local name, ty, desc = v[1], v[2], v[3] list_extend(param_block, render_param_or_return(name, ty, desc)) @@ -327,7 +336,7 @@ end --- @param path string --- @return string local function gen_functions_doc_from_file(path) - local i = read_file(path):gmatch("([^\n]*)\n?") --- @type Iterator[string] + local i = read_file(path):gmatch('([^\n]*)\n?') --- @type Iterator[string] local blocks = {} --- @type string[][] @@ -345,7 +354,7 @@ local function gen_functions_doc_from_file(path) -- First line after block local ok, header = pcall(parse_func_header, l) if ok then - blocks[#blocks+1] = render_block(header, desc, params, returns) + blocks[#blocks + 1] = render_block(header, desc, params, returns) end state = 'none' desc = {} @@ -358,9 +367,9 @@ local function gen_functions_doc_from_file(path) for j = #blocks, 1, -1 do local b = blocks[j] for k = 1, #b do - res[#res+1] = b[k]:match('^ *$') and '' or b[k] + res[#res + 1] = b[k]:match('^ *$') and '' or b[k] end - res[#res+1] = '' + res[#res + 1] = '' end return table.concat(res, '\n') @@ -369,11 +378,11 @@ end --- @param files string[] --- @return string local function gen_functions_doc(files) - local res = '' + local res = {} --- @type string[] for _, path in ipairs(files) do - res = res..'\n'..gen_functions_doc_from_file(path) + res[#res + 1] = gen_functions_doc_from_file(path) end - return res + return table.concat(res, '\n') end --- @return string @@ -395,16 +404,16 @@ local function gen_highlights_doc() if not spec.hidden then local fallbacks_tbl = {} --- @type string[] for _, f in ipairs(spec) do - fallbacks_tbl[#fallbacks_tbl+1] = string.format('`%s`', f) + fallbacks_tbl[#fallbacks_tbl + 1] = string.format('`%s`', f) end local fallbacks = table.concat(fallbacks_tbl, ', ') - res[#res+1] = string.format('%s*hl-%s*', string.rep(' ', 56), name) - res[#res+1] = string.format('%s', name) + res[#res + 1] = string.format('%s*hl-%s*', string.rep(' ', 56), name) + res[#res + 1] = string.format('%s', name) if spec.desc then - res[#res+1] = string.format('%s%s', string.rep(' ', 8), spec.desc) - res[#res+1] = '' + res[#res + 1] = string.format('%s%s', string.rep(' ', 8), spec.desc) + res[#res + 1] = '' end - res[#res+1] = string.format('%sFallbacks: %s', string.rep(' ', 8), fallbacks) + res[#res + 1] = string.format('%sFallbacks: %s', string.rep(' ', 8), fallbacks) end end end @@ -414,11 +423,11 @@ end --- @return string local function get_setup_from_readme() - local readme = read_file('README.md'):gmatch("([^\n]*)\n?") --- @type Iterator + local readme = read_file('README.md'):gmatch('([^\n]*)\n?') --- @type Iterator local res = {} --- @type string[] local function append(line) - res[#res+1] = line ~= '' and ' '..line or '' + res[#res + 1] = line ~= '' and ' ' .. line or '' end for l in readme do @@ -442,22 +451,22 @@ end --- @return string|fun():string local function get_marker_text(marker) return ({ - VERSION = '0.7-dev', - CONFIG = function() return gen_config_doc() end, + VERSION = '0.7-dev', + CONFIG = gen_config_doc, FUNCTIONS = function() - return gen_functions_doc{ + return gen_functions_doc({ 'lua/gitsigns.lua', 'lua/gitsigns/attach.lua', 'lua/gitsigns/actions.lua', - } + }) end, - HIGHLIGHTS = function() return gen_highlights_doc() end, - SETUP = function() return get_setup_from_readme() end, + HIGHLIGHTS = gen_highlights_doc, + SETUP = get_setup_from_readme, })[marker] end local function main() - local template = read_file('etc/doc_template.txt'):gmatch("([^\n]*)\n?") --- @type Iterator + local template = read_file('etc/doc_template.txt'):gmatch('([^\n]*)\n?') --- @type Iterator local out = assert(io.open('doc/gitsigns.txt', 'w')) @@ -470,7 +479,7 @@ local function main() sub = sub() end sub = sub:gsub('%%', '%%%%') - l = l:gsub('{{'..marker..'}}', sub) + l = l:gsub('{{' .. marker .. '}}', sub) end end out:write(l or '', '\n')