From a7d3f4c9ee4f9ff4eb499fbc160096c971036323 Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 8 Aug 2022 13:57:38 +0200 Subject: [PATCH] nvim: modularise plugin config --- .config/nvim/init.lua | 4 + .config/nvim/lua/completion.lua | 139 +++++++++++++++++-------------- .config/nvim/lua/fileformats.lua | 20 +++-- .config/nvim/lua/lsp.lua | 79 ++++++++++-------- .config/nvim/lua/plugins.lua | 43 +++------- .config/nvim/lua/settings.lua | 9 +- 6 files changed, 154 insertions(+), 140 deletions(-) diff --git a/.config/nvim/init.lua b/.config/nvim/init.lua index 7253ed5..9235dde 100644 --- a/.config/nvim/init.lua +++ b/.config/nvim/init.lua @@ -4,3 +4,7 @@ require('keymaps') require('fileformats') require('lsp') require('completion') + +if packer_bootstrap then + require('packer').sync() +end diff --git a/.config/nvim/lua/completion.lua b/.config/nvim/lua/completion.lua index c5cebb6..499ec72 100644 --- a/.config/nvim/lua/completion.lua +++ b/.config/nvim/lua/completion.lua @@ -1,71 +1,82 @@ require('plugins') +require('lsp') -local cmp = require'cmp' -cmp.setup({ - snippet = { - expand = function(args) - require('luasnip').lsp_expand(args.body) - end, - }, - window = { - -- completion = cmp.config.window.bordered(), - -- documentation = cmp.config.window.bordered(), - }, - mapping = cmp.mapping.preset.insert({ - [''] = cmp.mapping.scroll_docs(-4), - [''] = cmp.mapping.scroll_docs(4), - [''] = cmp.mapping.complete(), - [''] = cmp.mapping.abort(), - [''] = cmp.mapping.confirm({ select = false }), - [''] = cmp.mapping.confirm({ select = true }), - }), - sources = cmp.config.sources({ - { name = 'nvim_lsp' }, - { name = 'luasnip' }, -- For luasnip users. - }, { - { name = 'buffer' }, - }) -}) +use 'https://github.com/hrsh7th/cmp-nvim-lsp' +use 'https://github.com/hrsh7th/cmp-buffer' +use 'https://github.com/hrsh7th/cmp-path' +use 'https://github.com/hrsh7th/cmp-cmdline' +use 'https://github.com/hrsh7th/cmp-nvim-lua' +use { + 'https://github.com/hrsh7th/nvim-cmp', + config = function() + local cmp = require'cmp' + cmp.setup({ + snippet = { + expand = function(args) + require('luasnip').lsp_expand(args.body) + end, + }, + window = { + -- completion = cmp.config.window.bordered(), + -- documentation = cmp.config.window.bordered(), + }, + mapping = cmp.mapping.preset.insert({ + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.abort(), + [''] = cmp.mapping.confirm({ select = false }), + [''] = cmp.mapping.confirm({ select = true }), + }), + sources = cmp.config.sources({ + { name = 'nvim_lsp' }, + { name = 'luasnip' }, -- For luasnip users. + }, { + { name = 'buffer' }, + }) + }) --- Set configuration for specific filetype. -cmp.setup.filetype('gitcommit', { - sources = cmp.config.sources({ - { name = 'cmp_git' }, - }, { - { name = 'buffer' }, - }) -}) + -- Set configuration for specific filetype. + cmp.setup.filetype('gitcommit', { + sources = cmp.config.sources({ + { name = 'cmp_git' }, + }, { + { name = 'buffer' }, + }) + }) --- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore). -cmp.setup.cmdline('/', { - mapping = cmp.mapping.preset.cmdline(), - sources = { - { name = 'buffer' } - } -}) + -- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore). + cmp.setup.cmdline('/', { + mapping = cmp.mapping.preset.cmdline(), + sources = { + { name = 'buffer' } + } + }) --- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore). -cmp.setup.cmdline(':', { - mapping = cmp.mapping.preset.cmdline(), - sources = cmp.config.sources({ - { name = 'path' } - }, { - { name = 'cmdline' } - }) -}) + -- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore). + cmp.setup.cmdline(':', { + mapping = cmp.mapping.preset.cmdline(), + sources = cmp.config.sources({ + { name = 'path' } + }, { + { name = 'cmdline' } + }) + }) --- setup lspconfig -local capabilities = require('cmp_nvim_lsp').update_capabilities( - vim.lsp.protocol.make_client_capabilities()) --- add for each enabled lsp server -require('lspconfig')['clangd'].setup { - capabilities = capabilities + -- setup lspconfig + local capabilities = require('cmp_nvim_lsp').update_capabilities( + vim.lsp.protocol.make_client_capabilities()) + -- add for each enabled lsp server + require('lspconfig')['clangd'].setup { + capabilities = capabilities + } + + cmp.setup.filetype('lua', { + sources = cmp.config.sources({ + { name = 'nvim_lua' }, + }, { + { name = 'buffer' }, + }) + }) + end } - -cmp.setup.filetype('lua', { - sources = cmp.config.sources({ - { name = 'nvim_lua' }, - }, { - { name = 'buffer' }, - }) -}) diff --git a/.config/nvim/lua/fileformats.lua b/.config/nvim/lua/fileformats.lua index 88f7c23..24550e2 100644 --- a/.config/nvim/lua/fileformats.lua +++ b/.config/nvim/lua/fileformats.lua @@ -1,11 +1,17 @@ require('plugins') -- org -require('orgmode').setup_ts_grammar() -require'nvim-treesitter.configs'.setup { - highlight = { - enable = true, - additional_vim_regex_highlighting = {'org'} - }, - ensure_installed = {'org'}, +use { + 'https://github.com/nvim-orgmode/orgmode', + config = function() + require('orgmode').setup{} + require('orgmode').setup_ts_grammar() + require'nvim-treesitter.configs'.setup { + highlight = { + enable = true, + additional_vim_regex_highlighting = {'org'} + }, + ensure_installed = {'org'}, + } + end } diff --git a/.config/nvim/lua/lsp.lua b/.config/nvim/lua/lsp.lua index f59caa7..bc2b2ab 100644 --- a/.config/nvim/lua/lsp.lua +++ b/.config/nvim/lua/lsp.lua @@ -1,44 +1,49 @@ require('plugins') require('keymaps') -map('n', '', vim.diagnostic.goto_prev) -map('n', '', vim.diagnostic.goto_next) +use { + 'https://github.com/neovim/nvim-lspconfig', + config = function() + map('n', '', vim.diagnostic.goto_prev) + map('n', '', vim.diagnostic.goto_next) --- Only map the following keys after the language server attaches to the --- current buffer --- TODO: get key mappings to work. -local on_attach = function(client, bufnr) - -- Enable completion triggered by - vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') + -- Only map the following keys after the language server attaches to the + -- current buffer + -- TODO: get key mappings to work. + local on_attach = function(client, bufnr) + -- Enable completion triggered by + vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') - local function maplsp(mode, shortcut, command) - vim.keymap.set(mode, shortcut, command, - { noremap = true, silent = true, buffer=bufnr }) + local function maplsp(mode, shortcut, command) + vim.keymap.set(mode, shortcut, command, + { noremap = true, silent = true, buffer=bufnr }) + end + + maplsp('n', 'gD', vim.lsp.buf.declaration) + maplsp('n', 'gd', vim.lsp.buf.definition) + maplsp('n', 'K', vim.lsp.buf.hover) + maplsp('n', 'gi', vim.lsp.buf.implementation) + maplsp('n', '', vim.lsp.buf.signature_help) + maplsp('n', 'wa', vim.lsp.buf.add_workspace_folder) + maplsp('n', 'wr', vim.lsp.buf.remove_workspace_folder) + maplsp('n', 'wl', function() + print(vim.inspect(vim.lsp.buf.list_workspace_folders())) + end) + maplsp('n', 'D', vim.lsp.buf.type_definition) + maplsp('n', 'rn', vim.lsp.buf.rename) + maplsp('n', 'ca', vim.lsp.buf.code_action) + maplsp('n', 'gr', vim.lsp.buf.references) + maplsp('n', 'f', vim.lsp.buf.formatting) + end + + -- setup servers + require'lspconfig'.clangd.setup{ + cmd = { + 'clangd', + '--compile-commands-dir=build', + '--clang-tidy', -- needs >=clangd-9 + '--ranking-model=decision_forest' -- needs >=clangd-12 + } + } end - - maplsp('n', 'gD', vim.lsp.buf.declaration) - maplsp('n', 'gd', vim.lsp.buf.definition) - maplsp('n', 'K', vim.lsp.buf.hover) - maplsp('n', 'gi', vim.lsp.buf.implementation) - maplsp('n', '', vim.lsp.buf.signature_help) - maplsp('n', 'wa', vim.lsp.buf.add_workspace_folder) - maplsp('n', 'wr', vim.lsp.buf.remove_workspace_folder) - maplsp('n', 'wl', function() - print(vim.inspect(vim.lsp.buf.list_workspace_folders())) - end) - maplsp('n', 'D', vim.lsp.buf.type_definition) - maplsp('n', 'rn', vim.lsp.buf.rename) - maplsp('n', 'ca', vim.lsp.buf.code_action) - maplsp('n', 'gr', vim.lsp.buf.references) - maplsp('n', 'f', vim.lsp.buf.formatting) -end - --- setup servers -require'lspconfig'.clangd.setup{ - cmd = { - 'clangd', - '--compile-commands-dir=build', - '--clang-tidy', -- needs >=clangd-9 - '--ranking-model=decision_forest' -- needs >=clangd-12 - } } diff --git a/.config/nvim/lua/plugins.lua b/.config/nvim/lua/plugins.lua index 9b699f1..6e7159e 100644 --- a/.config/nvim/lua/plugins.lua +++ b/.config/nvim/lua/plugins.lua @@ -5,39 +5,20 @@ if fn.empty(fn.glob(install_path)) > 0 then vim.cmd [[packadd packer.nvim]] end -require('packer').startup(function(use) - use 'https://github.com/wbthomason/packer.nvim' - use 'https://github.com/owozsh/amora' - -- file formats - use { - 'https://github.com/nvim-orgmode/orgmode', - config = function() - require('orgmode').setup{} - end - } - -- source code - use 'https://github.com/tpope/vim-commentary' - ---- lsp - use { - 'https://github.com/nvim-treesitter/nvim-treesitter', - run = function() - require('nvim-treesitter.install').update({ with_sync = true }) - end - } - use 'https://github.com/neovim/nvim-lspconfig' - ---- completion - use 'https://github.com/L3MON4D3/LuaSnip' - use 'https://github.com/hrsh7th/cmp-nvim-lsp' - use 'https://github.com/hrsh7th/cmp-buffer' - use 'https://github.com/hrsh7th/cmp-path' - use 'https://github.com/hrsh7th/cmp-cmdline' - use 'https://github.com/hrsh7th/cmp-nvim-lua' - use 'https://github.com/hrsh7th/nvim-cmp' +packer = require('packer') +packer.init() +use = packer.use - if packer_bootstrap then - require('packer').sync() +use 'https://github.com/wbthomason/packer.nvim' + +-- common dependencies +use { + 'https://github.com/nvim-treesitter/nvim-treesitter', + run = function() + require('nvim-treesitter.install').update({ with_sync = true }) end -end) +} +use 'https://github.com/L3MON4D3/LuaSnip' vim.cmd([[ augroup packer_user_config diff --git a/.config/nvim/lua/settings.lua b/.config/nvim/lua/settings.lua index ed757a0..53088fa 100644 --- a/.config/nvim/lua/settings.lua +++ b/.config/nvim/lua/settings.lua @@ -11,7 +11,14 @@ vim.o.completeopt = 'menu,menuone,noselect' -- completion popup -- theme vim.o.termguicolors = true -- 24 bit colours vim.o.background = 'dark' -vim.cmd('colorscheme amora') +packer.use { + 'https://github.com/owozsh/amora', + config = function() + vim.cmd('colorscheme amora') + end +} + +use 'https://github.com/tpope/vim-commentary' -- toggle comments -- remove trailing whitespace function remove_trailing_whitespace()