Path: blob/main/src/resources/lua-plugin/plugin.lua
12921 views
-- Lua LSP Plugin to automatically recognize types for Pandoc Lua filter callbacks12local kBlockElements = {3['BlockQuote'] = true,4['BulletList'] = true,5['CodeBlock'] = true,6['DefinitionList'] = true,7['Div'] = true,8['Header'] = true,9['HorizontalRule'] = true,10['LineBlock'] = true,11['OrderedList'] = true,12['Para'] = true,13['Plain'] = true,14['RawBlock'] = true,15['Table'] = true16}17local kInlineElements = {18['Cite'] = true,19['Code'] = true,20['Emph'] = true,21['Image'] = true,22['LineBreak'] = true,23['Link'] = true,24['Math'] = true,25['Note'] = true,26['Quote'] = true,27['RawInline'] = true,28['SmallCaps'] = true,29['SoftBreak'] = true,30['Space'] = true,31['Span'] = true,32['Str'] = true,33['Strikeout'] = true,34['Strong'] = true,35['Superscript'] = true,36['Subscript'] = true,37['Underline'] = true38}3940local kListElements = {41['Blocks'] = true,42['Inlines'] = true43}4445local kTopLevelElements = {46['Meta'] = true,47['Pandoc'] = true48}4950---@param newline boolean51---@param start integer52---@param leading string53---@param elType string54---@param elVar string55---@return { start: integer, finish: integer, prefix: string } | nil56local functionDiff = function(newline, start, leading, elType, elVar)5758-- determine return type and tweak elType59local returnType = nil60if kBlockElements[elType] ~= nil then61returnType = 'pandoc.Block|pandoc.List|nil'62elseif kInlineElements[elType] ~= nil then63returnType = 'pandoc.Inline|pandoc.List|nil'64elseif kListElements[elType] ~= nil then65elType = 'List'66returnType = 'pandoc.List|nil'67elseif kTopLevelElements[elType] ~= nil then68returnType = 'pandoc.' .. elType .. '|nil'69end7071-- if this is one of ours then return it72if returnType ~= nil then73-- handle variation between beginning of file and beginning of line74local prefix = ''75local finish = nil76if newline then77start = start - 178finish = start79prefix = '\n'80else81finish = start - 182end83-- provide the diff w/ requisite start and finish84return {85start = start,86finish = finish,87text = (prefix .. '%s---@param %s pandoc.%s\n%s---@return %s\n'):format(88leading,elVar,elType,leading,returnType89)90}91else92return nil93end9495end9697---@param uri string98---@param text string99function OnSetText(uri, text)100101-- manage list of diffs102local diffs = {}103local addDiff = function(diff)104if diff then105diffs[#diffs+1] = diff106end107end108109local patterns = {110-- Div = function(el)111'()([\t ]*)(%w+)%s*=%s*function%((%w+)%)',112-- function Div(el)113'()([\t ]*)function%s+(%w+)%((%w+)%)'114}115116for _, pattern in pairs(patterns) do117-- patterns in file (after first line)118for start, leading, elType, elVar in text:gmatch('\n' .. pattern) do119addDiff(functionDiff(true, start --[[@as integer]], leading, elType, elVar))120end121-- pattern on first line122local start, leading, elType, elVar = text:match('^' .. pattern)123if start ~= nil then124addDiff(functionDiff(false, start, leading, elType, elVar))125end126end127128return diffs129130end131132