From ff4c8cf8b3bb386c47eb065565dd431289202de8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=C3=ADn=20=C5=98=C3=ADha?= <43958476+anriha@users.noreply.github.com> Date: Fri, 30 Jun 2023 15:33:26 +0200 Subject: [PATCH] plugins/neogen: init + test (#456) --- plugins/default.nix | 1 + plugins/utils/neogen.nix | 201 ++++++++++++++++++++ tests/test-sources/plugins/utils/neogen.nix | 42 ++++ 3 files changed, 244 insertions(+) create mode 100644 plugins/utils/neogen.nix create mode 100644 tests/test-sources/plugins/utils/neogen.nix diff --git a/plugins/default.nix b/plugins/default.nix index 66dab56b..ed3c2468 100644 --- a/plugins/default.nix +++ b/plugins/default.nix @@ -95,6 +95,7 @@ ./utils/mark-radar.nix ./utils/mini.nix ./utils/neorg.nix + ./utils/neogen.nix ./utils/notify.nix ./utils/netman.nix ./utils/nvim-autopairs.nix diff --git a/plugins/utils/neogen.nix b/plugins/utils/neogen.nix new file mode 100644 index 00000000..e48309d1 --- /dev/null +++ b/plugins/utils/neogen.nix @@ -0,0 +1,201 @@ +{ + pkgs, + config, + lib, + ... +}: +with lib; let + cfg = config.plugins.neogen; + helpers = import ../helpers.nix {inherit lib;}; + + keymapDef = { + generate = { + command = ""; + + description = '' + The only function required to use Neogen. + + It'll try to find the first parent that matches a certain type. + For example, if you are inside a function, and called `generate({ type = "func" })`, + Neogen will go until the start of the function and start annotating for you. + ''; + }; + + generateClass = { + command = "class"; + description = "Generates annotation for class."; + }; + + generateFunction = { + command = "func"; + description = "Generates annotation for function."; + }; + + generateType = { + command = "type"; + description = "Generates annotation for type."; + }; + + generateFile = { + command = "file"; + description = "Generates annotation for file."; + }; + }; +in { + options.plugins.neogen = + helpers.extraOptionsOptions + // { + enable = mkEnableOption "neogen"; + + package = helpers.mkPackageOption "neogen" pkgs.vimPlugins.neogen; + + keymaps = mapAttrs (optionsName: properties: helpers.mkNullOrOption types.str properties.description) keymapDef; + + keymapsSilent = mkOption { + type = types.bool; + description = "Whether Neogen keymaps should be silent"; + default = false; + }; + + inputAfterComment = helpers.defaultNullOpts.mkBool true '' + If true, go to annotation after insertion, and change to insert mode + ''; + + enablePlaceholders = helpers.defaultNullOpts.mkBool true '' + If true, enables placeholders when inserting annotation + ''; + + languages = helpers.defaultNullOpts.mkNullable types.attrs "see upstream documentation" '' + Configuration for languages. + + `template.annotation_convention` (default: check the language default configurations): + Change the annotation convention to use with the language. + + `template.use_default_comment` (default: true): + Prepend any template line with the default comment for the filetype + + `template.position` (fun(node: userdata, type: string):(number,number)?): + Provide an absolute position for the annotation. + If return values are nil, use default position + + `template.append`: + If you want to customize the position of the annotation. + + `template.append.child_name`: + What child node to use for appending the annotation. + + `template.append.position` (before/after): + Relative positioning with `child_name`. + + `template.` (replace with an annotation convention): + Template for an annotation convention. + To know more about how to create your own template, go here: + https://github.com/danymat/neogen/blob/main/docs/adding-languages.md#default-generator + + Example: + ``` + { + csharp = { + template = { + annotation_convention = "..."; + }; + }; + } + ``` + ''; + + snippetEngine = helpers.mkNullOrOption types.str '' + Use a snippet engine to generate annotations. + Some snippet engines come out of the box bundled with neogen: + - `"luasnip"` (https://github.com/L3MON4D3/LuaSnip) + - `"snippy"` (https://github.com/dcampos/nvim-snippy) + - `"vsnip"` (https://github.com/hrsh7th/vim-vsnip) + ''; + + placeholderHighligt = helpers.defaultNullOpts.mkStr "DiagnosticHint" '' + Placeholders highlights to use. If you don't want custom highlight, pass "None" + ''; + + placeholdersText = { + description = helpers.defaultNullOpts.mkStr "[TODO:description]" '' + Placholder for description. + ''; + + tparam = helpers.defaultNullOpts.mkStr "[TODO:tparam]" '' + Placholder for tparam. + ''; + + parameter = helpers.defaultNullOpts.mkStr "[TODO:parameter]" '' + Placholder for parameter. + ''; + + return = helpers.defaultNullOpts.mkStr "[TODO:return]" '' + Placholder for return. + ''; + + class = helpers.defaultNullOpts.mkStr "[TODO:class]" '' + Placholder for class. + ''; + + throw = helpers.defaultNullOpts.mkStr "[TODO:throw]" '' + Placholder for throw. + ''; + + varargs = helpers.defaultNullOpts.mkStr "[TODO:varargs]" '' + Placholder for varargs. + ''; + + type = helpers.defaultNullOpts.mkStr "[TODO:type]" '' + Placholder for type. + ''; + + attribute = helpers.defaultNullOpts.mkStr "[TODO:attribute]" '' + Placholder for attribute. + ''; + + args = helpers.defaultNullOpts.mkStr "[TODO:args]" '' + Placholder for args. + ''; + + kwargs = helpers.defaultNullOpts.mkStr "[TODO:kwargs]" '' + Placholder for kwargs. + ''; + }; + }; + + config = let + setupOptions = with cfg; { + enabled = enable; + input_after_comment = inputAfterComment; + inherit languages; + snippet_engine = snippetEngine; + enable_placeholders = enablePlaceholders; + placeholder_text = placeholdersText; + placeholder_hl = placeholderHighligt; + }; + in + mkIf cfg.enable { + extraPlugins = [cfg.package]; + + extraConfigLua = '' + require("neogen").setup(${helpers.toLuaObject setupOptions}) + ''; + + maps.normal = mkMerge ( + mapAttrsToList + ( + optionName: properties: let + key = cfg.keymaps.${optionName}; + in + mkIf (key != null) + { + ${key} = { + action = ":Neogen ${properties.command}"; + silent = cfg.keymapsSilent; + }; + } + ) + keymapDef + ); + }; +} diff --git a/tests/test-sources/plugins/utils/neogen.nix b/tests/test-sources/plugins/utils/neogen.nix new file mode 100644 index 00000000..71cdfdaa --- /dev/null +++ b/tests/test-sources/plugins/utils/neogen.nix @@ -0,0 +1,42 @@ +{ + empty = { + plugins.neogen.enable = true; + }; + + example = { + plugins.neogen = { + enable = true; + + enablePlaceholders = false; + inputAfterComment = false; + keymaps = { + generate = "a"; + generateClass = "b"; + generateFile = "c"; + generateFunction = "d"; + generateType = "e"; + }; + keymapsSilent = true; + languages = { + csharp = { + template = { + annotation_convention = "..."; + }; + }; + }; + placeholderHighligt = "None"; + placeholdersText = { + attribute = "attribute"; + class = "class"; + description = "description"; + kwargs = "kwargs"; + parameter = "parameter"; + throw = "throw"; + tparam = "tparam"; + type = "type"; + varargs = "varargs"; + }; + snippetEngine = "vsnip"; + }; + }; +}