robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local api =vim . api 2 3local M ={} 4 5---@type table<string,string?> 6local ft_to_lang ={ 7help ='vimdoc' , 8checkhealth ='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 ) 19local r ={ lang } ---@type string[] 20for ft , p in pairs ( ft_to_lang ) do 21if p == lang then 22r [ # r + 1 ] =ft 23end 24end 25return 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 ) 37if filetype == '' then 38return 39end 40if ft_to_lang [ filetype ] then 41return ft_to_lang [ filetype ] 42end 43-- for subfiletypes like html.glimmer use only "main" filetype 44filetype =assert ( vim . split ( filetype , '.' , { plain =true } )[ 1 ]) 45return 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 ) 54vim . deprecate ( 55'vim.treesitter.language.require_language()' , 56'vim.treesitter.language.add()' , 57'0.12' 58) 59local opts ={ 60silent =silent , 61path =path , 62symbol_name =symbol_name , 63} 64 65if silent then 66local installed =pcall ( M . add , lang , opts ) 67return installed 68end 69 70return 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 ) 81if vim . endswith ( path , '.wasm' ) then 82return vim . _ts_add_language_from_wasm and vim . _ts_add_language_from_wasm ( path , lang ) 83else 84return vim . _ts_add_language_from_object ( path , lang , symbol_name ) 85end 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 ) 112opts =opts or {} 113local path =opts . path 114local symbol_name =opts . symbol_name 115 116vim . validate ( 'lang' , lang , 'string' ) 117vim . validate ( 'path' , path , 'string' , true ) 118vim . validate ( 'symbol_name' , symbol_name , 'string' , true ) 119 120-- parser names are assumed to be lowercase (consistent behavior on case-insensitive file systems) 121lang =lang : lower () 122 123if vim . _ts_has_language ( lang ) then 124return true 125end 126 127if path == nil then 128-- allow only safe language names when looking for libraries to load 129if not ( lang and lang : match ( '[%w_]+' ) == lang ) then 130return nil , string . format ( 'Invalid language name "%s"' , lang ) 131end 132 133local fname ='parser/' .. lang .. '.*' 134local paths =api . nvim_get_runtime_file ( fname , false ) 135if # paths == 0 then 136return nil , string . format ( 'No parser for language "%s"' , lang ) 137end 138path =assert ( paths [ 1 ]) 139end 140 141local res =loadparser ( path , lang , symbol_name ) 142return res , 143res == 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 ) 149if type ( x ) == 'table' then 150return x 151end 152return { 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 ) 163vim . validate ( 'lang' , lang , 'string' ) 164vim . validate ( 'filetype' , filetype , { 'string' , 'table' } ) 165 166for _ , f in ipairs ( ensure_list ( filetype )) do 167if f ~= '' then 168ft_to_lang [ f ] =lang 169end 170end 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 ) 189M . add ( lang ) 190return vim . _ts_inspect_language ( lang ) 191end 192 193--- Returns available treesitter languages. 194function M . _complete () 195local parsers =api . nvim_get_runtime_file ( 'parser/*' , true ) 196local parser_names_set ={} ---@type table<string, boolean> 197for _ , parser in ipairs ( parsers ) do 198local parser_name =vim . fn . fnamemodify ( parser , ':t:r' ) 199parser_names_set [ parser_name ] =true 200end 201return vim . tbl_keys ( parser_names_set ) 202end 203 204return M