diff options
| author | Searidang Pa <dangsyncpa@gmail.com> | 2026-01-14 14:30:59 -0500 |
|---|---|---|
| committer | Searidang Pa <dangsyncpa@gmail.com> | 2026-01-14 14:30:59 -0500 |
| commit | 732d03f1fa0a669592e3a947df4c92835a63b283 (patch) | |
| tree | 0e04b74f710120ecfe78aa969e111b81f9623751 /scripts | |
| parent | 09636bbb542da72b059fa28213b327c2da57a1e8 (diff) | |
| download | a4-732d03f1fa0a669592e3a947df4c92835a63b283.tar.xz a4-732d03f1fa0a669592e3a947df4c92835a63b283.zip | |
scripts/tests: auto-install optional TS parsers, error only if required missing
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/tests/minimal.vim | 55 |
1 files changed, 46 insertions, 9 deletions
diff --git a/scripts/tests/minimal.vim b/scripts/tests/minimal.vim index 67d55b7..2f2f746 100644 --- a/scripts/tests/minimal.vim +++ b/scripts/tests/minimal.vim @@ -23,12 +23,18 @@ runtime! plugin/nvim-treesitter.lua lua <<EOF vim.opt.rtp:append(vim.fn.stdpath('data') .. '/site') --- so far, only lua and typescript parser are used in the test -local required_parsers = { "lua", "typescript" } +-- parsers to attempt to install (for user convenience) +local all_parsers = { + 'c', 'cpp', 'go', 'lua', 'php', 'python', 'typescript', + 'javascript', 'java', 'ruby', 'tsx', 'c_sharp', 'vue' +} + +-- parsers actually required for tests to run +local required_parsers = { 'lua', 'typescript' } local function missing_parsers(parsers) local missing = {} - local buf = vim.api.nvim_create_buf(false, true) -- false: no list, true: scratch buffer + local buf = vim.api.nvim_create_buf(false, true) for _, lang in ipairs(parsers) do local ok = pcall(vim.treesitter.get_parser, buf, lang) if not ok then @@ -39,12 +45,43 @@ local function missing_parsers(parsers) return missing end -local to_install = missing_parsers(required_parsers) +local function install_with_main_branch_api(parsers) + local install_dir = vim.fn.stdpath('data') .. '/site' + require('nvim-treesitter').setup({ install_dir = install_dir }) + require('nvim-treesitter').install(parsers):wait(300000) +end + +-- master branch is deprecated but still widely used +local function install_with_master_branch_api(parsers) + -- fixes 'pos_delta >= 0' error - https://github.com/nvim-lua/plenary.nvim/issues/52 + vim.cmd('set display=lastline') + -- make "TSInstall*" available + vim.cmd('runtime! plugin/nvim-treesitter.vim') + vim.cmd('TSInstallSync ' .. table.concat(parsers, ' ')) +end + +local to_install = missing_parsers(all_parsers) if #to_install > 0 then - error( - "Missing Tree-sitter parsers: " - .. table.concat(to_install, ", ") - .. "\nInstall them with: TSInstall " .. table.concat(to_install, " ") - ) + -- Detect which nvim-treesitter API is available (main vs master branch) + local has_main_api, ts = pcall(require, 'nvim-treesitter') + has_main_api = has_main_api and type(ts.install) == 'function' + + if has_main_api then + local ok, err = pcall(install_with_main_branch_api, to_install) + if not ok then + vim.print('Tree-sitter install error (main API): ' .. tostring(err)) + end + else + local ok, err = pcall(install_with_master_branch_api, to_install) + if not ok then + print('Tree-sitter install error (master API): ' .. tostring(err)) + end + end +end + +-- only error if required parsers are still missing +local still_missing = missing_parsers(required_parsers) +if #still_missing > 0 then + error('Missing required Tree-sitter parsers: ' .. table.concat(still_missing, ', ')) end EOF |
