Skip to content

Commit

Permalink
home (nvim): Configure luasnip to load custom lua snippets
Browse files Browse the repository at this point in the history
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 `<file-type>.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.
  • Loading branch information
britter committed Nov 22, 2024
1 parent 3174659 commit 9d2102f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 34 deletions.
93 changes: 59 additions & 34 deletions home/terminal/nvim/completion.nix
Original file line number Diff line number Diff line change
@@ -1,41 +1,66 @@
_: {
programs.nixvim.plugins = {
# Review the configuration for cmp after switching to 24.11
cmp = {
enable = true;
autoEnableSources = true;
settings = {
mapping = {
"<C-Space>" = "cmp.mapping.complete()";
"<C-d>" = "cmp.mapping.scroll_docs(-4)";
"<C-e>" = "cmp.mapping.close()";
"<C-f>" = "cmp.mapping.scroll_docs(4)";
"<CR>" = "cmp.mapping.confirm({ select = true })";
"<S-Tab>" = "cmp.mapping(cmp.mapping.select_prev_item(), {'i', 's'})";
"<Tab>" = "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 = {
"<C-Space>" = "cmp.mapping.complete()";
"<C-d>" = "cmp.mapping.scroll_docs(-4)";
"<C-e>" = "cmp.mapping.close()";
"<C-f>" = "cmp.mapping.scroll_docs(4)";
"<CR>" = "cmp.mapping.confirm({ select = true })";
"<S-Tab>" = "cmp.mapping(cmp.mapping.select_prev_item(), {'i', 's'})";
"<Tab>" = "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 = "<Tab>";
cmp_luasnip.enable = true;
luasnip = {
enable = true;
extraConfig = {
enable_autosnippets = true;
store_selection_keys = "<Tab>";
};
fromLua = [
{
paths = ./snippets;
}
];
};
friendly-snippets.enable = true;
};
friendly-snippets.enable = true;
keymaps = [
{
action.__raw = "function() require(\"luasnip\").jump(1) end";
key = "<C-L>";
mode = ["i" "s"];
options = {
silent = true;
};
}
{
action.__raw = "function() require(\"luasnip\").jump(-1) end";
key = "<C-K>";
mode = ["i" "s"];
options = {
silent = true;
};
}
];
};
}
33 changes: 33 additions & 0 deletions home/terminal/nvim/snippets/nix.lua
Original file line number Diff line number Diff line change
@@ -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),
}
)
),
}, {}
2 changes: 2 additions & 0 deletions treefmt.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ _: {
deadnix.enable = true;
keep-sorted.enable = true;
statix.enable = true;
stylua.enable = true;
};
settings.formatter.stylua.options = ["--indent-type" "Spaces"];
}

0 comments on commit 9d2102f

Please sign in to comment.