From 901ffe1a363f21c168eacf011dce4f905fd26d5f Mon Sep 17 00:00:00 2001 From: Duncan McDougall Date: Mon, 20 Jun 2022 21:50:07 +0100 Subject: Add support for custom parser install locations #2959 (#3031) --- lua/nvim-treesitter/configs.lua | 45 +++++++++++ lua/nvim-treesitter/install.lua | 19 ++--- lua/nvim-treesitter/shell_command_selectors.lua | 6 +- lua/nvim-treesitter/utils.lua | 100 ++++++++++-------------- 4 files changed, 95 insertions(+), 75 deletions(-) (limited to 'lua') diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index 5ae702a49..ee01f3ede 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -1,4 +1,5 @@ local api = vim.api +local luv = vim.loop local queries = require "nvim-treesitter.query" local ts_query = require "vim.treesitter.query" @@ -14,6 +15,7 @@ local config = { ensure_installed = {}, ignore_install = {}, update_strategy = "lockfile", + parser_install_dir = nil, } -- List of modules that need to be setup on initialization. local queued_modules_defs = {} @@ -379,6 +381,10 @@ end function M.setup(user_data) config.modules = vim.tbl_deep_extend("force", config.modules, user_data) config.ignore_install = user_data.ignore_install or {} + config.parser_install_dir = user_data.parser_install_dir or nil + if config.parser_install_dir then + config.parser_install_dir = vim.fn.expand(config.parser_install_dir, ":p") + end local ensure_installed = user_data.ensure_installed or {} if #ensure_installed > 0 then @@ -528,6 +534,45 @@ function M.init() end end +-- If parser_install_dir is not nil is used or created. +-- If parser_install_dir is nil try the package dir of the nvim-treesitter +-- plugin first, followed by the "site" dir from "runtimepath". "site" dir will +-- be created if it doesn't exist. Using only the package dir won't work when +-- the plugin is installed with Nix, since the "/nix/store" is read-only. +function M.get_parser_install_dir(folder_name) + folder_name = folder_name or "parser" + + if config.parser_install_dir then + local parser_dir = utils.join_path(config.parser_install_dir, folder_name) + return utils.create_or_resue_writable_dir( + parser_dir, + utils.join_space("Could not create parser dir '", parser_dir, "': "), + utils.join_space("Parser dir '", parser_dir, "' should be read/write.") + ) + end + + local package_path = utils.get_package_path() + local package_path_parser_dir = utils.join_path(package_path, folder_name) + + -- If package_path is read/write, use that + if luv.fs_access(package_path_parser_dir, "RW") then + return package_path_parser_dir + end + + local site_dir = utils.get_site_dir() + local parser_dir = utils.join_path(site_dir, folder_name) + + return utils.create_or_resue_writable_dir( + parser_dir, + nil, + utils.join_space("Invalid rights,", package_path, "or", parser_dir, "should be read/write") + ) +end + +function M.get_parser_info_dir(parser_install_dir) + return M.get_parser_install_dir "parser-info" +end + function M.get_update_strategy() return config.update_strategy end diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index 10f08dace..822e00e69 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -80,7 +80,7 @@ local function get_revision(lang) end local function get_installed_revision(lang) - local lang_file = utils.join_path(utils.get_parser_info_dir(), lang .. ".revision") + local lang_file = utils.join_path(configs.get_parser_info_dir(), lang .. ".revision") if vim.fn.filereadable(lang_file) == 1 then return vim.fn.readfile(lang_file)[1] end @@ -243,9 +243,9 @@ local function run_install(cache_folder, install_folder, lang, repo, with_sync, compile_location = repo.url else local repo_location = string.gsub(repo.location or project_name, "/", path_sep) - compile_location = cache_folder .. path_sep .. repo_location + compile_location = utils.join_path(cache_folder, repo_location) end - local parser_lib_name = install_folder .. path_sep .. lang .. ".so" + local parser_lib_name = utils.join_path(install_folder, lang) .. ".so" generate_from_grammar = repo.requires_generate_from_grammar or generate_from_grammar @@ -329,7 +329,7 @@ local function run_install(cache_folder, install_folder, lang, repo, with_sync, shell.select_mv_cmd("parser.so", parser_lib_name, compile_location), { cmd = function() - vim.fn.writefile({ revision or "" }, utils.join_path(utils.get_parser_info_dir(), lang .. ".revision")) + vim.fn.writefile({ revision or "" }, utils.join_path(configs.get_parser_info_dir(), lang .. ".revision")) end, }, { -- auto-attach modules after installation @@ -392,7 +392,7 @@ local function install(options) return api.nvim_err_writeln(err) end - local install_folder, err = utils.get_parser_install_dir() + local install_folder, err = configs.get_parser_install_dir() if err then return api.nvim_err_writeln(err) end @@ -459,11 +459,6 @@ function M.update(options) end function M.uninstall(...) - local path_sep = "/" - if fn.has "win32" == 1 then - path_sep = "\\" - end - if vim.tbl_contains({ "all" }, ...) then reset_progress_counter() local installed = info.installed_parsers() @@ -473,12 +468,12 @@ function M.uninstall(...) elseif ... then local languages = vim.tbl_flatten { ... } for _, lang in ipairs(languages) do - local install_dir, err = utils.get_parser_install_dir() + local install_dir, err = configs.get_parser_install_dir() if err then return api.nvim_err_writeln(err) end - local parser_lib = install_dir .. path_sep .. lang .. ".so" + local parser_lib = utils.join_path(install_dir, lang) .. ".so" local command_list = { shell.select_rm_file_cmd(parser_lib, "Uninstalling parser for " .. lang), diff --git a/lua/nvim-treesitter/shell_command_selectors.lua b/lua/nvim-treesitter/shell_command_selectors.lua index 0983d850b..522ac727a 100644 --- a/lua/nvim-treesitter/shell_command_selectors.lua +++ b/lua/nvim-treesitter/shell_command_selectors.lua @@ -187,7 +187,7 @@ function M.select_download_commands(repo, project_name, cache_folder, revision, M.select_install_rm_cmd(cache_folder, project_name .. "-tmp"), { cmd = "curl", - info = "Downloading...", + info = "Downloading " .. project_name .. "...", err = "Error during download, please verify your internet connection", opts = { args = { @@ -204,7 +204,7 @@ function M.select_download_commands(repo, project_name, cache_folder, revision, M.select_mkdir_cmd(project_name .. "-tmp", cache_folder, "Creating temporary directory"), { cmd = "tar", - info = "Extracting...", + info = "Extracting " .. project_name .. "...", err = "Error during tarball extraction.", opts = { args = { @@ -231,7 +231,7 @@ function M.select_download_commands(repo, project_name, cache_folder, revision, return { { cmd = "git", - info = "Downloading...", + info = "Downloading " .. project_name .. "...", err = clone_error, opts = { args = { diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index b64f5b1d9..873c96eae 100644 --- a/lua/nvim-treesitter/utils.lua +++ b/lua/nvim-treesitter/utils.lua @@ -10,6 +10,27 @@ function M.notify(msg, log_level, opts) vim.notify(msg, log_level, vim.tbl_extend("force", default_opts, opts or {})) end +-- Returns the system specific path seperator. +function M.get_path_sep() + return fn.has "win32" == 1 and "\\" or "/" +end + +-- Returns a function that joins the given arguments with separator. Arguments +-- can't be nil. Example: +--[[ +print(M.generate_join(" ")("foo", "bar")) +--]] +-- prints "foo bar" +function M.generate_join(separator) + return function(...) + return table.concat({ ... }, separator) + end +end + +M.join_path = M.generate_join(M.get_path_sep()) + +M.join_space = M.generate_join " " + --- Define user defined vim command which calls nvim-treesitter module function --- - If module name is 'mod', it should be defined in hierarchy 'nvim-treesitter.mod' --- - A table with name 'commands' should be defined in 'mod' which needs to be passed as @@ -65,25 +86,27 @@ function M.setup_commands(mod, commands) end end -function M.get_path_sep() - return fn.has "win32" == 1 and "\\" or "/" -end +function M.create_or_resue_writable_dir(dir, create_err, writeable_err) + create_err = create_err or M.join_space("Could not create dir '", dir, "': ") + writeable_err = writeable_err or M.join_space("Invalid rights, '", dir, "' should be read/write") + -- Try creating and using parser_dir if it doesn't exist + if not luv.fs_stat(dir) then + local ok, error = pcall(vim.fn.mkdir, dir, "p", "0755") + if not ok then + return nil, M.join_space(create_err, error) + end --- Returns a function that joins the given arguments with separator. Arguments --- can't be nil. Example: ---[[ -print(M.generate_join(" ")("foo", "bar")) ---]] --- prints "foo bar" -function M.generate_join(separator) - return function(...) - return table.concat({ ... }, separator) + return dir end -end -M.join_path = M.generate_join(M.get_path_sep()) + -- parser_dir exists, use it if it's read/write + if luv.fs_access(dir, "RW") then + return dir + end -local join_space = M.generate_join " " + -- parser_dir exists but isn't read/write, give up + return nil, M.join_space(writeable_err, dir, "'") +end function M.get_package_path() -- Path to this source file, removing the leading '@' @@ -102,56 +125,13 @@ function M.get_cache_dir() return "/tmp" end - return nil, join_space("Invalid cache rights,", fn.stdpath "data", "or /tmp should be read/write") + return nil, M.join_space("Invalid cache rights,", fn.stdpath "data", "or /tmp should be read/write") end -- Returns $XDG_DATA_HOME/nvim/site, but could use any directory that is in -- runtimepath function M.get_site_dir() - local path_sep = M.get_path_sep() - return M.join_path(fn.stdpath "data", path_sep, "site") -end - --- Try the package dir of the nvim-treesitter plugin first, followed by the --- "site" dir from "runtimepath". "site" dir will be created if it doesn't --- exist. Using only the package dir won't work when the plugin is installed --- with Nix, since the "/nix/store" is read-only. -function M.get_parser_install_dir(folder_name) - folder_name = folder_name or "parser" - local package_path = M.get_package_path() - local package_path_parser_dir = M.join_path(package_path, folder_name) - - -- If package_path is read/write, use that - if luv.fs_access(package_path_parser_dir, "RW") then - return package_path_parser_dir - end - - local site_dir = M.get_site_dir() - local path_sep = M.get_path_sep() - local parser_dir = M.join_path(site_dir, path_sep, folder_name) - - -- Try creating and using parser_dir if it doesn't exist - if not luv.fs_stat(parser_dir) then - local ok, error = pcall(vim.fn.mkdir, parser_dir, "p", "0755") - if not ok then - return nil, join_space("Couldn't create parser dir", parser_dir, ":", error) - end - - return parser_dir - end - - -- parser_dir exists, use it if it's read/write - if luv.fs_access(parser_dir, "RW") then - return parser_dir - end - - -- package_path isn't read/write, parser_dir exists but isn't read/write - -- either, give up - return nil, join_space("Invalid cache rights,", package_path, "or", parser_dir, "should be read/write") -end - -function M.get_parser_info_dir() - return M.get_parser_install_dir "parser-info" + return M.join_path(fn.stdpath "data", "site") end -- Gets a property at path -- cgit v1.2.3-70-g09d2