Skip to content

Commit 98f3d1d

Browse files
authored
feat(docgen.lua): convert @brief codeblocks for vimdoc #3787
1 parent f69ebda commit 98f3d1d

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

lsp/jqls.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
--- go install github.com/wader/jq-lsp@master
99
--- # copy binary to $PATH
1010
--- cp $(go env GOPATH)/bin/jq-lsp /usr/local/bin
11+
---
1112
--- ```
1213
--- Note: To activate properly nvim needs to know the jq filetype.
1314
--- You can add it via:

lsp/uvls.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
--- cd uvls
99
--- cargo install --path .
1010
--- ```
11+
---
1112
--- Note: To activate properly nvim needs to know the uvl filetype.
1213
--- You can add it via:
1314
--- ```lua

scripts/docgen.lua

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,32 @@ Default config:
8787
---
8888
]]
8989

90+
--- Converts markdown "```" codeblock to vimdoc format.
91+
local function codeblock_to_vimdoc(doc)
92+
local function make_fn(before, extra)
93+
return function(lang, code)
94+
if not code then
95+
code = lang
96+
lang = ''
97+
end
98+
-- Indent code by 2 spaces.
99+
return before .. '>' .. lang .. extra .. code:gsub('[^\n]+', ' %0')
100+
end
101+
end
102+
103+
doc = doc
104+
-- "```lang" following a nonblank line.
105+
:gsub('[^%s]\n```(%w+)\n(.-)\n```', make_fn(' ', '\n'))
106+
-- "```lang" following a blank line.
107+
:gsub('\n```(%w+)\n(.-)\n```', make_fn('', '\n'))
108+
-- "```" (no language)
109+
:gsub('\n```\n(..-)\n```', make_fn('', '\n'))
110+
111+
return doc
112+
end
113+
90114
--- Gets docstring by looking for "@brief" in a Lua code docstring.
91-
local function extract_brief(text)
115+
local function extract_brief(text, is_markdown)
92116
local doc = text:match('%-%-+ *%@brief.-(\n%-%-.*)')
93117
if not doc then
94118
return ''
@@ -99,12 +123,18 @@ local function extract_brief(text)
99123
doc = doc:gsub('\n%-%-+', '\n')
100124
-- Remove leading whitespace (shared indent).
101125
doc = vim.trim(vim.text.indent(0, doc))
126+
127+
-- Convert codeblocks for vimdoc.
128+
if not is_markdown then
129+
doc = codeblock_to_vimdoc(doc)
130+
end
131+
102132
return doc
103133
end
104134

105135
local function make_lsp_section(config_sections, config_name, config_file, is_markdown)
106136
local config = require('lsp.' .. config_name)
107-
local docstring = extract_brief(readfile(config_file))
137+
local docstring = extract_brief(readfile(config_file), is_markdown)
108138
local params = {
109139
config_name = config_name,
110140
preamble = docstring,
@@ -186,8 +216,7 @@ local function make_lsp_sections(is_markdown)
186216
-- vim.env.HOME = '/home/user'
187217
-- vim.env.XDG_CACHE_HOME = '/home/user/.cache'
188218
local old_fn = vim.fn
189-
local new_fn = {}
190-
vim.fn = setmetatable(new_fn, {
219+
vim.fn = setmetatable({}, {
191220
__index = function(_t, key)
192221
if key == 'getpid' then
193222
return function()

0 commit comments

Comments
 (0)