plugins/copilot-lua: migrate to mkNeovimPlugin

This commit is contained in:
Heitor Augusto 2024-12-25 07:34:33 -03:00
parent a352bb89b0
commit 73303938ee
No known key found for this signature in database
6 changed files with 322 additions and 228 deletions

View file

@ -47,7 +47,7 @@ lib.nixvim.plugins.mkNeovimPlugin {
extraConfig = { extraConfig = {
warnings = warnings =
let let
copilot-lua-cfg = config.plugins.copilot-lua; copilot-lua-cfg = config.plugins.copilot-lua.settings;
isEnabled = b: builtins.isBool b && b; isEnabled = b: builtins.isBool b && b;
in in
lib.optionals (isEnabled copilot-lua-cfg.suggestion.enabled) [ lib.optionals (isEnabled copilot-lua-cfg.suggestion.enabled) [

View file

@ -57,7 +57,7 @@ lib.nixvim.plugins.mkNeovimPlugin {
extraConfig = { extraConfig = {
warnings = warnings =
let let
copilot-lua-cfg = config.plugins.copilot-lua; copilot-lua-cfg = config.plugins.copilot-lua.settings;
isEnabled = b: (lib.isBool b && b); isEnabled = b: (lib.isBool b && b);
in in
lib.optional (isEnabled copilot-lua-cfg.suggestion.enabled) '' lib.optional (isEnabled copilot-lua-cfg.suggestion.enabled) ''

View file

@ -1,172 +1,181 @@
{ {
lib,
helpers,
config, config,
lib,
pkgs, pkgs,
... ...
}: }:
with lib;
let let
cfg = config.plugins.copilot-lua; inherit (lib) types;
inherit (lib.nixvim) defaultNullOpts;
in in
{ lib.nixvim.plugins.mkNeovimPlugin {
options = { name = "copilot-lua";
plugins.copilot-lua = moduleName = "copilot";
let packPathName = "copilot.lua";
keymapOption = helpers.defaultNullOpts.mkNullable (with types; either (enum [ false ]) str);
in
lib.nixvim.plugins.neovim.extraOptionsOptions
// {
enable = mkEnableOption "copilot.lua";
package = lib.mkPackageOption pkgs "copilot.lua" { maintainers = [ lib.maintainers.HeitorAugustoLN ];
default = [
"vimPlugins" settingsOptions =
"copilot-lua" let
]; mkKeymapOption = defaultNullOpts.mkNullableWithRaw (with types; either (enum [ false ]) str);
in
{
panel = {
enabled = defaultNullOpts.mkBool true "Enable the panel.";
auto_refresh = defaultNullOpts.mkBool false "Enable auto-refresh.";
keymap = {
jump_prev = mkKeymapOption "[[" "Keymap for jumping to the previous suggestion.";
jump_next = mkKeymapOption "]]" "Keymap for jumping to the next suggestion.";
accept = mkKeymapOption "<CR>" "Keymap to accept the proposed suggestion.";
refresh = mkKeymapOption "gr" "Keymap to refresh the suggestions.";
open = mkKeymapOption "<M-CR>" "Keymap to open.";
}; };
panel = { layout = {
enabled = helpers.defaultNullOpts.mkBool true "Enable the panel."; position =
defaultNullOpts.mkEnumFirstDefault
[
"bottom"
"top"
"left"
"right"
"horizontal"
"vertical"
]
''
The panel position.
'';
autoRefresh = helpers.defaultNullOpts.mkBool false "Enable auto-refresh."; ratio = defaultNullOpts.mkProportion 0.4 ''
The panel ratio.
keymap = { '';
jumpPrev = keymapOption "[[" "Keymap for jumping to the previous suggestion.";
jumpNext = keymapOption "]]" "Keymap for jumping to the next suggestion.";
accept = keymapOption "<CR>" "Keymap to accept the proposed suggestion.";
refresh = keymapOption "gr" "Keymap to refresh the suggestions.";
open = keymapOption "<M-CR>" "Keymap to open.";
};
layout = {
position =
helpers.defaultNullOpts.mkEnumFirstDefault
[
"bottom"
"top"
"left"
"right"
]
''
The panel position.
'';
ratio = helpers.defaultNullOpts.mkProportion 0.4 ''
The panel ratio.
'';
};
}; };
};
suggestion = { suggestion = {
enabled = helpers.defaultNullOpts.mkBool true "Enable suggestion."; enabled = defaultNullOpts.mkBool true "Enable suggestion.";
auto_trigger = defaultNullOpts.mkBool false "Enable auto-trigger.";
hide_during_completion = defaultNullOpts.mkBool true "Hide during completion.";
debounce = defaultNullOpts.mkInt 75 "Debounce.";
autoTrigger = helpers.defaultNullOpts.mkBool false "Enable auto-trigger."; keymap = {
accept = mkKeymapOption "<M-l>" "Keymap for accepting the suggestion.";
debounce = helpers.defaultNullOpts.mkInt 75 "Debounce."; accept_word = mkKeymapOption false "Keymap for accepting a word suggestion.";
accept_line = mkKeymapOption false "Keymap for accepting a line suggestion.";
keymap = { next = mkKeymapOption "<M-]>" "Keymap for accepting the next suggestion.";
accept = keymapOption "<M-l>" "Keymap for accepting the suggestion."; prev = mkKeymapOption "<M-[>" "Keymap for accepting the previous suggestion.";
dismiss = mkKeymapOption "<C-]>" "Keymap to dismiss the suggestion.";
acceptWord = keymapOption false "Keymap for accepting a word suggestion.";
acceptLine = keymapOption false "Keymap for accepting a line suggestion.";
next = keymapOption "<M-]>" "Keymap for accepting the next suggestion.";
prev = keymapOption "<M-[>" "Keymap for accepting the previous suggestion.";
dismiss = keymapOption "<C-]>" "Keymap to dismiss the suggestion.";
};
}; };
};
filetypes = filetypes = defaultNullOpts.mkAttrsOf' {
helpers.defaultNullOpts.mkAttrsOf types.bool type = types.bool;
{ pluginDefault = {
yaml = false; yaml = true;
markdown = false; markdown = false;
help = false; help = false;
gitcommit = false; gitcommit = false;
gitrebase = false; gitrebase = false;
hgcommit = false; hgcommit = false;
svn = false; svn = false;
cvs = false; cvs = false;
"." = false; "." = false;
} };
'' example = lib.literalExpression ''
Specify filetypes for attaching copilot. {
Each value can be either a boolean or a lua function that returns a boolean. markdown = true; # overrides default
terraform = false; # disallow specific filetype
sh.__raw = '''
function()
if string.match(vim.fs.basename(vim.api.nvim_buf_get_name(0)), '^%.env.*') then
-- disable for .env files
return false
end
return true
end
''';
javascript = true; # allow specific type
"*" = false; # disable for all other filetypes and ignore default filetypes
}
'';
description = ''
Specify filetypes for attaching copilot.
Each value can be either a boolean or a lua function that returns a boolean.
'';
};
Example: copilot_node_command =
```nix let
{ inherit (config.plugins.copilot-lua) nodePackage;
markdown = true; # overrides default in
terraform = false; # disallow specific filetype lib.nixvim.mkNullOrOption' {
sh.__raw = \'\'
function ()
if string.match(vim.fs.basename(vim.api.nvim_buf_get_name(0)), '^%.env.*') then
-- disable for .env files
return false
end
return true
end
\'\';
}
```
The key `"*"` can be used to disable the default configuration.
Example:
```nix
{
javascript = true; # allow specific filetype
typescript = true; # allow specific filetype
"*" = false; # disable for all other filetypes and ignore default `filetypes`
}
```
'';
copilotNodeCommand = mkOption {
type = types.str; type = types.str;
default = "${pkgs.nodejs-18_x}/bin/node"; default = if nodePackage == null then null else lib.getExe nodePackage;
defaultText = lib.literalMD "`lib.getExe nodePackage` if `nodePackage` is not null, otherwise `null`";
pluginDefault = "node";
example = lib.literalExpression "lib.getExe pkgs.nodejs";
description = '' description = ''
Use this field to provide the path to a specific node version such as one installed by Define the node command to use for copilot-lua.
`nvm`. Node.js version must be 18.x or newer.
Node.js version must be 16.x or newer.
''; '';
}; };
serverOptsOverrides = helpers.defaultNullOpts.mkAttrsOf' { server_opts_overrides = defaultNullOpts.mkAttrsOf' {
type = types.anything; type = types.anything;
pluginDefault = { }; pluginDefault = { };
description = '' description = ''
Override copilot lsp client `settings`. Override copilot lsp client `settings`.
The settings field is where you can set the values of the options defined in The settings field is where you can set the values of the options defined in
https://github.com/zbirenbaum/copilot.lua/blob/master/SettingsOpts.md. https://github.com/zbirenbaum/copilot.lua/blob/master/SettingsOpts.md.
These options are specific to the copilot lsp and can be used to customize its behavior. These options are specific to the copilot lsp and can be used to customize its behavior.
Ensure that the `name` field is not overridden as is is used for efficiency reasons in Ensure that the `name` field is not overridden as is is used for efficiency reasons in
numerous checks to verify copilot is actually running. numerous checks to verify copilot is actually running.
See `:h vim.lsp.start_client` for list of options. See `:h vim.lsp.start_client` for list of options.
''; '';
example = { example = {
trace = "verbose"; trace = "verbose";
settings = { settings = {
advanced = { advanced = {
listCount = 10; # number of completions for panel listCount = 10; # number of completions for panel
inlineSuggestCount = 3; # number of completions for getCompletions inlineSuggestCount = 3; # number of completions for getCompletions
};
}; };
}; };
}; };
}; };
};
settingsExample = {
panel = {
enabled = true;
auto_refresh = true;
};
suggestion = {
enabled = true;
auto_trigger = false;
hide_during_completion = false;
debounce = 90;
keymap = {
accept_word = false;
accept_line = false;
};
};
}; };
config = mkIf cfg.enable { extraOptions = {
nodePackage = lib.mkPackageOption pkgs "nodejs" {
default = [ "nodejs-18_x" ];
example = [ "nodejs" ];
nullable = true;
extraDescription = ''
If non-null, will provide a default for `settings.copilot_node_command`.
'';
};
};
extraConfig = {
assertions = [ assertions = [
{ {
assertion = !config.plugins.copilot-vim.enable; assertion = !config.plugins.copilot-vim.enable;
@ -176,45 +185,9 @@ in
''; '';
} }
]; ];
extraPlugins = [ cfg.package ];
extraConfigLua =
let
setupOptions =
with cfg;
{
panel = with panel; {
inherit enabled;
auto_refresh = autoRefresh;
keymap = with keymap; {
jump_prev = jumpPrev;
jump_next = jumpNext;
inherit accept refresh open;
};
layout = with layout; {
inherit position ratio;
};
};
suggestion = with suggestion; {
inherit enabled;
auto_trigger = autoTrigger;
inherit debounce;
keymap = with keymap; {
inherit accept;
accept_word = acceptWord;
accept_line = acceptLine;
inherit next prev dismiss;
};
};
inherit filetypes;
copilot_node_command = copilotNodeCommand;
server_opts_overrides = serverOptsOverrides;
}
// cfg.extraOptions;
in
''
require('copilot').setup(${lib.nixvim.toLuaObject setupOptions})
'';
}; };
# TODO: introduced 2025-01-07: remove after 25.05
deprecateExtraOptions = true;
optionsRenamedToSettings = import ./renamed-options.nix;
} }

View file

@ -0,0 +1,58 @@
let
panelOptions = [
"enabled"
"autoRefresh"
];
panelKeymapOptions = [
"jumpPrev"
"jumpNext"
"accept"
"refresh"
"open"
];
panelLayoutOptions = [
"position"
"ratio"
];
suggestionOptions = [
"enabled"
"autoTrigger"
"debounce"
];
suggestionKeymapOptions = [
"accept"
"acceptWord"
"acceptLine"
"next"
"prev"
"dismiss"
];
in
[
"filetypes"
"copilotNodeCommand"
"serverOptsOverrides"
]
++ map (oldOption: [
"panel"
oldOption
]) panelOptions
++ map (oldOption: [
"panel"
"keymap"
oldOption
]) panelKeymapOptions
++ map (oldOption: [
"panel"
"layout"
oldOption
]) panelLayoutOptions
++ map (oldOption: [
"suggestion"
oldOption
]) suggestionOptions
++ map (oldOption: [
"suggestion"
"keymap"
oldOption
]) suggestionKeymapOptions

View file

@ -8,8 +8,10 @@
copilot-lua = { copilot-lua = {
enable = true; enable = true;
panel.enabled = false; settings = {
suggestion.enabled = false; panel.enabled = false;
suggestion.enabled = false;
};
}; };
copilot-cmp.settings = { copilot-cmp.settings = {
@ -31,53 +33,112 @@
plugins.copilot-lua = { plugins.copilot-lua = {
enable = true; enable = true;
panel = { settings = {
enabled = true; panel = {
autoRefresh = false; enabled = true;
keymap = { auto_refresh = false;
jumpPrev = "[["; keymap = {
jumpNext = "]]"; jump_prev = "[[";
accept = "<CR>"; jump_next = "]]";
refresh = "gr"; accept = "<CR>";
open = "<M-CR>"; refresh = "gr";
open = "<M-CR>";
};
layout = {
position = "bottom";
ratio = 0.4;
};
}; };
layout = { suggestion = {
position = "bottom"; enabled = true;
ratio = 0.4; auto_trigger = false;
hide_during_completion = true;
debounce = 75;
keymap = {
accept = "<M-l>";
accept_word = false;
accept_line = false;
next = "<M-]>";
prev = "<M-[>";
dismiss = "<C-]>";
};
};
filetypes = {
yaml = false;
markdown = false;
help = false;
gitcommit = false;
gitrebase = false;
hgcommit = false;
svn = false;
cvs = false;
"." = false;
};
server_opts_overrides = {
trace = "verbose";
settings = {
advanced = {
listCount = 10; # number of completions for panel
inlineSuggestCount = 3; # number of completions for getCompletions
};
};
}; };
}; };
suggestion = { };
enabled = true; };
autoTrigger = false;
debounce = 75; examples = {
keymap = { plugins.copilot-lua = {
accept = "<M-l>"; enable = true;
acceptWord = false;
acceptLine = false; settings = {
next = "<M-]>"; panel = {
prev = "<M-[>"; enabled = true;
dismiss = "<C-]>"; auto_refresh = true;
keymap = {
jump_prev = "[[";
jump_next = "]]";
accept = "<CR>";
refresh = "gr";
open = "<M-CR>";
};
layout = {
position = "top";
ratio = 0.5;
};
}; };
}; suggestion = {
filetypes = { enabled = true;
markdown = true; auto_trigger = false;
terraform = false; hide_during_completion = false;
sh.__raw = '' debounce = 90;
function () keymap = {
if string.match(vim.fs.basename(vim.api.nvim_buf_get_name(0)), '^%.env.*') then accept = "<M-l>";
-- disable for .env files accept_word = false;
return false accept_line = false;
end next = "<M-]>";
return true prev = "<M-[>";
end dismiss = "<C-]>";
''; };
}; };
serverOptsOverrides = { filetypes = {
trace = "verbose"; yaml = true;
settings = { markdown = true;
advanced = { help = true;
listCount = 10; # number of completions for panel gitcommit = true;
inlineSuggestCount = 3; # number of completions for getCompletions gitrebase = true;
hgcommit = true;
svn = true;
cvs = true;
"." = true;
};
server_opts_overrides = {
trace = "verbose";
settings = {
advanced = {
listCount = 10; # number of completions for panel
inlineSuggestCount = 3; # number of completions for getCompletions
};
}; };
}; };
}; };

View file

@ -7,8 +7,10 @@
copilot-lua = { copilot-lua = {
enable = true; enable = true;
panel.enabled = false; settings = {
suggestion.enabled = false; panel.enabled = false;
suggestion.enabled = false;
};
}; };
cmp = { cmp = {