robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40579 from barrettruth/fix/del-keymap-lhs-only-30258256a88d0ac

master
6.8 KiB256 linesraw
1local cdoc_grammar = require('gen.cdoc_grammar')
2local c_grammar = require('gen.c_grammar')
3local api_type = require('gen.api_types')
4
5--- @class nvim.cdoc.parser.param
6--- @field name string
7--- @field type string
8--- @field desc string
9
10--- @class nvim.cdoc.parser.return
11--- @field name string
12--- @field type string
13--- @field desc? string
14
15--- @class nvim.cdoc.parser.note
16--- @field desc string
17
18--- @class nvim.cdoc.parser.brief
19--- @field kind 'brief'
20--- @field desc string
21
22--- @class nvim.cdoc.parser.fun
23--- @field name string
24--- @field params nvim.cdoc.parser.param[]
25--- @field returns nvim.cdoc.parser.return[]
26--- @field desc string
27--- @field deprecated? true
28--- @field deprecated_since? integer
29--- @field since? string|integer
30--- @field attrs? string[]
31--- @field nodoc? true
32--- @field notes? nvim.cdoc.parser.note[]
33--- @field see? nvim.cdoc.parser.note[]
34
35--- @class nvim.cdoc.parser.State
36--- @field doc_lines? string[]
37--- @field cur_obj? nvim.cdoc.parser.obj
38--- @field last_doc_item? nvim.cdoc.parser.param|nvim.cdoc.parser.return|nvim.cdoc.parser.note
39--- @field last_doc_item_indent? integer
40
41--- @alias nvim.cdoc.parser.obj
42--- | nvim.cdoc.parser.fun
43--- | nvim.cdoc.parser.brief
44
45--- If we collected any `---` lines. Add them to the existing (or new) object
46--- Used for function/class descriptions and multiline param descriptions.
47--- @param state nvim.cdoc.parser.State
48local function add_doc_lines_to_obj(state)
49  if state.doc_lines then
50    state.cur_obj = state.cur_obj or {}
51    local cur_obj = assert(state.cur_obj)
52    local txt = table.concat(state.doc_lines, '\n')
53    if cur_obj.desc then
54      cur_obj.desc = cur_obj.desc .. '\n' .. txt
55    else
56      cur_obj.desc = txt
57    end
58    state.doc_lines = nil
59  end
60end
61
62--- @param line string
63--- @param state nvim.cdoc.parser.State
64local function process_doc_line(line, state)
65  line = line:gsub('^%s+@', '@')
66
67  local parsed = cdoc_grammar:match(line)
68
69  if not parsed then
70    if line:match('^ ') then
71      line = line:sub(2)
72    end
73
74    if state.last_doc_item then
75      if not state.last_doc_item_indent then
76        state.last_doc_item_indent = #line:match('^%s*') + 1
77      end
78      state.last_doc_item.desc = (state.last_doc_item.desc or '')
79        .. '\n'
80        .. line:sub(state.last_doc_item_indent or 1)
81    else
82      state.doc_lines = state.doc_lines or {}
83      table.insert(state.doc_lines, line)
84    end
85    return
86  end
87
88  state.last_doc_item_indent = nil
89  state.last_doc_item = nil
90
91  local kind = parsed.kind
92
93  state.cur_obj = state.cur_obj or {}
94  local cur_obj = assert(state.cur_obj)
95
96  if kind == 'brief' then
97    state.cur_obj = {
98      kind = 'brief',
99      desc = parsed.desc,
100    }
101  elseif kind == 'param' then
102    state.last_doc_item_indent = nil
103    cur_obj.params = cur_obj.params or {}
104    state.last_doc_item = {
105      name = parsed.name,
106      desc = parsed.desc,
107    }
108    table.insert(cur_obj.params, state.last_doc_item)
109  elseif kind == 'return' then
110    cur_obj.returns = { {
111      name = '',
112      type = '',
113      desc = parsed.desc,
114    } }
115    state.last_doc_item_indent = nil
116    state.last_doc_item = cur_obj.returns[1]
117  elseif kind == 'deprecated' then
118    cur_obj.deprecated = true
119  elseif kind == 'nodoc' then
120    cur_obj.nodoc = true
121  elseif kind == 'since' then
122    cur_obj.since = parsed.desc
123  elseif kind == 'see' then
124    cur_obj.see = cur_obj.see or {}
125    table.insert(cur_obj.see, { desc = parsed.desc })
126  elseif kind == 'note' then
127    state.last_doc_item_indent = nil
128    state.last_doc_item = {
129      desc = parsed.desc,
130    }
131    cur_obj.notes = cur_obj.notes or {}
132    table.insert(cur_obj.notes, state.last_doc_item)
133  else
134    error('Unhandled' .. vim.inspect(parsed))
135  end
136end
137
138--- @param item nvim.c_grammar.Proto
139--- @param state nvim.cdoc.parser.State
140local function process_proto(item, state)
141  state.cur_obj = state.cur_obj or {}
142  local cur_obj = assert(state.cur_obj)
143  cur_obj.name = item.name
144  cur_obj.params = cur_obj.params or {}
145
146  local documented = {} --- @type table<string,nvim.cdoc.parser.param>
147  local matched = {} --- @type table<nvim.cdoc.parser.param,true>
148  local params = {} --- @type nvim.cdoc.parser.param[]
149
150  for _, p in ipairs(cur_obj.params) do
151    documented[p.name] = documented[p.name] or p
152  end
153
154  for _, p in ipairs(item.parameters) do
155    local event_type = 'vim.api.keyset.events|vim.api.keyset.events[]'
156    local event = (item.name == 'nvim_create_autocmd' or item.name == 'nvim_exec_autocmds')
157      and p[2] == 'event'
158    local param = documented[p[2]]
159    if param then
160      matched[param] = true
161    else
162      param = { name = p[2] } --[[@as nvim.cdoc.parser.param]]
163    end
164    param.type = event and event_type or api_type(p[1])
165    params[#params + 1] = param
166  end
167
168  for _, p in ipairs(cur_obj.params) do
169    if not matched[p] then
170      params[#params + 1] = p
171    end
172  end
173
174  cur_obj.params = params
175
176  cur_obj.returns = cur_obj.returns or { {
177    name = '',
178    type = '',
179    desc = nil,
180  } }
181  cur_obj.returns[1].type = api_type(item.return_type)
182
183  for _, a in ipairs({
184    'fast',
185    'remote_only',
186    'lua_only',
187    'textlock',
188  }) do
189    if item[a] then
190      cur_obj.attrs = cur_obj.attrs or {}
191      table.insert(cur_obj.attrs, a)
192    end
193  end
194
195  cur_obj.since = item.since
196  cur_obj.deprecated_since = item.deprecated_since
197
198  -- Remove some arguments
199  for i = #cur_obj.params, 1, -1 do
200    local p = cur_obj.params[i]
201    if p.name == 'channel_id' or vim.tbl_contains({ 'lstate', 'arena', 'error' }, p.type) then
202      table.remove(cur_obj.params, i)
203    end
204  end
205
206  -- HACK: Mark optional params (:help api-contract) with "?" so docs render them as optional.
207  if c_grammar.opts_index(item.parameters) then
208    local optional = false
209    for _, p in ipairs(cur_obj.params) do
210      optional = optional or p.name == 'opts'
211      if optional and not p.type:match('%?$') then
212        p.type = p.type .. '?'
213      end
214    end
215  end
216end
217
218local M = {}
219
220--- @param filename string
221--- @return table<string,nvim.luacats.parser.class> classes
222--- @return nvim.cdoc.parser.fun[] funs
223--- @return string[] briefs
224function M.parse(filename)
225  local funs = {} --- @type nvim.cdoc.parser.fun[]
226  local briefs = {} --- @type string[]
227  local state = {} --- @type nvim.cdoc.parser.State
228
229  local txt = assert(io.open(filename, 'r')):read('*all')
230
231  local parsed = c_grammar.grammar:match(txt)
232  for _, item in ipairs(parsed) do
233    if item.comment then
234      process_doc_line(item.comment, state)
235    else
236      add_doc_lines_to_obj(state)
237      if item[1] == 'proto' then
238        process_proto(item --[[@as nvim.c_grammar.Proto]], state)
239        table.insert(funs, state.cur_obj)
240      end
241      local cur_obj = state.cur_obj
242      if cur_obj and not item.static then
243        if cur_obj.kind == 'brief' then
244          table.insert(briefs, cur_obj.desc)
245        end
246      end
247      state = {}
248    end
249  end
250
251  return {}, funs, briefs
252end
253
254-- M.parse('src/nvim/api/vim.c')
255
256return M