forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
74 lines (58 loc) · 1.82 KB
/
init.lua
File metadata and controls
74 lines (58 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
local config = require("copilot.config")
local logger = require("copilot.logger")
local keymaps = require("copilot.keymaps")
local nes_api = require("copilot.nes.api")
local M = {
initialized = false,
}
---@param goto_end boolean Whether to move the cursor to the end of the accepted suggestion
---@return boolean
local function accept_suggestion(goto_end)
local result = nes_api.nes_apply_pending_nes()
if goto_end then
nes_api.nes_walk_cursor_end_edit()
end
return result
end
---@class NesKeymap
local function set_keymap(keymap)
keymaps.register_keymap_with_passthrough("n", keymap.accept_and_goto, function()
return accept_suggestion(true)
end, "[copilot] (nes) accept suggestion and go to end")
keymaps.register_keymap_with_passthrough("n", keymap.accept, function()
return accept_suggestion(false)
end, "[copilot] (nes) accept suggestion")
keymaps.register_keymap_with_passthrough("n", keymap.dismiss, function()
return nes_api.nes_clear()
end, "[copilot] (nes) dismiss suggestion")
end
---@param keymap NesKeymap
local function unset_keymap(keymap)
keymaps.unset_keymap_if_exists("n", keymap.accept_and_goto)
keymaps.unset_keymap_if_exists("n", keymap.accept)
keymaps.unset_keymap_if_exists("n", keymap.dismiss)
end
---@param lsp_client vim.lsp.Client
function M.setup(lsp_client)
if not config.nes.enabled then
return
end
local au = vim.api.nvim_create_augroup("copilotlsp.init", { clear = true })
local ok, err = pcall(function()
nes_api.nes_lsp_on_init(lsp_client, au)
end)
if ok then
logger.info("copilot-lsp nes loaded")
else
logger.error("copilot-lsp nes failed to load:", err)
end
set_keymap(config.nes.keymap)
M.initialized = true
end
function M.teardown()
if not M.initialized then
return
end
unset_keymap(config.nes.keymap)
end
return M