Skip to content

Commit fca744c

Browse files
committed
feat: implement bin_preference
1 parent bc490e4 commit fca744c

File tree

4 files changed

+92
-14
lines changed

4 files changed

+92
-14
lines changed

lua/prettier/cache.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
local store = {}
2+
3+
local cache = {}
4+
5+
---@generic P : any[]
6+
---@generic R : any
7+
---@param fn fun(...: P): R
8+
---@param make_key fun(...: P): string
9+
---@return fun(...: P): R
10+
function cache.wrap(fn, make_key)
11+
store[fn] = {}
12+
13+
return function(...)
14+
local key = make_key(...)
15+
16+
if store[fn][key] == nil then
17+
store[fn][key] = fn(...) or false
18+
end
19+
20+
return store[fn][key]
21+
end
22+
end
23+
24+
return cache

lua/prettier/init.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ local M = {}
55

66
function M.setup(user_options)
77
options.setup(user_options)
8-
null_ls.setup()
8+
vim.schedule(function()
9+
null_ls.setup()
10+
end)
911
end
1012

1113
function M.format(method)

lua/prettier/null-ls.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ local function create_generator(opts)
1616
local cli_options = opts.cli_options or {}
1717
local null_ls_options = opts["null-ls"] or {}
1818

19-
local command = utils.resolve_bin(bin)
19+
local command = utils.resolve_bin(bin, opts.bin_preference)
2020

2121
if not command then
2222
return
@@ -78,6 +78,7 @@ local function create_formatter(opts)
7878

7979
local generator = create_generator({
8080
bin = opts.bin,
81+
bin_preference = opts.bin_preference,
8182
cli_options = opts.cli_options,
8283
["null-ls"] = opts["null-ls"],
8384
})

lua/prettier/utils.lua

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local cache = require("prettier.cache")
12
local find_git_ancestor = require("lspconfig.util").find_git_ancestor
23
local find_package_json_ancestor = require("lspconfig.util").find_package_json_ancestor
34
local path_join = require("lspconfig.util").path.join
@@ -26,23 +27,73 @@ function M.prettier_enabled()
2627
return config_file_exists()
2728
end
2829

29-
---@param cmd string
30-
---@return nil|string
31-
function M.resolve_bin(cmd)
32-
local project_root = get_working_directory()
30+
---@param _cwd string
31+
---@param scope '"global"'|'"local"'
32+
---@return string|false bin_dir
33+
local function _get_bin_dir(_cwd, scope)
34+
local cmd = "npm bin"
35+
if scope == "global" then
36+
cmd = cmd .. " --global"
37+
end
3338

34-
if project_root then
35-
local local_bin = path_join(project_root, "/node_modules/.bin", cmd)
36-
if vim.fn.executable(local_bin) == 1 then
37-
return local_bin
38-
end
39+
local result = vim.fn.systemlist(cmd)
40+
if vim.fn.isdirectory(result[1]) == 1 then
41+
return result[1]
42+
end
43+
44+
return false
45+
end
46+
47+
---@type fun(cwd: string, scope: '"global"'|'"local"'): string|false
48+
local get_bin_dir = cache.wrap(_get_bin_dir, function(cwd, scope)
49+
return scope .. "::" .. cwd
50+
end)
51+
52+
---@param name string
53+
---@param scope '"global"'|'"local"'
54+
---@return string|false bin
55+
local function _get_bin_path(cwd, name, scope)
56+
local bin_dir = get_bin_dir(cwd, scope)
57+
if not bin_dir then
58+
return false
59+
end
60+
61+
local bin = path_join(bin_dir, name)
62+
if vim.fn.executable(bin) == 1 then
63+
return bin
3964
end
4065

41-
if vim.fn.executable(cmd) == 1 then
42-
return cmd
66+
if scope == "global" and vim.fn.executable(name) == 1 then
67+
return vim.fn.exepath(name)
68+
end
69+
70+
return false
71+
end
72+
73+
---@type fun(cwd: string, name: string, scope: '"global"'|'"local"'): string|false
74+
local get_bin_path = cache.wrap(_get_bin_path, function(cwd, name, scope)
75+
return scope .. "::" .. name .. "::" .. cwd
76+
end)
77+
78+
---@param name string
79+
---@param preference? '"global"'|'"local"'|'"prefer-local"'
80+
---@return string|false
81+
function M.resolve_bin(name, preference)
82+
local cwd = vim.fn.getcwd()
83+
84+
preference = preference or "prefer-local"
85+
86+
if preference == "global" then
87+
return get_bin_path(cwd, name, "global")
88+
end
89+
90+
local bin = get_bin_path(cwd, name, "local")
91+
92+
if bin or preference == "local" then
93+
return bin
4394
end
4495

45-
return nil
96+
return get_bin_path(cwd, name, "global")
4697
end
4798

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

0 commit comments

Comments
 (0)