From fd6d09fd1f52d0c7d307c7f10b95509400d859c9 Mon Sep 17 00:00:00 2001 From: Markus Koller Date: Thu, 10 Sep 2020 14:18:15 +0200 Subject: Find package path based on the Lua source file This is more robust compared to the previous method where we walked up the tree and matched on the directory name, which also required that the repository was cloned in a directory named `nvim-treesitter`. --- lua/nvim-treesitter/install.lua | 10 ++-------- lua/nvim-treesitter/utils.lua | 11 ++++------- 2 files changed, 6 insertions(+), 15 deletions(-) (limited to 'lua') diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index 3b75362c1..61b1a9f92 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -200,9 +200,7 @@ local function install(with_sync, ask_reinstall) 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 = utils.get_package_path() local cache_folder, err = utils.get_cache_dir() if err then return api.nvim_err_writeln(err) end @@ -267,11 +265,7 @@ function M.uninstall(lang) M.uninstall(lang) end elseif lang then - local package_path, err = utils.get_package_path() - if err then - print(err) - return - end + local package_path = utils.get_package_path() local parser_lib = package_path..path_sep.."parser"..path_sep..lang..".so" local command_list = { diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index c080c2a91..7a5fbe5e9 100644 --- a/lua/nvim-treesitter/utils.lua +++ b/lua/nvim-treesitter/utils.lua @@ -18,14 +18,11 @@ function M.setup_commands(mod, commands) end function M.get_package_path() - for _, path in pairs(api.nvim_list_runtime_paths()) do - local last_segment = vim.fn.fnamemodify(path, ":p:h:t") - if last_segment == "nvim-treesitter" then - return path - end - end + -- Path to this source file, removing the leading '@' + local source = string.sub(debug.getinfo(1, 'S').source, 2) - return nil, 'Plugin runtime path not found.' + -- Path to the package root + return fn.fnamemodify(source, ":p:h:h:h") end function M.get_cache_dir() -- cgit v1.2.3-70-g09d2