Skip to content

Commit

Permalink
feat: implement bin_preference
Browse files Browse the repository at this point in the history
  • Loading branch information
MunifTanjim committed Aug 24, 2022
1 parent bc490e4 commit fca744c
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 14 deletions.
24 changes: 24 additions & 0 deletions lua/prettier/cache.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local store = {}

local cache = {}

---@generic P : any[]
---@generic R : any
---@param fn fun(...: P): R
---@param make_key fun(...: P): string
---@return fun(...: P): R
function cache.wrap(fn, make_key)
store[fn] = {}

return function(...)
local key = make_key(...)

if store[fn][key] == nil then
store[fn][key] = fn(...) or false
end

return store[fn][key]
end
end

return cache
4 changes: 3 additions & 1 deletion lua/prettier/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ local M = {}

function M.setup(user_options)
options.setup(user_options)
null_ls.setup()
vim.schedule(function()
null_ls.setup()
end)
end

function M.format(method)
Expand Down
3 changes: 2 additions & 1 deletion lua/prettier/null-ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local function create_generator(opts)
local cli_options = opts.cli_options or {}
local null_ls_options = opts["null-ls"] or {}

local command = utils.resolve_bin(bin)
local command = utils.resolve_bin(bin, opts.bin_preference)

if not command then
return
Expand Down Expand Up @@ -78,6 +78,7 @@ local function create_formatter(opts)

local generator = create_generator({
bin = opts.bin,
bin_preference = opts.bin_preference,
cli_options = opts.cli_options,
["null-ls"] = opts["null-ls"],
})
Expand Down
75 changes: 63 additions & 12 deletions lua/prettier/utils.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local cache = require("prettier.cache")
local find_git_ancestor = require("lspconfig.util").find_git_ancestor
local find_package_json_ancestor = require("lspconfig.util").find_package_json_ancestor
local path_join = require("lspconfig.util").path.join
Expand Down Expand Up @@ -26,23 +27,73 @@ function M.prettier_enabled()
return config_file_exists()
end

---@param cmd string
---@return nil|string
function M.resolve_bin(cmd)
local project_root = get_working_directory()
---@param _cwd string
---@param scope '"global"'|'"local"'
---@return string|false bin_dir
local function _get_bin_dir(_cwd, scope)
local cmd = "npm bin"
if scope == "global" then
cmd = cmd .. " --global"
end

if project_root then
local local_bin = path_join(project_root, "/node_modules/.bin", cmd)
if vim.fn.executable(local_bin) == 1 then
return local_bin
end
local result = vim.fn.systemlist(cmd)
if vim.fn.isdirectory(result[1]) == 1 then
return result[1]
end

return false
end

---@type fun(cwd: string, scope: '"global"'|'"local"'): string|false
local get_bin_dir = cache.wrap(_get_bin_dir, function(cwd, scope)
return scope .. "::" .. cwd
end)

---@param name string
---@param scope '"global"'|'"local"'
---@return string|false bin
local function _get_bin_path(cwd, name, scope)
local bin_dir = get_bin_dir(cwd, scope)
if not bin_dir then
return false
end

local bin = path_join(bin_dir, name)
if vim.fn.executable(bin) == 1 then
return bin
end

if vim.fn.executable(cmd) == 1 then
return cmd
if scope == "global" and vim.fn.executable(name) == 1 then
return vim.fn.exepath(name)
end

return false
end

---@type fun(cwd: string, name: string, scope: '"global"'|'"local"'): string|false
local get_bin_path = cache.wrap(_get_bin_path, function(cwd, name, scope)
return scope .. "::" .. name .. "::" .. cwd
end)

---@param name string
---@param preference? '"global"'|'"local"'|'"prefer-local"'
---@return string|false
function M.resolve_bin(name, preference)
local cwd = vim.fn.getcwd()

preference = preference or "prefer-local"

if preference == "global" then
return get_bin_path(cwd, name, "global")
end

local bin = get_bin_path(cwd, name, "local")

if bin or preference == "local" then
return bin
end

return nil
return get_bin_path(cwd, name, "global")
end

function M.tbl_flatten(tbl, should_flatten, result, prefix, depth)
Expand Down

0 comments on commit fca744c

Please sign in to comment.