From 3dfdb88915379d22aa51cba714db266c7c30d275 Mon Sep 17 00:00:00 2001 From: Quentin Boyer Date: Sun, 10 Nov 2024 23:07:35 +0100 Subject: [PATCH] neovim: Isolate completion configuration --- neovim/completion.nix | 68 +++++++++++++++++++++++++++++++++++++++++++ neovim/default.nix | 68 +------------------------------------------ 2 files changed, 69 insertions(+), 67 deletions(-) create mode 100644 neovim/completion.nix diff --git a/neovim/completion.nix b/neovim/completion.nix new file mode 100644 index 0000000..07f02b4 --- /dev/null +++ b/neovim/completion.nix @@ -0,0 +1,68 @@ +{ + extraConfigLuaPre = '' + 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") + ''; + + plugins.cmp = { + enable = true; + + settings = { + snippet.expand = '' + function(args) + require('luasnip').lsp_expand(args.body) + end + ''; + mapping = { + "" = "cmp.mapping.confirm({select = true })"; + "" = "cmp.mapping.scroll_docs(-4)"; + "" = "cmp.mapping.scroll_docs(4)"; + "" = "cmp.mapping.complete()"; + "" = # lua + '' + cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + -- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable() + -- they way you will only jump inside the snippet region + elseif luasnip.expand_or_locally_jumpable() then + luasnip.expand_or_jump() + elseif has_words_before() then + cmp.complete() + else + fallback() + end + end, { "i", "s" }) + ''; + "" = # lua + '' + cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif luasnip.jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, { "i", "s" }) + ''; + "" = "cmp.mapping(cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Select }), {'i'})"; + "" = "cmp.mapping(cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Select }), {'i'})"; + }; + + sources = [ + { name = "luasnip"; } + { name = "nvim_lsp"; } + { name = "path"; } + { name = "buffer"; } + { name = "calc"; } + { name = "git"; } + { name = "omni"; } + ]; + }; + }; +} diff --git a/neovim/default.nix b/neovim/default.nix index 88dae92..89e6645 100644 --- a/neovim/default.nix +++ b/neovim/default.nix @@ -9,6 +9,7 @@ imports = [ ./lsp.nix ./notes.nix + ./completion.nix ./modules/commands.nix @@ -211,73 +212,6 @@ # plugins.firenvim.enable = false; - extraConfigLuaPre = '' - 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") - ''; - - plugins.cmp = { - enable = true; - - settings = { - snippet.expand = '' - function(args) - require('luasnip').lsp_expand(args.body) - end - ''; - mapping = { - "" = "cmp.mapping.confirm({select = true })"; - "" = "cmp.mapping.scroll_docs(-4)"; - "" = "cmp.mapping.scroll_docs(4)"; - "" = "cmp.mapping.complete()"; - "" = # lua - '' - cmp.mapping(function(fallback) - if cmp.visible() then - cmp.select_next_item() - -- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable() - -- they way you will only jump inside the snippet region - elseif luasnip.expand_or_locally_jumpable() then - luasnip.expand_or_jump() - elseif has_words_before() then - cmp.complete() - else - fallback() - end - end, { "i", "s" }) - ''; - "" = # lua - '' - cmp.mapping(function(fallback) - if cmp.visible() then - cmp.select_prev_item() - elseif luasnip.jumpable(-1) then - luasnip.jump(-1) - else - fallback() - end - end, { "i", "s" }) - ''; - "" = "cmp.mapping(cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Select }), {'i'})"; - "" = "cmp.mapping(cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Select }), {'i'})"; - }; - - sources = [ - { name = "luasnip"; } - { name = "nvim_lsp"; } - { name = "path"; } - { name = "buffer"; } - { name = "calc"; } - { name = "git"; } - { name = "omni"; } - ]; - }; - }; - plugins.telescope = { enable = true; extensions = {