Skip to content

Commit 94abd14

Browse files
committed
feat(client): support vim.g.copilot_proxy
1 parent 741f62c commit 94abd14

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

lua/copilot/api.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ end
3030

3131
---@alias copilot_editor_info { name: string, version: string }
3232
---@alias copilot_editor_plugin_info { name: string, version: string }
33-
---@alias copilot_set_editor_info_params { editorInfo: copilot_editor_info, editorPluginInfo: copilot_editor_plugin_info, editorConfiguration: copilot_editor_configuration }
33+
---@alias copilot_network_proxy { host: string, port: integer, username?: string, password?: string }
34+
---@alias copilot_set_editor_info_params { editorInfo: copilot_editor_info, editorPluginInfo: copilot_editor_plugin_info, editorConfiguration: copilot_editor_configuration, networkProxy?: copilot_network_proxy }
3435

3536
---@param params copilot_set_editor_info_params
3637
function mod.set_editor_info(client, params)

lua/copilot/client.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ M.merge_server_opts = function(params)
5858
local set_editor_info_params = util.get_editor_info()
5959
set_editor_info_params.editorInfo.version = set_editor_info_params.editorInfo.version .. ' + Node.js ' .. M.get_node_version()
6060
set_editor_info_params.editorConfiguration = util.get_editor_configuration()
61+
set_editor_info_params.networkProxy = util.get_network_proxy()
6162
api.set_editor_info(client, set_editor_info_params)
6263
end)
6364
vim.schedule(M.buf_attach_copilot)

lua/copilot/util.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local config = require("copilot.config")
22

3+
local unpack = unpack or table.unpack
4+
35
local M = {}
46

57
local id = 0
@@ -230,6 +232,45 @@ function M.get_editor_configuration()
230232
}
231233
end
232234

235+
---@param str string
236+
local function url_decode(str)
237+
return vim.fn.substitute(str, [[%\(\x\x\)]], [[\=iconv(nr2char("0x".submatch(1)), "utf-8", "latin1")]], "g")
238+
end
239+
240+
---@return copilot_network_proxy|nil
241+
function M.get_network_proxy()
242+
local proxy_uri = vim.g.copilot_proxy
243+
244+
if type(proxy_uri) ~= "string" then
245+
return
246+
end
247+
248+
proxy_uri = string.gsub(proxy_uri, "^[^:]+://", "")
249+
250+
---@type string|nil, string|nil
251+
local user_pass, host_port = unpack(vim.split(proxy_uri, "@", { plain = true, trimempty = true }))
252+
253+
if not host_port then
254+
host_port = user_pass --[[@as string]]
255+
user_pass = nil
256+
end
257+
258+
local host, port = unpack(vim.split(host_port, ":", { plain = true, trimempty = true }))
259+
local username, password
260+
261+
if user_pass then
262+
username, password = unpack(vim.split(user_pass, ":", { plain = true, trimempty = true }))
263+
username, password = username and url_decode(username), password and url_decode(password)
264+
end
265+
266+
return {
267+
host = host,
268+
port = tonumber(port or 80),
269+
username = username,
270+
password = password,
271+
}
272+
end
273+
233274
M.get_copilot_path = function()
234275
local copilot_path = vim.api.nvim_get_runtime_file('copilot/index.js', false)[1]
235276
if vim.fn.filereadable(copilot_path) ~= 0 then

0 commit comments

Comments
 (0)