|
1 | | -local api = require("copilot.api") |
2 | | -local config = require("copilot.config") |
3 | | -local util = require("copilot.util") |
4 | | -local logger = require("copilot.logger") |
5 | | -local lsp = require("copilot.lsp") |
6 | | -local utils = require("copilot.client.utils") |
7 | | -local M = {} |
8 | | - |
9 | | ----@type table<fun(client:table)> |
10 | | -local callbacks = {} |
11 | | - |
12 | | ----@param overrides table<string, any> |
13 | | ----@param client CopilotClient |
14 | | -function M.prepare_client_config(overrides, client) |
15 | | - if lsp.binary.initialization_failed then |
16 | | - client.startup_error = "initialization of copilot-language-server failed" |
17 | | - return |
18 | | - end |
19 | | - |
20 | | - client.startup_error = nil |
21 | | - |
22 | | - local cmd = lsp.get_execute_command() |
23 | | - |
24 | | - if not cmd then |
25 | | - logger.error("copilot server type not supported") |
26 | | - return |
27 | | - end |
28 | | - |
29 | | - local capabilities = vim.lsp.protocol.make_client_capabilities() |
30 | | - capabilities.window.showDocument.support = true |
31 | | - |
32 | | - capabilities.workspace = { |
33 | | - workspaceFolders = true, |
34 | | - } |
35 | | - |
36 | | - local root_dir = utils.get_root_dir(config.root_dir) |
37 | | - local workspace_folders = { |
38 | | - --- @type workspace_folder |
39 | | - { |
40 | | - uri = vim.uri_from_fname(root_dir), |
41 | | - -- important to keep root_dir as-is for the name as lsp.lua uses this to check the workspace has not changed |
42 | | - name = root_dir, |
43 | | - }, |
44 | | - } |
45 | | - |
46 | | - local config_workspace_folders = config.workspace_folders |
47 | | - |
48 | | - for _, config_workspace_folder in ipairs(config_workspace_folders) do |
49 | | - if config_workspace_folder ~= "" then |
50 | | - table.insert( |
51 | | - workspace_folders, |
52 | | - --- @type workspace_folder |
53 | | - { |
54 | | - uri = vim.uri_from_fname(config_workspace_folder), |
55 | | - name = config_workspace_folder, |
56 | | - } |
57 | | - ) |
58 | | - end |
59 | | - end |
60 | | - |
61 | | - local editor_info = util.get_editor_info() |
62 | | - local provider_url = config.auth_provider_url |
63 | | - local proxy_uri = vim.g.copilot_proxy |
64 | | - |
65 | | - local settings = { ---@type copilot_settings |
66 | | - telemetry = { ---@type github_settings_telemetry |
67 | | - telemetryLevel = "all", |
68 | | - }, |
69 | | - } |
70 | | - |
71 | | - if proxy_uri then |
72 | | - settings = vim.tbl_extend("force", settings, { |
73 | | - http = { ---@type copilot_settings_http |
74 | | - proxy = proxy_uri, |
75 | | - proxyStrictSSL = vim.g.copilot_proxy_strict_ssl or false, |
76 | | - proxyKerberosServicePrincipal = nil, |
77 | | - }, |
78 | | - }) |
79 | | - end |
80 | | - |
81 | | - if provider_url then |
82 | | - settings = vim.tbl_extend("force", settings, { |
83 | | - ["github-enterprise"] = { ---@type copilot_settings_github-enterprise |
84 | | - uri = provider_url, |
85 | | - }, |
86 | | - }) |
87 | | - end |
88 | | - |
89 | | - -- LSP config, not to be confused with config.lua |
90 | | - return vim.tbl_deep_extend("force", { |
91 | | - cmd = cmd, |
92 | | - root_dir = root_dir, |
93 | | - name = "copilot", |
94 | | - capabilities = capabilities, |
95 | | - get_language_id = function(_, filetype) |
96 | | - return require("copilot.client.filetypes").language_for_file_type(filetype) |
97 | | - end, |
98 | | - on_init = function(lsp_client, initialize_result) |
99 | | - if client.id == lsp_client.id then |
100 | | - client.capabilities = initialize_result.capabilities |
101 | | - end |
102 | | - |
103 | | - vim.schedule(function() |
104 | | - local configurations = utils.get_workspace_configurations() |
105 | | - api.notify_change_configuration(lsp_client, configurations) |
106 | | - logger.trace("workspace configuration", configurations) |
107 | | - |
108 | | - -- to activate tracing if we want it |
109 | | - local logger_conf = config.logger |
110 | | - local trace_params = { value = logger_conf.trace_lsp } --[[@as copilot_nofify_set_trace_params]] |
111 | | - api.notify_set_trace(lsp_client, trace_params) |
112 | | - |
113 | | - -- prevent requests to copilot prior to being initialized |
114 | | - client.initialized = true |
115 | | - |
116 | | - for _, callback in ipairs(callbacks) do |
117 | | - callback(lsp_client) |
118 | | - end |
119 | | - end) |
120 | | - end, |
121 | | - on_exit = function(code, _, client_id) |
122 | | - if client.id == client_id then |
123 | | - vim.schedule(function() |
124 | | - client.teardown() |
125 | | - client.id = nil |
126 | | - client.capabilities = nil |
127 | | - end) |
128 | | - end |
129 | | - if code > 0 then |
130 | | - vim.schedule(function() |
131 | | - require("copilot.status").status() |
132 | | - end) |
133 | | - end |
134 | | - end, |
135 | | - handlers = require("copilot.client.handlers").get_handlers(), |
136 | | - init_options = { |
137 | | - editorInfo = editor_info.editorInfo, |
138 | | - editorPluginInfo = editor_info.editorPluginInfo, |
139 | | - }, |
140 | | - settings = settings, |
141 | | - workspace_folders = workspace_folders, |
142 | | - }, overrides) |
143 | | -end |
144 | | - |
145 | | ----@param callback fun(client:table) |
146 | | -function M.add_callback(callback) |
147 | | - table.insert(callbacks, callback) |
148 | | -end |
149 | | - |
150 | | -return M |
| 1 | +local api = require("copilot.api") |
| 2 | +local config = require("copilot.config") |
| 3 | +local util = require("copilot.util") |
| 4 | +local logger = require("copilot.logger") |
| 5 | +local lsp = require("copilot.lsp") |
| 6 | +local utils = require("copilot.client.utils") |
| 7 | +local M = {} |
| 8 | + |
| 9 | +---@type table<fun(client:table)> |
| 10 | +local callbacks = {} |
| 11 | + |
| 12 | +---@param overrides table<string, any> |
| 13 | +---@param client CopilotClient |
| 14 | +function M.prepare_client_config(overrides, client) |
| 15 | + if lsp.binary.initialization_failed then |
| 16 | + client.startup_error = "initialization of copilot-language-server failed" |
| 17 | + return |
| 18 | + end |
| 19 | + |
| 20 | + client.startup_error = nil |
| 21 | + |
| 22 | + local cmd = lsp.get_execute_command() |
| 23 | + |
| 24 | + if not cmd then |
| 25 | + logger.error("copilot server type not supported") |
| 26 | + return |
| 27 | + end |
| 28 | + |
| 29 | + local capabilities = vim.lsp.protocol.make_client_capabilities() |
| 30 | + capabilities.window.showDocument.support = true |
| 31 | + |
| 32 | + capabilities.workspace = { |
| 33 | + workspaceFolders = true, |
| 34 | + } |
| 35 | + |
| 36 | + local root_dir = utils.get_root_dir(config.root_dir) |
| 37 | + local workspace_folders = { |
| 38 | + --- @type workspace_folder |
| 39 | + { |
| 40 | + uri = vim.uri_from_fname(root_dir), |
| 41 | + -- important to keep root_dir as-is for the name as lsp.lua uses this to check the workspace has not changed |
| 42 | + name = root_dir, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + local config_workspace_folders = config.workspace_folders |
| 47 | + |
| 48 | + for _, config_workspace_folder in ipairs(config_workspace_folders) do |
| 49 | + if config_workspace_folder ~= "" then |
| 50 | + table.insert( |
| 51 | + workspace_folders, |
| 52 | + --- @type workspace_folder |
| 53 | + { |
| 54 | + uri = vim.uri_from_fname(config_workspace_folder), |
| 55 | + name = config_workspace_folder, |
| 56 | + } |
| 57 | + ) |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + local editor_info = util.get_editor_info() |
| 62 | + local provider_url = config.auth_provider_url |
| 63 | + local proxy_uri = vim.g.copilot_proxy |
| 64 | + |
| 65 | + local settings = { ---@type copilot_settings |
| 66 | + telemetry = { ---@type github_settings_telemetry |
| 67 | + telemetryLevel = "all", |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + if proxy_uri then |
| 72 | + settings = vim.tbl_extend("force", settings, { |
| 73 | + http = { ---@type copilot_settings_http |
| 74 | + proxy = proxy_uri, |
| 75 | + proxyStrictSSL = vim.g.copilot_proxy_strict_ssl or false, |
| 76 | + proxyKerberosServicePrincipal = nil, |
| 77 | + }, |
| 78 | + }) |
| 79 | + end |
| 80 | + |
| 81 | + if provider_url then |
| 82 | + settings = vim.tbl_extend("force", settings, { |
| 83 | + ["github-enterprise"] = { ---@type copilot_settings_github-enterprise |
| 84 | + uri = provider_url, |
| 85 | + }, |
| 86 | + }) |
| 87 | + end |
| 88 | + |
| 89 | + -- LSP config, not to be confused with config.lua |
| 90 | + return vim.tbl_deep_extend("force", { |
| 91 | + cmd = cmd, |
| 92 | + root_dir = root_dir, |
| 93 | + name = "copilot", |
| 94 | + capabilities = capabilities, |
| 95 | + get_language_id = function(_, filetype) |
| 96 | + return require("copilot.client.filetypes").language_for_file_type(filetype) |
| 97 | + end, |
| 98 | + on_init = function(lsp_client, initialize_result) |
| 99 | + if client.id == lsp_client.id then |
| 100 | + client.capabilities = initialize_result.capabilities |
| 101 | + end |
| 102 | + |
| 103 | + vim.schedule(function() |
| 104 | + local configurations = utils.get_workspace_configurations() |
| 105 | + api.notify_change_configuration(lsp_client, configurations) |
| 106 | + logger.trace("workspace configuration", configurations) |
| 107 | + |
| 108 | + -- to activate tracing if we want it |
| 109 | + local logger_conf = config.logger |
| 110 | + local trace_params = { value = logger_conf.trace_lsp } --[[@as copilot_nofify_set_trace_params]] |
| 111 | + api.notify_set_trace(lsp_client, trace_params) |
| 112 | + |
| 113 | + -- prevent requests to copilot prior to being initialized |
| 114 | + client.initialized = true |
| 115 | + |
| 116 | + for _, callback in ipairs(callbacks) do |
| 117 | + callback(lsp_client) |
| 118 | + end |
| 119 | + end) |
| 120 | + end, |
| 121 | + on_exit = function(code, _, client_id) |
| 122 | + if client.id == client_id then |
| 123 | + vim.schedule(function() |
| 124 | + client.teardown() |
| 125 | + client.id = nil |
| 126 | + client.capabilities = nil |
| 127 | + end) |
| 128 | + end |
| 129 | + if code > 0 then |
| 130 | + vim.schedule(function() |
| 131 | + require("copilot.status").status() |
| 132 | + end) |
| 133 | + end |
| 134 | + end, |
| 135 | + handlers = require("copilot.client.handlers").get_handlers(), |
| 136 | + init_options = { |
| 137 | + editorInfo = editor_info.editorInfo, |
| 138 | + editorPluginInfo = editor_info.editorPluginInfo, |
| 139 | + }, |
| 140 | + settings = settings, |
| 141 | + workspace_folders = workspace_folders, |
| 142 | + }, overrides) |
| 143 | +end |
| 144 | + |
| 145 | +---@param callback fun(client:table) |
| 146 | +function M.add_callback(callback) |
| 147 | + table.insert(callbacks, callback) |
| 148 | +end |
| 149 | + |
| 150 | +return M |
0 commit comments