aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorkiyan42 <yazdani.kiyan@protonmail.com>2020-06-29 16:07:16 +0200
committerThomas Vigouroux <39092278+vigoux@users.noreply.github.com>2020-06-30 08:32:49 +0200
commitce119de2e3a85ed7f0b081c6733ae167c2fc5599 (patch)
tree2cd70208b41ef9af9f62d5f534dd7f0e963bb960 /lua
parentJava highlights: Add operators ":" "?" (diff)
downloadnvim-treesitter-ce119de2e3a85ed7f0b081c6733ae167c2fc5599.tar
nvim-treesitter-ce119de2e3a85ed7f0b081c6733ae167c2fc5599.tar.gz
nvim-treesitter-ce119de2e3a85ed7f0b081c6733ae167c2fc5599.tar.bz2
nvim-treesitter-ce119de2e3a85ed7f0b081c6733ae167c2fc5599.tar.lz
nvim-treesitter-ce119de2e3a85ed7f0b081c6733ae167c2fc5599.tar.xz
nvim-treesitter-ce119de2e3a85ed7f0b081c6733ae167c2fc5599.tar.zst
nvim-treesitter-ce119de2e3a85ed7f0b081c6733ae167c2fc5599.zip
update installer with sync and some fixes
- add sync method for installing using `system` - remove `descriptions` in command configs - use install(lang) in ensure_installed and make it compatible
Diffstat (limited to 'lua')
-rw-r--r--lua/nvim-treesitter/configs.lua12
-rw-r--r--lua/nvim-treesitter/info.lua6
-rw-r--r--lua/nvim-treesitter/install.lua139
3 files changed, 92 insertions, 65 deletions
diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua
index da80c818d..c31ca4f29 100644
--- a/lua/nvim-treesitter/configs.lua
+++ b/lua/nvim-treesitter/configs.lua
@@ -184,32 +184,28 @@ M.commands = {
args = {
"-nargs=1",
"-complete=custom,v:lua.ts_available_modules"
- },
- description = '`:TSBufEnable module_name` enable a specified module on the current buffer'
+ }
},
TSBufDisable = {
run = disable_module,
args = {
"-nargs=1",
"-complete=custom,v:lua.ts_available_modules"
- },
- description = '`:TSBufDisable module_name` disable a specified module on the current buffer'
+ }
},
TSEnableAll = {
run = enable_all,
args = {
"-nargs=+",
"-complete=custom,v:lua.ts_available_modules"
- },
- description = '`:TSEnableAll module_name (filetype)` enables a specified module on all buffers. If filetype is specified, enable only for specified filetype'
+ }
},
TSDisableAll = {
run = disable_all,
args = {
"-nargs=+",
"-complete=custom,v:lua.ts_available_modules"
- },
- description = '`:TSDisableAll module_name (filetype)` disables a specified module on all buffers. If filetype is specified, disable only for specified filetype'
+ }
},
}
diff --git a/lua/nvim-treesitter/info.lua b/lua/nvim-treesitter/info.lua
index 0c99f3ff2..98b759518 100644
--- a/lua/nvim-treesitter/info.lua
+++ b/lua/nvim-treesitter/info.lua
@@ -81,16 +81,14 @@ M.commands = {
run = install_info,
args = {
"-nargs=0",
- },
- description = '`:TSInstallInfo` print installation state for every filetype'
+ }
},
TSModuleInfo = {
run = module_info,
args = {
"-nargs=?",
"-complete=custom,v:lua.ts_available_modules"
- },
- description = '`:TSModuleInfo` print module state for every filetype, if module is specified, only for current module'
+ }
}
}
diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua
index 686688bf7..e91987120 100644
--- a/lua/nvim-treesitter/install.lua
+++ b/lua/nvim-treesitter/install.lua
@@ -22,7 +22,42 @@ local function iter_cmd(cmd_list, i, lang)
end))
end
-local function run_install(cache_folder, package_path, lang, repo)
+local function get_command(cmd)
+ local ret = ''
+ if cmd.opts and cmd.opts.cwd then
+ ret = string.format('cd %s;\n', cmd.opts.cwd)
+ end
+
+ ret = string.format('%s%s ', ret, cmd.cmd)
+
+ local options = ""
+ if cmd.opts and cmd.opts.args then
+ for _, opt in ipairs(cmd.opts.args) do
+ options = string.format("%s %s", options, opt)
+ end
+ end
+
+ return string.format('%s%s', ret, options)
+end
+
+local function iter_cmd_sync(cmd_list)
+ for _, cmd in ipairs(cmd_list) do
+ if cmd.info then
+ print(cmd.info)
+ end
+
+ vim.fn.system(get_command(cmd))
+ if vim.v.shell_error ~= 0 then
+ api.nvim_err_writeln(cmd.err)
+ return false
+ end
+
+ end
+
+ return true
+end
+
+local function run_install(cache_folder, package_path, lang, repo, with_sync)
local project_name = 'tree-sitter-'..lang
local project_repo = cache_folder..'/'..project_name
-- compile_location only needed for typescript installs.
@@ -33,7 +68,7 @@ local function run_install(cache_folder, package_path, lang, repo)
cmd = 'rm',
opts = {
args = { '-rf', project_repo },
- }
+ },
},
{
cmd = 'git',
@@ -76,83 +111,81 @@ local function run_install(cache_folder, package_path, lang, repo)
}
}
- iter_cmd(command_list, 1, lang)
+ if with_sync then
+ if iter_cmd_sync(command_list, lang) == true then
+ print('Treesitter parser for '..lang..' has been installed')
+ end
+ else
+ iter_cmd(command_list, 1, lang)
+ end
end
-- TODO(kyazdani): this should work on windows too
-local function install(...)
- if fn.has('win32') == 1 then
- return api.nvim_err_writeln('This command is not available on windows at the moment.')
- end
+local function install(with_sync)
+ return function (...)
+ if fn.has('win32') == 1 then
+ return api.nvim_err_writeln('This command is not available on windows at the moment.')
+ end
- if fn.executable('git') == 0 then
- return api.nvim_err_writeln('Git is required on your system to run this command')
- end
+ if fn.executable('git') == 0 then
+ return api.nvim_err_writeln('Git is required on your system to run this command')
+ end
- local package_path, err = utils.get_package_path()
- if err then return api.nvim_err_writeln(err) end
+ local package_path, err = utils.get_package_path()
+ if err then return api.nvim_err_writeln(err) end
- local cache_folder, err = utils.get_cache_dir()
- if err then return api.nvim_err_writeln(err) end
+ local cache_folder, err = utils.get_cache_dir()
+ if err then return api.nvim_err_writeln(err) end
- local languages = { ... }
- local check_installed = true
- if ... == 'all' then
- languages = parsers.available_parsers()
- check_installed = false
- end
+ local languages = vim.tbl_flatten({...})
+ local ask_reinstall = true
+ if ... == 'all' then
+ languages = parsers.available_parsers()
+ ask_reinstall = false
+ end
- for _, lang in ipairs(languages) do
- if check_installed then
+ for _, lang in ipairs(languages) do
if #api.nvim_get_runtime_file('parser/'..lang..'.so', false) > 0 then
+ if not ask_reinstall then goto continue end
+
local yesno = fn.input(lang .. ' parser already available: would you like to reinstall ? y/n: ')
print('\n ') -- mandatory to avoid messing up command line
if not string.match(yesno, '^y.*') then goto continue end
end
- end
-
- local parser_config = parsers.get_parser_configs()[lang]
- if not parser_config then
- return api.nvim_err_writeln('Parser not available for language '..lang)
- end
-
- local install_info = parser_config.install_info
- vim.validate {
- url={ install_info.url, 'string' },
- files={ install_info.files, 'table' }
- }
-
- run_install(cache_folder, package_path, lang, install_info)
- ::continue::
- end
-end
+ local parser_config = parsers.get_parser_configs()[lang]
+ if not parser_config then
+ return api.nvim_err_writeln('Parser not available for language '..lang)
+ end
-M.ensure_installed = function(languages)
- if type(languages) == 'string' then
- if languages == 'all' then
- languages = parsers.available_parsers()
- else
- languages = {languages}
- end
- end
+ local install_info = parser_config.install_info
+ vim.validate {
+ url={ install_info.url, 'string' },
+ files={ install_info.files, 'table' }
+ }
- for _, lang in ipairs(languages) do
- if not parsers.has_parser(lang) then
- install(lang)
+ run_install(cache_folder, package_path, lang, install_info, with_sync)
+ ::continue::
end
end
end
+M.ensure_installed = install(false)
M.commands = {
TSInstall = {
- run = install,
+ run = install(false),
args = {
"-nargs=+",
"-complete=custom,v:lua.ts_installable_parsers"
- },
- description = '`:TSInstall {lang}` installs a parser under nvim-treesitter/parser/{lang}.so'
+ }
+ },
+ TSInstallSync = {
+ run = install(true),
+ args = {
+ "-nargs=+",
+ "-complete=custom,v:lua.ts_installable_parsers"
+ }
}
}