robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
5.9 KiB204 linesraw
1local api = vim.api
2
3local M = {}
4
5---@type table<string,string?>
6local ft_to_lang = {
7  help = 'vimdoc',
8  checkhealth = 'vimdoc',
9}
10
11--- Returns the filetypes for which a parser named {lang} is used.
12---
13--- The list includes {lang} itself plus all filetypes registered via
14--- |vim.treesitter.language.register()|.
15---
16--- @param lang string Name of parser
17--- @return string[] filetypes
18function M.get_filetypes(lang)
19  local r = { lang } ---@type string[]
20  for ft, p in pairs(ft_to_lang) do
21    if p == lang then
22      r[#r + 1] = ft
23    end
24  end
25  return r
26end
27
28--- Returns the language name to be used when loading a parser for {filetype}.
29---
30--- If no language has been explicitly registered via |vim.treesitter.language.register()|,
31--- default to {filetype}. For composite filetypes like `html.glimmer`, only the main filetype is
32--- returned.
33---
34--- @param filetype string
35--- @return string|nil
36function M.get_lang(filetype)
37  if filetype == '' then
38    return
39  end
40  if ft_to_lang[filetype] then
41    return ft_to_lang[filetype]
42  end
43  -- for subfiletypes like html.glimmer use only "main" filetype
44  filetype = assert(vim.split(filetype, '.', { plain = true })[1])
45  return ft_to_lang[filetype] or filetype
46end
47
48---@deprecated
49---@param lang string
50---@param path? string
51---@param silent? boolean
52---@param symbol_name? string
53function M.require_language(lang, path, silent, symbol_name)
54  vim.deprecate(
55    'vim.treesitter.language.require_language()',
56    'vim.treesitter.language.add()',
57    '0.12'
58  )
59  local opts = {
60    silent = silent,
61    path = path,
62    symbol_name = symbol_name,
63  }
64
65  if silent then
66    local installed = pcall(M.add, lang, opts)
67    return installed
68  end
69
70  return M.add(lang, opts)
71end
72
73--- Load wasm or native parser (wrapper)
74--- todo(clason): move to C
75---
76---@param path string Path of parser library
77---@param lang string Language name
78---@param symbol_name? string Internal symbol name for the language to load (default lang)
79---@return boolean? True if parser is loaded
80local function loadparser(path, lang, symbol_name)
81  if vim.endswith(path, '.wasm') then
82    return vim._ts_add_language_from_wasm and vim._ts_add_language_from_wasm(path, lang)
83  else
84    return vim._ts_add_language_from_object(path, lang, symbol_name)
85  end
86end
87
88---@class vim.treesitter.language.add.Opts
89---@inlinedoc
90---
91---Optional path the parser is located at
92---@field path? string
93---
94---Internal symbol name for the language to load
95---@field symbol_name? string
96
97--- Load parser with name {lang}
98---
99--- Parsers are searched in the `parser` runtime directory, or the provided {path}.
100--- Can be used to check for available parsers before enabling treesitter features, e.g.,
101--- ```lua
102---   if vim.treesitter.language.add('markdown') then
103---     vim.treesitter.start(bufnr, 'markdown')
104---   end
105--- ```
106---
107---@param lang string Name of the parser (alphanumerical and `_` only)
108---@param opts? vim.treesitter.language.add.Opts Options:
109---@return boolean? True if parser is loaded
110---@return string? Error if parser cannot be loaded
111function M.add(lang, opts)
112  opts = opts or {}
113  local path = opts.path
114  local symbol_name = opts.symbol_name
115
116  vim.validate('lang', lang, 'string')
117  vim.validate('path', path, 'string', true)
118  vim.validate('symbol_name', symbol_name, 'string', true)
119
120  -- parser names are assumed to be lowercase (consistent behavior on case-insensitive file systems)
121  lang = lang:lower()
122
123  if vim._ts_has_language(lang) then
124    return true
125  end
126
127  if path == nil then
128    -- allow only safe language names when looking for libraries to load
129    if not (lang and lang:match('[%w_]+') == lang) then
130      return nil, string.format('Invalid language name "%s"', lang)
131    end
132
133    local fname = 'parser/' .. lang .. '.*'
134    local paths = api.nvim_get_runtime_file(fname, false)
135    if #paths == 0 then
136      return nil, string.format('No parser for language "%s"', lang)
137    end
138    path = assert(paths[1])
139  end
140
141  local res = loadparser(path, lang, symbol_name)
142  return res,
143    res == nil and string.format('Cannot load parser %s for language "%s"', path, lang) or nil
144end
145
146--- @param x string|string[]
147--- @return string[]
148local function ensure_list(x)
149  if type(x) == 'table' then
150    return x
151  end
152  return { x }
153end
154
155--- Register a parser named {lang} to be used for {filetype}(s).
156---
157--- Note: this adds or overrides the mapping for {filetype}, any existing mappings from other
158--- filetypes to {lang} will be preserved.
159---
160--- @param lang string Name of parser
161--- @param filetype string|string[] Filetype(s) to associate with lang
162function M.register(lang, filetype)
163  vim.validate('lang', lang, 'string')
164  vim.validate('filetype', filetype, { 'string', 'table' })
165
166  for _, f in ipairs(ensure_list(filetype)) do
167    if f ~= '' then
168      ft_to_lang[f] = lang
169    end
170  end
171end
172
173--- Inspects the provided language.
174---
175--- Inspecting provides some useful information on the language like ABI version, parser state count
176--- (a measure of parser complexity), node and field names, and whether the language came from a
177--- WASM module.
178---
179--- Node names are returned in a table mapping each node name to a `boolean` indicating whether or
180--- not the node is named (i.e., not anonymous). Anonymous nodes are surrounded with double quotes
181--- (`"`).
182---
183--- For ABI 15 parsers, also show parser metadata (major, minor, patch version) and a table of
184--- supertypes with their respective subtypes.
185---
186---@param lang string Language
187---@return TSLangInfo
188function M.inspect(lang)
189  M.add(lang)
190  return vim._ts_inspect_language(lang)
191end
192
193--- Returns available treesitter languages.
194function M._complete()
195  local parsers = api.nvim_get_runtime_file('parser/*', true)
196  local parser_names_set = {} ---@type table<string, boolean>
197  for _, parser in ipairs(parsers) do
198    local parser_name = vim.fn.fnamemodify(parser, ':t:r')
199    parser_names_set[parser_name] = true
200  end
201  return vim.tbl_keys(parser_names_set)
202end
203
204return M