summaryrefslogtreecommitdiff
path: root/lua/nvim-treesitter/utils.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/nvim-treesitter/utils.lua')
-rw-r--r--lua/nvim-treesitter/utils.lua100
1 files changed, 40 insertions, 60 deletions
diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua
index b64f5b1d..873c96ea 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