refactor: use new mapping format

This commit is contained in:
n3oney 2023-04-11 14:38:27 +02:00
parent 86d91dbac5
commit 136958af36
No known key found for this signature in database
GPG key ID: C786693DE727850E
4 changed files with 56 additions and 17 deletions

View file

@ -7,10 +7,6 @@
with lib; with lib;
with builtins; let with builtins; let
cfg = config.vim.assistant.copilot; cfg = config.vim.assistant.copilot;
keyOrFalse = key:
if key != null
then "'${key}'"
else "false";
in { in {
config = mkIf cfg.enable { config = mkIf cfg.enable {
vim.startPlugins = [ vim.startPlugins = [
@ -24,24 +20,47 @@ in {
copilot_node_command = "${cfg.copilot_node_command}", copilot_node_command = "${cfg.copilot_node_command}",
panel = { panel = {
keymap = { keymap = {
jump_prev = ${keyOrFalse cfg.mappings.panel.jumpPrev}, jump_prev = false,
jump_next = ${keyOrFalse cfg.mappings.panel.jumpNext}, jump_next = false,
accept = ${keyOrFalse cfg.mappings.panel.accept}, accept = false,
refresh = ${keyOrFalse cfg.mappings.panel.refresh}, refresh = false,
open = ${keyOrFalse cfg.mappings.panel.open}, open = false,
},
layout = {
position = "${cfg.panel.position}",
ratio = ${toString cfg.panel.ratio},
}, },
}, },
suggestion = { suggestion = {
keymap = { keymap = {
accept = ${keyOrFalse cfg.mappings.suggestion.accept}, accept = false,
accept_word = ${keyOrFalse cfg.mappings.suggestion.acceptWord}, accept_word = false,
accept_line = ${keyOrFalse cfg.mappings.suggestion.acceptLine}, accept_line = false,
next = ${keyOrFalse cfg.mappings.suggestion.next}, next = false,
prev = ${keyOrFalse cfg.mappings.suggestion.prev}, prev = false,
dismiss = ${keyOrFalse cfg.mappings.suggestion.dismiss}, dismiss = false,
}, },
}, },
}) })
''; '';
vim.maps.normal = mkMerge [
(mkLuaBinding cfg.mappings.panel.jumpPrev "require(\"copilot.panel\").jump_prev" "[copilot] Accept suggestion")
(mkLuaBinding cfg.mappings.panel.jumpNext "require(\"copilot.panel\").jump_next" "[copilot] Accept suggestion")
(mkLuaBinding cfg.mappings.panel.accept "require(\"copilot.panel\").accept" "[copilot] Accept suggestion")
(mkLuaBinding cfg.mappings.panel.refresh "require(\"copilot.panel\").refresh" "[copilot] Accept suggestion")
(mkLuaBinding cfg.mappings.panel.open ''
function() require("copilot.panel").open({ position = "${cfg.panel.position}", ratio = ${toString cfg.panel.ratio}, }) end
'' "[copilot] Accept suggestion")
];
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")
];
}; };
} }

View file

@ -9,6 +9,24 @@ with builtins; {
options.vim.assistant.copilot = { options.vim.assistant.copilot = {
enable = mkEnableOption "Enable GitHub Copilot"; enable = mkEnableOption "Enable GitHub Copilot";
panel = {
position = mkOption {
type = types.enum [
"bottom"
"top"
"left"
"right"
];
default = "bottom";
description = "Panel position";
};
ratio = mkOption {
type = types.float;
default = 0.4;
description = "Panel size";
};
};
mappings = { mappings = {
panel = { panel = {
jumpPrev = mkOption { jumpPrev = mkOption {

View file

@ -14,7 +14,7 @@ in {
vim.luaConfigRC.toggleterm = nvim.dag.entryAnywhere '' vim.luaConfigRC.toggleterm = nvim.dag.entryAnywhere ''
require("toggleterm").setup({ require("toggleterm").setup({
open_mapping = '${cfg.mappings.open}', open_mapping = null,
direction = '${toString cfg.direction}', direction = '${toString cfg.direction}',
-- TODO: this should probably be turned into a module that uses the lua function if and only if the user has not set it -- TODO: this should probably be turned into a module that uses the lua function if and only if the user has not set it
size = function(term) size = function(term)
@ -32,5 +32,7 @@ in {
}, },
}) })
''; '';
vim.maps.normal = mkBinding cfg.mappings.open "<Cmd>execute v:count . \"ToggleTerm\"<CR>" "Toggle terminal";
}; };
} }

View file

@ -9,7 +9,7 @@ with builtins; {
enable = mkEnableOption "Enable toggleterm as a replacement to built-in terminal command"; enable = mkEnableOption "Enable toggleterm as a replacement to built-in terminal command";
mappings = { mappings = {
open = mkOption { open = mkOption {
type = types.str; type = types.nullOr types.str;
description = "The keymapping to open toggleterm"; description = "The keymapping to open toggleterm";
default = "<c-t>"; default = "<c-t>";
}; };