plugin/lsp: add autocompletion

This commit is contained in:
Dmitriy Pleshevskiy 2022-09-16 23:42:53 +03:00
parent 354cef331a
commit 865db595c9
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
14 changed files with 148 additions and 98 deletions

View file

@ -1,9 +1,9 @@
{ callPackage, wrapNeovim, neovim-unwrapped, vimPlugins, ... }: { wrapNeovim, neovim-unwrapped, vimPlugins, ... }:
let let
lib = import ./lib.nix; lib = import ./lib.nix;
callPlugins = list: builtins.map (p: callPackage p { }) list; callPlugins = list: builtins.map (p: import p vimPlugins) list;
plugins = callPlugins [ plugins = callPlugins [
./plugins/syntax ./plugins/syntax

View file

@ -17,7 +17,11 @@
in in
{ {
devShells.default = pkgs.mkShell { devShells.default = pkgs.mkShell {
packages = [ neovim nil ]; packages = [
neovim
nil # nix lsp
pkgs.stylua # lua formatter
];
}; };
}); });
} }

View file

@ -1,9 +1,7 @@
{ vimPlugins, ... }: { nvim-tree-lua, ... }:
{ {
luaConfig = builtins.readFile ./nvim-tree.lua; luaConfig = builtins.readFile ./nvim-tree.lua;
plugins = [ plugins = [ nvim-tree-lua ];
vimPlugins.nvim-tree-lua
];
} }

View file

@ -9,4 +9,3 @@ require("nvim-tree").setup()
vim.keymap.set("n", "<leader>nt", ":NvimTreeFocus<CR>") vim.keymap.set("n", "<leader>nt", ":NvimTreeFocus<CR>")
vim.keymap.set("n", "<leader>nf", ":NvimTreeFindFile<CR>") vim.keymap.set("n", "<leader>nf", ":NvimTreeFindFile<CR>")

View file

@ -1,9 +1,7 @@
{ vimPlugins, ... }: { neoformat, ... }:
{ {
luaConfig = builtins.readFile ./neoformat.lua; luaConfig = builtins.readFile ./neoformat.lua;
plugins = [ plugins = [ neoformat ];
vimPlugins.neoformat
];
} }

View file

@ -2,9 +2,9 @@ vim.g.neoformat_try_node_exe = 1
vim.g.neoformat_only_msg_on_error = 1 vim.g.neoformat_only_msg_on_error = 1
vim.g.neoformat_enabled_markdown = { "denofmt" } vim.g.neoformat_enabled_markdown = { "denofmt" }
vim.cmd[[ vim.cmd([[
aug fmt aug fmt
au! au!
au BufWritePre * try | undojoin | Neoformat | catch /E790/ | Neoformat | endtry au BufWritePre * try | undojoin | Neoformat | catch /E790/ | Neoformat | endtry
aug END aug END
]] ]])

View file

@ -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', '<space>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', '<space>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 <c-x><c-o>
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', '<C-k>', vim.lsp.buf.signature_help, bufopts)
vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, bufopts)
vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, bufopts)
vim.keymap.set('n', '<space>wl', function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, bufopts)
vim.keymap.set('n', '<localleader>n', vim.lsp.buf.rename, bufopts)
vim.keymap.set('n', '<localleader>a', vim.lsp.buf.code_action, bufopts)
vim.keymap.set('n', '<localleader>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;
}
}
}
]]--

View file

@ -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 = [ plugins = [
vimPlugins.nvim-lspconfig nvim-lspconfig # Collection of configurations for built-in LSP client
vimPlugins.nvim-cmp # auto-completion
luasnip # Snippets plugin
nvim-cmp # Autocompletion
cmp-nvim-lsp # LSP source for nvim-cmp
cmp_luasnip # Snippets source for nvim-cmp
]; ];
} }

69
plugins/lsp/lspconfig.lua Normal file
View file

@ -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", "<space>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", "<space>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 <c-x><c-o>
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", "<C-k>", vim.lsp.buf.signature_help, bufopts)
vim.keymap.set("n", "<space>wa", vim.lsp.buf.add_workspace_folder, bufopts)
vim.keymap.set("n", "<space>wr", vim.lsp.buf.remove_workspace_folder, bufopts)
vim.keymap.set("n", "<space>wl", function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, bufopts)
vim.keymap.set("n", "<localleader>n", vim.lsp.buf.rename, bufopts)
vim.keymap.set("n", "<localleader>a", vim.lsp.buf.code_action, bufopts)
vim.keymap.set("n", "<localleader>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,
},
},
})

43
plugins/lsp/nvim-cmp.lua Normal file
View file

@ -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({
["<C-d>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(),
["<CR>"] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Replace,
select = true,
}),
["<Tab>"] = 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" }),
["<S-Tab>"] = 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" },
},
})

View file

@ -1,10 +1,10 @@
{ vimPlugins, ... }: { nvim-treesitter, ... }:
{ {
luaConfig = builtins.readFile ./treesitter.lua; luaConfig = builtins.readFile ./treesitter.lua;
plugins = [ plugins = [
(vimPlugins.nvim-treesitter.withPlugins (nvim-treesitter.withPlugins
(ts: with ts; [ (ts: with ts; [
tree-sitter-nix tree-sitter-nix
tree-sitter-lua tree-sitter-lua

View file

@ -1,12 +1,12 @@
require("nvim-treesitter.configs").setup({ require("nvim-treesitter.configs").setup({
ensure_installed = {}, ensure_installed = {},
sync_install = false, sync_install = false,
highlight = { highlight = {
enable = true, enable = true,
}, },
indent = { indent = {
enable = true, enable = true,
}, },
}) })

View file

@ -2,4 +2,4 @@ vim.g.catppuccin_flavour = "frappe" -- latte, frappe, macchiato, mocha
require("catppuccin").setup() require("catppuccin").setup()
vim.cmd [[colorscheme catppuccin]] vim.cmd([[colorscheme catppuccin]])

View file

@ -1,7 +1,7 @@
{ vimPlugins, ... }: { catppuccin-nvim, ... }:
{ {
luaConfig = builtins.readFile ./catppuccin-nvim.lua; luaConfig = builtins.readFile ./catppuccin-nvim.lua;
plugins = [ vimPlugins.catppuccin-nvim ]; plugins = [ catppuccin-nvim ];
} }