Skip to content

Commit 8ab1f54

Browse files
committed
1 parent fc58bfa commit 8ab1f54

File tree

4 files changed

+29
-22
lines changed

4 files changed

+29
-22
lines changed

lua/copilot/copilot_handler.lua

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
11
local util = require("copilot.util")
2-
local M = {}
2+
local M = { params = {} }
33

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
613

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, {
916
cmd = { require("copilot.util").get_copilot_path(params.plugin_manager_path) },
1017
name = "copilot",
1118
trace = "messages",
1219
root_dir = vim.loop.cwd(),
1320
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()
1623
if vim.fn.has("nvim-0.7") > 0 then
1724
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),
2126
once = false,
2227
})
2328
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)")
2530
end
2631
end,
2732
on_attach = function()
28-
vim.schedule(function()
29-
params.on_attach()
30-
end)
33+
vim.schedule_wrap(params.on_attach())
3134
end,
3235
})
3336
end
3437

38+
M.start = function(params)
39+
M.params = params
40+
vim.lsp.start_client(M.merge_server_opts(params))
41+
end
42+
3543
return M

lua/copilot/init.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ local defaults = {
55
on_attach = function()
66
require("copilot_cmp")._on_insert_enter()
77
end,
8+
startup_function = function()
9+
vim.defer_fn(function()
10+
require("copilot_cmp")._on_insert_enter()
11+
end, 100)
12+
end,
13+
server_opts_overrides = {},
14+
ft_disable = {},
815
}
916

1017
local config_handler = function(opts)

lua/copilot/util.lua

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local M = {}
22

3-
43
local format_pos = function()
54
local pos = vim.api.nvim_win_get_cursor(0)
65
return { character = pos[2], line = pos[1] - 1 }
@@ -27,13 +26,6 @@ M.find_copilot_buf_client = function()
2726
end
2827
end
2928

30-
M.attach_copilot = function()
31-
local client_id = require("copilot.util").find_copilot_client()
32-
if client_id and not vim.lsp.buf_get_clients(0)[client_id] then
33-
vim.lsp.buf_attach_client(0, client_id)
34-
end
35-
end
36-
3729
M.get_completion_params = function()
3830
local params = {
3931
options = vim.empty_dict(),

0 commit comments

Comments
 (0)