From 9e1fcd5cafc42b5dfbe2e942ddbece0dada4e1d0 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Tue, 26 Sep 2023 10:15:21 -0700 Subject: feat: new utility function --- lua/conform/util.lua | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) (limited to 'lua') diff --git a/lua/conform/util.lua b/lua/conform/util.lua index 959e88c..ec6af45 100644 --- a/lua/conform/util.lua +++ b/lua/conform/util.lua @@ -1,19 +1,47 @@ local M = {} +---Find a command in node_modules ---@param cmd string ---@return fun(ctx: conform.Context): string M.from_node_modules = function(cmd) + return M.find_executable({ "node_modules/.bin/" .. cmd }, cmd) +end + +---Search parent directories for a relative path to a command +---@param paths string[] +---@param default string +---@return fun(ctx: conform.Context): string +---@example +--- local cmd = require("conform.util").find_executable({ "node_modules/.bin/prettier" }, "prettier") +M.find_executable = function(paths, default) return function(ctx) - local fs = require("conform.fs") - local found = - vim.fs.find("node_modules", { upward = true, type = "directory", path = ctx.dirname }) - for _, dir in ipairs(found) do - local executable = fs.join(dir, ".bin", cmd) - if vim.fn.executable(executable) == 1 then - return executable + for _, path in ipairs(paths) do + local normpath = vim.fs.normalize(path) + local is_absolute = vim.startswith(normpath, "/") + if is_absolute and vim.fn.executable(normpath) then + return normpath + end + + local idx = normpath:find("/", 1, true) + local dir, subpath + if idx then + dir = normpath:sub(1, idx - 1) + subpath = normpath:sub(idx) + else + -- This is a bare relative-path executable + dir = normpath + subpath = "" + end + local results = vim.fs.find(dir, { upward = true, path = ctx.dirname, limit = math.huge }) + for _, result in ipairs(results) do + local fullpath = result .. subpath + if vim.fn.executable(fullpath) == 1 then + return fullpath + end end end - return cmd + + return default end end -- cgit v1.2.3-70-g09d2