From 9d2102f0f4e97a3808b8435e444aa2d0922b7252 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Fri, 22 Nov 2024 15:02:16 +0100 Subject: [PATCH] home (nvim): Configure luasnip to load custom lua snippets The `path` option passed to `fromLua` needs to point to a directory with a particular structure: The directory needs to contain either files with name `.lua` or subdirectories named after the file type they apply to. See [this section](https://github.com/L3MON4D3/LuaSnip/blob/master/README.md#add-snippets) of luasnip's documentation for more details. The lua files themselves also have to follow a certain structure. First of all common luasnip imports such as `local s = ls.snippet` don't need to be added because they a brought into scope automatically (see the documentation above, and https://github.com/L3MON4D3/LuaSnip/blob/0f7bbce41ea152a94d12aea286f2ce98e63c0f58/lua/luasnip/default_config.lua#L20-L99 for all automatically imported symbols. Furthermore each lua file needs to return a list with two entries. The first entry being normal snippets and the second one being auto snippets. In order for snippets to show up in cmp's popup menu each snippet needs to define a `trig` and a `desc`. If this is missing, cmp won't show them! Keymappings for jumping between placeholders in the snippet are added, because otherwise insert nodes don't make sense. A keymapping for cycling through choises of choise nodes is currently missing, because none of the snippets uses choise at the moment. Last but not least a single snippet for generating a NixOS module including an enable option is added. The snippet allows for defining the option namespace, generates an enable option using the file name without extension for the enable option paramter, assigns a cfg variable, and adds the typical `config = lib.mkIf cfg.enable {}` block. --- home/terminal/nvim/completion.nix | 93 ++++++++++++++++++----------- home/terminal/nvim/snippets/nix.lua | 33 ++++++++++ treefmt.nix | 2 + 3 files changed, 94 insertions(+), 34 deletions(-) create mode 100644 home/terminal/nvim/snippets/nix.lua diff --git a/home/terminal/nvim/completion.nix b/home/terminal/nvim/completion.nix index 06fbd6b..836ac74 100644 --- a/home/terminal/nvim/completion.nix +++ b/home/terminal/nvim/completion.nix @@ -1,41 +1,66 @@ _: { - programs.nixvim.plugins = { - # Review the configuration for cmp after switching to 24.11 - cmp = { - enable = true; - autoEnableSources = true; - settings = { - mapping = { - "" = "cmp.mapping.complete()"; - "" = "cmp.mapping.scroll_docs(-4)"; - "" = "cmp.mapping.close()"; - "" = "cmp.mapping.scroll_docs(4)"; - "" = "cmp.mapping.confirm({ select = true })"; - "" = "cmp.mapping(cmp.mapping.select_prev_item(), {'i', 's'})"; - "" = "cmp.mapping(cmp.mapping.select_next_item(), {'i', 's'})"; + programs.nixvim = { + plugins = { + # Review the configuration for cmp after switching to 24.11 + cmp = { + enable = true; + autoEnableSources = true; + settings = { + mapping = { + "" = "cmp.mapping.complete()"; + "" = "cmp.mapping.scroll_docs(-4)"; + "" = "cmp.mapping.close()"; + "" = "cmp.mapping.scroll_docs(4)"; + "" = "cmp.mapping.confirm({ select = true })"; + "" = "cmp.mapping(cmp.mapping.select_prev_item(), {'i', 's'})"; + "" = "cmp.mapping(cmp.mapping.select_next_item(), {'i', 's'})"; + }; + snippet.expand = + # lua + '' + function(args) + require('luasnip').lsp_expand(args.body) + end + ''; + sources = [ + {name = "nvim_lsp";} + {name = "luasnip";} + {name = "buffer";} + ]; }; - snippet.expand = - # lua - '' - function(args) - require('luasnip').lsp_expand(args.body) - end - ''; - sources = [ - {name = "nvim_lsp";} - {name = "luasnip";} - {name = "buffer";} - ]; }; - }; - cmp_luasnip.enable = true; - luasnip = { - enable = true; - extraConfig = { - enable_autosnippets = true; - store_selection_keys = ""; + cmp_luasnip.enable = true; + luasnip = { + enable = true; + extraConfig = { + enable_autosnippets = true; + store_selection_keys = ""; + }; + fromLua = [ + { + paths = ./snippets; + } + ]; }; + friendly-snippets.enable = true; }; - friendly-snippets.enable = true; + keymaps = [ + { + action.__raw = "function() require(\"luasnip\").jump(1) end"; + key = ""; + mode = ["i" "s"]; + options = { + silent = true; + }; + } + { + action.__raw = "function() require(\"luasnip\").jump(-1) end"; + key = ""; + mode = ["i" "s"]; + options = { + silent = true; + }; + } + ]; }; } diff --git a/home/terminal/nvim/snippets/nix.lua b/home/terminal/nvim/snippets/nix.lua new file mode 100644 index 0000000..c61e75e --- /dev/null +++ b/home/terminal/nvim/snippets/nix.lua @@ -0,0 +1,33 @@ +return { + s( + { trig = "module", desc = "Module template with options" }, + fmt( + [[ + {{ + config, + lib, + pkgs, + ... + }}: let + cfg = config.{}; + in {{ + options.{} = {{ + enable = lib.mkEnableOption "{}"; + }}; + + config = lib.mkIf cfg.enable {{ + {} + }}; + }} + ]], + { + i(1), + rep(1), + f(function() + return vim.fn.expand("%:t:r") + end), + i(0), + } + ) + ), +}, {} diff --git a/treefmt.nix b/treefmt.nix index 299ca23..b21554a 100644 --- a/treefmt.nix +++ b/treefmt.nix @@ -5,5 +5,7 @@ _: { deadnix.enable = true; keep-sorted.enable = true; statix.enable = true; + stylua.enable = true; }; + settings.formatter.stylua.options = ["--indent-type" "Spaces"]; }