aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/treesitter/language.lua
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2023-02-21 17:09:18 +0000
committerGitHub <noreply@github.com>2023-02-21 17:09:18 +0000
commit8714a4009c0f0be0bb27a6b3eb486eeb3d9f3049 (patch)
treeba930952af970a189e735f3b6f001d92a0fff72f /runtime/lua/vim/treesitter/language.lua
parent344a1ee8e6b7d78120f8393d1babfd285e866334 (diff)
downloadrneovim-8714a4009c0f0be0bb27a6b3eb486eeb3d9f3049.tar.gz
rneovim-8714a4009c0f0be0bb27a6b3eb486eeb3d9f3049.tar.bz2
rneovim-8714a4009c0f0be0bb27a6b3eb486eeb3d9f3049.zip
feat(treesitter): add filetype -> lang API
Problem: vim.treesitter does not know how to map a specific filetype to a parser. This creates problems since in a few places (including in vim.treesitter itself), the filetype is incorrectly used in place of lang. Solution: Add an API to enable this: - Add vim.treesitter.language.add() as a replacement for vim.treesitter.language.require_language(). - Optional arguments are now passed via an opts table. - Also takes a filetype (or list of filetypes) so we can keep track of what filetypes are associated with which langs. - Deprecated vim.treesitter.language.require_language(). - Add vim.treesitter.language.get_lang() which returns the associated lang for a given filetype. - Add vim.treesitter.language.register() to associate filetypes to a lang without loading the parser.
Diffstat (limited to 'runtime/lua/vim/treesitter/language.lua')
-rw-r--r--runtime/lua/vim/treesitter/language.lua84
1 files changed, 76 insertions, 8 deletions
diff --git a/runtime/lua/vim/treesitter/language.lua b/runtime/lua/vim/treesitter/language.lua
index 8634e53b7b..8637d7d544 100644
--- a/runtime/lua/vim/treesitter/language.lua
+++ b/runtime/lua/vim/treesitter/language.lua
@@ -2,19 +2,66 @@ local a = vim.api
local M = {}
+---@type table<string,string>
+local ft_to_lang = {}
+
+---@param filetype string
+---@return string|nil
+function M.get_lang(filetype)
+ return ft_to_lang[filetype]
+end
+
+---@deprecated
+function M.require_language(lang, path, silent, symbol_name)
+ return M.add(lang, {
+ silent = silent,
+ path = path,
+ symbol_name = symbol_name,
+ })
+end
+
+---@class treesitter.RequireLangOpts
+---@field path? string
+---@field silent? boolean
+---@field filetype? string|string[]
+---@field symbol_name? string
+
--- Asserts that a parser for the language {lang} is installed.
---
--- Parsers are searched in the `parser` runtime directory, or the provided {path}
---
---@param lang string Language the parser should parse (alphanumerical and `_` only)
----@param path (string|nil) Optional path the parser is located at
----@param silent (boolean|nil) Don't throw an error if language not found
----@param symbol_name (string|nil) Internal symbol name for the language to load
+---@param opts (table|nil) Options:
+--- - filetype (string|string[]) Filetype(s) that lang can be parsed with.
+--- Note this is not strictly the same as lang since a single lang can
+--- parse multiple filetypes.
+--- Defaults to lang.
+--- - path (string|nil) Optional path the parser is located at
+--- - symbol_name (string|nil) Internal symbol name for the language to load
+--- - silent (boolean|nil) Don't throw an error if language not found
---@return boolean If the specified language is installed
-function M.require_language(lang, path, silent, symbol_name)
+function M.add(lang, opts)
+ ---@cast opts treesitter.RequireLangOpts
+ opts = opts or {}
+ local path = opts.path
+ local silent = opts.silent
+ local filetype = opts.filetype or lang
+ local symbol_name = opts.symbol_name
+
+ vim.validate({
+ lang = { lang, 'string' },
+ path = { path, 'string', true },
+ silent = { silent, 'boolean', true },
+ symbol_name = { symbol_name, 'string', true },
+ filetype = { filetype, { 'string', 'table' }, true },
+ })
+
+ M.register(lang, filetype or lang)
+
if vim._ts_has_language(lang) then
return true
end
+
if path == nil then
if not (lang and lang:match('[%w_]+') == lang) then
if silent then
@@ -35,9 +82,9 @@ function M.require_language(lang, path, silent, symbol_name)
end
if silent then
- return pcall(function()
- vim._ts_add_language(path, lang, symbol_name)
- end)
+ if not pcall(vim._ts_add_language, path, lang, symbol_name) then
+ return false
+ end
else
vim._ts_add_language(path, lang, symbol_name)
end
@@ -45,6 +92,27 @@ function M.require_language(lang, path, silent, symbol_name)
return true
end
+--- Register a lang to be used for a filetype (or list of filetypes).
+---@param lang string Language to register
+---@param filetype string|string[] Filetype(s) to associate with lang
+function M.register(lang, filetype)
+ vim.validate({
+ lang = { lang, 'string' },
+ filetype = { filetype, { 'string', 'table' } },
+ })
+
+ local filetypes ---@type string[]
+ if type(filetype) == 'string' then
+ filetypes = { filetype }
+ else
+ filetypes = filetype
+ end
+
+ for _, f in ipairs(filetypes) do
+ ft_to_lang[f] = lang
+ end
+end
+
--- Inspects the provided language.
---
--- Inspecting provides some useful information on the language like node names, ...
@@ -52,7 +120,7 @@ end
---@param lang string Language
---@return table
function M.inspect_language(lang)
- M.require_language(lang)
+ M.add(lang)
return vim._ts_inspect_language(lang)
end