|
| 1 | +local config = require("copilot.config") |
| 2 | +local logger = require("copilot.logger") |
| 3 | +local util = require("copilot.util") |
| 4 | + |
| 5 | +local M = { |
| 6 | + initialized = false, |
| 7 | +} |
| 8 | + |
| 9 | +---@param goto_end boolean Whether to move the cursor to the end of the accepted suggestion |
| 10 | +---@return boolean |
| 11 | +local function accept_suggestion(goto_end) |
| 12 | + local nes_api = require("copilot-lsp.api") |
| 13 | + local result = nes_api.nes_apply_pending_nes() |
| 14 | + |
| 15 | + if goto_end then |
| 16 | + nes_api.nes_walk_cursor_end_edit() |
| 17 | + end |
| 18 | + |
| 19 | + return result |
| 20 | +end |
| 21 | + |
| 22 | +---@class NesKeymap |
| 23 | +local function set_keymap(keymap) |
| 24 | + if keymap.accept_and_goto then |
| 25 | + vim.keymap.set("n", keymap.accept_and_goto, function() |
| 26 | + if not accept_suggestion(true) then |
| 27 | + -- Pass through the key |
| 28 | + vim.api.nvim_feedkeys(keymap.accept_and_goto, "n", false) |
| 29 | + end |
| 30 | + end, { |
| 31 | + desc = "[copilot] (nes) accept suggestion and go to end", |
| 32 | + silent = true, |
| 33 | + }) |
| 34 | + end |
| 35 | + |
| 36 | + if keymap.accept then |
| 37 | + vim.keymap.set("n", keymap.accept, function() |
| 38 | + if not accept_suggestion(false) then |
| 39 | + -- Pass through the key |
| 40 | + vim.api.nvim_feedkeys(keymap.accept, "n", false) |
| 41 | + end |
| 42 | + end, { |
| 43 | + desc = "[copilot] (nes) accept suggestion", |
| 44 | + silent = true, |
| 45 | + }) |
| 46 | + end |
| 47 | + |
| 48 | + if keymap.dismiss then |
| 49 | + vim.keymap.set("n", keymap.dismiss, function() |
| 50 | + local cleared = require("copilot-lsp.api").nes_clear() |
| 51 | + -- Pass through the key |
| 52 | + if not cleared then |
| 53 | + vim.api.nvim_feedkeys(keymap.dismiss, "n", false) |
| 54 | + end |
| 55 | + end, { |
| 56 | + desc = "[copilot] (nes) dismiss suggestion", |
| 57 | + silent = true, |
| 58 | + }) |
| 59 | + end |
| 60 | +end |
| 61 | + |
| 62 | +---@param keymap NesKeymap |
| 63 | +local function unset_keymap(keymap) |
| 64 | + util.unset_keymap_if_exists("n", keymap.accept_and_goto) |
| 65 | + util.unset_keymap_if_exists("n", keymap.accept) |
| 66 | + util.unset_keymap_if_exists("n", keymap.dismiss) |
| 67 | +end |
| 68 | + |
| 69 | +---@param lsp_client vim.lsp.Client |
| 70 | +function M.setup(lsp_client) |
| 71 | + if not config.nes.enabled then |
| 72 | + return |
| 73 | + end |
| 74 | + |
| 75 | + local au = vim.api.nvim_create_augroup("copilotlsp.init", { clear = true }) |
| 76 | + |
| 77 | + local ok, err = pcall(function() |
| 78 | + require("copilot-lsp.api").nes_lsp_on_init(lsp_client, au) |
| 79 | + end) |
| 80 | + |
| 81 | + if ok then |
| 82 | + logger.info("copilot-lsp nes loaded") |
| 83 | + else |
| 84 | + logger.error("copilot-lsp nes failed to load:", err) |
| 85 | + end |
| 86 | + |
| 87 | + set_keymap(config.nes.keymap) |
| 88 | + M.initialized = true |
| 89 | +end |
| 90 | + |
| 91 | +function M.teardown() |
| 92 | + if not M.initialized then |
| 93 | + return |
| 94 | + end |
| 95 | + |
| 96 | + unset_keymap(config.nes.keymap) |
| 97 | +end |
| 98 | + |
| 99 | +return M |
0 commit comments