fix(config): check both installed parsers and queries

Problem: Can't uninstall custom parsers without queries since
`installed_parsers` only iterates over installed queries (to include
query-only languages, and to avoid string manipulation).

Solution: Iterate over both queries and parsers to collect list of
installed languages (optionally only queries or only parsers).
This commit is contained in:
Christian Clason 2025-05-28 13:55:33 +02:00 committed by Christian Clason
parent 03c9048090
commit 0860b9b107
6 changed files with 24 additions and 18 deletions

View file

@ -39,22 +39,28 @@ function M.get_install_dir(dir_name)
return dir
end
---@param type 'queries'|'parsers'?
---@return string[]
function M.installed_parsers()
local install_dir = M.get_install_dir('queries')
local installed = {} --- @type string[]
for f in vim.fs.dir(install_dir) do
installed[#installed + 1] = f
function M.installed_languages(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
installed[f] = true
end
end
return installed
if not (type and type == 'queries') then
for f in vim.fs.dir(M.get_install_dir('parser')) do
installed[vim.fn.fnamemodify(f, ':r')] = true
end
end
return vim.tbl_keys(installed)
end
-- Get a list of all available parsers
---@param tier integer? only get parsers of specified tier
---@return string[]
function M.get_available(tier)
vim.api.nvim_exec_autocmds('User', { pattern = 'TSUpdate' })
local parsers = require('nvim-treesitter.parsers')
--- @type string[]
local languages = vim.tbl_keys(parsers)
@ -101,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_parsers()
return M.installed_languages()
end
languages = M.get_available()
end
@ -109,7 +115,7 @@ function M.norm_languages(languages, skip)
languages = expand_tiers(languages)
if skip and skip.installed then
local installed = M.installed_parsers()
local installed = M.installed_languages()
languages = vim.tbl_filter(
--- @param v string
function(v)
@ -120,7 +126,7 @@ function M.norm_languages(languages, skip)
end
if skip and skip.missing then
local installed = M.installed_parsers()
local installed = M.installed_languages()
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_parsers()
local languages = config.installed_languages()
for _, lang in pairs(languages) do
local parser = parsers[lang]
local out = lang .. string.rep(' ', 22 - #lang)

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_parsers(), lang) then
if not force and vim.list_contains(config.installed_languages(), lang) then
install_status[lang] = 'installed'
return 'installed'
end
@ -532,12 +532,12 @@ end
---@param languages string[]|string
M.uninstall = a.async(function(languages)
reload_parsers()
vim.api.nvim_exec_autocmds('User', { pattern = 'TSUpdate' })
languages = config.norm_languages(languages or 'all', { missing = true, dependencies = true })
local parser_dir = config.get_install_dir('parser')
local query_dir = config.get_install_dir('queries')
local installed = config.installed_parsers()
local installed = config.installed_languages()
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_parsers()
require('nvim-treesitter.config').installed_languages()
)
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_parsers()
or require('nvim-treesitter.config').installed_languages('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_parsers()
or require('nvim-treesitter.config').installed_languages('queries')
-- Check queries for each installed parser in parsers
local errors = {} ---@type string[]