Skip to content

Commit

Permalink
feat(copilot): custom setup opts
Browse files Browse the repository at this point in the history
  • Loading branch information
horriblename committed Feb 17, 2024
1 parent 2b17496 commit c48481e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 64 deletions.
54 changes: 23 additions & 31 deletions modules/assistant/copilot/config.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,55 +25,47 @@ in {
vim.startPlugins =
[
"copilot-lua"
cfg.copilotNodePackage
# 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}",
panel = {
enabled = ${lib.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 = ${lib.boolToString (!cfg.cmp.enable)},
keymap = {
accept = false,
accept_word = false,
accept_line = false,
next = false,
prev = false,
dismiss = false,
},
},
})
require("copilot").setup(${nvim.lua.expToLua 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")
];
Expand Down
73 changes: 40 additions & 33 deletions modules/assistant/copilot/copilot.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,54 @@
lib,
...
}: let
inherit (lib) mkEnableOption mkOption types;
inherit (lib) mkEnableOption mkOption types mkRenamedOptionModule;
inherit (lib.nvim.types) mkPluginSetupOption;

cfg = config.vim.assistant.copilot;
in {
options.vim.assistant.copilot = {
enable = mkEnableOption "GitHub Copilot AI assistant";
cmp.enable = mkEnableOption "nvim-cmp integration for GitHub Copilot";

panel = {
position = mkOption {
type = types.enum [
"bottom"
"top"
"left"
"right"
];
default = "bottom";
description = "Panel position";
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"])
];

setupOpts = mkPluginSetupOption "Copilot" {
copilot_node_command = mkOption {
type = types.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 = types.float;
default = 0.4;
description = "Panel size";
panel = {
enabled = mkEnableOption "Completion Panel" // {default = !cfg.cmp.enable;};
layout = {
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";
};
};
};

suggestion = {
enabled = mkEnableOption "Suggestions" // {default = !cfg.cmp.enable;};
# keymap = { };
};
};

Expand Down Expand Up @@ -91,23 +116,5 @@ in {
};
};
};

copilotNodeCommand = mkOption {
type = types.str;
default = "${lib.getExe cfg.copilotNodePackage}";
description = ''
The command that will be executed to initiate nodejs for GitHub Copilot.
Recommended to leave as default.
'';
};

copilotNodePackage = mkOption {
type = with types; 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.
'';
};
};
}

0 comments on commit c48481e

Please sign in to comment.