2023-02-27 15:05:43 +01:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
...
|
2023-11-07 01:50:27 +01:00
|
|
|
}: let
|
2023-11-14 08:18:32 +01:00
|
|
|
inherit (builtins) toJSON;
|
2024-02-17 13:51:05 +01:00
|
|
|
inherit (lib.nvim.lua) toLuaObject;
|
2024-02-26 06:05:23 +01:00
|
|
|
inherit (lib.modules) mkIf mkMerge;
|
|
|
|
inherit (lib.nvim.dag) entryAnywhere;
|
|
|
|
inherit (lib.lists) optionals;
|
|
|
|
inherit (lib.nvim.binds) mkLuaBinding;
|
2023-11-07 01:50:27 +01:00
|
|
|
|
2023-02-27 15:05:43 +01:00
|
|
|
cfg = config.vim.assistant.copilot;
|
2023-04-22 17:30:32 +02:00
|
|
|
|
|
|
|
wrapPanelBinding = luaFunction: key: ''
|
|
|
|
function()
|
|
|
|
local s, _ = pcall(${luaFunction})
|
|
|
|
|
|
|
|
if not s then
|
2023-11-14 08:18:32 +01:00
|
|
|
local termcode = vim.api.nvim_replace_termcodes(${toJSON key}, true, false, true)
|
2023-04-22 17:30:32 +02:00
|
|
|
|
|
|
|
vim.fn.feedkeys(termcode, 'n')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
'';
|
2023-02-27 15:05:43 +01:00
|
|
|
in {
|
|
|
|
config = mkIf cfg.enable {
|
2023-07-24 15:51:24 +02:00
|
|
|
vim.startPlugins =
|
|
|
|
[
|
|
|
|
"copilot-lua"
|
2024-02-17 13:51:05 +01:00
|
|
|
# cfg.copilotNodePackage
|
2023-07-24 15:51:24 +02:00
|
|
|
]
|
2024-07-08 23:57:58 +02:00
|
|
|
++ optionals cfg.cmp.enable [
|
2023-07-24 15:51:24 +02:00
|
|
|
"copilot-cmp"
|
|
|
|
];
|
2023-02-27 15:05:43 +01:00
|
|
|
|
2024-07-15 12:40:05 +02:00
|
|
|
vim.pluginRC.copilot = entryAnywhere ''
|
2024-02-17 13:51:05 +01:00
|
|
|
require("copilot").setup(${toLuaObject cfg.setupOpts})
|
2023-07-24 15:51:24 +02:00
|
|
|
|
2024-07-08 23:57:58 +02:00
|
|
|
${lib.optionalString cfg.cmp.enable ''
|
2023-07-24 15:51:24 +02:00
|
|
|
require("copilot_cmp").setup()
|
|
|
|
''}
|
2023-02-27 15:05:43 +01:00
|
|
|
'';
|
2023-04-11 14:38:27 +02:00
|
|
|
|
2024-02-17 13:51:05 +01:00
|
|
|
# Disable plugin handled keymaps.
|
|
|
|
# Setting it here so that it doesn't show up in user docs
|
|
|
|
vim.assistant.copilot.setupOpts = {
|
|
|
|
panel.keymap = {
|
|
|
|
jump_prev = lib.mkDefault false;
|
|
|
|
jump_next = lib.mkDefault false;
|
|
|
|
accept = lib.mkDefault false;
|
|
|
|
refresh = lib.mkDefault false;
|
|
|
|
open = lib.mkDefault false;
|
|
|
|
};
|
|
|
|
suggestion.keymap = {
|
|
|
|
accept = lib.mkDefault false;
|
|
|
|
accept_word = lib.mkDefault false;
|
|
|
|
accept_line = lib.mkDefault false;
|
|
|
|
next = lib.mkDefault false;
|
|
|
|
prev = lib.mkDefault false;
|
|
|
|
dismiss = lib.mkDefault false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-04-11 14:38:27 +02:00
|
|
|
vim.maps.normal = mkMerge [
|
2023-04-22 17:30:32 +02:00
|
|
|
(mkLuaBinding cfg.mappings.panel.jumpPrev (wrapPanelBinding "require(\"copilot.panel\").jump_prev" cfg.mappings.panel.jumpPrev) "[copilot] Accept suggestion")
|
|
|
|
(mkLuaBinding cfg.mappings.panel.jumpNext (wrapPanelBinding "require(\"copilot.panel\").jump_next" cfg.mappings.panel.jumpNext) "[copilot] Accept suggestion")
|
|
|
|
(mkLuaBinding cfg.mappings.panel.accept (wrapPanelBinding ''require("copilot.panel").accept'' cfg.mappings.panel.accept) "[copilot] Accept suggestion")
|
|
|
|
(mkLuaBinding cfg.mappings.panel.refresh (wrapPanelBinding "require(\"copilot.panel\").refresh" cfg.mappings.panel.refresh) "[copilot] Accept suggestion")
|
|
|
|
(mkLuaBinding cfg.mappings.panel.open (wrapPanelBinding ''
|
2024-02-17 13:51:05 +01:00
|
|
|
function() require("copilot.panel").open({ position = "${cfg.setupOpts.panel.layout.position}", ratio = ${toString cfg.setupOpts.panel.layout.ratio}, }) end
|
2023-04-22 17:30:32 +02:00
|
|
|
''
|
|
|
|
cfg.mappings.panel.open) "[copilot] Accept suggestion")
|
2023-04-11 14:38:27 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
vim.maps.insert = mkMerge [
|
|
|
|
(mkLuaBinding cfg.mappings.suggestion.accept "require(\"copilot.suggestion\").accept" "[copilot] Accept suggestion")
|
|
|
|
(mkLuaBinding cfg.mappings.suggestion.acceptLine "require(\"copilot.suggestion\").accept_line" "[copilot] Accept suggestion (line)")
|
|
|
|
(mkLuaBinding cfg.mappings.suggestion.acceptWord "require(\"copilot.suggestion\").accept_word" "[copilot] Accept suggestion (word)")
|
|
|
|
(mkLuaBinding cfg.mappings.suggestion.next "require(\"copilot.suggestion\").next" "[copilot] next suggestion")
|
|
|
|
(mkLuaBinding cfg.mappings.suggestion.prev "require(\"copilot.suggestion\").prev" "[copilot] previous suggestion")
|
|
|
|
(mkLuaBinding cfg.mappings.suggestion.dismiss "require(\"copilot.suggestion\").dismiss" "[copilot] dismiss suggestion")
|
|
|
|
];
|
2023-02-27 15:05:43 +01:00
|
|
|
};
|
|
|
|
}
|