From 22a6b5156ca9d7234612455db0e0b6d3d7e35a32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Mon, 7 Oct 2024 09:31:06 +0200 Subject: [PATCH] feat: improve luasnip --- modules/programs/nvim/default.nix | 4 -- modules/programs/nvim/new_plugins/cmp.nix | 11 ++- modules/programs/nvim/new_plugins/luasnip.nix | 43 ++++++----- .../programs/nvim/plugins/snippets/elixir.lua | 46 ------------ .../programs/nvim/snippets/elixir.snippets | 71 +++++++++++++++++++ .../nvim/{plugins => }/snippets/gleam.lua | 0 .../nvim/{plugins => }/snippets/heex.lua | 0 .../nvim/{plugins => }/snippets/python.lua | 0 .../programs/nvim/snippets/snippets.snippets | 7 ++ 9 files changed, 115 insertions(+), 67 deletions(-) delete mode 100644 modules/programs/nvim/plugins/snippets/elixir.lua create mode 100644 modules/programs/nvim/snippets/elixir.snippets rename modules/programs/nvim/{plugins => }/snippets/gleam.lua (100%) rename modules/programs/nvim/{plugins => }/snippets/heex.lua (100%) rename modules/programs/nvim/{plugins => }/snippets/python.lua (100%) create mode 100644 modules/programs/nvim/snippets/snippets.snippets diff --git a/modules/programs/nvim/default.nix b/modules/programs/nvim/default.nix index 789809d..7e26b6e 100644 --- a/modules/programs/nvim/default.nix +++ b/modules/programs/nvim/default.nix @@ -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 diff --git a/modules/programs/nvim/new_plugins/cmp.nix b/modules/programs/nvim/new_plugins/cmp.nix index 9e894f6..b496b6d 100644 --- a/modules/programs/nvim/new_plugins/cmp.nix +++ b/modules/programs/nvim/new_plugins/cmp.nix @@ -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 [""] = 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() diff --git a/modules/programs/nvim/new_plugins/luasnip.nix b/modules/programs/nvim/new_plugins/luasnip.nix index 7f84147..fdaf5c6 100644 --- a/modules/programs/nvim/new_plugins/luasnip.nix +++ b/modules/programs/nvim/new_plugins/luasnip.nix @@ -4,24 +4,35 @@ let inherit (lib) mkEnableOption mkIf readFile; in { - home-manager.users.moritz.programs.nixvim = { - plugins.luasnip = { - enable = true; - settings = { - history = true; - update_events = "TextChanged,TextChangedI"; - delete_check_events = "TextChanged"; - ext_opts.__raw = '' - { - [require("luasnip.util.types").choiceNode] = { - active = { - virt_text = { { "<--", "Error" } }, + home-manager.users.moritz = { + xdg.configFile."nvim/snippets" = { + recursive = true; + source = ../snippets; + }; + programs.nixvim = { + plugins.luasnip = { + enable = true; + settings = { + history = true; + update_events = "TextChanged,TextChangedI"; + delete_check_events = "TextChanged"; + ext_opts.__raw = '' + { + [require("luasnip.util.types").choiceNode] = { + active = { + virt_text = { { "<--", "Error" } }, + }, }, - }, - } - ''; - ft_func.__raw = ''require("luasnip.extras.filetype_functions").from_pos_or_filetype''; + } + ''; + 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() + ''; }; }; } diff --git a/modules/programs/nvim/plugins/snippets/elixir.lua b/modules/programs/nvim/plugins/snippets/elixir.lua deleted file mode 100644 index e393f11..0000000 --- a/modules/programs/nvim/plugins/snippets/elixir.lua +++ /dev/null @@ -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, -} diff --git a/modules/programs/nvim/snippets/elixir.snippets b/modules/programs/nvim/snippets/elixir.snippets new file mode 100644 index 0000000..a02788e --- /dev/null +++ b/modules/programs/nvim/snippets/elixir.snippets @@ -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 diff --git a/modules/programs/nvim/plugins/snippets/gleam.lua b/modules/programs/nvim/snippets/gleam.lua similarity index 100% rename from modules/programs/nvim/plugins/snippets/gleam.lua rename to modules/programs/nvim/snippets/gleam.lua diff --git a/modules/programs/nvim/plugins/snippets/heex.lua b/modules/programs/nvim/snippets/heex.lua similarity index 100% rename from modules/programs/nvim/plugins/snippets/heex.lua rename to modules/programs/nvim/snippets/heex.lua diff --git a/modules/programs/nvim/plugins/snippets/python.lua b/modules/programs/nvim/snippets/python.lua similarity index 100% rename from modules/programs/nvim/plugins/snippets/python.lua rename to modules/programs/nvim/snippets/python.lua diff --git a/modules/programs/nvim/snippets/snippets.snippets b/modules/programs/nvim/snippets/snippets.snippets new file mode 100644 index 0000000..f242e82 --- /dev/null +++ b/modules/programs/nvim/snippets/snippets.snippets @@ -0,0 +1,7 @@ +snippet snip + snippet ${1:trigger} "${2:description}" + ${0:${VISUAL}} +snippet v + {VISUAL} +snippet $ + ${${1:1}:${0:text}}