aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2023-06-07 22:30:26 +0100
committerChristian Clason <c.clason@uni-graz.at>2025-05-12 18:43:40 +0200
commitc5152f3e8303d5cb241fc3eb3f339755719c45ad (patch)
tree8466591c9e34bfd398dd086a815b4b5f21c22699
parentfix: expand tiers in ignore_install (diff)
downloadnvim-treesitter-c5152f3e8303d5cb241fc3eb3f339755719c45ad.tar
nvim-treesitter-c5152f3e8303d5cb241fc3eb3f339755719c45ad.tar.gz
nvim-treesitter-c5152f3e8303d5cb241fc3eb3f339755719c45ad.tar.bz2
nvim-treesitter-c5152f3e8303d5cb241fc3eb3f339755719c45ad.tar.lz
nvim-treesitter-c5152f3e8303d5cb241fc3eb3f339755719c45ad.tar.xz
nvim-treesitter-c5152f3e8303d5cb241fc3eb3f339755719c45ad.tar.zst
nvim-treesitter-c5152f3e8303d5cb241fc3eb3f339755719c45ad.zip
refactor: use vim.system (#4923)
-rw-r--r--lua/nvim-treesitter/async.lua44
-rw-r--r--lua/nvim-treesitter/health.lua13
-rw-r--r--lua/nvim-treesitter/install.lua73
-rw-r--r--lua/nvim-treesitter/job.lua122
-rw-r--r--lua/nvim-treesitter/parsers.lua2
-rwxr-xr-xscripts/update-lockfile.lua65
6 files changed, 103 insertions, 216 deletions
diff --git a/lua/nvim-treesitter/async.lua b/lua/nvim-treesitter/async.lua
index 57a670ef6..8a5e7c8af 100644
--- a/lua/nvim-treesitter/async.lua
+++ b/lua/nvim-treesitter/async.lua
@@ -3,15 +3,15 @@ local co = coroutine
local M = {}
---Executes a future with a callback when it is done
---- @param func function
---- @param callback function
---- @param ... unknown
+---@param func function
+---@param callback function
+---@param ... unknown
local function execute(func, callback, ...)
local thread = co.create(func)
local function step(...)
local ret = { co.resume(thread, ...) }
- --- @type boolean, any
+ ---@type boolean, any
local stat, nargs_or_err = unpack(ret)
if not stat then
@@ -31,7 +31,7 @@ local function execute(func, callback, ...)
return
end
- --- @type function, any[]
+ ---@type function, any[]
local fn, args = ret[3], { unpack(ret, 4, table.maxn(ret)) }
args[nargs_or_err] = step
fn(unpack(args, 1, nargs_or_err))
@@ -41,13 +41,15 @@ local function execute(func, callback, ...)
end
--- Creates an async function with a callback style function.
---- @generic F: function
---- @param func F
---- @param argc integer
---- @return F
+---@generic F: function
+---@param func F
+---@param argc integer
+---@return F
function M.wrap(func, argc)
- --- @param ... unknown
- --- @return unknown
+ vim.validate('func', func, 'function')
+ vim.validate('argc', argc, 'number')
+ ---@param ... unknown
+ ---@return unknown
return function(...)
return co.yield(argc, func, ...)
end
@@ -56,10 +58,10 @@ end
---Use this to create a function which executes in an async context but
---called from a non-async context. Inherently this cannot return anything
---since it is non-blocking
---- @generic F: function
---- @param func async F
---- @param nargs? integer
---- @return F
+---@generic F: function
+---@param func async F
+---@param nargs? integer
+---@return F
function M.sync(func, nargs)
nargs = nargs or 0
return function(...)
@@ -68,10 +70,10 @@ function M.sync(func, nargs)
end
end
---- @param n integer max number of concurrent jobs
---- @param interrupt_check? function
---- @param thunks function[]
---- @return any
+---@param n integer max number of concurrent jobs
+---@param interrupt_check? function
+---@param thunks function[]
+---@return any
function M.join(n, interrupt_check, thunks)
return co.yield(1, function(finish)
if #thunks == 0 then
@@ -81,7 +83,7 @@ function M.join(n, interrupt_check, thunks)
local remaining = { select(n + 1, unpack(thunks)) }
local to_go = #thunks
- local ret = {} --- @type any[]
+ local ret = {} ---@type any[]
local function cb(...)
ret[#ret + 1] = { ... }
@@ -104,7 +106,7 @@ end
---An async function that when called will yield to the Neovim scheduler to be
---able to call the API.
---- @type fun()
+---@type fun()
M.main = M.wrap(vim.schedule, 1)
return M
diff --git a/lua/nvim-treesitter/health.lua b/lua/nvim-treesitter/health.lua
index 4d5e39c43..099e766da 100644
--- a/lua/nvim-treesitter/health.lua
+++ b/lua/nvim-treesitter/health.lua
@@ -12,12 +12,7 @@ local NVIM_TREESITTER_MINIMUM_ABI = 13
---@return string|nil
local function ts_cli_version()
if vim.fn.executable('tree-sitter') == 1 then
- local handle = io.popen('tree-sitter -V')
- if not handle then
- return
- end
- local result = handle:read('*a')
- handle:close()
+ local result = assert(vim.system({ 'tree-sitter', '-V' }):wait().stdout)
return vim.split(result, '\n')[1]:match('[^tree%psitter ].*')
end
end
@@ -45,9 +40,7 @@ local function install_health()
if vim.fn.executable('node') == 0 then
health.warn('`node` executable not found (only needed for `:TSInstallFromGrammar`.')
else
- local handle = assert(io.popen('node --version'))
- local result = handle:read('*a')
- handle:close()
+ local result = assert(vim.system({ 'node', '--version' }):wait().stdout)
local version = vim.split(result, '\n')[1]
health.ok('`node` found ' .. version .. ' (only needed for `:TSInstallFromGrammar`)')
end
@@ -70,7 +63,7 @@ local function install_health()
.. ' or set `$CC` or `require"nvim-treesitter.install".compilers` explicitly.',
})
else
- local version = vim.fn.systemlist(cc .. (cc == 'cl' and '' or ' --version'))[1]
+ local version = assert(vim.system({ cc, cc == 'cl' and '' or '--version' }):wait().stdout)
health.ok(
'`'
.. cc
diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua
index 77242d730..097a5ab0e 100644
--- a/lua/nvim-treesitter/install.lua
+++ b/lua/nvim-treesitter/install.lua
@@ -3,7 +3,6 @@ local uv = vim.uv
local a = require('nvim-treesitter.async')
local config = require('nvim-treesitter.config')
-local job = require('nvim-treesitter.job')
local log = require('nvim-treesitter.log')
local parsers = require('nvim-treesitter.parsers')
local util = require('nvim-treesitter.util')
@@ -42,6 +41,20 @@ if uv.os_getenv('CC') then
table.insert(M.compilers, 1, uv.os_getenv('CC'))
end
+local function system(cmd, opts)
+ log.trace('running job: (cwd=%s) %s', opts.cwd, table.concat(cmd, ' '))
+ local r = a.wrap(vim.system, 3)(cmd, opts) --[[@as SystemCompleted]]
+ a.main()
+ if r.stdout and r.stdout ~= '' then
+ log.trace('stdout -> %s', r.stdout)
+ end
+ if r.stderr and r.stderr ~= '' then
+ log.trace('stderr -> %s', r.stderr)
+ end
+
+ return r
+end
+
---
--- PARSER INFO
---
@@ -156,24 +169,22 @@ local function do_generate_from_grammar(logger, repo, compile_location)
end
logger:info('Installing NPM dependencies')
- local r = job.run({ 'npm', 'install' }, { cwd = compile_location })
- a.main()
- if r.exit_code > 0 then
+ local r = system({ 'npm', 'install' }, { cwd = compile_location })
+ if r.code > 0 then
logger:error('Error during `npm install`')
end
end
logger:info('Generating source files from grammar.js...')
- local r = job.run({
+ local r = system({
vim.fn.exepath('tree-sitter'),
'generate',
'--no-bindings',
'--abi',
tostring(vim.treesitter.language_version),
}, { cwd = compile_location })
- a.main()
- if r.exit_code > 0 then
+ if r.code > 0 then
logger:error('Error during "tree-sitter generate"')
end
end
@@ -201,7 +212,7 @@ local function do_download_tar(logger, repo, project_name, cache_dir, revision,
local target = is_github and url .. '/archive/' .. revision .. '.tar.gz'
or url .. '/-/archive/' .. revision .. '/' .. project_name .. '-' .. revision .. '.tar.gz'
- local r = job.run({
+ local r = system({
'curl',
'--silent',
'--show-error',
@@ -212,11 +223,8 @@ local function do_download_tar(logger, repo, project_name, cache_dir, revision,
}, {
cwd = cache_dir,
})
- a.main()
- if r.exit_code > 0 then
- logger:error(
- 'Error during download, please verify your internet connection: ' .. vim.inspect(r.stderr)
- )
+ if r.code > 0 then
+ logger:error('Error during download, please verify your internet connection: %s', r.stderr)
end
logger:debug('Creating temporary directory: ' .. temp_dir)
@@ -228,7 +236,7 @@ local function do_download_tar(logger, repo, project_name, cache_dir, revision,
end
logger:info('Extracting ' .. project_name .. '...')
- r = job.run({
+ r = system({
'tar',
'-xzf',
project_name .. '.tar.gz',
@@ -238,14 +246,13 @@ local function do_download_tar(logger, repo, project_name, cache_dir, revision,
cwd = cache_dir,
})
- a.main()
- if r.exit_code > 0 then
- logger:error('Error during tarball extraction: ' .. vim.inspect(r.stderr))
+ if r.code > 0 then
+ logger:error('Error during tarball extraction: %s', r.stderr)
end
err = uv_unlink(project_dir .. '.tar.gz')
if err then
- logger:error('Could not remove tarball: ' .. err)
+ logger:error('Could not remove tarball: %s', err)
end
a.main()
@@ -253,7 +260,7 @@ local function do_download_tar(logger, repo, project_name, cache_dir, revision,
a.main()
if err then
- logger:error('Could not rename temp: ' .. err)
+ logger:error('Could not rename temp: %s', err)