Merge pull request #34 from theHamsta/ensure_installed

Add function 'nvim-treesitter/install'.ensure_installed
This commit is contained in:
Kiyan Yazdani 2020-05-01 12:17:25 +02:00 committed by GitHub
commit 1d6ad9b560
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View file

@ -325,6 +325,9 @@ function M.setup(user_data)
if config[mod].keymaps and type(data.keymaps) == 'table' then
config[mod].keymaps = data.keymaps
end
if mod == 'ensure_installed' then
require'nvim-treesitter/install'.ensure_installed(data)
end
end
end
end

View file

@ -3,6 +3,7 @@ local fn = vim.fn
local luv = vim.loop
local configs = require'nvim-treesitter/configs'
local parsers = configs.get_parser_configs()
local has_parser = require'nvim-treesitter/parsers'.has_parser
local M = {}
@ -39,9 +40,11 @@ local function iter_cmd(cmd_list, i, ft)
local attr = cmd_list[i]
if attr.info then print(attr.info) end
local handle
handle = luv.spawn(attr.cmd, attr.opts, vim.schedule_wrap(function(code)
handle:close()
if code ~= 0 then return api.nvim_err_writeln(attr.err) end
if code ~= 0 then return api.nvim_err_writeln(attr.err) end
iter_cmd(cmd_list, i + 1, ft)
end))
end
@ -143,6 +146,24 @@ local function install(ft)
run_install(cache_folder, package_path, ft, install_info)
end
M.ensure_installed = function(languages)
if type(languages) == 'string' then
if languages == 'all' then
languages = configs.available_parsers()
else
languages = {languages}
end
end
for _, ft in ipairs(languages) do
if not has_parser(ft) then
install(ft)
end
end
end
M.commands = {
TSInstall = {
run = install,