feat: improve luasnip

nixos
Moritz Böhme 2024-10-07 09:31:06 +02:00
parent 85bab070c6
commit 22a6b5156c
Signed by: moritz
GPG Key ID: 970C6E89EB0547A9
9 changed files with 115 additions and 67 deletions

View File

@ -12,10 +12,6 @@ in
config = mkIf cfg.enable {
home-manager.users.moritz = {
xdg.configFile."nvim/snippets" = {
recursive = true;
source = ./plugins/snippets;
};
home.packages = with pkgs; [
xdotool # for vimtex

View File

@ -5,6 +5,15 @@ let
in
{
home-manager.users.moritz.programs.nixvim = {
extraConfigLuaPre = ''
local has_words_before = function()
if vim.api.nvim_buf_get_option(0, "buftype") == "prompt" then
return false
end
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_text(0, line-1, 0, line-1, col, {})[1]:match("^%s@*$") == nil
end
'';
plugins.cmp = {
autoEnableSources = true;
enable = true;
@ -27,7 +36,7 @@ in
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item({ behavior = cmp.SelectBehavior.Select })
elseif require("luasnip").locally_jumpable(1) then
elseif require("luasnip").locally_jumpable(1) and has_words_before() then
require("luasnip").jump(1)
else
fallback()

View File

@ -4,7 +4,12 @@ let
inherit (lib) mkEnableOption mkIf readFile;
in
{
home-manager.users.moritz.programs.nixvim = {
home-manager.users.moritz = {
xdg.configFile."nvim/snippets" = {
recursive = true;
source = ../snippets;
};
programs.nixvim = {
plugins.luasnip = {
enable = true;
settings = {
@ -23,5 +28,11 @@ in
ft_func.__raw = ''require("luasnip.extras.filetype_functions").from_pos_or_filetype'';
};
};
extraConfigLuaPost = ''
require("luasnip.loaders.from_lua").load({ paths = "~/.config/nvim/snippets" })
require("luasnip.loaders.from_snipmate").lazy_load({ paths = "~/.config/nvim/snippets" })
require("luasnip.loaders.from_vscode").lazy_load()
'';
};
};
}

View File

@ -1,46 +0,0 @@
local ls = require("luasnip")
local s = ls.snippet
local sn = ls.snippet_node
local isn = ls.indent_snippet_node
local t = ls.text_node
local i = ls.insert_node
local f = ls.function_node
local c = ls.choice_node
local d = ls.dynamic_node
local r = ls.restore_node
local events = require("luasnip.util.events")
local ai = require("luasnip.nodes.absolute_indexer")
local extras = require("luasnip.extras")
local l = extras.lambda
local rep = extras.rep
local p = extras.partial
local m = extras.match
local n = extras.nonempty
local dl = extras.dynamic_lambda
local fmt = require("luasnip.extras.fmt").fmt
local fmta = require("luasnip.extras.fmt").fmta
local conds = require("luasnip.extras.expand_conditions")
local postfix = require("luasnip.extras.postfix").postfix
local types = require("luasnip.util.types")
local parse = require("luasnip.util.parser").parse_snippet
local ms = ls.multi_snippet
local k = require("luasnip.nodes.key_indexer").new_key
local def_template = [[
def {fname}({args}) do
{final}
end
]]
local def = s(
"def",
fmt(def_template, {
fname = i(1, "fname"),
args = i(2),
final = i(3),
}, { priority = 1001 })
)
return {
def,
}

View File

@ -0,0 +1,71 @@
snippet defmodule "Define a new module"
defmodule ${1} do
${2}
end
snippet def "Define a function"
def ${1} do
${2}
end
snippet defw "Define a function with guard"
def ${1}(${2}) when ${3} do
${4}
end
snippet defa "Define a function with arguments"
def ${1}(${2}) do
${3}
end
snippet defp "Define a private function"
defp ${1} do
${2}
end
snippet defpw "Define a private function with guard"
defp ${1}(${2}) when ${3} do
${4}
end
snippet defpa "Define a private function with arguments"
defp ${1}(${2}) do
${3}
end
snippet defmacro "Define a macro"
defmacro ${1}(${2}) do
${3}
end
snippet defmacrow "Define a macro with guard"
defmacro ${1}(${2}) when ${3} do
${4}
end
snippet quote "Quote block"
quote do
${1}
end
snippet quoteb "Quote block with bind_quoted"
quote bind_quoted: [${1}] do
${2}
end
snippet do "Do block"
do
${1}
end
snippet if "If block"
if ${1} do
${2}
end
snippet ife "If-Else block"
if ${1} do
${2}
else
${3}
end

View File

@ -0,0 +1,7 @@
snippet snip
snippet ${1:trigger} "${2:description}"
${0:${VISUAL}}
snippet v
{VISUAL}
snippet $
${${1:1}:${0:text}}