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
82 lines (64 loc) · 1.96 KB
/
init.lua
File metadata and controls
82 lines (64 loc) · 1.96 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
75
76
77
78
79
80
81
82
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 and result then
nes_api.nes_walk_cursor_end_edit()
end
return result
end
---@param bufnr integer
function M.set_keymap(bufnr)
if not config.nes.enabled then
return
end
local keymap = config.nes.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", bufnr)
keymaps.register_keymap_with_passthrough("n", keymap.accept, function()
return accept_suggestion(false)
end, "[copilot] (nes) accept suggestion", bufnr)
keymaps.register_keymap_with_passthrough("n", keymap.dismiss, function()
return nes_api.nes_clear()
end, "[copilot] (nes) dismiss suggestion", bufnr)
end
---@param bufnr integer
function M.unset_keymap(bufnr)
if not config.nes.enabled then
return
end
local keymap = config.nes.keymap
keymaps.unset_keymap_if_exists("n", keymap.accept_and_goto, bufnr)
keymaps.unset_keymap_if_exists("n", keymap.accept, bufnr)
keymaps.unset_keymap_if_exists("n", keymap.dismiss, bufnr)
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
M.initialized = true
end
function M.teardown()
if not M.initialized then
return
end
end
return M