diff options
| author | Duncan McDougall <dncn.mcdougall@gmail.com> | 2022-06-20 21:50:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-20 22:50:07 +0200 |
| commit | 901ffe1a363f21c168eacf011dce4f905fd26d5f (patch) | |
| tree | 49f905970eff9ce1ebefa797798909ea1229cde9 | |
| parent | Update lockfile.json (diff) | |
| download | nvim-treesitter-901ffe1a363f21c168eacf011dce4f905fd26d5f.tar nvim-treesitter-901ffe1a363f21c168eacf011dce4f905fd26d5f.tar.gz nvim-treesitter-901ffe1a363f21c168eacf011dce4f905fd26d5f.tar.bz2 nvim-treesitter-901ffe1a363f21c168eacf011dce4f905fd26d5f.tar.lz nvim-treesitter-901ffe1a363f21c168eacf011dce4f905fd26d5f.tar.xz nvim-treesitter-901ffe1a363f21c168eacf011dce4f905fd26d5f.tar.zst nvim-treesitter-901ffe1a363f21c168eacf011dce4f905fd26d5f.zip | |
Add support for custom parser install locations #2959 (#3031)
| -rw-r--r-- | README.md | 32 | ||||
| -rw-r--r-- | doc/nvim-treesitter.txt | 7 | ||||
| -rw-r--r-- | lua/nvim-treesitter/configs.lua | 45 | ||||
| -rw-r--r-- | lua/nvim-treesitter/install.lua | 19 | ||||
| -rw-r--r-- | lua/nvim-treesitter/shell_command_selectors.lua | 6 | ||||
| -rw-r--r-- | lua/nvim-treesitter/utils.lua | 100 | ||||
| -rw-r--r-- | tests/query/injection_spec.lua | 4 |
7 files changed, 136 insertions, 77 deletions
@@ -357,6 +357,38 @@ This will respect your `foldminlines` and `foldnestmax` settings. # Advanced setup +## Changing the parser install directory + +If you want to install the parsers to a custom directory you can specify this +directory with `parser_install_dir` option in that is passed to `setup`. +`nvim-treesitter` will then install the parser files into this directory. + +This directory must be writeable and must be explicitly added to the +`runtimepath`. For example: + +``` lua + require'nvim-treesitter.configs'.setup { + parser_install_dir = "/some/path/to/store/parsers", + + ... + + } + vim.opt.runtimepath:append("/some/path/to/store/parsers") +``` + +If this option is not included in the setup options, or is explicitly set to +`nil` then the default install directories will be used. If this value is set +the default directories will be ignored. + +Bear in mind that any parser installed into a parser folder on the runtime path +will still be considered installed. (For example if +"~/.local/share/nvim/site/parser/c.so" exists then the "c" parser will be +considered installed, even though it is not in `parser_install_dir`) + +The default paths are: +1. first the package folder. Where `nvim-treesitter` is installed. +2. second the site directory. This is the "site" subdirectory of `stdpath("data")`. + ## Adding parsers If you have a parser that is not on the list of supported languages (either as a repository on Github or in a local directory), you can add it manually for use by `nvim-treesitter` as follows: diff --git a/doc/nvim-treesitter.txt b/doc/nvim-treesitter.txt index 52553fb0e..e870ab352 100644 --- a/doc/nvim-treesitter.txt +++ b/doc/nvim-treesitter.txt @@ -39,6 +39,12 @@ To enable supported features, put this in your `init.lua` file: > require'nvim-treesitter.configs'.setup { + -- A directory to install the parsers into. + -- If this is excluded or nil parsers are installed + -- to either the package dir, or the "site" dir. + -- If a custom path is used (not nil) it must be added to the runtimepath. + parser_install_dir = "/some/path/to/store/parsers", + -- A list of parser names, or "all" ensure_installed = { "c", "lua", "rust" }, @@ -62,6 +68,7 @@ To enable supported features, put this in your `init.lua` file: additional_vim_regex_highlighting = false, }, } + vim.opt.runtimepath:append("/some/path/to/store/parsers") < See |nvim-treesitter-modules| for a list of all available modules and its options. 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 diff --git a/tests/query/injection_spec.lua b/tests/query/injection_spec.lua index 7a98dd275..b10b7450f 100644 --- a/tests/query/injection_spec.lua +++ b/tests/query/injection_spec.lua @@ -1,6 +1,6 @@ require "nvim-treesitter.highlight" -- yes, this is necessary to set the hlmap local highlighter = require "vim.treesitter.highlighter" -local utils = require "nvim-treesitter.utils" +local configs = require "nvim-treesitter.configs" local ts_utils = require "nvim-treesitter.ts_utils" local parsers = require "nvim-treesitter.parsers" @@ -16,7 +16,7 @@ local function check_assertions(file) ) local assertions = vim.fn.json_decode( vim.fn.system( - "highlight-assertions -p '" .. utils.get_parser_install_dir() .. "/" .. lang .. ".so'" .. " -s '" .. file .. "'" + "highlight-assertions -p '" .. configs.get_parser_install_dir() .. "/" .. lang .. ".so'" .. " -s '" .. file .. "'" ) ) local parser = parsers.get_parser(buf, lang) |
