1
0
Fork 0
mirror of https://github.com/NotAShelf/neovim-flake.git synced 2024-12-28 02:39:49 +01:00
neovim-flake/modules/assistant/copilot/config.nix

90 lines
3.3 KiB
Nix
Raw Normal View History

{
pkgs,
config,
lib,
...
}:
with lib;
with builtins; let
cfg = config.vim.assistant.copilot;
wrapPanelBinding = luaFunction: key: ''
function()
local s, _ = pcall(${luaFunction})
if not s then
local termcode = vim.api.nvim_replace_termcodes(${builtins.toJSON key}, true, false, true)
vim.fn.feedkeys(termcode, 'n')
end
end
'';
in {
config = mkIf cfg.enable {
vim.startPlugins =
[
"copilot-lua"
cfg.copilotNodePackage
]
++ lib.optionals (cfg.cmp.enable) [
"copilot-cmp"
];
vim.luaConfigRC.copilot = nvim.dag.entryAnywhere ''
require("copilot").setup({
-- available options: https://github.com/zbirenbaum/copilot.lua
copilot_node_command = "${cfg.copilotNodeCommand}",
2023-04-05 00:53:21 +02:00
panel = {
enabled = ${lib.boolToString (!cfg.cmp.enable)},
2023-04-05 00:53:21 +02:00
keymap = {
2023-04-11 14:38:27 +02:00
jump_prev = false,
jump_next = false,
accept = false,
refresh = false,
open = false,
},
layout = {
position = "${cfg.panel.position}",
ratio = ${toString cfg.panel.ratio},
2023-04-05 00:53:21 +02:00
},
},
suggestion = {
enabled = ${lib.boolToString (!cfg.cmp.enable)},
2023-04-05 00:53:21 +02:00
keymap = {
2023-04-11 14:38:27 +02:00
accept = false,
accept_word = false,
accept_line = false,
next = false,
prev = false,
dismiss = false,
2023-04-05 00:53:21 +02:00
},
},
})
${lib.optionalString (cfg.cmp.enable) ''
require("copilot_cmp").setup()
''}
'';
2023-04-11 14:38:27 +02:00
vim.maps.normal = mkMerge [
(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 ''
function() require("copilot.panel").open({ position = "${cfg.panel.position}", ratio = ${toString cfg.panel.ratio}, }) end
''
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")
];
};
}