From 82fb9c7ee8067529c46fccd2d6be5389b4440837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 20 Apr 2022 20:58:25 +0200 Subject: [PATCH] LSP-NVIM - init - 0.0.1 --- .config/nvim/init.vim | 145 ++++++++++++++++++++++++++------------ .config/nvim/installer.sh | 6 ++ 2 files changed, 107 insertions(+), 44 deletions(-) create mode 100755 .config/nvim/installer.sh diff --git a/.config/nvim/init.vim b/.config/nvim/init.vim index 6a37885..4b71c94 100644 --- a/.config/nvim/init.vim +++ b/.config/nvim/init.vim @@ -103,9 +103,6 @@ call plug#begin('~/.config/nvim/plugged') " CSS Plug 'ap/vim-css-color' - " coc - Plug 'neoclide/coc.nvim', {'branch': 'release'} - " Copilot Plug 'github/copilot.vim' @@ -137,12 +134,109 @@ call plug#begin('~/.config/nvim/plugged') " Debug Plug 'puremourning/vimspector' Plug 'mfussenegger/nvim-dap' + + " LSP + Plug 'neovim/nvim-lspconfig' + Plug 'neovim/nvim-lspconfig' + Plug 'hrsh7th/nvim-cmp' + Plug 'hrsh7th/cmp-nvim-lsp' + Plug 'saadparwaiz1/cmp_luasnip' + Plug 'L3MON4D3/LuaSnip' + Plug 'sbdchd/neoformat' + call plug#end() -" Coc -let g:coc_global_extensions = ['coc-java', '@yaegassy/coc-ansible', 'coc-solargraph', 'coc-go', 'coc-yaml', 'coc-pyright', 'coc-json' , 'coc-markdownlint' , 'coc-sh', 'coc-prettier', 'coc-diagnostic', 'coc-perl'] +" LUA +lua< coc#refresh() +local capabilities = vim.lsp.protocol.make_client_capabilities() +capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities) +local lspconfig = require('lspconfig') + +local on_attach = function(client, bufnr) + vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', 'lua vim.lsp.buf.declaration()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gd', 'lua vim.lsp.buf.definition()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', 'lua vim.lsp.buf.hover()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gi', 'lua vim.lsp.buf.implementation()', opts) + --vim.api.nvim_buf_set_keymap(bufnr, 'n', 'wa', 'lua vim.lsp.buf.add_workspace_folder()', opts) + --vim.api.nvim_buf_set_keymap(bufnr, 'n', 'wr', 'lua vim.lsp.buf.remove_workspace_folder()', opts) + --vim.api.nvim_buf_set_keymap(bufnr, 'n', 'wl', 'lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))', opts) + --vim.api.nvim_buf_set_keymap(bufnr, 'n', 'D', 'lua vim.lsp.buf.type_definition()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'rn', 'lua vim.lsp.buf.rename()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'ca', 'lua vim.lsp.buf.code_action()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gr', 'lua vim.lsp.buf.references()', opts) + --vim.api.nvim_buf_set_keymap(bufnr, 'n', 'f', 'lua vim.lsp.buf.formatting()', opts) +end + +-- Use a loop to conveniently call 'setup' on multiple servers and +-- map buffer local keybindings when the language server attaches +local servers = { 'pyright', 'bashls' } +for _, lsp in pairs(servers) do + require('lspconfig')[lsp].setup { + on_attach = on_attach, + capabilities = capabilities, + flags = { + debounce_text_changes = 150, + } + } +end + +-- luasnip setup +local luasnip = require 'luasnip' + +-- nvim-cmp setup +local cmp = require 'cmp' +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' }, + }, +} + +EOF + +" Neoformat +let g:neoformat_try_formatprg = 1 +let g:neoformat_basic_format_trim = 1 +let g:neoformat_only_msg_on_error = 1 +autocmd BufWritePre * undojoin | Neoformat + +" Ebuild +let g:shfmt_opt="-ci" function! s:check_back_space() abort let col = col('.') - 1 @@ -195,19 +289,6 @@ function! StatuslineGit() return strlen(l:branchname) > 0?' '.l:branchname.' ':'' endfunction -function! StatusDiagnostic() abort - let info = get(b:, 'coc_diagnostic_info', {}) - if empty(info) | return '' | endif - let msgs = [] - if get(info, 'error', 0) - call add(msgs, 'E' . info['error']) - endif - if get(info, 'warning', 0) - call add(msgs, 'W' . info['warning']) - endif - return join(msgs, ' '). ' ' . get(g:, 'coc_status', '') -endfunction - set statusline= set statusline+=%#IncSearch# set statusline+=%{&filetype!=#''?'\ \ ['.&filetype.']\ ':'\ '} @@ -215,8 +296,6 @@ set statusline+=%{&modified?'[+]\ ':''} set statusline+=%#CursorLineNr# set statusline+=\ %F set statusline+=%= "Right side settings -set statusline+=%#warningmsg# -set statusline+=%{StatusDiagnostic()!=#''?'\ '.StatusDiagnostic():''} set statusline+=%#CursorLineNr# set statusline+=%{StatuslineGit()} set statusline+=%#Search# @@ -231,20 +310,8 @@ set completeopt-=preview " Keyboard shortcuts """""""""""""""""""""""""""""""" let mapleader = "\" -nmap a :CocAction -nmap d :CocDiagnostics nmap 2 :w! -" Go to definition -nmap gd (coc-definition) -nmap gy (coc-type-definition) -nmap gi (coc-implementation) -nmap gr (coc-references) -nmap rr (coc-rename) -nmap gp (coc-diagnostic-prev) -nmap gn (coc-diagnostic-next) -nnoremap cr :CocRestart - " Adding commentary xmap c Commentary nmap c Commentary @@ -281,13 +348,7 @@ nnoremap :edit! inoremap pumvisible() ? "\" : "\" -" Tab in the coc to help select right autocomplete -inoremap - \ pumvisible() ? "\" : - \ check_back_space() ? "\" : - \ coc#refresh() - -"" Moving line up or down using alt +" Moving line up or down using alt nnoremap :m-2 nnoremap :m+ inoremap :m-2 @@ -356,9 +417,6 @@ map :setlocal spell! spelllang=pl " Ansible au BufRead,BufNewFile *.yml set filetype=yaml.ansible autocmd BufWritePre *.yml :Prettier -let g:coc_filetype_map = { - \ 'yaml.ansible': 'ansible', - \ } " Bash autocmd FileType sh @@ -389,7 +447,6 @@ autocmd BufRead,BufNewFile *.yaml let g:indentLine_char = '⦙' " Go autocmd BufRead *.go set noexpandtab -autocmd BufWritePre *.go :silent call CocAction('runCommand', 'editor.action.organizeImport') " Conf au BufNewFile,BufRead *.conf setfiletype conf diff --git a/.config/nvim/installer.sh b/.config/nvim/installer.sh new file mode 100755 index 0000000..b933576 --- /dev/null +++ b/.config/nvim/installer.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +npm -g i pyright bash-language-server +GO111MODULE=on go install mvdan.cc/sh/v3/cmd/shfmt@latest +pip install black +pip3 install black