From 865db595c937703696cac6dd2002086fece4efb6 Mon Sep 17 00:00:00 2001 From: janabhumi Date: Fri, 16 Sep 2022 23:42:53 +0300 Subject: [PATCH] plugin/lsp: add autocompletion --- default.nix | 4 +- flake.nix | 6 ++- plugins/explorer/default.nix | 6 +-- plugins/explorer/nvim-tree.lua | 1 - plugins/formatter/default.nix | 6 +-- plugins/formatter/neoformat.lua | 4 +- plugins/lsp/config.lua | 66 ----------------------------- plugins/lsp/default.nix | 15 ++++--- plugins/lsp/lspconfig.lua | 69 +++++++++++++++++++++++++++++++ plugins/lsp/nvim-cmp.lua | 43 +++++++++++++++++++ plugins/syntax/default.nix | 4 +- plugins/syntax/treesitter.lua | 16 +++---- plugins/theme/catppuccin-nvim.lua | 2 +- plugins/theme/default.nix | 4 +- 14 files changed, 148 insertions(+), 98 deletions(-) delete mode 100644 plugins/lsp/config.lua create mode 100644 plugins/lsp/lspconfig.lua create mode 100644 plugins/lsp/nvim-cmp.lua diff --git a/default.nix b/default.nix index a3bf783..5b5a916 100644 --- a/default.nix +++ b/default.nix @@ -1,9 +1,9 @@ -{ callPackage, wrapNeovim, neovim-unwrapped, vimPlugins, ... }: +{ wrapNeovim, neovim-unwrapped, vimPlugins, ... }: let lib = import ./lib.nix; - callPlugins = list: builtins.map (p: callPackage p { }) list; + callPlugins = list: builtins.map (p: import p vimPlugins) list; plugins = callPlugins [ ./plugins/syntax diff --git a/flake.nix b/flake.nix index c504c61..598f95d 100644 --- a/flake.nix +++ b/flake.nix @@ -17,7 +17,11 @@ in { devShells.default = pkgs.mkShell { - packages = [ neovim nil ]; + packages = [ + neovim + nil # nix lsp + pkgs.stylua # lua formatter + ]; }; }); } diff --git a/plugins/explorer/default.nix b/plugins/explorer/default.nix index c3afe17..004c4cc 100644 --- a/plugins/explorer/default.nix +++ b/plugins/explorer/default.nix @@ -1,9 +1,7 @@ -{ vimPlugins, ... }: +{ nvim-tree-lua, ... }: { luaConfig = builtins.readFile ./nvim-tree.lua; - plugins = [ - vimPlugins.nvim-tree-lua - ]; + plugins = [ nvim-tree-lua ]; } diff --git a/plugins/explorer/nvim-tree.lua b/plugins/explorer/nvim-tree.lua index bd1ebef..01d6e0b 100644 --- a/plugins/explorer/nvim-tree.lua +++ b/plugins/explorer/nvim-tree.lua @@ -9,4 +9,3 @@ require("nvim-tree").setup() vim.keymap.set("n", "nt", ":NvimTreeFocus") vim.keymap.set("n", "nf", ":NvimTreeFindFile") - diff --git a/plugins/formatter/default.nix b/plugins/formatter/default.nix index 6aeef34..184e84d 100644 --- a/plugins/formatter/default.nix +++ b/plugins/formatter/default.nix @@ -1,9 +1,7 @@ -{ vimPlugins, ... }: +{ neoformat, ... }: { luaConfig = builtins.readFile ./neoformat.lua; - plugins = [ - vimPlugins.neoformat - ]; + plugins = [ neoformat ]; } diff --git a/plugins/formatter/neoformat.lua b/plugins/formatter/neoformat.lua index dcad4bc..62643dd 100644 --- a/plugins/formatter/neoformat.lua +++ b/plugins/formatter/neoformat.lua @@ -2,9 +2,9 @@ vim.g.neoformat_try_node_exe = 1 vim.g.neoformat_only_msg_on_error = 1 vim.g.neoformat_enabled_markdown = { "denofmt" } -vim.cmd[[ +vim.cmd([[ aug fmt au! au BufWritePre * try | undojoin | Neoformat | catch /E790/ | Neoformat | endtry aug END -]] +]]) diff --git a/plugins/lsp/config.lua b/plugins/lsp/config.lua deleted file mode 100644 index f000479..0000000 --- a/plugins/lsp/config.lua +++ /dev/null @@ -1,66 +0,0 @@ -local lsp_config = require 'lspconfig' - --- always show signcolumns -vim.opt.signcolumn = "yes" - --- Mappings. --- See `:help vim.diagnostic.*` for documentation on any of the below functions -local opts = { noremap=true, silent=true } -vim.keymap.set('n', 'e', vim.diagnostic.open_float, opts) -vim.keymap.set('n', '[c', vim.diagnostic.goto_prev, opts) -vim.keymap.set('n', ']c', vim.diagnostic.goto_next, opts) -vim.keymap.set('n', 'q', vim.diagnostic.setloclist, opts) - --- Use an on_attach function to only map the following keys --- after the language server attaches to the current buffer -local on_attach = function(client, bufnr) - -- Enable completion triggered by - vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') - - -- Mappings. - -- See `:help vim.lsp.*` for documentation on any of the below functions - local bufopts = { noremap=true, silent=true, buffer=bufnr } - vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts) - vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts) - vim.keymap.set('n', 'gy', vim.lsp.buf.type_definition, bufopts) - vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts) - vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts) - vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts) - vim.keymap.set('n', '', vim.lsp.buf.signature_help, bufopts) - vim.keymap.set('n', 'wa', vim.lsp.buf.add_workspace_folder, bufopts) - vim.keymap.set('n', 'wr', vim.lsp.buf.remove_workspace_folder, bufopts) - vim.keymap.set('n', 'wl', function() - print(vim.inspect(vim.lsp.buf.list_workspace_folders())) - end, bufopts) - vim.keymap.set('n', 'n', vim.lsp.buf.rename, bufopts) - vim.keymap.set('n', 'a', vim.lsp.buf.code_action, bufopts) - vim.keymap.set('n', 'f', vim.lsp.buf.formatting, bufopts) -end - -------------------------------------------------------------------------------- --- See: https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md --- --- nix -lsp_config.nil_ls.setup { on_attach = on_attach } - ---[[ --- js,ts -lsp_config.tsserver.setup { on_attach = on_attach } -lsp_config.eslint.setup { on_attach = on_attach } - --- rust -lsp_config.rust_analyzer.setup { - on_attach = on_attach, - -- Server-specific settings... - settings = { - ["rust-analyzer"] = { - "server.path" = "rust-analyzer"; - "updates.prompt" = false; - "updates.checkOnStartup" = false; - "checkOnSave.enable" = true; - "checkOnSave.command" = "clippy"; - "cargo.autoreload" = true; - } - } -} -]]-- diff --git a/plugins/lsp/default.nix b/plugins/lsp/default.nix index 8869b46..fe5950a 100644 --- a/plugins/lsp/default.nix +++ b/plugins/lsp/default.nix @@ -1,11 +1,16 @@ -{ vimPlugins, ... }: +{ nvim-lspconfig, luasnip, nvim-cmp, cmp-nvim-lsp, cmp_luasnip, ... }: { - luaConfig = builtins.readFile ./config.lua; + luaConfig = (builtins.readFile ./lspconfig.lua) + + (builtins.readFile ./nvim-cmp.lua); plugins = [ - vimPlugins.nvim-lspconfig - vimPlugins.nvim-cmp # auto-completion + nvim-lspconfig # Collection of configurations for built-in LSP client + + luasnip # Snippets plugin + + nvim-cmp # Autocompletion + cmp-nvim-lsp # LSP source for nvim-cmp + cmp_luasnip # Snippets source for nvim-cmp ]; } - diff --git a/plugins/lsp/lspconfig.lua b/plugins/lsp/lspconfig.lua new file mode 100644 index 0000000..8fa0306 --- /dev/null +++ b/plugins/lsp/lspconfig.lua @@ -0,0 +1,69 @@ +local lsp_config = require("lspconfig") + +-- always show signcolumns +vim.opt.signcolumn = "yes" + +-- Mappings. +-- See `:help vim.diagnostic.*` for documentation on any of the below functions +local opts = { noremap = true, silent = true } +vim.keymap.set("n", "e", vim.diagnostic.open_float, opts) +vim.keymap.set("n", "[c", vim.diagnostic.goto_prev, opts) +vim.keymap.set("n", "]c", vim.diagnostic.goto_next, opts) +vim.keymap.set("n", "q", vim.diagnostic.setloclist, opts) + +-- Use an on_attach function to only map the following keys +-- after the language server attaches to the current buffer +local on_attach = function(client, bufnr) + -- Enable completion triggered by + vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc") + + -- Mappings. + -- See `:help vim.lsp.*` for documentation on any of the below functions + local bufopts = { noremap = true, silent = true, buffer = bufnr } + vim.keymap.set("n", "gD", vim.lsp.buf.declaration, bufopts) + vim.keymap.set("n", "gd", vim.lsp.buf.definition, bufopts) + vim.keymap.set("n", "gy", vim.lsp.buf.type_definition, bufopts) + vim.keymap.set("n", "gr", vim.lsp.buf.references, bufopts) + vim.keymap.set("n", "gi", vim.lsp.buf.implementation, bufopts) + vim.keymap.set("n", "K", vim.lsp.buf.hover, bufopts) + vim.keymap.set("n", "", vim.lsp.buf.signature_help, bufopts) + vim.keymap.set("n", "wa", vim.lsp.buf.add_workspace_folder, bufopts) + vim.keymap.set("n", "wr", vim.lsp.buf.remove_workspace_folder, bufopts) + vim.keymap.set("n", "wl", function() + print(vim.inspect(vim.lsp.buf.list_workspace_folders())) + end, bufopts) + vim.keymap.set("n", "n", vim.lsp.buf.rename, bufopts) + vim.keymap.set("n", "a", vim.lsp.buf.code_action, bufopts) + vim.keymap.set("n", "f", vim.lsp.buf.formatting, bufopts) +end + +-- nvim-cmp +local capabilities = vim.lsp.protocol.make_client_capabilities() +capabilities = require("cmp_nvim_lsp").update_capabilities(capabilities) + +------------------------------------------------------------------------------- +-- See: https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md +-- +-- nix +lsp_config.nil_ls.setup({ on_attach = on_attach, capabilities = capabilities }) + +-- js,ts +lsp_config.tsserver.setup({ on_attach = on_attach, capabilities = capabilities }) +lsp_config.eslint.setup({ on_attach = on_attach, capabilities = capabilities }) + +-- rust +lsp_config.rust_analyzer.setup({ + on_attach = on_attach, + capabilities = capabilities, + -- Server-specific settings... + settings = { + ["rust-analyzer"] = { + ["server.path"] = "rust-analyzer", + ["updates.prompt"] = false, + ["updates.checkOnStartup"] = false, + ["checkOnSave.enable"] = true, + ["checkOnSave.command"] = "clippy", + ["cargo.autoreload"] = true, + }, + }, +}) diff --git a/plugins/lsp/nvim-cmp.lua b/plugins/lsp/nvim-cmp.lua new file mode 100644 index 0000000..5c22613 --- /dev/null +++ b/plugins/lsp/nvim-cmp.lua @@ -0,0 +1,43 @@ +local luasnip = require("luasnip") +local cmp = require("cmp") + +vim.opt.completeopt = { "menu", "menuone", "noselect" } + +cmp.setup({ + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + mapping = cmp.mapping.preset.insert({ + [""] = cmp.mapping.scroll_docs(-4), + [""] = cmp.mapping.scroll_docs(4), + [""] = cmp.mapping.complete(), + [""] = cmp.mapping.confirm({ + behavior = cmp.ConfirmBehavior.Replace, + select = true, + }), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + else + fallback() + end + end, { "i", "s" }), + [""] = 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" }), + }), + sources = { + { name = "nvim_lsp" }, + { name = "luasnip" }, + }, +}) diff --git a/plugins/syntax/default.nix b/plugins/syntax/default.nix index 195ba99..3b2a70a 100644 --- a/plugins/syntax/default.nix +++ b/plugins/syntax/default.nix @@ -1,10 +1,10 @@ -{ vimPlugins, ... }: +{ nvim-treesitter, ... }: { luaConfig = builtins.readFile ./treesitter.lua; plugins = [ - (vimPlugins.nvim-treesitter.withPlugins + (nvim-treesitter.withPlugins (ts: with ts; [ tree-sitter-nix tree-sitter-lua diff --git a/plugins/syntax/treesitter.lua b/plugins/syntax/treesitter.lua index a22df49..9da58ad 100644 --- a/plugins/syntax/treesitter.lua +++ b/plugins/syntax/treesitter.lua @@ -1,12 +1,12 @@ require("nvim-treesitter.configs").setup({ - ensure_installed = {}, - sync_install = false, + ensure_installed = {}, + sync_install = false, - highlight = { - enable = true, - }, + highlight = { + enable = true, + }, - indent = { - enable = true, - }, + indent = { + enable = true, + }, }) diff --git a/plugins/theme/catppuccin-nvim.lua b/plugins/theme/catppuccin-nvim.lua index 014497a..08e9166 100644 --- a/plugins/theme/catppuccin-nvim.lua +++ b/plugins/theme/catppuccin-nvim.lua @@ -2,4 +2,4 @@ vim.g.catppuccin_flavour = "frappe" -- latte, frappe, macchiato, mocha require("catppuccin").setup() -vim.cmd [[colorscheme catppuccin]] +vim.cmd([[colorscheme catppuccin]]) diff --git a/plugins/theme/default.nix b/plugins/theme/default.nix index 0804262..123027c 100644 --- a/plugins/theme/default.nix +++ b/plugins/theme/default.nix @@ -1,7 +1,7 @@ -{ vimPlugins, ... }: +{ catppuccin-nvim, ... }: { luaConfig = builtins.readFile ./catppuccin-nvim.lua; - plugins = [ vimPlugins.catppuccin-nvim ]; + plugins = [ catppuccin-nvim ]; }