Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/resources/lua-plugin/plugin.lua
12925 views
1
-- Lua LSP Plugin to automatically recognize types for Pandoc Lua filter callbacks
2
3
local kBlockElements = {
4
['BlockQuote'] = true,
5
['BulletList'] = true,
6
['CodeBlock'] = true,
7
['DefinitionList'] = true,
8
['Div'] = true,
9
['Header'] = true,
10
['HorizontalRule'] = true,
11
['LineBlock'] = true,
12
['OrderedList'] = true,
13
['Para'] = true,
14
['Plain'] = true,
15
['RawBlock'] = true,
16
['Table'] = true
17
}
18
local kInlineElements = {
19
['Cite'] = true,
20
['Code'] = true,
21
['Emph'] = true,
22
['Image'] = true,
23
['LineBreak'] = true,
24
['Link'] = true,
25
['Math'] = true,
26
['Note'] = true,
27
['Quote'] = true,
28
['RawInline'] = true,
29
['SmallCaps'] = true,
30
['SoftBreak'] = true,
31
['Space'] = true,
32
['Span'] = true,
33
['Str'] = true,
34
['Strikeout'] = true,
35
['Strong'] = true,
36
['Superscript'] = true,
37
['Subscript'] = true,
38
['Underline'] = true
39
}
40
41
local kListElements = {
42
['Blocks'] = true,
43
['Inlines'] = true
44
}
45
46
local kTopLevelElements = {
47
['Meta'] = true,
48
['Pandoc'] = true
49
}
50
51
---@param newline boolean
52
---@param start integer
53
---@param leading string
54
---@param elType string
55
---@param elVar string
56
---@return { start: integer, finish: integer, prefix: string } | nil
57
local functionDiff = function(newline, start, leading, elType, elVar)
58
59
-- determine return type and tweak elType
60
local returnType = nil
61
if kBlockElements[elType] ~= nil then
62
returnType = 'pandoc.Block|pandoc.List|nil'
63
elseif kInlineElements[elType] ~= nil then
64
returnType = 'pandoc.Inline|pandoc.List|nil'
65
elseif kListElements[elType] ~= nil then
66
elType = 'List'
67
returnType = 'pandoc.List|nil'
68
elseif kTopLevelElements[elType] ~= nil then
69
returnType = 'pandoc.' .. elType .. '|nil'
70
end
71
72
-- if this is one of ours then return it
73
if returnType ~= nil then
74
-- handle variation between beginning of file and beginning of line
75
local prefix = ''
76
local finish = nil
77
if newline then
78
start = start - 1
79
finish = start
80
prefix = '\n'
81
else
82
finish = start - 1
83
end
84
-- provide the diff w/ requisite start and finish
85
return {
86
start = start,
87
finish = finish,
88
text = (prefix .. '%s---@param %s pandoc.%s\n%s---@return %s\n'):format(
89
leading,elVar,elType,leading,returnType
90
)
91
}
92
else
93
return nil
94
end
95
96
end
97
98
---@param uri string
99
---@param text string
100
function OnSetText(uri, text)
101
102
-- manage list of diffs
103
local diffs = {}
104
local addDiff = function(diff)
105
if diff then
106
diffs[#diffs+1] = diff
107
end
108
end
109
110
local patterns = {
111
-- Div = function(el)
112
'()([\t ]*)(%w+)%s*=%s*function%((%w+)%)',
113
-- function Div(el)
114
'()([\t ]*)function%s+(%w+)%((%w+)%)'
115
}
116
117
for _, pattern in pairs(patterns) do
118
-- patterns in file (after first line)
119
for start, leading, elType, elVar in text:gmatch('\n' .. pattern) do
120
addDiff(functionDiff(true, start --[[@as integer]], leading, elType, elVar))
121
end
122
-- pattern on first line
123
local start, leading, elType, elVar = text:match('^' .. pattern)
124
if start ~= nil then
125
addDiff(functionDiff(false, start, leading, elType, elVar))
126
end
127
end
128
129
return diffs
130
131
end
132