feat(copilot): custom setup opts

This commit is contained in:
Ching Pei Yang 2024-02-17 13:51:05 +01:00
parent 62b0791b75
commit 88c22ef026
2 changed files with 65 additions and 66 deletions

View file

@ -4,10 +4,10 @@
...
}: let
inherit (builtins) toJSON;
inherit (lib.nvim.lua) toLuaObject;
inherit (lib.modules) mkIf mkMerge;
inherit (lib.nvim.dag) entryAnywhere;
inherit (lib.lists) optionals;
inherit (lib.trivial) boolToString;
inherit (lib.nvim.binds) mkLuaBinding;
cfg = config.vim.assistant.copilot;
@ -28,55 +28,47 @@ in {
vim.startPlugins =
[
"copilot-lua"
cfg.copilotNodePackage
# cfg.copilotNodePackage
]
++ optionals (cfg.cmp.enable) [
"copilot-cmp"
];
vim.luaConfigRC.copilot = entryAnywhere ''
require("copilot").setup({
-- available options: https://github.com/zbirenbaum/copilot.lua
copilot_node_command = "${cfg.copilotNodeCommand}",
panel = {
enabled = ${boolToString (!cfg.cmp.enable)},
keymap = {
jump_prev = false,
jump_next = false,
accept = false,
refresh = false,
open = false,
},
layout = {
position = "${cfg.panel.position}",
ratio = ${toString cfg.panel.ratio},
},
},
suggestion = {
enabled = ${boolToString (!cfg.cmp.enable)},
keymap = {
accept = false,
accept_word = false,
accept_line = false,
next = false,
prev = false,
dismiss = false,
},
},
})
require("copilot").setup(${toLuaObject cfg.setupOpts})
${lib.optionalString (cfg.cmp.enable) ''
require("copilot_cmp").setup()
''}
'';
# 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;
};
};
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
function() require("copilot.panel").open({ position = "${cfg.setupOpts.panel.layout.position}", ratio = ${toString cfg.setupOpts.panel.layout.ratio}, }) end
''
cfg.mappings.panel.open) "[copilot] Accept suggestion")
];

View file

@ -4,31 +4,56 @@
lib,
...
}: let
inherit (lib) mkRenamedOptionModule;
inherit (lib.options) mkEnableOption mkOption;
inherit (lib.types) enum float nullOr str package;
inherit (lib.meta) getExe;
inherit (lib.types) nullOr str enum float;
inherit (lib.nvim.types) mkPluginSetupOption;
cfg = config.vim.assistant.copilot;
in {
imports = [
(mkRenamedOptionModule ["vim" "assistant" "copilot" "panel"] ["vim" "assistant" "copilot" "setupOpts" "panel"])
(mkRenamedOptionModule ["vim" "assistant" "copilot" "copilotNodeCommand"] ["vim" "assistant" "copilot" "setupOpts" "copilot_node_command"])
(mkRenamedOptionModule ["vim" "assistant" "copilot" "copilotNodePackage"] ["vim" "assistant" "copilot" "setupOpts" "copilot_node_command"])
];
options.vim.assistant.copilot = {
enable = mkEnableOption "GitHub Copilot AI assistant";
cmp.enable = mkEnableOption "nvim-cmp integration for GitHub Copilot";
panel = {
position = mkOption {
type = enum [
"bottom"
"top"
"left"
"right"
];
default = "bottom";
description = "Panel position";
setupOpts = mkPluginSetupOption "Copilot" {
copilot_node_command = mkOption {
type = str;
default = "${lib.getExe pkgs.nodejs-slim}";
description = ''
The command that will be executed to initiate nodejs for GitHub Copilot.
Recommended to leave as default.
'';
};
ratio = mkOption {
type = float;
default = 0.4;
description = "Panel size";
panel = {
enabled = mkEnableOption "Completion Panel" // {default = !cfg.cmp.enable;};
layout = {
position = mkOption {
type = enum [
"bottom"
"top"
"left"
"right"
];
default = "bottom";
description = "Panel position";
};
ratio = mkOption {
type = float;
default = 0.4;
description = "Panel size";
};
};
};
suggestion = {
enabled = mkEnableOption "Suggestions" // {default = !cfg.cmp.enable;};
# keymap = { };
};
};
@ -102,23 +127,5 @@ in {
};
};
};
copilotNodeCommand = mkOption {
type = str;
default = "${getExe cfg.copilotNodePackage}";
description = ''
The command that will be executed to initiate nodejs for GitHub Copilot.
Recommended to leave as default.
'';
};
copilotNodePackage = mkOption {
type = nullOr package;
default = pkgs.nodejs-slim;
description = ''
The nodeJS package that will be used for GitHub Copilot. If you are using a custom node command
you may want to set this option to null so that the package is not pulled from nixpkgs.
'';
};
};
}