Skip to content

Commit d68f6b1

Browse files
timhughAntoineGS
authored andcommitted
add support for table copilot node command
1 parent ba4cd43 commit d68f6b1

4 files changed

Lines changed: 65 additions & 10 deletions

File tree

lua/copilot/config/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local logger = require("copilot.logger")
1212
---@field copilot_model string|nil Model to use for Copilot, LSP server dictates the default
1313
---@field root_dir RootDirFuncOrString Root directory for the project, defaults to the nearest .git directory
1414
---@field should_attach ShouldAttachFunc Function to determine if Copilot should attach to the buffer
15-
---@field copilot_node_command string Path to the Node.js executable, defaults to "node"
15+
---@field copilot_node_command string|string[] Path to the Node.js executable, defaults to "node"
1616
---@field disable_limit_reached_message boolean Disable the limit reached message, defaults to false
1717

1818
local initialized = false
@@ -68,7 +68,7 @@ function M.validate(config)
6868
vim.validate("copilot_model", config.copilot_model, { "string", "nil" })
6969
vim.validate("root_dir", config.root_dir, { "string", "function" })
7070
vim.validate("should_attach", config.should_attach, "function")
71-
vim.validate("copilot_node_command", config.copilot_node_command, "string")
71+
vim.validate("copilot_node_command", config.copilot_node_command, {"string", "table"})
7272

7373
require("copilot.config.panel").validate(config.panel)
7474
require("copilot.config.suggestion").validate(config.suggestion)

lua/copilot/lsp/nodejs.lua

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
local logger = require("copilot.logger")
2+
local util = require("copilot.util")
23

34
local M = {
45
---@class copilot_nodejs_server_info
5-
---@type string
6+
---@type string|string[]
67
node_command = nil,
78
---@type string
89
server_path = nil,
910
initialization_failed = false,
1011
}
1112

13+
1214
---@return string node_version
1315
---@return nil|string node_version_error
1416
function M.get_node_version()
1517
if not M.node_version then
16-
local version_cmd = { M.node_command, "--version" }
18+
local version_cmd = util.append_command(M.node_command, "--version")
1719

1820
local node_version_major = 0
1921
local node_version = ""
@@ -95,14 +97,20 @@ end
9597

9698
---@return table
9799
function M.get_execute_command()
98-
return {
99-
M.node_command,
100-
M.server_path or M.get_server_path(),
101-
"--stdio",
102-
}
100+
if type(M.node_command) == "string" then
101+
return {
102+
M.node_command,
103+
M.server_path or M.get_server_path(),
104+
"--stdio",
105+
}
106+
elseif type(M.node_command) == "table" then
107+
return util.append_command(M.node_command, { M.server_path or M.get_server_path(), "--stdio" })
108+
else
109+
error(string.format("failed to build node command from %s (type %s)", M.node_command, type(M.node_command)))
110+
end
103111
end
104112

105-
---@param node_command? string
113+
---@param node_command? string|string[]
106114
---@param custom_server_path? string
107115
---@return boolean
108116
function M.setup(node_command, custom_server_path)

lua/copilot/util.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,31 @@ function M.get_buffer_previous_ft(bufnr)
186186
return (ok and result) or ""
187187
end
188188

189+
---@param cmd string|string[]
190+
---@param append string|string[]
191+
---@return string[]
192+
M.append_command = function(cmd, append)
193+
local full_cmd = {}
194+
195+
-- first append the base command
196+
if type(cmd) == "string" then
197+
table.insert(full_cmd, cmd)
198+
elseif type(cmd) == "table" then
199+
for _, part in ipairs(cmd) do
200+
table.insert(full_cmd, part)
201+
end
202+
end
203+
204+
-- then append the additional parts
205+
if type(append) == "string" then
206+
table.insert(full_cmd, append)
207+
elseif type(append) == "table" then
208+
for _, part in ipairs(append) do
209+
table.insert(full_cmd, part)
210+
end
211+
end
212+
213+
return full_cmd
214+
end
215+
189216
return M

tests/test_nodejs.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ T["get_node_version()"]["custom node command as string with spaces"] = function(
7373
eq(captured_args, { "/path to/node", "--version" })
7474
end
7575

76+
T["get_node_version()"]["custom node command as table"] = function()
77+
local captured_args = stub_process("v20.10.0", 0, false, function()
78+
local nodejs = require("copilot.lsp.nodejs")
79+
nodejs.setup({ "mise", "x", "node@lts", "--", "node" })
80+
81+
local version, error = nodejs.get_node_version()
82+
83+
eq(version, "20.10.0")
84+
eq(error, nil)
85+
end)
86+
eq(captured_args, { "mise", "x", "node@lts", "--", "node", "--version" })
87+
end
88+
7689
T["get_node_version()"]["handles vim.system failure"] = function()
7790
local captured_args = stub_process("", -1, true, function()
7891
local nodejs = require("copilot.lsp.nodejs")
@@ -144,4 +157,11 @@ T["get_execute_command()"]["custom node command as string with spaces"] = functi
144157
eq(cmd, { "/path to/node", nodejs.server_path, "--stdio" })
145158
end
146159

160+
T["get_execute_command()"]["custom node command as table"] = function()
161+
local nodejs = require("copilot.lsp.nodejs")
162+
nodejs.setup({ "mise", "x", "node@lts", "--", "node" })
163+
local cmd = nodejs.get_execute_command()
164+
eq(cmd, { "mise", "x", "node@lts", "--", "node", nodejs.server_path, "--stdio" })
165+
end
166+
147167
return T

0 commit comments

Comments
 (0)