diff options
author | Yuqian Yang <crupest@crupest.life> | 2024-11-27 23:01:02 +0800 |
---|---|---|
committer | Yuqian Yang <crupest@crupest.life> | 2024-11-27 23:01:02 +0800 |
commit | 6e60bb06bd7a5052a7d6c2b5b8df0ab084697fdd (patch) | |
tree | e842708278a28ab01552a6498cf59371c550ded1 /configs/nvim/config-root/lua/crupest/utils/find.lua | |
parent | 42012b59ba4fe53dac57d2c2b66cb64dbf0dabf1 (diff) | |
download | crupest-6e60bb06bd7a5052a7d6c2b5b8df0ab084697fdd.tar.gz crupest-6e60bb06bd7a5052a7d6c2b5b8df0ab084697fdd.tar.bz2 crupest-6e60bb06bd7a5052a7d6c2b5b8df0ab084697fdd.zip |
config(nvim): update config and plugins.
Diffstat (limited to 'configs/nvim/config-root/lua/crupest/utils/find.lua')
-rw-r--r-- | configs/nvim/config-root/lua/crupest/utils/find.lua | 187 |
1 files changed, 68 insertions, 119 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 |