diff --git a/init.lua b/init.lua index 221e8a0..c52ffe4 100644 --- a/init.lua +++ b/init.lua @@ -2,6 +2,4 @@ require('plugins') -- Setup packer.nvim and install plugins require('nvim_opts') -- Basic vim options require('autocmd') -- Convenient Autocommands require('lsp') -require('keybindings') -require('nvim-cmp-cfg') require('ts_config') diff --git a/lua/keybindings.lua b/lua/keybindings.lua index e70a027..2f16e77 100644 --- a/lua/keybindings.lua +++ b/lua/keybindings.lua @@ -52,33 +52,33 @@ map('n', 'gl', 'diffget //3', options) -- merge from right pane -- 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) - local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end - local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end - - -- Enable completion triggered by - buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc') - - -- Mappings. - local opts = { noremap=true, silent=true } - - - map('n', 'gD', function() vim.lsp.buf.declaration() end, options) - map('n', 'gd', function() vim.lsp.buf.definition() end, options) - map('n', 'k', function() vim.lsp.buf.hover() end, options) - map('n', 's', function() vim.lsp.buf.signature_help() end, options) - map('n', 'r', function() vim.lsp.buf.rename() end, options) - map('n', 'a', function() vim.lsp.buf.code_action() end, options) - map('n', 'gr', function() vim.lsp.buf.references() end, options) - map('n', 'd', function() vim.diagnostic.open_float() end, options) - map('n', '[d', function() vim.diagnostic.goto_prev() end, options) - map('n', ']d', function() vim.diagnostic.goto_next() end, options) - map('n', 'Q', function() vim.diagnostic.set_qflist() end, options) - map('n', 'F', function() vim.lsp.buf.format() end, options) - + local opts = { buffer = bufnr, remap = false } + + vim.keymap.set('n', 'gd', function() vim.lsp.buf.definition() end, opts) + vim.keymap.set('n', 'k', function() vim.lsp.buf.hover() end, opts) + vim.keymap.set('n', 'r', function() vim.lsp.buf.rename() end, opts) + vim.keymap.set('n', 'a', function() vim.lsp.buf.code_action() end, opts) + vim.keymap.set('n', 'gr', function() vim.lsp.buf.references() end, opts) + vim.keymap.set('n', 'd', function() vim.diagnostic.open_float() end, opts) + vim.keymap.set('n', '[d', function() vim.diagnostic.goto_prev() end, opts) + vim.keymap.set('n', ']d', function() vim.diagnostic.goto_next() end, opts) + vim.keymap.set('n', 'Q', function() vim.diagnostic.set_qflist() end, opts) + vim.keymap.set('n', 'F', function() vim.lsp.buf.format() end, opts) end --- Code actions --- TODO: Generalize to LSP or only attach on Rust buffers -map('n', 't', 'RustTest', options) -- Run test under cursor +local cmp_mappings = function () + local cmp = require('cmp') + return { + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.select_prev_item(cmp_select), + [''] = cmp.mapping.select_next_item(cmp_select), + [''] = cmp.mapping.confirm { + behavior = cmp.ConfirmBehavior.Replace, + select = true, + }, + } +end -return { on_attach = on_attach } +return { on_attach = on_attach, cmp_mappings = cmp_mappings } diff --git a/lua/lsp.lua b/lua/lsp.lua index 7fab28c..c0c58d0 100644 --- a/lua/lsp.lua +++ b/lua/lsp.lua @@ -1,18 +1,40 @@ -local on_attach = require'keybindings'.on_attach --- Add additional capabilities supported by nvim-cmp -local capabilities = vim.lsp.protocol.make_client_capabilities() -capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities) - -local lspconfig = require('lspconfig') - --- SERVERS --- rust_analyzer -vim.g.rustfmt_autosave = 1 -lspconfig.rust_analyzer.setup{ - on_attach = on_attach, - capabilities = capabilities, +local lsp = require('lsp-zero').preset({ + name = 'minimal', + set_lsp_keymaps = false, + manage_nvim_cmp = false, + suggest_lsp_servers = false, +}) + +local keybindings = require('keybindings') +local on_attach = keybindings.on_attach +local cmp_mapping = keybindings.cmp_mappings + +lsp.on_attach(on_attach) + +local cmp = require('cmp') +local cmp_config = lsp.defaults.cmp_config({ + preselect = 'none', + window = { + completion = cmp.config.window.bordered(), + documentation = cmp.config.window.bordered(), + }, + mapping = cmp.mapping.preset.insert(cmp_mappings), + sources = { + { name = 'nvim_lsp' }, + { name = 'luasnip' }, + }, +}) +cmp.setup(cmp_config) + +-- Configure lua language server for neovim +lsp.nvim_workspace() + +lsp.configure('rust_analyzer', { settings = { ["rust-analyzer"] = { + cargo = { + features = "all", + }, procMacro = { enable = true, }, @@ -20,17 +42,18 @@ lspconfig.rust_analyzer.setup{ command = 'clippy', extraArgs = { "--", "-W", "clippy::pedantic" } }, - } + }, } -} - -lspconfig.gopls.setup { - on_attach = on_attach, - capabilities=capabilities, -} - --- clangd -lspconfig.clangd.setup{ - on_attach = on_attach, - capabilities = capabilities, -} +}) + +lsp.configure('tsserver', { + cmd = { "npx", "typescript-language-server", "--stdio" }, +}) + +lsp.configure('html', { + cmd = { "npx", "vscode-html-language-server", "--stdio" }, +}) + +lsp.setup_servers({'tsserver', 'rust_analyzer', 'html'}) + +lsp.setup() diff --git a/lua/nvim_opts.lua b/lua/nvim_opts.lua index 34b092a..df1deac 100644 --- a/lua/nvim_opts.lua +++ b/lua/nvim_opts.lua @@ -1,4 +1,4 @@ -local o = vim.o -- global +local o = vim.o -- globalopts local g = vim.g -- global 2? local wo = vim.wo -- window local local bo = vim.bo -- buffer local @@ -43,3 +43,5 @@ o.dir = '/tmp' -- Buffers o.hidden = true -- Allow hidden buffers without saving + +vim.opt.completeopt = {'menu','menuone','noselect'} diff --git a/lua/plugins.lua b/lua/plugins.lua index 549c1fc..5a5483a 100644 --- a/lua/plugins.lua +++ b/lua/plugins.lua @@ -17,31 +17,28 @@ local plugins = require('packer').startup(function(use) -- git use 'tpope/vim-fugitive' - -- LSP - use 'neovim/nvim-lspconfig' -- Common lsp server configurations - use { - 'hrsh7th/nvim-cmp', - } -- Autocompletion plugin - use 'hrsh7th/cmp-nvim-lsp' -- LSP source for nvim-cmp - - -- LSP Extensions - use 'nvim-lua/lsp_extensions.nvim' - - -- Snippets (required for nvim-cmp) - use 'L3MON4D3/LuaSnip' - use { - 'saadparwaiz1/cmp_luasnip', - commit = 'b10829736542e7cc9291e60bab134df1273165c9', - } - use { 'nvim-treesitter/nvim-treesitter', run = ':TSUpdate' } - -- Language Specfic - use 'rust-lang/rust.vim' - use 'https://git.sr.ht/~sircmpwn/hare.vim' + -- lsp + use { + 'VonHeikemen/lsp-zero.nvim', + branch = 'v1.x', + requires = { + -- LSP Support + {'neovim/nvim-lspconfig'}, + + -- Autocompletion + {'hrsh7th/nvim-cmp'}, + {'hrsh7th/cmp-nvim-lsp'}, + {'hrsh7th/cmp-buffer'}, -- Optional + + -- Snippets + {'L3MON4D3/LuaSnip'}, + }, + } if packer_bootstrap then require('packer').sync()