Replace UltiSnips with LuaSnip

The UltiSnips snippets are still there, I just have to port them slowly
over time.
This commit is contained in:
Gabriel Arazas 2022-04-24 21:19:53 +08:00
parent cfb5685f66
commit 24b3b106ce
14 changed files with 251 additions and 26 deletions

View File

@ -28,18 +28,57 @@ return require("packer").startup(function(use)
-- Snippets engine. -- Snippets engine.
-- A must have for me. -- A must have for me.
-- TODO: Port some of my personal snippets.
use { use {
"L3MON4D3/LuaSnip", "L3MON4D3/LuaSnip",
requires = { requires = {
"rafamadriz/friendly-snippets", "rafamadriz/friendly-snippets",
"molleweide/LuaSnip-snippets.nvim",
}, },
config = function () config = function ()
require("luasnip.loaders.from_vscode").lazy_load() require("luasnip.loaders.from_vscode").lazy_load()
require("luasnip.loaders.from_lua").lazy_load() require("luasnip.loaders.from_lua").lazy_load()
local ls = require("luasnip")
local types = require("luasnip.util.types")
ls.config.set_config {
history = true,
update_events = "TextChanged,TextChangedI",
ext_opts = {
[types.choiceNode] = {
active = {
virt_text = { {"<- Current choice", "Comment"} },
},
},
[types.insertNode] = {
active = {
virt_text = { {"<>", "Comment"} },
},
},
},
}
vim.keymap.set({ "i", "s" }, "<c-j>", function()
if ls.jumpable(1) then
ls.jump(1)
end
end)
vim.keymap.set({ "i", "s" }, "<c-k>", function()
if ls.jumpable(-1) then
ls.jump(-1)
end
end)
vim.keymap.set({ "i", "s" }, "<c-l>", function()
if ls.expand_or_jumpable() then
ls.expand_or_jump()
end
end)
vim.keymap.set({ "i", "s" }, "<c-u>", function()
require("luasnip.extras.select_choice")()
end)
end, end,
event = "InsertEnter",
} }
-- Fuzzy finder of lists -- Fuzzy finder of lists

34
nvim/luasnippets/all.lua Normal file
View File

@ -0,0 +1,34 @@
return {
s(
"today",
f(function ()
return os.date "%F"
end)
),
s(
"retrieve",
f(function ()
return string.format("(retrieved %s)", os.date("%F"))
end)
),
s(
{ trig = "reldate (-?%d+) \"(.+)\"", regTrig = true },
f(function(_, snip)
-- The point is in number of days.
local point = 60 * 60 * 24 * snip.captures[1]
local now = os.time()
return os.date(snip.captures[2], now + point)
end)
),
s(
"#!",
fmt("#!{}", i(1, "/usr/bin/env bash"))
),
parse("ie", "(i.e., $1)"),
parse("eg", "(e.g., $1)"),
}

View File

@ -0,0 +1,117 @@
function max_asciidoc_header(level)
return math.min(level, 6)
end
return {
parse("bf", "**$1**"),
parse("it", "__$1__"),
parse("tt", "\\$1\\"),
parse("sp", "^$1^"),
parse("sb", "~$1~"),
parse("foot", "footnote:[$1]"),
parse("a", "link:$1[$2]"),
parse("var", ":$1: $2"),
parse("audio", "audio::$1[$2]"),
parse("video", "video::$1[$2]"),
s("fmt",
fmt("{}{}{}", {
c(1, {
t "**",
t "__",
t "`",
}),
i(2),
rep(1),
})),
s("dt",
fmt([[
{}::
{}
]], {
i(1, "TERM"),
i(2, "DEFINITION"),
})),
s("src",
fmt([[
[source, {}]
----
{}
----
{}
]], {
i(1, "LANGUAGE"),
i(2, "CODE"),
i(0),
})),
s(
{ trig = "h(%d)", regTrig = true },
fmt([[
{} {}
{}
{}
]], {
f(function(_, snip)
local level = max_asciidoc_header(snip.captures[1])
return string.rep("=", level)
end),
i(1, "CHAPTER"),
d(2, function(_, snip)
local nodes = {}
table.insert(nodes, t "")
local level = max_asciidoc_header(snip.captures[1])
if level == 1 then
table.insert(nodes, t ":toc:")
end
local parent = c(1, nodes)
if level > 1 then
parent = t ""
end
return sn(nil, parent)
end, {}),
i(0),
})
),
s(
"admon",
fmt("{}: {}", {
c(1, {
t "NOTE",
t "TIP",
t "IMPORTANT",
t "CAUTION",
t "WARNING",
}),
i(0),
})),
s(
"admonB",
fmt([[
[{}]
====
{}
====
{}
]], {
c(1, {
t "NOTE",
t "TIP",
t "IMPORTANT",
t "CAUTION",
t "WARNING",
}),
i(2, "BODY"),
i(0),
})),
}

View File

@ -1,19 +0,0 @@
snippet shell "Nix shell template" b
{ pkgs ? import <nixpkgs> {} }:
with pkgs;
mkShell {
buildInputs = [
${1}
];
}
endsnippet
snippet mkDerivation "Shorthand for stdenv.mkDerivation" b
{ stdenv, lib, $1 }:
stdenv.mkDerivation rec {
$2
}
endsnippet

View File

@ -8,7 +8,7 @@ def relative_date(days = 0):
return calculated_date return calculated_date
endglobal endglobal
snippet "reldate (\d+)( ".+")?" "Prints out the relative date in ISO format." ri snippet "reldate (-?\d+)( .+)?" "Prints out the relative date in ISO format." ri
`!p `!p
reldate = relative_date(match.group(1)) reldate = relative_date(match.group(1))
date_format = match.group(2).strip(" \"") if match.group(2) is not None else "%F" date_format = match.group(2).strip(" \"") if match.group(2) is not None else "%F"
@ -90,11 +90,10 @@ snippet #! "Quick snippet for a shebang." bi
endsnippet endsnippet
# This is only useful for decorative comment boxes and all of the jazz. # This is only useful for decorative comment boxes and all of the jazz.
snippet "boxen '(.*)'" "Create a box of stuff" ir snippet "boxen (.*)" "Create a box of stuff" ir
`!p snip.rv = (match.group(1) * (len(t[1]) + 4)).strip()[0:(len(t[1]) + 4)]` `!p snip.rv = (match.group(1) * (len(t[1]) + 4)).strip()[0:(len(t[1]) + 4)]`
`!p snip.rv = match.group(1)[0]` $1 `!p snip.rv = match.group(1)[0]` `!p snip.rv = match.group(1)[0]` $1 `!p snip.rv = match.group(1)[0]`
`!p snip.rv = (match.group(1) * (len(t[1]) + 4)).strip()[0:(len(t[1]) + 4)]` `!p snip.rv = (match.group(1) * (len(t[1]) + 4)).strip()[0:(len(t[1]) + 4)]`
$0
endsnippet endsnippet
# Also stolen from Gilles Castel's post at https://castel.dev/post/lecture-notes-1/. # Also stolen from Gilles Castel's post at https://castel.dev/post/lecture-notes-1/.
@ -102,6 +101,5 @@ snippet box "More box (that looks more like a box)."
`!p snip.rv = '┌' + '─' * (len(t[1]) + 2) + '┐'` `!p snip.rv = '┌' + '─' * (len(t[1]) + 2) + '┐'`
│ $1 │ │ $1 │
`!p snip.rv = '└' + '─' * (len(t[1]) + 2) + '┘'` `!p snip.rv = '└' + '─' * (len(t[1]) + 2) + '┘'`
$0
endsnippet endsnippet

View File

@ -0,0 +1,56 @@
snippet shell "Nix shell template" b
{ pkgs ? import <nixpkgs> {} }:
with pkgs;
mkShell {
buildInputs = [
${1}
];
}
endsnippet
snippet mkDerivation "Shorthand for stdenv.mkDerivation" b
{ stdenv, lib, $1 }:
stdenv.mkDerivation rec {
$2
}
endsnippet
snippet buildGoModule "Shorthand for building Go modules" b
{ stdenv, lib, buildGoModule, $1 }:
buildGoModule rec {
pname = $1;
version = $2;
vendorSha256 = "";
}
endsnippet
snippet homeManagerModule "A file template for " <flags>
{ config, options, lib, pkgs, ... }:
let
cfg = config.$1;
in {
options.$1 = {
enable = lib.mkEnableOption "$2";
};
config = lib.mkIf cfg.enable {
};
}
endsnippet
snippet mkOption "lib.mkOption from nixpkgs" i
lib.mkOption {
type = $1;
description = $2;
default = $3;
example = $4;
$5
}
endsnippet