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 /lua/nvim-treesitter/configs.lua | |
| parent | 37b9a2971f749ab6d5ac9c8792f6e133fed027aa (diff) | |
Add support for custom parser install locations #2959 (#3031)
Diffstat (limited to 'lua/nvim-treesitter/configs.lua')
| -rw-r--r-- | lua/nvim-treesitter/configs.lua | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index 5ae702a4..ee01f3ed 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 |
