2020-04-20 09:24:48 +02:00
|
|
|
local api = vim.api
|
2020-05-08 11:22:59 +02:00
|
|
|
local fn = vim.fn
|
|
|
|
|
local luv = vim.loop
|
2020-04-20 09:24:48 +02:00
|
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
|
2021-10-22 16:25:18 -05:00
|
|
|
-- Wrapper around vim.notify with common options set.
|
|
|
|
|
function M.notify(msg, log_level, opts)
|
|
|
|
|
local default_opts = { title = "nvim-treesitter" }
|
|
|
|
|
vim.notify(msg, log_level, vim.tbl_extend("force", default_opts, opts or {}))
|
|
|
|
|
end
|
|
|
|
|
|
2022-06-20 21:50:07 +01:00
|
|
|
-- 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 " "
|
|
|
|
|
|
2022-02-13 13:26:43 +05:30
|
|
|
--- 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
|
|
|
|
|
--- the commands param of this function
|
|
|
|
|
---
|
|
|
|
|
---@param mod string, Name of the module that resides in the heirarchy - nvim-treesitter.module
|
|
|
|
|
---@param commands table, Command list for the module
|
2022-02-13 13:30:50 +05:30
|
|
|
--- - {command_name} Name of the vim user defined command, Keys:
|
2022-02-13 13:26:43 +05:30
|
|
|
--- - {run}: (function) callback function that needs to be executed
|
|
|
|
|
--- - {f_args}: (string, default <f-args>)
|
|
|
|
|
--- - type of arguments that needs to be passed to the vim command
|
|
|
|
|
--- - {args}: (string, optional)
|
|
|
|
|
--- - vim command attributes
|
|
|
|
|
---
|
|
|
|
|
---Example:
|
|
|
|
|
--- If module is nvim-treesitter.custom_mod
|
|
|
|
|
--- <pre>
|
|
|
|
|
--- M.commands = {
|
|
|
|
|
--- custom_command = {
|
|
|
|
|
--- run = M.module_function,
|
|
|
|
|
--- f_args = "<f-args>",
|
|
|
|
|
--- args = {
|
|
|
|
|
--- "-range"
|
|
|
|
|
--- }
|
|
|
|
|
--- }
|
|
|
|
|
--- }
|
|
|
|
|
---
|
|
|
|
|
--- utils.setup_commands("custom_mod", require("nvim-treesitter.custom_mod").commands)
|
|
|
|
|
--- </pre>
|
|
|
|
|
---
|
|
|
|
|
--- Will generate command :
|
|
|
|
|
--- <pre>
|
|
|
|
|
--- command! -range custom_command \
|
|
|
|
|
--- lua require'nvim-treesitter.custom_mod'.commands.custom_command['run<bang>'](<f-args>)
|
|
|
|
|
--- </pre>
|
2020-04-22 11:13:05 +02:00
|
|
|
function M.setup_commands(mod, commands)
|
|
|
|
|
for command_name, def in pairs(commands) do
|
2022-01-05 19:31:06 +05:30
|
|
|
local f_args = def.f_args or "<f-args>"
|
2022-07-07 08:50:10 +02:00
|
|
|
local call_fn =
|
|
|
|
|
string.format("lua require'nvim-treesitter.%s'.commands.%s['run<bang>'](%s)", mod, command_name, f_args)
|
2021-07-04 16:12:17 -05:00
|
|
|
local parts = vim.tbl_flatten {
|
2021-02-22 23:01:12 +01:00
|
|
|
"command!",
|
2022-05-14 09:16:32 +02:00
|
|
|
"-bar",
|
2021-02-22 23:01:12 +01:00
|
|
|
def.args,
|
|
|
|
|
command_name,
|
|
|
|
|
call_fn,
|
2021-07-04 16:12:17 -05:00
|
|
|
}
|
2020-04-22 11:13:05 +02:00
|
|
|
api.nvim_command(table.concat(parts, " "))
|
|
|
|
|
end
|
2020-04-23 12:15:03 +02:00
|
|
|
end
|
|
|
|
|
|
2022-06-20 21:50:07 +01:00
|
|
|
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
|
2020-09-18 09:44:07 +02:00
|
|
|
|
2022-06-20 21:50:07 +01:00
|
|
|
return dir
|
2020-09-18 09:44:07 +02:00
|
|
|
end
|
|
|
|
|
|
2022-06-20 21:50:07 +01:00
|
|
|
-- parser_dir exists, use it if it's read/write
|
|
|
|
|
if luv.fs_access(dir, "RW") then
|
|
|
|
|
return dir
|
|
|
|
|
end
|
2020-09-18 09:44:07 +02:00
|
|
|
|
2022-06-20 21:50:07 +01:00
|
|
|
-- parser_dir exists but isn't read/write, give up
|
|
|
|
|
return nil, M.join_space(writeable_err, dir, "'")
|
|
|
|
|
end
|
2020-09-18 09:44:07 +02:00
|
|
|
|
2020-05-08 11:22:59 +02:00
|
|
|
function M.get_package_path()
|
2020-09-10 14:18:15 +02:00
|
|
|
-- Path to this source file, removing the leading '@'
|
2021-07-04 16:12:17 -05:00
|
|
|
local source = string.sub(debug.getinfo(1, "S").source, 2)
|
2020-04-23 08:43:35 +02:00
|
|
|
|
2020-09-10 14:18:15 +02:00
|
|
|
-- Path to the package root
|
|
|
|
|
return fn.fnamemodify(source, ":p:h:h:h")
|
2020-04-22 11:13:05 +02:00
|
|
|
end
|
|
|
|
|
|
2020-05-08 11:22:59 +02:00
|
|
|
function M.get_cache_dir()
|
2021-07-04 16:12:17 -05:00
|
|
|
local cache_dir = fn.stdpath "data"
|
2020-05-01 16:25:06 +02:00
|
|
|
|
2021-07-04 16:12:17 -05:00
|
|
|
if luv.fs_access(cache_dir, "RW") then
|
2020-08-25 20:00:47 +02:00
|
|
|
return cache_dir
|
2021-07-04 16:12:17 -05:00
|
|
|
elseif luv.fs_access("/tmp", "RW") then
|
|
|
|
|
return "/tmp"
|
2020-05-01 16:25:06 +02:00
|
|
|
end
|
2020-05-08 11:22:59 +02:00
|
|
|
|
2022-06-20 21:50:07 +01:00
|
|
|
return nil, M.join_space("Invalid cache rights,", fn.stdpath "data", "or /tmp should be read/write")
|
2020-09-18 09:44:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Returns $XDG_DATA_HOME/nvim/site, but could use any directory that is in
|
|
|
|
|
-- runtimepath
|
|
|
|
|
function M.get_site_dir()
|
2022-06-20 21:50:07 +01:00
|
|
|
return M.join_path(fn.stdpath "data", "site")
|
2020-11-21 18:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
2020-06-29 09:58:51 -05:00
|
|
|
-- Gets a property at path
|
2020-06-25 13:26:31 -05:00
|
|
|
-- @param tbl the table to access
|
2020-06-21 20:38:00 +02:00
|
|
|
-- @param path the '.' separated path
|
2020-06-25 13:26:31 -05:00
|
|
|
-- @returns the value at path or nil
|
|
|
|
|
function M.get_at_path(tbl, path)
|
2021-07-04 16:12:17 -05:00
|
|
|
if path == "" then
|
|
|
|
|
return tbl
|
|
|
|
|
end
|
|
|
|
|
local segments = vim.split(path, ".", true)
|
2020-06-25 13:26:31 -05:00
|
|
|
local result = tbl
|
|
|
|
|
|
|
|
|
|
for _, segment in ipairs(segments) do
|
2021-07-04 16:12:17 -05:00
|
|
|
if type(result) == "table" then
|
2020-06-25 13:26:31 -05:00
|
|
|
result = result[segment]
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
end
|
|
|
|
|
|
2020-08-06 09:01:38 +02:00
|
|
|
function M.set_jump()
|
|
|
|
|
vim.cmd "normal! m'"
|
|
|
|
|
end
|
|
|
|
|
|
2020-08-22 20:46:40 +02:00
|
|
|
function M.index_of(tbl, obj)
|
|
|
|
|
for i, o in ipairs(tbl) do
|
|
|
|
|
if o == obj then
|
|
|
|
|
return i
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-24 09:12:03 -05:00
|
|
|
-- Filters a list based on the given predicate
|
|
|
|
|
-- @param tbl The list to filter
|
|
|
|
|
-- @param predicate The predicate to filter with
|
|
|
|
|
function M.filter(tbl, predicate)
|
|
|
|
|
local result = {}
|
|
|
|
|
|
|
|
|
|
for i, v in ipairs(tbl) do
|
|
|
|
|
if predicate(v, i) then
|
|
|
|
|
table.insert(result, v)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Returns a list of all values from the first list
|
|
|
|
|
-- that are not present in the second list.
|
|
|
|
|
-- @params tbl1 The first table
|
|
|
|
|
-- @params tbl2 The second table
|
|
|
|
|
function M.difference(tbl1, tbl2)
|
|
|
|
|
return M.filter(tbl1, function(v)
|
|
|
|
|
return not vim.tbl_contains(tbl2, v)
|
|
|
|
|
end)
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-30 08:18:24 -05:00
|
|
|
function M.identity(a)
|
|
|
|
|
return a
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function M.constant(a)
|
2021-07-04 16:12:17 -05:00
|
|
|
return function()
|
|
|
|
|
return a
|
|
|
|
|
end
|
2021-03-30 08:18:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function M.to_func(a)
|
2021-07-04 16:12:17 -05:00
|
|
|
return type(a) == "function" and a or M.constant(a)
|
2021-03-30 08:18:24 -05:00
|
|
|
end
|
|
|
|
|
|
2022-01-22 11:02:55 +01:00
|
|
|
function M.ts_cli_version()
|
|
|
|
|
if fn.executable "tree-sitter" == 1 then
|
|
|
|
|
local handle = io.popen "tree-sitter -V"
|
|
|
|
|
local result = handle:read "*a"
|
|
|
|
|
handle:close()
|
|
|
|
|
return vim.split(result, "\n")[1]:match "[^tree%psitter ].*"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-04-20 09:24:48 +02:00
|
|
|
return M
|