nixos-config/users/simon/modules/neovim/snippets.nix

106 lines
3.5 KiB
Nix
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{ lib, pkgs }:
let
mkSnippet = prefix: description: body: mkSnippet' prefix description body { };
mkSnippet' = prefix: description: body: { autotrigger ? false }: {
inherit prefix description body;
luasnip = {
inherit autotrigger;
};
};
snippets = {
nix = {
Sha256Dummy = mkSnippet "sha256" "Dummy SHA256 hash" "sha256 = \"0000000000000000000000000000000000000000000000000000\";";
} // (lib.mapAttrs
(name: _:
let
upperName = (lib.toUpper (builtins.substring 0 1 name))
+ (builtins.substring 1 (builtins.stringLength name - 1) name);
in
mkSnippet "${name}Phase" "${name}Phase skeleton" ''
${name}Phase = '''
runHook pre${upperName}
$1
runHook post${upperName}
''';'')
(builtins.listToAttrs
(map
(name: lib.nameValuePair name { })
[ "unpack" "configure" "build" "install" ])));
rust = {
DisplayTrait = mkSnippet "disp" "Display trait implentation" ''
impl fmt::Display for ''${1:Struct} {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "$2", $3)
}
}$0'';
};
tex = {
Section = mkSnippet "s" "Section" ''
\section{$1}
'';
SubSection = mkSnippet "ss" "Subsection" ''
\subsection{$1}
'';
SubSubSection = mkSnippet "sss" "Subsubsection" ''
\subsubsection{$1}
'';
# Math
QuadraticFormula = mkSnippet
"qf"
"Quadratic formula (user is responsible for parentheses)"
''\frac{-''${2:b} \pm \sqrt{$2^2 - 4 \cdot ''${1:a} \cdot ''${3:c}}}{2 \cdot $1}$0'';
AlignedEnv = mkSnippet "aligned" "aligned environment (in math mode)" ''
\begin{aligned}
$1 &= $0 \\\\
\end{aligned}'';
SiUnit = mkSnippet
"si"
"Insert SI unit (only works with simple numbers)"
''\SI{''${1:amount}}{''${2:unit}}'';
MultiplicationSign = mkSnippet' "·" "Insert multiplication sign" ''\cdot $0'' { autotrigger = true; };
Fraction = mkSnippet' "//" "Fraction" ''\frac{$1}{$2}$0'' { autotrigger = true; };
};
java = {
CheckEmptyString = mkSnippet "chke" "Check for empty string" ''
if (''${1:var}.isEmpty()) {
throw new IllegalArgumentException("$1 cant be empty");
}$0'';
CheckNull = mkSnippet "chkn" "Check for null" ''
if (''${1:var} == null) {
throw new NullPointerException("$1 cant be null");
}$0'';
CheckNegative = mkSnippet "chkneg" "Check for negative numbers" ''
if (''${1:var} < 0) {
throw new IllegalArgumentException("$1 cant be negative");
}$0'';
};
};
snippetsIndex = pkgs.writeTextDir "package.json" (builtins.toJSON {
contributes.snippets = lib.mapAttrs
(id: language: { inherit language; path = "./snippets/${id}.json"; })
{
java = [ "java" ];
nix = [ "nix" ];
rust = [ "rust" ];
tex = [ "tex" ];
};
});
snippetsDir = pkgs.symlinkJoin {
name = "luasnip-snippets";
paths = (lib.mapAttrsToList
(ft: content: pkgs.writeTextDir "snippets/${ft}.json" (builtins.toJSON content))
snippets) ++ (lib.singleton snippetsIndex);
};
in
pkgs.writeText "snippets.lua" ''
require("luasnip/loaders/from_vscode").load({ paths = { '${snippetsDir}' } })
require("luasnip/loaders/from_vscode").lazy_load() -- other snippets
''