feat(api): expose list of available and installed languages

This commit is contained in:
Christian Clason
2025-05-29 12:04:37 +02:00
committed by Christian Clason
parent 0860b9b107
commit ce903fde5d
8 changed files with 36 additions and 13 deletions

View File

@@ -102,7 +102,7 @@ setup({opts}) *nvim-treesitter.setup()*
directory to install parsers and queries to. Note: will be
prepended to |runtimepath|.
install({languages} [, {opts}]) *nvim-treesitter.install()*
install({languages} [, {opts}]) *nvim-treesitter.install()*
Download, compile, and install the specified treesitter parsers and copy
the corresponding queries to a directory on |runtimepath|, enabling their
@@ -126,7 +126,7 @@ install({languages} [, {opts}]) *nvim-treesitter.install()*
• {max_jobs} (`integer?`) limit parallel tasks (useful in
combination with {generate} on memory-limited systems).
uninstall({languages}) *nvim-treesitter.uninstall()*
uninstall({languages}) *nvim-treesitter.uninstall()*
Remove the parser and queries for the specified language(s).
@@ -134,7 +134,7 @@ uninstall({languages}) *nvim-treesitter.uninstall()
• {languages} `(string[]|string)` (List of) languages or tiers (`stable`,
`unstable`) to update.
update([{languages}]) *nvim-treesitter.update()*
update([{languages}]) *nvim-treesitter.update()*
Update the parsers and queries if older than the revision specified in the
manifest.
@@ -153,5 +153,20 @@ indentexpr() *nvim-treesitter.indentexpr()*
Used to enable treesitter indentation for a language via >lua
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
<
get_available([{tier}]) *nvim-treesitter.get_available()*
Return list of languages available for installation.
Parameters: ~
• {tier} `(integer?)` Only return languages of specified {tier} (`1`:
stable, `2`: unstable, `3`: unmaintained, `4`: unsupported)
get_installed([{type}]) *nvim-treesitter.get_installed()*
Return list of languages installed via `nvim-treesitter`.
Parameters: ~
• {type} `('queries'|parsers'?)` If specified, only show languages with
installed queries or parsers, respectively.
vim:tw=78:ts=8:expandtab:noet:ft=help:norl:

View File

@@ -41,7 +41,7 @@ end
---@param type 'queries'|'parsers'?
---@return string[]
function M.installed_languages(type)
function M.get_installed(type)
local installed = {} --- @type table<string, boolean>
if not (type and type == 'parsers') then
for f in vim.fs.dir(M.get_install_dir('queries')) do
@@ -107,7 +107,7 @@ function M.norm_languages(languages, skip)
if vim.list_contains(languages, 'all') then
if skip and skip.missing then
return M.installed_languages()
return M.get_installed()
end
languages = M.get_available()
end
@@ -115,7 +115,7 @@ function M.norm_languages(languages, skip)
languages = expand_tiers(languages)
if skip and skip.installed then
local installed = M.installed_languages()
local installed = M.get_installed()
languages = vim.tbl_filter(
--- @param v string
function(v)
@@ -126,7 +126,7 @@ function M.norm_languages(languages, skip)
end
if skip and skip.missing then
local installed = M.installed_languages()
local installed = M.get_installed()
languages = vim.tbl_filter(
--- @param v string
function(v)

View File

@@ -136,7 +136,7 @@ function M.check()
-- Parser installation checks
health.start('Installed languages' .. string.rep(' ', 5) .. 'H L F I J')
local languages = config.installed_languages()
local languages = config.get_installed()
for _, lang in pairs(languages) do
local parser = parsers[lang]
local out = lang .. string.rep(' ', 22 - #lang)

View File

@@ -4,6 +4,14 @@ function M.setup(...)
require('nvim-treesitter.config').setup(...)
end
function M.get_available(...)
return require('nvim-treesitter.config').get_available(...)
end
function M.get_installed(...)
return require('nvim-treesitter.config').get_installed(...)
end
function M.install(...)
return require('nvim-treesitter.install').install(...)
end

View File

@@ -402,7 +402,7 @@ local install_status = {} ---@type table<string,InstallStatus?>
---@param generate? boolean
---@return InstallStatus status
local function install_lang(lang, cache_dir, install_dir, force, generate)
if not force and vim.list_contains(config.installed_languages(), lang) then
if not force and vim.list_contains(config.get_installed(), lang) then
install_status[lang] = 'installed'
return 'installed'
end
@@ -537,7 +537,7 @@ M.uninstall = a.async(function(languages)
local parser_dir = config.get_install_dir('parser')
local query_dir = config.get_install_dir('queries')
local installed = config.installed_languages()
local installed = config.get_installed()
local task_funs = {} ---@type async.TaskFun[]
local done = 0

View File

@@ -21,7 +21,7 @@ local function complete_installed_parsers(arglead)
function(v)
return v:find(arglead) ~= nil
end,
require('nvim-treesitter.config').installed_languages()
require('nvim-treesitter.config').get_installed()
)
end

View File

@@ -3,7 +3,7 @@ vim.opt.runtimepath:append('.')
local configs = require('nvim-treesitter.parsers')
local parsers = #_G.arg > 0 and { unpack(_G.arg) }
or require('nvim-treesitter.config').installed_languages('parsers')
or require('nvim-treesitter.config').get_installed('parsers')
local data = {} ---@type table[]
local errors = {} ---@type string[]

View File

@@ -4,7 +4,7 @@ vim.opt.runtimepath:append('.')
local query_types = require('nvim-treesitter.health').bundled_queries
local configs = require('nvim-treesitter.parsers')
local parsers = #_G.arg > 0 and { unpack(_G.arg) }
or require('nvim-treesitter.config').installed_languages('queries')
or require('nvim-treesitter.config').get_installed('queries')
-- Check queries for each installed parser in parsers
local errors = {} ---@type string[]