|
| 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 | +local function accept_suggestion(goto_end) |
| 11 | + local nes_api = require("copilot-lsp.api") |
| 12 | + nes_api.nes_apply_pending_nes() |
| 13 | + |
| 14 | + if goto_end then |
| 15 | + nes_api.nes_walk_cursor_end_edit() |
| 16 | + end |
| 17 | +end |
| 18 | + |
| 19 | +local previous_keymaps = {} |
| 20 | +---@class NesKeymap |
| 21 | +local function set_keymap(keymap) |
| 22 | + if keymap.accept_and_goto then |
| 23 | + vim.keymap.set("n", keymap.accept_and_goto, function() |
| 24 | + accept_suggestion(true) |
| 25 | + end, { |
| 26 | + desc = "[copilot] (nes) accept suggestion and go to end", |
| 27 | + silent = true, |
| 28 | + }) |
| 29 | + end |
| 30 | + |
| 31 | + if keymap.accept then |
| 32 | + vim.keymap.set("n", keymap.accept, function() |
| 33 | + accept_suggestion(false) |
| 34 | + end, { |
| 35 | + desc = "[copilot] (nes) accept suggestion", |
| 36 | + silent = true, |
| 37 | + }) |
| 38 | + end |
| 39 | + |
| 40 | + -- This is to get cancellation keys to work like <Esc> when there is nothing to cancel |
| 41 | + if keymap.dismiss then |
| 42 | + -- Save previous mapping |
| 43 | + local rhs = vim.fn.maparg(keymap.dismiss, "n", false, true) |
| 44 | + if rhs and rhs.lhs ~= "" then |
| 45 | + previous_keymaps[keymap.dismiss] = rhs |
| 46 | + else |
| 47 | + previous_keymaps[keymap.dismiss] = nil |
| 48 | + end |
| 49 | + |
| 50 | + vim.keymap.set("n", keymap.dismiss, function() |
| 51 | + local cleared = require("copilot-lsp.api").nes_clear() |
| 52 | + if not cleared and previous_keymaps[keymap.dismiss] then |
| 53 | + -- Call the previous mapping |
| 54 | + local prev = previous_keymaps[keymap.dismiss] |
| 55 | + if prev.callback then |
| 56 | + prev.callback() |
| 57 | + elseif prev.rhs and prev.rhs ~= "" then |
| 58 | + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(prev.rhs, true, false, true), "n", false) |
| 59 | + end |
| 60 | + end |
| 61 | + end, { |
| 62 | + desc = "[copilot] (nes) dismiss suggestion", |
| 63 | + silent = true, |
| 64 | + }) |
| 65 | + end |
| 66 | +end |
| 67 | + |
| 68 | +---@param keymap NesKeymap |
| 69 | +local function unset_keymap(keymap) |
| 70 | + util.unset_keymap_if_exists("n", keymap.accept_and_goto) |
| 71 | + util.unset_keymap_if_exists("n", keymap.accept) |
| 72 | + util.unset_keymap_if_exists("n", keymap.dismiss) |
| 73 | +end |
| 74 | + |
| 75 | +---@param lsp_client vim.lsp.Client |
| 76 | +function M.setup(lsp_client) |
| 77 | + if not config.nes.enabled then |
| 78 | + return |
| 79 | + end |
| 80 | + |
| 81 | + local au = vim.api.nvim_create_augroup("copilotlsp.init", { clear = true }) |
| 82 | + |
| 83 | + local ok, err = pcall(function() |
| 84 | + require("copilot-lsp.api").nes_lsp_on_init(lsp_client, au) |
| 85 | + end) |
| 86 | + |
| 87 | + if ok then |
| 88 | + logger.info("copilot-lsp nes loaded") |
| 89 | + else |
| 90 | + logger.error("copilot-lsp nes failed to load:", err) |
| 91 | + end |
| 92 | + |
| 93 | + set_keymap(config.nes.keymap) |
| 94 | + M.initialized = true |
| 95 | +end |
| 96 | + |
| 97 | +function M.teardown() |
| 98 | + if not M.initialized then |
| 99 | + return |
| 100 | + end |
| 101 | + |
| 102 | + unset_keymap(config.nes.keymap) |
| 103 | +end |
| 104 | + |
| 105 | +return M |
0 commit comments