fix: cleanup diagnostics

This commit is contained in:
Lewis Russell 2023-08-07 11:03:02 +01:00 committed by Christian Clason
parent c6db88c23b
commit 1b7cde34e3
2 changed files with 39 additions and 49 deletions

View file

@ -29,7 +29,8 @@ function M.setup(user_data)
if config.auto_install then if config.auto_install then
vim.api.nvim_create_autocmd('FileType', { vim.api.nvim_create_autocmd('FileType', {
callback = function(args) callback = function(args)
local ft = vim.bo[args.buf].filetype local buf = args.buf --- @type integer
local ft = vim.bo[buf].filetype
local lang = vim.treesitter.language.get_lang(ft) or ft local lang = vim.treesitter.language.get_lang(ft) or ft
if if
require('nvim-treesitter.parsers').configs[lang] require('nvim-treesitter.parsers').configs[lang]
@ -39,7 +40,7 @@ function M.setup(user_data)
require('nvim-treesitter.install').install(lang, nil, function() require('nvim-treesitter.install').install(lang, nil, function()
-- Need to pcall since 'FileType' can be triggered multiple times -- Need to pcall since 'FileType' can be triggered multiple times
-- per buffer -- per buffer
pcall(vim.treesitter.start, args.buf, lang) pcall(vim.treesitter.start, buf, lang)
end) end)
end end
end, end,

View file

@ -1,3 +1,4 @@
local fn = vim.fn
local fs = vim.fs local fs = vim.fs
local uv = vim.uv local uv = vim.uv
@ -60,31 +61,21 @@ end
--- ---
---@param lang string ---@param lang string
---@param validate? boolean ---@return InstallInfo?
---@return InstallInfo local function get_parser_install_info(lang)
local function get_parser_install_info(lang, validate)
local parser_config = parsers.configs[lang] local parser_config = parsers.configs[lang]
if not parser_config then if not parser_config then
log.error('Parser not available for language "' .. lang .. '"') log.error('Parser not available for language "' .. lang .. '"')
end end
local install_info = parser_config.install_info return parser_config.install_info
if validate then
vim.validate({
url = { install_info.url, 'string' },
files = { install_info.files, 'table' },
})
end
return install_info
end end
--- @param ... string --- @param ... string
--- @return string --- @return string
function M.get_package_path(...) function M.get_package_path(...)
return fs.joinpath(vim.fn.fnamemodify(debug.getinfo(1, 'S').source:sub(2), ':p:h:h:h'), ...) return fs.joinpath(fn.fnamemodify(debug.getinfo(1, 'S').source:sub(2), ':p:h:h:h'), ...)
end end
---@param lang string ---@param lang string
@ -163,12 +154,18 @@ local function cc_err()
) .. '" are not executable.') ) .. '" are not executable.')
end end
--- @param x string
--- @return boolean
local function executable(x)
return fn.executable(x) == 1
end
--- @param logger Logger --- @param logger Logger
--- @param repo InstallInfo --- @param repo InstallInfo
--- @param compile_location string --- @param compile_location string
local function do_generate_from_grammar(logger, repo, compile_location) local function do_generate_from_grammar(logger, repo, compile_location)
if repo.generate_requires_npm then if repo.generate_requires_npm then
if vim.fn.executable('npm') ~= 1 then if not executable('npm') then
logger:error('NPM requires to be installed from grammar.js') logger:error('NPM requires to be installed from grammar.js')
end end
@ -182,7 +179,7 @@ local function do_generate_from_grammar(logger, repo, compile_location)
logger:info('Generating source files from grammar.js...') logger:info('Generating source files from grammar.js...')
local r = system({ local r = system({
vim.fn.exepath('tree-sitter'), fn.exepath('tree-sitter'),
'generate', 'generate',
'--abi', '--abi',
tostring(vim.treesitter.language_version), tostring(vim.treesitter.language_version),
@ -230,7 +227,7 @@ local function do_download_tar(logger, repo, project_name, cache_dir, revision,
end end
logger:debug('Creating temporary directory: ' .. temp_dir) logger:debug('Creating temporary directory: ' .. temp_dir)
--TODO(clason): use vim.fn.mkdir(temp_dir, 'p') in case stdpath('cache') is not created --TODO(clason): use fn.mkdir(temp_dir, 'p') in case stdpath('cache') is not created
local err = uv_mkdir(temp_dir, 493) local err = uv_mkdir(temp_dir, 493)
a.main() a.main()
if err then if err then
@ -323,14 +320,7 @@ end
---@param executables string[] ---@param executables string[]
---@return string? ---@return string?
function M.select_executable(executables) function M.select_executable(executables)
return vim.iter.filter( return vim.iter.filter(executable, executables)[1]
---@param c string
---@return boolean
function(c)
return vim.fn.executable(c) == 1
end,
executables
)[1]
end end
-- Returns the compiler arguments based on the compiler and OS -- Returns the compiler arguments based on the compiler and OS
@ -383,7 +373,7 @@ local function select_compiler_args(repo, compiler)
--- @param file string --- @param file string
--- @return boolean --- @return boolean
function(file) function(file)
local ext = vim.fn.fnamemodify(file, ':e') local ext = fn.fnamemodify(file, ':e')
return ext == 'cc' or ext == 'cpp' or ext == 'cxx' return ext == 'cc' or ext == 'cpp' or ext == 'cxx'
end, end,
repo.files repo.files
@ -408,9 +398,9 @@ end
---@param repo InstallInfo ---@param repo InstallInfo
---@return boolean ---@return boolean
local function can_download_tar(repo) local function can_download_tar(repo)
local can_use_tar = vim.fn.executable('tar') == 1 and vim.fn.executable('curl') == 1 local can_use_tar = executable('tar') and executable('curl')
local is_github = repo.url:find('github.com', 1, true) local is_github = repo.url:find('github.com', 1, true) ~= nil
local is_gitlab = repo.url:find('gitlab.com', 1, true) local is_gitlab = repo.url:find('gitlab.com', 1, true) ~= nil
return can_use_tar and (is_github or is_gitlab) and not iswin return can_use_tar and (is_github or is_gitlab) and not iswin
end end
@ -429,13 +419,13 @@ end
---@param lang string ---@param lang string
---@param cache_dir string ---@param cache_dir string
---@param install_dir string ---@param install_dir string
---@param force boolean ---@param force? boolean
---@param generate_from_grammar boolean ---@param generate_from_grammar? boolean
local function install_lang(lang, cache_dir, install_dir, force, generate_from_grammar) local function install_lang(lang, cache_dir, install_dir, force, generate_from_grammar)
if vim.list_contains(config.installed_parsers(), lang) then if vim.list_contains(config.installed_parsers(), lang) then
if not force then if not force then
local yesno = local yesno =
vim.fn.input(lang .. ' parser already available: would you like to reinstall ? y/n: ') fn.input(lang .. ' parser already available: would you like to reinstall ? y/n: ')
print('\n ') print('\n ')
if yesno:sub(1, 1) ~= 'y' then if yesno:sub(1, 1) ~= 'y' then
return return
@ -444,7 +434,6 @@ local function install_lang(lang, cache_dir, install_dir, force, generate_from_g
end end
local logger = log.new('install/' .. lang) local logger = log.new('install/' .. lang)
local err
local repo = get_parser_install_info(lang) local repo = get_parser_install_info(lang)
if repo then if repo then
@ -458,18 +447,18 @@ local function install_lang(lang, cache_dir, install_dir, force, generate_from_g
generate_from_grammar = repo.requires_generate_from_grammar or generate_from_grammar generate_from_grammar = repo.requires_generate_from_grammar or generate_from_grammar
if generate_from_grammar and vim.fn.executable('tree-sitter') ~= 1 then if generate_from_grammar and not executable('tree-sitter') then
logger:error('tree-sitter CLI not found: `tree-sitter` is not executable') logger:error('tree-sitter CLI not found: `tree-sitter` is not executable')
end end
if generate_from_grammar and vim.fn.executable('node') ~= 1 then if generate_from_grammar and not executable('node') then
logger:error('Node JS not found: `node` is not executable') logger:error('Node JS not found: `node` is not executable')
end end
local revision = get_target_revision(lang) local revision = get_target_revision(lang)
local maybe_local_path = fs.normalize(repo.url) local maybe_local_path = fs.normalize(repo.url)
local from_local_path = vim.fn.isdirectory(maybe_local_path) == 1 local from_local_path = fn.isdirectory(maybe_local_path) == 1
if from_local_path then if from_local_path then
repo.url = maybe_local_path repo.url = maybe_local_path
end end
@ -501,7 +490,7 @@ local function install_lang(lang, cache_dir, install_dir, force, generate_from_g
local parser_lib_name = fs.joinpath(install_dir, lang) .. '.so' local parser_lib_name = fs.joinpath(install_dir, lang) .. '.so'
err = uv_copyfile(fs.joinpath(compile_location, 'parser.so'), parser_lib_name) local err = uv_copyfile(fs.joinpath(compile_location, 'parser.so'), parser_lib_name)
a.main() a.main()
if err then if err then
logger:error(err) logger:error(err)
@ -518,7 +507,7 @@ local function install_lang(lang, cache_dir, install_dir, force, generate_from_g
local queries = fs.joinpath(config.get_install_dir('queries'), lang) local queries = fs.joinpath(config.get_install_dir('queries'), lang)
local queries_src = M.get_package_path('runtime', 'queries', lang) local queries_src = M.get_package_path('runtime', 'queries', lang)
uv_unlink(queries) uv_unlink(queries)
err = uv_symlink(queries_src, queries, { dir = true, junction = true }) local err = uv_symlink(queries_src, queries, { dir = true, junction = true })
a.main() a.main()
if err then if err then
logger:error(err) logger:error(err)
@ -539,14 +528,14 @@ end
--- ---
--- ---
--- @generic F: function --- @generic F: function
--- @param fn F Function to throttle --- @param f F Function to throttle
--- @return F throttled function. --- @return F throttled function.
local function throttle_by_id(fn) local function throttle_by_id(f)
local scheduled = {} --- @type table<any,boolean> local scheduled = {} --- @type table<any,boolean>
local running = {} --- @type table<any,boolean> local running = {} --- @type table<any,boolean>
return function(id, ...) return function(id, ...)
if scheduled[id] then if scheduled[id] then
-- If fn is already scheduled, then drop -- If f is already scheduled, then drop
return return
end end
if not running[id] then if not running[id] then
@ -558,7 +547,7 @@ local function throttle_by_id(fn)
while scheduled[id] do while scheduled[id] do
scheduled[id] = nil scheduled[id] = nil
running[id] = true running[id] = true
fn(id, ...) f(id, ...)
running[id] = nil running[id] = nil
end end
end end
@ -568,9 +557,9 @@ end
local install_lang_throttled = throttle_by_id(install_lang) local install_lang_throttled = throttle_by_id(install_lang)
---@class InstallOptions ---@class InstallOptions
---@field force boolean ---@field force? boolean
---@field generate_from_grammar boolean ---@field generate_from_grammar? boolean
---@field skip table ---@field skip? table
--- Install a parser --- Install a parser
--- @param languages? string[]|string --- @param languages? string[]|string
@ -582,12 +571,12 @@ local function install(languages, options, _callback)
local generate_from_grammar = options.generate_from_grammar local generate_from_grammar = options.generate_from_grammar
local skip = options.skip local skip = options.skip
if vim.fn.executable('git') == 0 then if not executable('git') then
log.error('Git is required on your system to run this command') log.error('Git is required on your system to run this command')
return return
end end
local cache_dir = vim.fs.normalize(vim.fn.stdpath('cache')) local cache_dir = vim.fs.normalize(fn.stdpath('cache'))
local install_dir = config.get_install_dir('parser') local install_dir = config.get_install_dir('parser')
if not languages or type(languages) == 'string' then if not languages or type(languages) == 'string' then