|
1 | 1 | local config = require("copilot.config") |
2 | 2 |
|
| 3 | +local unpack = unpack or table.unpack |
| 4 | + |
3 | 5 | local M = {} |
4 | 6 |
|
5 | 7 | local id = 0 |
@@ -230,6 +232,45 @@ function M.get_editor_configuration() |
230 | 232 | } |
231 | 233 | end |
232 | 234 |
|
| 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 | + |
233 | 274 | M.get_copilot_path = function() |
234 | 275 | local copilot_path = vim.api.nvim_get_runtime_file('copilot/index.js', false)[1] |
235 | 276 | if vim.fn.filereadable(copilot_path) ~= 0 then |
|
0 commit comments