From 104c21c904adbd94a619ed41e279c0db0879c5aa Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Tue, 18 Apr 2023 01:48:44 +0300 Subject: [PATCH] feat(LSP): lspkind and sources --- extra.nix | 11 +++- modules/completion/nvim-cmp/config.nix | 72 ++++++++++++++++-------- modules/completion/nvim-cmp/nvim-cmp.nix | 43 +++++++++++--- modules/languages/rust.nix | 2 +- modules/lsp/default.nix | 1 + modules/lsp/lspkind/config.nix | 20 +++++++ modules/lsp/lspkind/default.nix | 6 ++ modules/lsp/lspkind/lspkind.nix | 22 ++++++++ modules/treesitter/config.nix | 2 +- modules/visuals/config.nix | 8 --- modules/visuals/visuals.nix | 6 -- 11 files changed, 143 insertions(+), 50 deletions(-) create mode 100644 modules/lsp/lspkind/config.nix create mode 100644 modules/lsp/lspkind/default.nix create mode 100644 modules/lsp/lspkind/lspkind.nix diff --git a/extra.nix b/extra.nix index 91b20be..3828409 100644 --- a/extra.nix +++ b/extra.nix @@ -29,6 +29,16 @@ inputs: let }; }; + vim.lsp = { + formatOnSave = true; + lspkind.enable = false; + lightbulb.enable = true; + lspsaga.enable = false; + nvimCodeActionMenu.enable = true; + trouble.enable = true; + lspSignature.enable = true; + }; + vim.languages = { enableLSP = true; enableFormat = true; @@ -56,7 +66,6 @@ inputs: let smoothScroll.enable = true; cellularAutomaton.enable = true; fidget-nvim.enable = true; - lspkind.enable = true; indentBlankline = { enable = true; fillChar = ""; diff --git a/modules/completion/nvim-cmp/config.nix b/modules/completion/nvim-cmp/config.nix index 4495243..b9fecec 100644 --- a/modules/completion/nvim-cmp/config.nix +++ b/modules/completion/nvim-cmp/config.nix @@ -7,8 +7,27 @@ with lib; with builtins; let cfg = config.vim.autocomplete; + lspkindEnabled = config.vim.lsp.enable && config.vim.lsp.lspkind.enable; builtSources = - concatMapStringsSep "\n" (x: "{ name = '${x}'},") cfg.sources; + concatMapStringsSep + "\n" + (n: "{ name = '${n}'},") + (attrNames cfg.sources); + + builtMaps = + concatStringsSep + "\n" + (mapAttrsToList + (n: v: + if v == null + then "" + else "${n} = '${v}',") + cfg.sources); + + dagPlacement = + if lspkindEnabled + then nvim.dag.entryAfter ["lspkind"] + else nvim.dag.entryAnywhere; in { config = mkIf cfg.enable { vim.startPlugins = [ @@ -18,14 +37,28 @@ in { "cmp-path" ]; - vim.autocomplete.sources = [ - "nvim-cmp" - "vsnip" - "buffer" - "path" - ]; + vim.autocomplete.sources = { + "nvim-cmp" = null; + "vsnip" = "[VSnip]"; + "buffer" = "[Buffer]"; + "crates" = "[Crates]"; + "path" = "[Path]"; + }; + + vim.luaConfigRC.completion = mkIf (cfg.type == "nvim-cmp") (dagPlacement '' + local nvim_cmp_menu_map = function(entry, vim_item) + -- name for each source + vim_item.menu = ({ + ${builtMaps} + })[entry.source.name] + print(vim_item.menu) + return vim_item + end + + ${optionalString lspkindEnabled '' + lspkind_opts.before = ${cfg.formatting.format} + ''} - vim.luaConfigRC.completion = mkIf (cfg.type == "nvim-cmp") (nvim.dag.entryAnywhere '' 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 @@ -80,23 +113,12 @@ in { completeopt = 'menu,menuone,noinsert', }, formatting = { - format = function(entry, vim_item) - -- type of kind - vim_item.kind = ${ - optionalString (config.vim.visuals.lspkind.enable) - "require('lspkind').presets.default[vim_item.kind] .. ' ' .." - } vim_item.kind - - -- name for each source - vim_item.menu = ({ - buffer = "[Buffer]", - nvim_lsp = "[LSP]", - vsnip = "[VSnip]", - crates = "[Crates]", - path = "[Path]", - })[entry.source.name] - return vim_item - end, + format = + ${ + if lspkindEnabled + then "lspkind.cmp_format(lspkind_opts)" + else cfg.formatting.format + }, } }) ${optionalString (config.vim.autopairs.enable && config.vim.autopairs.type == "nvim-autopairs") '' diff --git a/modules/completion/nvim-cmp/nvim-cmp.nix b/modules/completion/nvim-cmp/nvim-cmp.nix index 6585218..77c677e 100644 --- a/modules/completion/nvim-cmp/nvim-cmp.nix +++ b/modules/completion/nvim-cmp/nvim-cmp.nix @@ -5,11 +5,7 @@ ... }: with lib; -with builtins; let - cfg = config.vim.autocomplete; - builtSources = - concatMapStringsSep "\n" (x: "{ name = '${x}'},") cfg.sources; -in { +with builtins; { options.vim = { autocomplete = { enable = mkOption { @@ -25,9 +21,40 @@ in { }; sources = mkOption { - description = "List of source names for nvim-cmp"; - type = with types; listOf str; - default = []; + description = nvim.nmd.asciiDoc '' + Attribute set of source names for nvim-cmp. + + If an attribute set is provided, then the menu value of + `vim_item` in the format will be set to the value (if + utilizing the `nvim_cmp_menu_map` function). + + Note: only use a single attribute name per attribute set + ''; + type = with types; attrsOf (nullOr str); + default = {}; + example = '' + {nvim-cmp = null; buffer = "[Buffer]";} + ''; + }; + + formatting = { + format = mkOption { + description = nvim.nmd.asciiDoc '' + The function used to customize the appearance of the completion menu. + + If <> is true, then the function + will be called before modifications from lspkind. + + Default is to call the menu mapping function. + ''; + type = types.str; + default = "nvim_cmp_menu_map"; + example = '' + function(entry, vim_item) + return vim_item + end + ''; + }; }; }; }; diff --git a/modules/languages/rust.nix b/modules/languages/rust.nix index 8c330b3..25930cd 100644 --- a/modules/languages/rust.nix +++ b/modules/languages/rust.nix @@ -54,7 +54,7 @@ in { vim.startPlugins = ["crates-nvim"]; - vim.autocomplete.sources = ["crates"]; + vim.autocomplete.sources = {"crates" = "[Crates]";}; vim.luaConfigRC.rust-crates = nvim.dag.entryAnywhere '' require('crates').setup { null_ls = { diff --git a/modules/lsp/default.nix b/modules/lsp/default.nix index b9a39c6..050187b 100644 --- a/modules/lsp/default.nix +++ b/modules/lsp/default.nix @@ -14,6 +14,7 @@ _: { ./trouble ./lsp-signature ./lightbulb + ./lspkind # flutter-tools ./flutter-tools-nvim diff --git a/modules/lsp/lspkind/config.nix b/modules/lsp/lspkind/config.nix new file mode 100644 index 0000000..a243d62 --- /dev/null +++ b/modules/lsp/lspkind/config.nix @@ -0,0 +1,20 @@ +{ + pkgs, + config, + lib, + ... +}: +with lib; +with builtins; let + cfg = config.vim.lsp; +in { + config = mkIf (cfg.enable && cfg.lspkind.enable) { + vim.startPlugins = ["lspkind"]; + vim.luaConfigRC.lspkind = nvim.dag.entryAnywhere '' + local lspkind = require'lspkind' + local lspkind_opts = { + mode = '${cfg.lspkind.mode}' + } + ''; + }; +} diff --git a/modules/lsp/lspkind/default.nix b/modules/lsp/lspkind/default.nix new file mode 100644 index 0000000..81f7418 --- /dev/null +++ b/modules/lsp/lspkind/default.nix @@ -0,0 +1,6 @@ +_: { + imports = [ + ./config.nix + ./lspkind.nix + ]; +} diff --git a/modules/lsp/lspkind/lspkind.nix b/modules/lsp/lspkind/lspkind.nix new file mode 100644 index 0000000..82d721b --- /dev/null +++ b/modules/lsp/lspkind/lspkind.nix @@ -0,0 +1,22 @@ +{ + pkgs, + config, + lib, + ... +}: +with lib; +with builtins; let + cfg = config.vim.lsp; +in { + options.vim.lsp = { + lspkind = { + enable = mkEnableOption "vscode-like pictograms for lsp [lspkind]"; + + mode = mkOption { + description = "Defines how annotations are shown"; + type = with types; enum ["text" "text_symbol" "symbol_text" "symbol"]; + default = "symbol_text"; + }; + }; + }; +} diff --git a/modules/treesitter/config.nix b/modules/treesitter/config.nix index 27744ef..b75121b 100644 --- a/modules/treesitter/config.nix +++ b/modules/treesitter/config.nix @@ -14,7 +14,7 @@ in { ["nvim-treesitter"] ++ optional usingNvimCmp "cmp-treesitter"; - vim.autocomplete.sources = ["treesitter"]; + vim.autocomplete.sources = {"treesitter" = "[Treesitter]";}; # For some reason treesitter highlighting does not work on start if this is set before syntax on vim.configRC.treesitter-fold = mkIf cfg.fold (nvim.dag.entryBefore ["basic"] '' diff --git a/modules/visuals/config.nix b/modules/visuals/config.nix index 532df52..d1dd0ec 100644 --- a/modules/visuals/config.nix +++ b/modules/visuals/config.nix @@ -7,14 +7,6 @@ with lib; let cfg = config.vim.visuals; in { config = mkIf cfg.enable (mkMerge [ - (mkIf cfg.lspkind.enable { - vim.startPlugins = ["lspkind"]; - vim.luaConfigRC.lspkind = nvim.dag.entryAnywhere '' - -- lspkind - require'lspkind'.init() - ''; - }) - (mkIf cfg.indentBlankline.enable { vim.startPlugins = ["indent-blankline"]; vim.luaConfigRC.indent-blankline = nvim.dag.entryAnywhere '' diff --git a/modules/visuals/visuals.nix b/modules/visuals/visuals.nix index 7fe03ad..695e3b6 100644 --- a/modules/visuals/visuals.nix +++ b/modules/visuals/visuals.nix @@ -18,12 +18,6 @@ with builtins; { default = false; }; - lspkind.enable = mkOption { - type = types.bool; - description = "Enable vscode-like pictograms for lsp [lspkind]"; - default = false; - }; - scrollBar.enable = mkOption { type = types.bool; description = "Enable scrollbar [scrollbar.nvim]";