From 2e07db7efcd450f951255ec3f1ee1f88a6e9ba2a Mon Sep 17 00:00:00 2001 From: diniamo Date: Sun, 6 Oct 2024 20:29:05 +0200 Subject: [PATCH] treewide: remove vsnip, add luasnip --- configuration.nix | 2 +- flake.lock | 85 +++++---- flake.nix | 19 +- .../plugins/completion/nvim-cmp/config.nix | 175 ++++++------------ modules/plugins/languages/rust.nix | 3 +- modules/plugins/snippets/default.nix | 2 +- modules/plugins/snippets/luasnip/config.nix | 17 ++ modules/plugins/snippets/luasnip/default.nix | 6 + modules/plugins/snippets/luasnip/luasnip.nix | 36 ++++ modules/plugins/snippets/vsnip/config.nix | 30 --- modules/plugins/snippets/vsnip/default.nix | 5 - modules/plugins/snippets/vsnip/vsnip.nix | 7 - 12 files changed, 183 insertions(+), 204 deletions(-) create mode 100644 modules/plugins/snippets/luasnip/config.nix create mode 100644 modules/plugins/snippets/luasnip/default.nix create mode 100644 modules/plugins/snippets/luasnip/luasnip.nix delete mode 100644 modules/plugins/snippets/vsnip/config.nix delete mode 100644 modules/plugins/snippets/vsnip/default.nix delete mode 100644 modules/plugins/snippets/vsnip/vsnip.nix diff --git a/configuration.nix b/configuration.nix index b08479aa..797edba3 100644 --- a/configuration.nix +++ b/configuration.nix @@ -108,7 +108,7 @@ isMaximal: { autopairs.nvim-autopairs.enable = true; autocomplete.nvim-cmp.enable = true; - snippets.vsnip.enable = true; + snippets.luasnip.enable = true; filetree = { nvimTree = { diff --git a/flake.lock b/flake.lock index 3e267311..ad9efefa 100644 --- a/flake.lock +++ b/flake.lock @@ -316,6 +316,22 @@ "type": "github" } }, + "plugin-cmp-luasnip": { + "flake": false, + "locked": { + "lastModified": 1696878902, + "narHash": "sha256-nUJJl2zyK/oSwz5RzI9j3gf9zpDfCImCYbPbVsyXgz8=", + "owner": "saadparwaiz1", + "repo": "cmp_luasnip", + "rev": "05a9ab28b53f71d1aece421ef32fee2cb857a843", + "type": "github" + }, + "original": { + "owner": "saadparwaiz1", + "repo": "cmp_luasnip", + "type": "github" + } + }, "plugin-cmp-nvim-lsp": { "flake": false, "locked": { @@ -364,22 +380,6 @@ "type": "github" } }, - "plugin-cmp-vsnip": { - "flake": false, - "locked": { - "lastModified": 1669100283, - "narHash": "sha256-2mkN03noOr5vBvRbSb35xZKorSH+8savQNZtgM9+QcM=", - "owner": "hrsh7th", - "repo": "cmp-vsnip", - "rev": "989a8a73c44e926199bfd05fa7a516d51f2d2752", - "type": "github" - }, - "original": { - "owner": "hrsh7th", - "repo": "cmp-vsnip", - "type": "github" - } - }, "plugin-codewindow-nvim": { "flake": false, "locked": { @@ -588,6 +588,22 @@ "type": "github" } }, + "plugin-friendly-snippets": { + "flake": false, + "locked": { + "lastModified": 1727061933, + "narHash": "sha256-yTsuV5unoujY0mhLINssYYBWCeefe+nJaxQHJKm7hlk=", + "owner": "rafamadriz", + "repo": "friendly-snippets", + "rev": "00ba9dd3df89509f95437b8d595553707c46d5ea", + "type": "github" + }, + "original": { + "owner": "rafamadriz", + "repo": "friendly-snippets", + "type": "github" + } + }, "plugin-gesture-nvim": { "flake": false, "locked": { @@ -828,6 +844,22 @@ "type": "github" } }, + "plugin-luasnip": { + "flake": false, + "locked": { + "lastModified": 1726165831, + "narHash": "sha256-nkaa1NGOI28Et2QitQB+Spv+J42QVdHE1oywteLcJJw=", + "owner": "L3MON4D3", + "repo": "LuaSnip", + "rev": "e808bee352d1a6fcf902ca1a71cee76e60e24071", + "type": "github" + }, + "original": { + "owner": "L3MON4D3", + "repo": "LuaSnip", + "type": "github" + } + }, "plugin-mind-nvim": { "flake": false, "locked": { @@ -1757,22 +1789,6 @@ "type": "github" } }, - "plugin-vim-vsnip": { - "flake": false, - "locked": { - "lastModified": 1704937299, - "narHash": "sha256-gvm6z4pgSULBVPukewRyjwxZ0vZgreQWbG/0kOB1QBo=", - "owner": "hrsh7th", - "repo": "vim-vsnip", - "rev": "02a8e79295c9733434aab4e0e2b8c4b7cea9f3a9", - "type": "github" - }, - "original": { - "owner": "hrsh7th", - "repo": "vim-vsnip", - "type": "github" - } - }, "plugin-which-key": { "flake": false, "locked": { @@ -1827,10 +1843,10 @@ "plugin-cheatsheet-nvim": "plugin-cheatsheet-nvim", "plugin-cinnamon-nvim": "plugin-cinnamon-nvim", "plugin-cmp-buffer": "plugin-cmp-buffer", + "plugin-cmp-luasnip": "plugin-cmp-luasnip", "plugin-cmp-nvim-lsp": "plugin-cmp-nvim-lsp", "plugin-cmp-path": "plugin-cmp-path", "plugin-cmp-treesitter": "plugin-cmp-treesitter", - "plugin-cmp-vsnip": "plugin-cmp-vsnip", "plugin-codewindow-nvim": "plugin-codewindow-nvim", "plugin-comment-nvim": "plugin-comment-nvim", "plugin-copilot-cmp": "plugin-copilot-cmp", @@ -1844,6 +1860,7 @@ "plugin-fastaction-nvim": "plugin-fastaction-nvim", "plugin-fidget-nvim": "plugin-fidget-nvim", "plugin-flutter-tools": "plugin-flutter-tools", + "plugin-friendly-snippets": "plugin-friendly-snippets", "plugin-gesture-nvim": "plugin-gesture-nvim", "plugin-gitsigns-nvim": "plugin-gitsigns-nvim", "plugin-glow-nvim": "plugin-glow-nvim", @@ -1859,6 +1876,7 @@ "plugin-lspkind": "plugin-lspkind", "plugin-lspsaga": "plugin-lspsaga", "plugin-lualine": "plugin-lualine", + "plugin-luasnip": "plugin-luasnip", "plugin-mind-nvim": "plugin-mind-nvim", "plugin-minimap-vim": "plugin-minimap-vim", "plugin-modes-nvim": "plugin-modes-nvim", @@ -1917,7 +1935,6 @@ "plugin-vim-markdown": "plugin-vim-markdown", "plugin-vim-repeat": "plugin-vim-repeat", "plugin-vim-startify": "plugin-vim-startify", - "plugin-vim-vsnip": "plugin-vim-vsnip", "plugin-which-key": "plugin-which-key", "rnix-lsp": "rnix-lsp", "systems": "systems_2" diff --git a/flake.nix b/flake.nix index c4a1a2a8..53e9e939 100644 --- a/flake.nix +++ b/flake.nix @@ -277,11 +277,6 @@ flake = false; }; - plugin-cmp-vsnip = { - url = "github:hrsh7th/cmp-vsnip"; - flake = false; - }; - plugin-cmp-path = { url = "github:hrsh7th/cmp-path"; flake = false; @@ -292,9 +287,19 @@ flake = false; }; + plugin-cmp-luasnip = { + url = "github:saadparwaiz1/cmp_luasnip"; + flake = false; + }; + # snippets - plugin-vim-vsnip = { - url = "github:hrsh7th/vim-vsnip"; + plugin-luasnip = { + url = "github:L3MON4D3/LuaSnip"; + flake = false; + }; + + plugin-friendly-snippets = { + url = "github:rafamadriz/friendly-snippets"; flake = false; }; diff --git a/modules/plugins/completion/nvim-cmp/config.nix b/modules/plugins/completion/nvim-cmp/config.nix index 97415ae1..30f38832 100644 --- a/modules/plugins/completion/nvim-cmp/config.nix +++ b/modules/plugins/completion/nvim-cmp/config.nix @@ -7,12 +7,12 @@ inherit (lib.strings) optionalString; inherit (lib.generators) mkLuaInline; inherit (lib.nvim.binds) addDescriptionsToMappings; - inherit (lib.nvim.dag) entryAnywhere; + inherit (lib.nvim.dag) entryAfter; inherit (lib.nvim.lua) toLuaObject; inherit (builtins) attrNames; cfg = config.vim.autocomplete.nvim-cmp; - vsnipEnable = config.vim.snippets.vsnip.enable; + luasnipEnable = config.vim.snippets.luasnip.enable; self = import ./nvim-cmp.nix {inherit lib config;}; mappingDefinitions = self.options.vim.autocomplete.nvim-cmp.mappings; @@ -32,8 +32,6 @@ in { path = "[Path]"; }; - autopairs.nvim-autopairs.setupOpts.map_cr = false; - autocomplete.nvim-cmp.setupOpts = { sources = map (s: {name = s;}) (attrNames cfg.sources); @@ -46,133 +44,74 @@ in { formatting.format = cfg.format; }; - pluginRC.nvim-cmp = mkIf cfg.enable (entryAnywhere '' + pluginRC.nvim-cmp = mkIf cfg.enable (entryAfter ["autopairs"] '' + local luasnip = require("luasnip") local cmp = require("cmp") cmp.setup(${toLuaObject cfg.setupOpts}) - - ${ - optionalString config.vim.autopairs.nvim-autopairs.enable - '' - cmp.event:on('confirm_done', require("nvim-autopairs.completion.cmp").on_confirm_done({ map_char = { text = "" } })) - '' - } ''); - keymaps = [ - { - mode = ["i" "c"]; - key = mappings.complete.value; - lua = true; - action = "require('cmp').complete"; - desc = mappings.complete.description; - } + # `cmp` and `luasnip` are defined above, in the `nvim-cmp` section + autocomplete.nvim-cmp.setupOpts.mapping = { + ${mappings.complete.value} = mkLuaInline "cmp.mapping.complete()"; + ${mappings.close.value} = mkLuaInline "cmp.mapping.abort()"; + ${mappings.scrollDocsUp.value} = mkLuaInline "cmp.mapping.scroll_docs(-4)"; + ${mappings.scrollDocsDown.value} = mkLuaInline "cmp.mapping.scroll_docs(4)"; - { - mode = "i"; - key = mappings.confirm.value; - lua = true; - action = let - defaultKeys = - if config.vim.autopairs.nvim-autopairs.enable - then "require('nvim-autopairs').autopairs_cr()" - else "vim.api.nvim_replace_termcodes(${toLuaObject mappings.confirm.value}, true, false, true)"; - in '' - function() - if not require('cmp').confirm({ select = true }) then - vim.fn.feedkeys(${defaultKeys}, 'n') - end - end - ''; - desc = mappings.confirm.description; - } - - { - mode = ["i" "s"]; - key = mappings.next.value; - lua = true; - action = '' - function() - local has_words_before = function() - local line, col = unpack(vim.api.nvim_win_get_cursor(0)) - return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil - end - - local cmp = require('cmp') - - local feedkey = function(key, mode) - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true) - end - - if cmp.visible() then - cmp.select_next_item() + ${mappings.confirm.value} = mkLuaInline '' + cmp.mapping(function(fallback) + if cmp.visible() then ${ - optionalString vsnipEnable - '' - elseif vim.fn['vsnip#available'](1) == 1 then - feedkey("(vsnip-expand-or-jump)", "") - '' - } - elseif has_words_before() then - cmp.complete() + if luasnipEnable + then '' + if luasnip.expandable() then + luasnip.expand() else - vim.fn.feedkeys(vim.api.nvim_replace_termcodes(${toLuaObject mappings.next.value}, true, false, true), 'n') + cmp.confirm({ select = true }) end + '' + else "cmp.confirm({ select = true })" + } + else + fallback() end - ''; - desc = mappings.next.description; - } + end) + ''; - { - mode = ["i" "s"]; - key = mappings.previous.value; - lua = true; - action = '' - function() - local cmp = require('cmp') - - local feedkey = function(key, mode) - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true) - end - - if cmp.visible() then - cmp.select_prev_item() - ${ - optionalString vsnipEnable - '' - elseif vim.fn['vsnip#available'](-1) == 1 then - feedkeys("(vsnip-jump-prev)", "") - '' - } - end + ${mappings.next.value} = mkLuaInline '' + cmp.mapping(function(fallback) + local has_words_before = function() + local line, col = unpack(vim.api.nvim_win_get_cursor(0)) + return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil end - ''; - desc = mappings.previous.description; - } - { - mode = ["i" "c"]; - key = mappings.close.value; - lua = true; - action = "require('cmp').mapping.abort()"; - desc = mappings.close.description; - } + if cmp.visible() then + cmp.select_next_item() + ${optionalString luasnipEnable '' + elseif luasnip.locally_jumpable(1) then + luasnip.jump(1) + ''} + elseif has_words_before() then + cmp.complete() + else + fallback() + end + end) + ''; - { - mode = ["i" "c"]; - key = mappings.scrollDocsUp.value; - lua = true; - action = "require('cmp').mapping.scroll_docs(-4)"; - desc = mappings.scrollDocsUp.description; - } - - { - mode = ["i" "c"]; - key = mappings.scrollDocsDown.value; - lua = true; - action = "require('cmp').mapping.scroll_docs(4)"; - desc = mappings.scrollDocsDown.description; - } - ]; + ${mappings.previous.value} = mkLuaInline '' + cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + ${optionalString luasnipEnable '' + elseif luasnip.locally_jumpable(-1) then + luasnip.jump(-1) + ''} + else + fallback() + end + end) + ''; + }; }; }; } diff --git a/modules/plugins/languages/rust.nix b/modules/plugins/languages/rust.nix index b78ef74a..9a388896 100644 --- a/modules/plugins/languages/rust.nix +++ b/modules/plugins/languages/rust.nix @@ -3,7 +3,8 @@ pkgs, lib, ... -}: let inherit (builtins) attrNames; +}: let + inherit (builtins) attrNames; inherit (lib.modules) mkIf mkMerge; inherit (lib.options) mkOption mkEnableOption; inherit (lib.strings) optionalString; diff --git a/modules/plugins/snippets/default.nix b/modules/plugins/snippets/default.nix index 3f9758a1..1b97343f 100644 --- a/modules/plugins/snippets/default.nix +++ b/modules/plugins/snippets/default.nix @@ -1,5 +1,5 @@ { imports = [ - ./vsnip + ./luasnip ]; } diff --git a/modules/plugins/snippets/luasnip/config.nix b/modules/plugins/snippets/luasnip/config.nix new file mode 100644 index 00000000..541fd0fd --- /dev/null +++ b/modules/plugins/snippets/luasnip/config.nix @@ -0,0 +1,17 @@ +{ + config, + lib, + ... +}: let + inherit (lib.modules) mkIf; + + cfg = config.vim.snippets.luasnip; +in { + config = mkIf cfg.enable { + vim = { + startPlugins = ["luasnip" "cmp-luasnip"] ++ cfg.providers; + autocomplete.nvim-cmp.sources = {luasnip = "[LuaSnip]";}; + pluginRC.luasnip = cfg.loaders; + }; + }; +} diff --git a/modules/plugins/snippets/luasnip/default.nix b/modules/plugins/snippets/luasnip/default.nix new file mode 100644 index 00000000..f7e94d38 --- /dev/null +++ b/modules/plugins/snippets/luasnip/default.nix @@ -0,0 +1,6 @@ +{ + imports = [ + ./luasnip.nix + ./config.nix + ]; +} diff --git a/modules/plugins/snippets/luasnip/luasnip.nix b/modules/plugins/snippets/luasnip/luasnip.nix new file mode 100644 index 00000000..7d6943cf --- /dev/null +++ b/modules/plugins/snippets/luasnip/luasnip.nix @@ -0,0 +1,36 @@ +{lib, ...}: let + inherit (lib.options) mkEnableOption mkOption literalExpression literalMD; + inherit (lib.types) listOf lines; + inherit (lib.nvim.types) pluginType; +in { + options.vim.snippets.luasnip = { + enable = mkEnableOption "luasnip" // {default = false;}; + providers = mkOption { + type = listOf pluginType; + default = ["friendly-snippets"]; + description = '' + The snippet provider packages. + + ::: {.note} + These are simply appended to `vim.startPlugins`. + ::: + ''; + example = literalExpression "[\"vimPlugins.vim-snippets\"]"; + }; + loaders = mkOption { + type = lines; + default = "require('luasnip.loaders.from_vscode').lazy_load()"; + defaultText = literalMD '' + ```lua + require('luasnip.loaders.from_vscode').lazy_load() + ``` + ''; + description = "Lua code used to load snippet providers."; + example = literalMD '' + ```lua + require("luasnip.loaders.from_snipmate").lazy_load() + ``` + ''; + }; + }; +} diff --git a/modules/plugins/snippets/vsnip/config.nix b/modules/plugins/snippets/vsnip/config.nix deleted file mode 100644 index 3d58bbca..00000000 --- a/modules/plugins/snippets/vsnip/config.nix +++ /dev/null @@ -1,30 +0,0 @@ -{ - config, - lib, - ... -}: let - inherit (lib.modules) mkIf mkMerge; - inherit (lib.generators) mkLuaInline; - - cfg = config.vim.snippets.vsnip; -in { - config = mkIf cfg.enable (mkMerge [ - { - vim.startPlugins = ["vim-vsnip"]; - } - - (mkIf config.vim.autocomplete.nvim-cmp.enable { - vim = { - startPlugins = ["cmp-vsnip"]; - autocomplete.nvim-cmp = { - sources = {"vsnip" = "[VSnip]";}; - setupOpts.snippet.expand = mkLuaInline '' - function(args) - vim.fn["vsnip#anonymous"](args.body) - end - ''; - }; - }; - }) - ]); -} diff --git a/modules/plugins/snippets/vsnip/default.nix b/modules/plugins/snippets/vsnip/default.nix deleted file mode 100644 index 87ef29e0..00000000 --- a/modules/plugins/snippets/vsnip/default.nix +++ /dev/null @@ -1,5 +0,0 @@ -{ - imports = [ - ./vsnip.nix - ]; -} diff --git a/modules/plugins/snippets/vsnip/vsnip.nix b/modules/plugins/snippets/vsnip/vsnip.nix deleted file mode 100644 index 5d3b6d97..00000000 --- a/modules/plugins/snippets/vsnip/vsnip.nix +++ /dev/null @@ -1,7 +0,0 @@ -{lib, ...}: let - inherit (lib.options) mkEnableOption; -in { - options.vim.snippets.vsnip = { - enable = mkEnableOption "vim-vsnip: snippet LSP/VSCode's format"; - }; -}