From 50936596b5eafd1a25553a9f9131c7612393c7d2 Mon Sep 17 00:00:00 2001 From: Sonny Bakker Date: Thu, 25 Nov 2021 10:12:24 +0100 Subject: [PATCH] Split neovim lua configuration into separate files These seperate files should be put into $HOME/.config/nvim/lua --- nvim/lua/auto-commands.lua | 27 ++++++++++++++++++ nvim/lua/keybindings.lua | 1 + nvim/lua/lsp.lua | 36 +++++++++++++++++++++++ nvim/lua/nvim-cmp.lua | 33 ++++++++++++++++++++++ nvim/lua/options.lua | 58 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 155 insertions(+) create mode 100644 nvim/lua/auto-commands.lua create mode 100644 nvim/lua/keybindings.lua create mode 100644 nvim/lua/lsp.lua create mode 100644 nvim/lua/nvim-cmp.lua create mode 100644 nvim/lua/options.lua diff --git a/nvim/lua/auto-commands.lua b/nvim/lua/auto-commands.lua new file mode 100644 index 0000000..0b00f8e --- /dev/null +++ b/nvim/lua/auto-commands.lua @@ -0,0 +1,27 @@ +--colorscheme +vim.cmd('colorscheme space_vim_theme') +vim.cmd('syntax enable') + +--open folds by default +vim.cmd('autocmd Syntax * normal zR') + +--file specific formatting +vim.cmd([[ + autocmd Filetype python,bash,sh,java,php,json + \ setlocal tabstop=4 softtabstop=4 shiftwidth=4 + \ expandtab autoindent fileformat=unix +]]) + +vim.cmd([[ + autocmd Filetype css,scss,html,htmldjango,javascript,yaml + \ setlocal tabstop=2 softtabstop=2 shiftwidth=2 + \ expandtab autoindent +]]) + +--color trailing spaces with red color +vim.cmd('highlight ExtraWhitespace ctermbg=green guibg=green') +vim.cmd('match ExtraWhitespace /s+$/') +vim.cmd('autocmd BufWinEnter * match ExtraWhitespace /s+$/') +vim.cmd('autocmd InsertEnter * match ExtraWhitespace /s+%#@', ':NERDTreeToggle', {noremap=true, silent=true}) diff --git a/nvim/lua/lsp.lua b/nvim/lua/lsp.lua new file mode 100644 index 0000000..bc0365f --- /dev/null +++ b/nvim/lua/lsp.lua @@ -0,0 +1,36 @@ +--add additional capabilities supported by nvim-cmp +local capabilities = vim.lsp.protocol.make_client_capabilities() +capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities) + +--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 } + + --see `:help vim.lsp.*` for documentation on any of the below functions + buf_set_keymap('n', 'gD', 'lua vim.lsp.buf.declaration()', opts) + buf_set_keymap('n', 'gd', 'lua vim.lsp.buf.definition()', opts) + buf_set_keymap('n', 'K', 'lua vim.lsp.buf.hover()', opts) + buf_set_keymap('n', 'gi', 'lua vim.lsp.buf.implementation()', opts) + buf_set_keymap('n', '', 'lua vim.lsp.buf.signature_help()', opts) +end + +local nvim_lsp = require('lspconfig') + +--enable some language servers with the additional completion capabilities +--offered by nvim-cmp +local servers = { 'pyright', 'ansiblels', 'yamlls', 'tsserver' } + +for _, lsp in ipairs(servers) do + nvim_lsp[lsp].setup { + on_attach = on_attach, + capabilities = capabilities, + } +end diff --git a/nvim/lua/nvim-cmp.lua b/nvim/lua/nvim-cmp.lua new file mode 100644 index 0000000..385ac54 --- /dev/null +++ b/nvim/lua/nvim-cmp.lua @@ -0,0 +1,33 @@ +local cmp = require('cmp') + +cmp.setup { + mapping = { + [''] = cmp.mapping.select_prev_item(), + [''] = cmp.mapping.select_next_item(), + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.close(), + [''] = cmp.mapping.confirm { + behavior = cmp.ConfirmBehavior.Replace, + select = true, + }, + [''] = function(fallback) + if cmp.visible() then + cmp.select_next_item() + else + fallback() + end + end, + [''] = function(fallback) + if cmp.visible() then + cmp.select_prev_item() + else + fallback() + end + end, + }, + sources = { + { name = 'nvim_lsp' }, + }, +} diff --git a/nvim/lua/options.lua b/nvim/lua/options.lua new file mode 100644 index 0000000..bdc5936 --- /dev/null +++ b/nvim/lua/options.lua @@ -0,0 +1,58 @@ +--fix different locale settings when ssh'ing +vim.o.encoding = 'utf-8' + +vim.o.syntax = 'on' + +--display all matching files when we tab complete +vim.o.wildmenu = true + +--replace vertical split pipe character with space +vim.o.fillchars = 'vert:|,fold: ' + +--Having longer updatetime (default is 4000 ms = 4 s) leads to noticeable +--delays and poor user experience. +vim.o.updatetime = 300 + +vim.o.splitright = true +vim.o.splitbelow = true + +--switch buffers without writing to file +vim.o.hidden = true + +--fold indents +vim.o.foldmethod = 'indent' + +--don't open folds when jumping over one with (, {, [[ or [{ +vim.opt.foldopen = vim.opt.foldopen - { 'block' } + +--search down into subfolders +--provides tab-completion for all file-related tasks +vim.opt.path = vim.opt.path + { '**' } + +--line numbers +vim.o.number = true + +--changes to current directory when creating new files +vim.o.autochdir = true + +--higlhight search +vim.o.hls = true + +--search as characters are entered +vim.o.incsearch = true + +--line for linewrapping +vim.o.colorcolumn = '80' + +--wrap text instead of being on one line +vim.o.lbr = true + +--default Colors for CursorLine +vim.o.cursorline = true + +vim.o.termguicolors = true +vim.o.background = 'dark' + +--enable statusbar +vim.o.laststatus = 2 +vim.o.statusline = ' %F %m%r%w %= %{hostname()} %{strlen(&ft)?&ft:"none"} %{(&bomb?",BOM":"")} %{&ff} %l/%L %c %P'