From 73303938ee2e8cdc7138db6fc6e42d83cc546a56 Mon Sep 17 00:00:00 2001 From: Heitor Augusto Date: Wed, 25 Dec 2024 07:34:33 -0300 Subject: [PATCH] plugins/copilot-lua: migrate to mkNeovimPlugin --- plugins/by-name/blink-cmp-copilot/default.nix | 2 +- plugins/by-name/copilot-cmp/default.nix | 2 +- plugins/by-name/copilot-lua/default.nix | 331 ++++++++---------- .../by-name/copilot-lua/renamed-options.nix | 58 +++ .../plugins/by-name/copilot-lua/default.nix | 151 +++++--- .../test-sources/plugins/cmp/all-sources.nix | 6 +- 6 files changed, 322 insertions(+), 228 deletions(-) create mode 100644 plugins/by-name/copilot-lua/renamed-options.nix diff --git a/plugins/by-name/blink-cmp-copilot/default.nix b/plugins/by-name/blink-cmp-copilot/default.nix index 5fe0faf6..6c71c355 100644 --- a/plugins/by-name/blink-cmp-copilot/default.nix +++ b/plugins/by-name/blink-cmp-copilot/default.nix @@ -47,7 +47,7 @@ lib.nixvim.plugins.mkNeovimPlugin { extraConfig = { warnings = let - copilot-lua-cfg = config.plugins.copilot-lua; + copilot-lua-cfg = config.plugins.copilot-lua.settings; isEnabled = b: builtins.isBool b && b; in lib.optionals (isEnabled copilot-lua-cfg.suggestion.enabled) [ diff --git a/plugins/by-name/copilot-cmp/default.nix b/plugins/by-name/copilot-cmp/default.nix index cc823c32..b654f875 100644 --- a/plugins/by-name/copilot-cmp/default.nix +++ b/plugins/by-name/copilot-cmp/default.nix @@ -57,7 +57,7 @@ lib.nixvim.plugins.mkNeovimPlugin { extraConfig = { warnings = let - copilot-lua-cfg = config.plugins.copilot-lua; + copilot-lua-cfg = config.plugins.copilot-lua.settings; isEnabled = b: (lib.isBool b && b); in lib.optional (isEnabled copilot-lua-cfg.suggestion.enabled) '' diff --git a/plugins/by-name/copilot-lua/default.nix b/plugins/by-name/copilot-lua/default.nix index 795e40b7..2739b0ee 100644 --- a/plugins/by-name/copilot-lua/default.nix +++ b/plugins/by-name/copilot-lua/default.nix @@ -1,172 +1,181 @@ { - lib, - helpers, config, + lib, pkgs, ... }: -with lib; let - cfg = config.plugins.copilot-lua; + inherit (lib) types; + inherit (lib.nixvim) defaultNullOpts; in -{ - options = { - plugins.copilot-lua = - let - keymapOption = helpers.defaultNullOpts.mkNullable (with types; either (enum [ false ]) str); - in - lib.nixvim.plugins.neovim.extraOptionsOptions - // { - enable = mkEnableOption "copilot.lua"; +lib.nixvim.plugins.mkNeovimPlugin { + name = "copilot-lua"; + moduleName = "copilot"; + packPathName = "copilot.lua"; - package = lib.mkPackageOption pkgs "copilot.lua" { - default = [ - "vimPlugins" - "copilot-lua" - ]; + maintainers = [ lib.maintainers.HeitorAugustoLN ]; + + settingsOptions = + 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 "" "Keymap to accept the proposed suggestion."; + refresh = mkKeymapOption "gr" "Keymap to refresh the suggestions."; + open = mkKeymapOption "" "Keymap to open."; }; - panel = { - enabled = helpers.defaultNullOpts.mkBool true "Enable the panel."; + layout = { + position = + defaultNullOpts.mkEnumFirstDefault + [ + "bottom" + "top" + "left" + "right" + "horizontal" + "vertical" + ] + '' + The panel position. + ''; - autoRefresh = helpers.defaultNullOpts.mkBool false "Enable auto-refresh."; - - keymap = { - jumpPrev = keymapOption "[[" "Keymap for jumping to the previous suggestion."; - - jumpNext = keymapOption "]]" "Keymap for jumping to the next suggestion."; - - accept = keymapOption "" "Keymap to accept the proposed suggestion."; - - refresh = keymapOption "gr" "Keymap to refresh the suggestions."; - - open = keymapOption "" "Keymap to open."; - }; - - layout = { - position = - helpers.defaultNullOpts.mkEnumFirstDefault - [ - "bottom" - "top" - "left" - "right" - ] - '' - The panel position. - ''; - - ratio = helpers.defaultNullOpts.mkProportion 0.4 '' - The panel ratio. - ''; - }; + ratio = defaultNullOpts.mkProportion 0.4 '' + The panel ratio. + ''; }; + }; - suggestion = { - enabled = helpers.defaultNullOpts.mkBool true "Enable suggestion."; + 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."; - - debounce = helpers.defaultNullOpts.mkInt 75 "Debounce."; - - keymap = { - accept = keymapOption "" "Keymap for accepting the suggestion."; - - acceptWord = keymapOption false "Keymap for accepting a word suggestion."; - - acceptLine = keymapOption false "Keymap for accepting a line suggestion."; - - next = keymapOption "" "Keymap for accepting the next suggestion."; - - prev = keymapOption "" "Keymap for accepting the previous suggestion."; - - dismiss = keymapOption "" "Keymap to dismiss the suggestion."; - }; + keymap = { + accept = mkKeymapOption "" "Keymap for accepting the suggestion."; + accept_word = mkKeymapOption false "Keymap for accepting a word suggestion."; + accept_line = mkKeymapOption false "Keymap for accepting a line suggestion."; + next = mkKeymapOption "" "Keymap for accepting the next suggestion."; + prev = mkKeymapOption "" "Keymap for accepting the previous suggestion."; + dismiss = mkKeymapOption "" "Keymap to dismiss the suggestion."; }; + }; - filetypes = - helpers.defaultNullOpts.mkAttrsOf types.bool - { - yaml = false; - markdown = false; - help = false; - gitcommit = false; - gitrebase = false; - hgcommit = false; - svn = false; - cvs = false; - "." = false; - } - '' - Specify filetypes for attaching copilot. - Each value can be either a boolean or a lua function that returns a boolean. + filetypes = defaultNullOpts.mkAttrsOf' { + type = types.bool; + pluginDefault = { + yaml = true; + markdown = false; + help = false; + gitcommit = false; + gitrebase = false; + hgcommit = false; + svn = false; + cvs = false; + "." = false; + }; + example = lib.literalExpression '' + { + 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: - ```nix - { - 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 - \'\'; - } - ``` - - 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 { + copilot_node_command = + let + inherit (config.plugins.copilot-lua) nodePackage; + in + lib.nixvim.mkNullOrOption' { 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 = '' - Use this field to provide the path to a specific node version such as one installed by - `nvm`. - Node.js version must be 16.x or newer. + Define the node command to use for copilot-lua. + Node.js version must be 18.x or newer. ''; }; - serverOptsOverrides = helpers.defaultNullOpts.mkAttrsOf' { - type = types.anything; - pluginDefault = { }; - description = '' - Override copilot lsp client `settings`. - 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. - These options are specific to the copilot lsp and can be used to customize its behavior. + server_opts_overrides = defaultNullOpts.mkAttrsOf' { + type = types.anything; + pluginDefault = { }; + description = '' + Override copilot lsp client `settings`. + 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. + 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 - numerous checks to verify copilot is actually running. + Ensure that the `name` field is not overridden as is is used for efficiency reasons in + numerous checks to verify copilot is actually running. - See `:h vim.lsp.start_client` for list of options. - ''; - example = { - trace = "verbose"; - settings = { - advanced = { - listCount = 10; # number of completions for panel - inlineSuggestCount = 3; # number of completions for getCompletions - }; + See `:h vim.lsp.start_client` for list of options. + ''; + example = { + trace = "verbose"; + settings = { + advanced = { + listCount = 10; # number of completions for panel + 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 = [ { 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; } diff --git a/plugins/by-name/copilot-lua/renamed-options.nix b/plugins/by-name/copilot-lua/renamed-options.nix new file mode 100644 index 00000000..949b4411 --- /dev/null +++ b/plugins/by-name/copilot-lua/renamed-options.nix @@ -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 diff --git a/tests/test-sources/plugins/by-name/copilot-lua/default.nix b/tests/test-sources/plugins/by-name/copilot-lua/default.nix index 7b3281f0..92db4baf 100644 --- a/tests/test-sources/plugins/by-name/copilot-lua/default.nix +++ b/tests/test-sources/plugins/by-name/copilot-lua/default.nix @@ -8,8 +8,10 @@ copilot-lua = { enable = true; - panel.enabled = false; - suggestion.enabled = false; + settings = { + panel.enabled = false; + suggestion.enabled = false; + }; }; copilot-cmp.settings = { @@ -31,53 +33,112 @@ plugins.copilot-lua = { enable = true; - panel = { - enabled = true; - autoRefresh = false; - keymap = { - jumpPrev = "[["; - jumpNext = "]]"; - accept = ""; - refresh = "gr"; - open = ""; + settings = { + panel = { + enabled = true; + auto_refresh = false; + keymap = { + jump_prev = "[["; + jump_next = "]]"; + accept = ""; + refresh = "gr"; + open = ""; + }; + layout = { + position = "bottom"; + ratio = 0.4; + }; }; - layout = { - position = "bottom"; - ratio = 0.4; + suggestion = { + enabled = true; + auto_trigger = false; + hide_during_completion = true; + debounce = 75; + keymap = { + accept = ""; + accept_word = false; + accept_line = false; + next = ""; + prev = ""; + dismiss = ""; + }; + }; + 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; - keymap = { - accept = ""; - acceptWord = false; - acceptLine = false; - next = ""; - prev = ""; - dismiss = ""; + }; + }; + + examples = { + plugins.copilot-lua = { + enable = true; + + settings = { + panel = { + enabled = true; + auto_refresh = true; + keymap = { + jump_prev = "[["; + jump_next = "]]"; + accept = ""; + refresh = "gr"; + open = ""; + }; + layout = { + position = "top"; + ratio = 0.5; + }; }; - }; - filetypes = { - markdown = true; - terraform = false; - 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 - ''; - }; - serverOptsOverrides = { - trace = "verbose"; - settings = { - advanced = { - listCount = 10; # number of completions for panel - inlineSuggestCount = 3; # number of completions for getCompletions + suggestion = { + enabled = true; + auto_trigger = false; + hide_during_completion = false; + debounce = 90; + keymap = { + accept = ""; + accept_word = false; + accept_line = false; + next = ""; + prev = ""; + dismiss = ""; + }; + }; + filetypes = { + yaml = true; + markdown = true; + help = true; + gitcommit = true; + 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 + }; }; }; }; diff --git a/tests/test-sources/plugins/cmp/all-sources.nix b/tests/test-sources/plugins/cmp/all-sources.nix index 7d1980e8..623ec1fb 100644 --- a/tests/test-sources/plugins/cmp/all-sources.nix +++ b/tests/test-sources/plugins/cmp/all-sources.nix @@ -7,8 +7,10 @@ copilot-lua = { enable = true; - panel.enabled = false; - suggestion.enabled = false; + settings = { + panel.enabled = false; + suggestion.enabled = false; + }; }; cmp = {