8000 mini.nvim/lua/mini/doc.lua at main · cryptomilk/mini.nvim · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1313 lines (1143 loc) · 50 KB

doc.lua

File metadata and controls

1313 lines (1143 loc) · 50 KB
local hook = hooks.sections[section.info.id]
if hook ~= nil then hook(section) end
hooks.section_post(section)
end
hooks.block_post(block)
end
hooks.file(file)
end
hooks.doc(doc)
end
H.alias_register = function(s)
if #s == 0 then return end
-- Remove first word (with bits of surrounding whitespace) while capturing it
local alias_name
s[1] = s[1]:gsub('%s*(%S+) ?', function(x)
alias_name = x
return ''
end, 1)
if alias_name == nil then return end
MiniDoc.current.aliases = MiniDoc.current.aliases or {}
MiniDoc.current.aliases[alias_name] = table.concat(s, '\n')
end
H.alias_replace = function(s)
if MiniDoc.current.aliases == nil then return end
for i, _ in ipairs(s) do
for alias_name, alias_desc in pairs(MiniDoc.current.aliases) do
-- Escape special characters. This is done here and not while registering
-- alias to allow user to refer to aliases by its original name.
-- Store escaped words in separate variables because `vim.pesc()` returns
-- two values which might conflict if outputs are used as arguments.
local name_escaped = vim.pesc(alias_name)
local desc_escaped = vim.pesc(alias_desc)
s[i] = s[i]:gsub(name_escaped, desc_escaped)
end
end
end
H.toc_register = function(s)
MiniDoc.current.toc = MiniDoc.current.toc or {}
table.insert(MiniDoc.current.toc, s)
end
H.toc_insert = function(s)
if MiniDoc.current.toc == nil then return end
-- Render table of contents
local toc_lines = {}
for _, toc_entry in ipairs(MiniDoc.current.toc) do
local _, tag_section = toc_entry.parent:has_descendant(
function(x) return type(x) == 'table' and x.type == 'section' and x.info.id == '@tag' end
)
tag_section = tag_section or {}
local lines = {}
for i = 1, math.max(#toc_entry, #tag_section) do
local left = toc_entry[i] or ''
-- Use tag refernce instead of tag enclosure
local right = vim.trim((tag_section[i] or ''):gsub('%*', '|'))
-- Add visual line only at first entry (while not adding trailing space)
local filler = i == 1 and '.' or (right == '' and '' or ' ')
-- Make padding of 2 spaces at both left and right
local n_filler = math.max(74 - H.visual_text_width(left) - H.visual_text_width(right), 3)
table.insert(lines, (' %s%s%s'):format(left, filler:rep(n_filler), right))
end
table.insert(toc_lines, lines)
-- Don't show `toc_entry` lines in output
toc_entry:clear_lines()
end
for _, l in ipairs(vim.tbl_flatten(toc_lines)) do
s:insert(l)
end
end
H.add_section_heading = function(s, heading)
if #s == 0 or s.type ~= 'section' then return end
-- Add heading
0