From 7e566c9687a6caa468903a77cfba7fe4fe62e2ba Mon Sep 17 00:00:00 2001 From: n3oney Date: Tue, 11 Apr 2023 14:38:27 +0200 Subject: [PATCH] refactor: use new mapping format --- modules/assistant/copilot/config.nix | 49 +++++++++++++++------- modules/assistant/copilot/copilot.nix | 18 ++++++++ modules/terminal/toggleterm/config.nix | 4 +- modules/terminal/toggleterm/toggleterm.nix | 2 +- 4 files changed, 56 insertions(+), 17 deletions(-) diff --git a/modules/assistant/copilot/config.nix b/modules/assistant/copilot/config.nix index 4ecac27..f147ea0 100644 --- a/modules/assistant/copilot/config.nix +++ b/modules/assistant/copilot/config.nix @@ -7,10 +7,6 @@ with lib; with builtins; let cfg = config.vim.assistant.copilot; - keyOrFalse = key: - if key != null - then "'${key}'" - else "false"; in { config = mkIf cfg.enable { vim.startPlugins = [ @@ -24,24 +20,47 @@ in { copilot_node_command = "${cfg.copilot_node_command}", panel = { keymap = { - jump_prev = ${keyOrFalse cfg.mappings.panel.jumpPrev}, - jump_next = ${keyOrFalse cfg.mappings.panel.jumpNext}, - accept = ${keyOrFalse cfg.mappings.panel.accept}, - refresh = ${keyOrFalse cfg.mappings.panel.refresh}, - open = ${keyOrFalse cfg.mappings.panel.open}, + jump_prev = false, + jump_next = false, + accept = false, + refresh = false, + open = false, + }, + layout = { + position = "${cfg.panel.position}", + ratio = ${toString cfg.panel.ratio}, }, }, suggestion = { keymap = { - accept = ${keyOrFalse cfg.mappings.suggestion.accept}, - accept_word = ${keyOrFalse cfg.mappings.suggestion.acceptWord}, - accept_line = ${keyOrFalse cfg.mappings.suggestion.acceptLine}, - next = ${keyOrFalse cfg.mappings.suggestion.next}, - prev = ${keyOrFalse cfg.mappings.suggestion.prev}, - dismiss = ${keyOrFalse cfg.mappings.suggestion.dismiss}, + accept = false, + accept_word = false, + accept_line = false, + next = false, + prev = false, + 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") + ]; }; } diff --git a/modules/assistant/copilot/copilot.nix b/modules/assistant/copilot/copilot.nix index fcf5a5d..5529dcb 100644 --- a/modules/assistant/copilot/copilot.nix +++ b/modules/assistant/copilot/copilot.nix @@ -9,6 +9,24 @@ with builtins; { options.vim.assistant.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 = { panel = { jumpPrev = mkOption { diff --git a/modules/terminal/toggleterm/config.nix b/modules/terminal/toggleterm/config.nix index c4934dc..e60b9b4 100644 --- a/modules/terminal/toggleterm/config.nix +++ b/modules/terminal/toggleterm/config.nix @@ -14,7 +14,7 @@ in { vim.luaConfigRC.toggleterm = nvim.dag.entryAnywhere '' require("toggleterm").setup({ - open_mapping = '${cfg.mappings.open}', + open_mapping = null, 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 size = function(term) @@ -32,5 +32,7 @@ in { }, }) ''; + + vim.maps.normal = mkBinding cfg.mappings.open "execute v:count . \"ToggleTerm\"" "Toggle terminal"; }; } diff --git a/modules/terminal/toggleterm/toggleterm.nix b/modules/terminal/toggleterm/toggleterm.nix index 8c60ea8..d563e22 100644 --- a/modules/terminal/toggleterm/toggleterm.nix +++ b/modules/terminal/toggleterm/toggleterm.nix @@ -9,7 +9,7 @@ with builtins; { enable = mkEnableOption "Enable toggleterm as a replacement to built-in terminal command"; mappings = { open = mkOption { - type = types.str; + type = types.nullOr types.str; description = "The keymapping to open toggleterm"; default = ""; };