|
1 | 1 | local util = require("copilot.util") |
2 | | -local M = {} |
| 2 | +local M = { params = {} } |
3 | 3 |
|
4 | | -local capabilities = vim.lsp.protocol.make_client_capabilities() |
5 | | -capabilities.getCompletions = true |
| 4 | +M.buf_attach_copilot = function() |
| 5 | + if vim.tbl_contains(M.params.ft_disable, vim.bo.filetype) then return end |
| 6 | + if not vim.bo.buflisted or not vim.bo.buftype == "" then return end |
| 7 | + local client_id = require("copilot.util").find_copilot_client() |
| 8 | + local buf_clients = vim.lsp.buf_get_clients(0) |
| 9 | + if client_id and buf_clients and not buf_clients[client_id] then |
| 10 | + vim.lsp.buf_attach_client(0, client_id) |
| 11 | + end |
| 12 | +end |
6 | 13 |
|
7 | | -M.start = function(params) |
8 | | - vim.lsp.start_client({ |
| 14 | +M.merge_server_opts = function (params) |
| 15 | + return vim.tbl_deep_extend("force", params.server_opts_overrides, { |
9 | 16 | cmd = { require("copilot.util").get_copilot_path(params.plugin_manager_path) }, |
10 | 17 | name = "copilot", |
11 | 18 | trace = "messages", |
12 | 19 | root_dir = vim.loop.cwd(), |
13 | 20 | autostart = true, |
14 | | - on_init = function(client, _) |
15 | | - vim.lsp.buf_attach_client(0, client.id) |
| 21 | + on_init = function(_, _) |
| 22 | + M.buf_attach_copilot() |
16 | 23 | if vim.fn.has("nvim-0.7") > 0 then |
17 | 24 | vim.api.nvim_create_autocmd({ "BufEnter" }, { |
18 | | - callback = function() |
19 | | - util.attach_copilot() |
20 | | - end, |
| 25 | + callback = vim.schedule(function() M.buf_attach_copilot() end), |
21 | 26 | once = false, |
22 | 27 | }) |
23 | 28 | else |
24 | | - vim.cmd("au BufEnter * lua require('copilot.util').attach_copilot()") |
| 29 | + vim.cmd("au BufEnter * lua vim.schedule(function() require('copilot.copilot_handler').buf_attach_copilot() end)") |
25 | 30 | end |
26 | 31 | end, |
27 | 32 | on_attach = function() |
28 | | - vim.schedule(function() |
29 | | - params.on_attach() |
30 | | - end) |
| 33 | + vim.schedule_wrap(params.on_attach()) |
31 | 34 | end, |
32 | 35 | }) |
33 | 36 | end |
34 | 37 |
|
| 38 | +M.start = function(params) |
| 39 | + M.params = params |
| 40 | + vim.lsp.start_client(M.merge_server_opts(params)) |
| 41 | +end |
| 42 | + |
35 | 43 | return M |
0 commit comments