diff options
Diffstat (limited to 'configs/nvim/config-root/lua/crupest/utils')
-rw-r--r-- | configs/nvim/config-root/lua/crupest/utils/find.lua | 187 | ||||
-rw-r--r-- | configs/nvim/config-root/lua/crupest/utils/fs.lua | 104 | ||||
-rw-r--r-- | configs/nvim/config-root/lua/crupest/utils/nvim.lua | 38 | ||||
-rw-r--r-- | configs/nvim/config-root/lua/crupest/utils/table.lua | 42 |
4 files changed, 71 insertions, 300 deletions
diff --git a/configs/nvim/config-root/lua/crupest/utils/find.lua b/configs/nvim/config-root/lua/crupest/utils/find.lua index 83968d2..dd1f663 100644 --- a/configs/nvim/config-root/lua/crupest/utils/find.lua +++ b/configs/nvim/config-root/lua/crupest/utils/find.lua @@ -1,152 +1,101 @@ -local fs = require("crupest.utils.fs"); local is_win = vim.fn.has("win32") ~= 0 -local win_exe_exts = { "exe", "CMD", "cmd", "ps1" } +local M = {} -local function get_exe(path) - if is_win then - for _, ext in ipairs(win_exe_exts) do - if string.find(path, "%." .. ext .. "$") and fs.isfile(path) then - return path - end - end - for _, ext in ipairs(win_exe_exts) do - local p = path .. "." .. ext - if fs.isfile(p) then return p end - end - return nil - end - - if vim.fn.executable(path) ~= 0 then - return path - end - - return nil -end +local windows_exe_ext = { "exe", "bat", "cmd", "ps1" } -local function find_global_exe(name) - if vim.fn.executable(name) ~= 0 then - return name +--- Find real path (with ext) for an executable. +--- @param dir string +--- @param name string | string[] +--- @return string | nil +function M.find_exe_file(dir, name) + if type(name) == "string" then + name = { name } end - - return nil -end - -local function first_exe(paths) - for _, v in ipairs(paths) do - local exe = get_exe(v) - if exe then return exe end - end - - return nil -end - -local function find_file_or_directory(path, name) - return fs.walk_up(path, function(current_path) - local p = current_path .. "/" .. name - if fs.isdir(p) then - return p, "directory" - elseif fs.isfile(p) then - return p, "file" + for _, n in ipairs(name) do + if vim.uv.fs_stat(vim.fs.joinpath(dir, n)) ~= nil then + return n end - return nil - end) -end - -local function find_file(path, name) - return fs.walk_up(path, function(current_path) - local p = current_path .. "/" .. name - if fs.isfile(p) then - return p + if is_win then + for _, ext in ipairs(windows_exe_ext) do + if vim.uv.fs_stat(vim.fs.joinpath(dir, n .. "." .. ext)) ~= nil then + return n .. "." .. ext + end + end end - return nil - end) -end - -local function find_files_or_directories(path, names) - for _, name in ipairs(names) do - local p, type = find_file_or_directory(path, name) - if p then return p, type end - end - return nil -end - -local function find_files(path, names) - for _, name in ipairs(names) do - local p = find_file(path, name) - if p then return p end end return nil end -local function find_node_modules(path) - return fs.walk_up(path, function(current_path) - local node_modules_path = current_path .. "/node_modules" - if fs.isdir(node_modules_path) then - return node_modules_path - end - return nil - end) +--- Walk up until found an executable in node_modules. +--- @param path string +--- @param name string +--- @return string | nil exe_path Path to the executable. +function M.find_node_modules_exe(path, name) + local bin_dirs = vim.fs.find("node_modules/.bin", { path = path, upward = true, type = "directory" }) + if #bin_dirs == 0 then return nil end + local exe = M.find_exe_file(bin_dirs[1], name) + return exe and vim.fs.joinpath(bin_dirs[1], exe) end -local function find_npm_exe(path, exe) - local node_modules_path = find_node_modules(path) - if not node_modules_path then return nil end - local try_exe_path = node_modules_path .. "/.bin/" .. exe - local exe_path = get_exe(try_exe_path) - if exe_path then return exe_path end - return nil +--- Find executable in PATH. +--- @param name string +--- @return string | nil +function M.find_global_exe(name) + local exe = vim.fn.exepath(name) + if exe == "" then return nil end + return exe end -local function find_exe(path, exe, places) +--- @alias ExePlace "node_modules" | "global" +--- @param path string +--- @param name string +--- @param places ExePlace[] +--- @return string | nil, ExePlace? +function M.find_exe(path, name, places) for _, place in ipairs(places) do - if place == "npm" then - local r = find_npm_exe(path, exe) - if r then return r end + if place == "node_modules" then + local r = M.find_node_modules_exe(path, name) + if r then return r, "node_modules" end end if place == "global" then - local r = find_global_exe(exe) - if r then return r end + local r = M.find_global_exe(name) + if r then return r, "global" end end end - return nil + return nil, nil end -local function find_exe_for_buf(buf, opts) - local r = {} +--- @alias FindExeForBufOpts { name: string, exe: string?, places: ExePlace[], config_files: string[]?, filetypes: string[]? } +--- @alias FindExeForBufResult { name: string, file: string, exe: string, exe_path: string, place: ExePlace, config_file: string?, filetype: string? } +--- @param buf number +--- @param opts FindExeForBufOpts +--- @return FindExeForBufResult | nil +function M.find_exe_for_buf(buf, opts) + local r = {} --- @type FindExeForBufResult r.name = opts.name r.file = vim.api.nvim_buf_get_name(buf) - r.filetype = vim.api.nvim_buf_get_option(buf, "filetype") - r.exe_name = opts.name - r.exe_places = opts.exe_places or { "global" } + r.exe = opts.exe or opts.name - if opts.config_files then - r.config_file = find_files(r.file, opts.config_files) - if r.config_file == nil then return nil end + if opts.filetypes then + r.filetype = vim.api.nvim_get_option_value("filetype", { scope = "buffer", buf = buf }) + if not vim.tbl_contains(opts.filetypes, r.filetype) then return nil end end - if opts.filetypes then - if not require("crupest.table").includes(opts.filetypes, r.filetype) then - return nil - end + if opts.config_files then + local config_file_list = vim.fs.find(opts.config_files, { path = r.file, upward = true }) + if #config_file_list == 0 then return nil end + r.config_file = config_file_list[1] end - r.exe_path = find_exe(r.file, r.exe_name, r.exe_places) - if r.exe_path == nil then return nil end + local exe_path, place = M.find_exe(r.file, r.exe, opts.places) + if exe_path == nil then return nil end + r.exe_path = exe_path + + --- @cast place ExePlace + r.place = place return r end -return { - get_exe = get_exe, - find_global_exe = find_global_exe, - first_exe = first_exe, - find_file_or_directory = find_file_or_directory, - find_files_or_directories = find_files_or_directories, - find_file = find_file, - find_files = find_files, - find_node_modules = find_node_modules, - find_npm_exe = find_npm_exe, - find_exe = find_exe, - find_exe_for_buf = find_exe_for_buf, -} +return M diff --git a/configs/nvim/config-root/lua/crupest/utils/fs.lua b/configs/nvim/config-root/lua/crupest/utils/fs.lua deleted file mode 100644 index f1363ff..0000000 --- a/configs/nvim/config-root/lua/crupest/utils/fs.lua +++ /dev/null @@ -1,104 +0,0 @@ -local is_win = vim.fn.has("win32") ~= 0 - -local function clean_path(path) - if path == "/" then return path end - path = string.gsub(path, "[/\\]+", "/") - if string.sub(path, string.len(path)) == '/' then - path = string.sub(path, 1, string.len(path) - 1) - end - return path -end - -local function full_path(name) - if is_win and string.match(name, "^[a-zA-Z]:[/\\]?$") then return clean_path(name) end - local path = vim.fn.fnamemodify(name, ":p") - return clean_path(path) -end - -local function escape_space(str) - return (string.gsub(str, " ", "\\ ")) -end - -local function path_get_dir(path) - return full_path(vim.fn.fnamemodify(clean_path(path), ":h")) -end - -local function walk_up(path, func) - local current_path = full_path(path) - while true do - local result = func(current_path) - if result ~= nil then - return result - end - local new_path = path_get_dir(current_path) - if new_path == current_path then - break - end - current_path = new_path - end - return nil -end - -local function exist(path) - return vim.uv.fs_stat(path) -end - -local function isfile(path) - local s = vim.uv.fs_stat(path) - if not s then return false end - return s.type == "file" -end - -local function isdir(path) - local s = vim.uv.fs_stat(path) - if not s then return false end - return s.type == "directory" -end - -local function mkdir(dir) - local parents = {} - - walk_up(dir, function(p) - table.insert(parents, 1, p) - end) - - for _, v in ipairs(parents) do - if exist(v) and not isdir(v) then - vim.notify(v .. " is not a dir. Can't make dir " .. dir, vim.log.levels.ERROR) - return - end - if not exist(v) then - vim.notify("Creating dir " .. v) - assert(vim.uv.fs_mkdir(v, 504)) -- mode = 0770 - end - end -end - -local function copy(old, new) - mkdir(path_get_dir(new)) - assert(vim.uv.fs_copyfile(old, new)) -end - -local function remove(path) - assert(vim.uv.fs_unlink(path)) -end - -local function move(old, new) - mkdir(path_get_dir(new)) - assert(vim.uv.fs_rename(old, new)) -end - -return { - clean_path = clean_path, - full_path = full_path, - escape_space = escape_space, - path_get_dir = path_get_dir, - walk_up = walk_up, - exist = exist, - isfile = isfile, - isdir = isdir, - mkdir = mkdir, - copy = copy, - remove = remove, - move = move -} diff --git a/configs/nvim/config-root/lua/crupest/utils/nvim.lua b/configs/nvim/config-root/lua/crupest/utils/nvim.lua index ac732fd..4477ecc 100644 --- a/configs/nvim/config-root/lua/crupest/utils/nvim.lua +++ b/configs/nvim/config-root/lua/crupest/utils/nvim.lua @@ -1,32 +1,6 @@ -local function list_listed_bufs() - local bufs = vim.api.nvim_list_bufs() - local result = {} - for _, v in ipairs(bufs) do - if vim.fn.buflisted(v) ~= 0 then - table.insert(result, v) - end - end - return result -end - --- list the windows that are currently editing the given buffer -local function list_wins_editing_buf(buf) - local wins = vim.api.nvim_list_wins() - local result = {} - for _, win in ipairs(wins) do - if vim.api.nvim_win_get_buf(win) == buf then - table.insert(result, win) - end - end - return result -end - -local function buf_is_normal(buf) - return vim.fn.bufexists(buf) ~= 0 and vim.fn.buflisted(buf) ~= 0 -end - +local M = {} -local function close_float() +function M.close_float() local wins = vim.api.nvim_list_wins() for _, v in ipairs(wins) do if vim.api.nvim_win_get_config(v).relative ~= '' then @@ -35,10 +9,4 @@ local function close_float() end end -return { - list_listed_bufs = list_listed_bufs, - buf_is_normal = buf_is_normal, - list_wins_editing_buf = list_wins_editing_buf, - close_float = close_float, -} - +return M diff --git a/configs/nvim/config-root/lua/crupest/utils/table.lua b/configs/nvim/config-root/lua/crupest/utils/table.lua deleted file mode 100644 index 22419b0..0000000 --- a/configs/nvim/config-root/lua/crupest/utils/table.lua +++ /dev/null @@ -1,42 +0,0 @@ -local function remove_element(tbl, element) - local index = nil - for i, v in ipairs(tbl) do - if element == v then - index = i - break - end - end - if index then - table.remove(tbl, index) - end - return tbl -end - -local function element_at(tbl, element) - local at = nil - for i, v in ipairs(tbl) do - if element == v then - at = i - break - end - end - return at -end - -local function includes(tbl, element) - for _, v in ipairs(tbl) do - if v == element then return true end - end - return false -end - -local function string_start_with(str, prefix) - return string.find(str, prefix, 0, true) == 1 -end - -return { - remove_element = remove_element, - element_at = element_at, - includes = includes, - string_start_with = string_start_with, -} |