From 6e4130fd265030612050c40d3db27069d981c87f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geoffrey=20=E2=80=9CFrogeye=E2=80=9D=20Preud=27homme?= Date: Tue, 9 Jan 2024 21:53:00 +0100 Subject: [PATCH] vim: Better snippets (and split completion file) --- hm/common.nix | 2 +- hm/vim/completion.nix | 54 +++++++++++++++++++++++++++++++++++++++ hm/vim/completion_pre.lua | 32 +++++++++++++++++++++++ hm/vim/default.nix | 39 ++++------------------------ 4 files changed, 92 insertions(+), 35 deletions(-) create mode 100644 hm/vim/completion.nix create mode 100644 hm/vim/completion_pre.lua diff --git a/hm/common.nix b/hm/common.nix index 30e6818..0efd9aa 100644 --- a/hm/common.nix +++ b/hm/common.nix @@ -29,7 +29,7 @@ }; commonShellAliases = { # Replacement commands - ls = "lsd"; + # ls = "lsd"; # lsd is suuuper slow for large directories cat = "bat -pp"; # Completion for existing commands diff --git a/hm/vim/completion.nix b/hm/vim/completion.nix new file mode 100644 index 0000000..693ff21 --- /dev/null +++ b/hm/vim/completion.nix @@ -0,0 +1,54 @@ +{ pkgs, lib, config, ... }: +{ + config = { + # Completion + programs.nixvim = { + extraConfigLuaPre = builtins.readFile ./completion_pre.lua; + plugins = { + # Snippets + # UltiSnips should be the better snippet engine, but: + # - Not built-in into Nixvim yet (UPST?) + # - Couldn't manage to make it work with friendly-snippets, which seems to give better snippets for Python at least + # - Tab switching between fields seems hard to configure + luasnip = { + enable = true; + fromVscode = [ + { paths = "${pkgs.vimPlugins.friendly-snippets}"; } + # { paths = "${pkgs.vimPlugins.vim-snippets}"; } # UPST Needs snipmate support + ]; + }; + + # Completion + nvim-cmp = { + enable = true; + mapping = { + # Proposed example, since there's no default + "" = "cmp.mapping.complete()"; + "" = "cmp.mapping.scroll_docs(-4)"; + "" = "cmp.mapping.close()"; + "" = "cmp.mapping.scroll_docs(4)"; + "" = "cmp.mapping.confirm({ select = true })"; + "" = { + action = "nvim_cmp_stab"; + modes = [ "i" "s" ]; + }; + "" = { + action = "nvim_cmp_tab"; + modes = [ "i" "s" ]; + }; + }; + sources = [ + # Respective plugins will get installed automatically + { name = "buffer"; } + { name = "calc"; } + { name = "nvim_lsp"; } + { name = "path"; } + { name = "luasnip"; } + ]; + snippet.expand = "luasnip"; + }; + lspkind.enable = true; # Add icons to LSP completions + }; + }; + }; +} diff --git a/hm/vim/completion_pre.lua b/hm/vim/completion_pre.lua new file mode 100644 index 0000000..24cfa9b --- /dev/null +++ b/hm/vim/completion_pre.lua @@ -0,0 +1,32 @@ +-- https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#luasnip + +local has_words_before = function() + unpack = unpack or table.unpack + 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 luasnip = require("luasnip") +local cmp = require("cmp") + +local nvim_cmp_tab = function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + elseif has_words_before() then + cmp.complete() + else + fallback() + end +end + +local nvim_cmp_stab = function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif luasnip.jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end +end diff --git a/hm/vim/default.nix b/hm/vim/default.nix index f556afc..1a5fac5 100644 --- a/hm/vim/default.nix +++ b/hm/vim/default.nix @@ -1,5 +1,6 @@ { pkgs, lib, config, ... }: let + # UPST vim-shot-f = pkgs.vimUtils.buildVimPlugin { pname = "vim-shot-f"; version = "2016-02-05"; @@ -78,33 +79,6 @@ in # Tabline barbar.enable = true; - # Completion - nvim-cmp = { - enable = true; - mapping = { # Proposed example, since there's no default - "" = "cmp.mapping.complete()"; - "" = "cmp.mapping.scroll_docs(-4)"; - "" = "cmp.mapping.close()"; - "" = "cmp.mapping.scroll_docs(4)"; - "" = "cmp.mapping.confirm({ select = true })"; - "" = { - action = "cmp.mapping.select_prev_item()"; - modes = [ "i" "s" ]; - }; - "" = { - action = "cmp.mapping.select_next_item()"; - modes = [ "i" "s" ]; - }; - }; - sources = [ # Respective plugins will get installed automatically - { name = "buffer"; } - { name = "calc"; } - { name = "nvim_lsp"; } - { name = "path"; } - { name = "vsnip"; } - ]; - }; - lspkind.enable = true; # Add icons to LSP completions # Go to whatever telescope = { @@ -254,12 +228,6 @@ in # Language server lsp_signature-nvim # Show argument definition when typing a function - # Snippets - vim-vsnip - vim-vsnip-integ - friendly-snippets - # TODO luasnip seems to be in Lua and more integrated with Nix. - # Git fugitive-gitlab-vim # Open files in GitLab # TODO Connect it! @@ -291,7 +259,6 @@ in " vim-gutentags let g:gutentags_cache_dir = expand('~/.cache/nvim/tags') - '' + lib.optionalString config.frogeye.extra '' " languagetool let g:languagetool_cmd = "${pkgs.languagetool}/bin/languagetool-commandline" @@ -368,4 +335,8 @@ in ]; }; }; + + imports = [ + ./completion.nix + ]; }