From 50c761119a86cd12676e550e93f615e1fce0c3f9 Mon Sep 17 00:00:00 2001 From: jdhao Date: Tue, 6 Apr 2021 01:32:19 +0800 Subject: [PATCH] Use nvim builtin lsp instead of vim-lsp --- core/plugins.vim | 273 +++++++++++++++++++++-------------------------- 1 file changed, 122 insertions(+), 151 deletions(-) diff --git a/core/plugins.vim b/core/plugins.vim index 360ba02..22e9f7f 100644 --- a/core/plugins.vim +++ b/core/plugins.vim @@ -19,17 +19,8 @@ endif "{{ Autocompletion related plugins call plug#begin() -" Auto-completion -Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' } -" Language Server Protocol client -Plug 'prabirshrestha/vim-lsp' -Plug 'lighttiger2505/deoplete-vim-lsp' - -" Vim source for deoplete -if !executable('vim-language-server') - " only use neco-vim when vim-language-server is not available - Plug 'Shougo/neco-vim', { 'for': 'vim' } -endif +Plug 'neovim/nvim-lspconfig' +Plug 'hrsh7th/nvim-compe' "}} "{{ language-specific plugins @@ -218,10 +209,6 @@ if g:is_win || g:is_mac Plug 'iamcco/markdown-preview.nvim', { 'do': { -> mkdp#util#install() }, 'for': ['markdown', 'vim-plug'] } endif -" emoji -" Plug 'https://gitlab.com/gi1242/vim-emoji-ab' -Plug 'fszymanski/deoplete-emoji', {'for': 'markdown'} - if g:is_mac Plug 'rhysd/vim-grammarous' endif @@ -324,146 +311,138 @@ call utils#Cabbrev('pc', 'PlugClean') "}} "{{ Auto-completion related -"""""""""""""""""""""""""""" deoplete settings"""""""""""""""""""""""""" -" Wheter to enable deoplete automatically after start nvim -let g:deoplete#enable_at_startup = 1 +lua << EOF +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 -" Maximum candidate window width -call deoplete#custom#option('max_menu_width', 80) + buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc') -" Minimum character length needed to activate auto-completion. -call deoplete#custom#option('min_pattern_length', 1) + -- Mappings. + local opts = { noremap=true, silent=true } + 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) + buf_set_keymap('n', 'wa', 'lua vim.lsp.buf.add_workspace_folder()', opts) + buf_set_keymap('n', 'wr', 'lua vim.lsp.buf.remove_workspace_folder()', opts) + buf_set_keymap('n', 'wl', 'lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))', opts) + buf_set_keymap('n', 'D', 'lua vim.lsp.buf.type_definition()', opts) + buf_set_keymap('n', 'rn', 'lua vim.lsp.buf.rename()', opts) + buf_set_keymap('n', 'gr', 'lua vim.lsp.buf.references()', opts) + buf_set_keymap('n', 'e', 'lua vim.lsp.diagnostic.show_line_diagnostics()', opts) + buf_set_keymap('n', '[d', 'lua vim.lsp.diagnostic.goto_prev()', opts) + buf_set_keymap('n', ']d', 'lua vim.lsp.diagnostic.goto_next()', opts) + buf_set_keymap('n', 'q', 'lua vim.lsp.diagnostic.set_loclist()', opts) -" use fuzzy matcher -call deoplete#custom#source('_', 'matchers', ['matcher_full_fuzzy']) + -- Show diagnostic automatically when cursor is on hold. + vim.api.nvim_command('autocmd CursorHold lua vim.lsp.diagnostic.show_line_diagnostics()') -" Whether to disable completion for certain syntax -" call deoplete#custom#source('_', { -" \ 'filetype': ['vim'], -" \ 'disabled_syntaxes': ['String'] -" \ }) -call deoplete#custom#source('_', { - \ 'filetype': ['python'], - \ 'disabled_syntaxes': ['Comment'] - \ }) + -- Set some keybinds conditional on server capabilities + if client.resolved_capabilities.document_formatting then + buf_set_keymap("n", "f", "lua vim.lsp.buf.formatting()", opts) + elseif client.resolved_capabilities.document_range_formatting then + buf_set_keymap("n", "f", "lua vim.lsp.buf.range_formatting()", opts) + end -" Ignore certain sources, because they only cause nosie most of the time -call deoplete#custom#option('ignore_sources', { - \ '_': ['around', 'buffer'] - \ }) + -- Set autocommands conditional on server_capabilities + if client.resolved_capabilities.document_highlight then + vim.api.nvim_exec([[ + hi LspReferenceRead cterm=bold ctermbg=red guibg=LightYellow + hi LspReferenceText cterm=bold ctermbg=red guibg=LightYellow + hi LspReferenceWrite cterm=bold ctermbg=red guibg=LightYellow + augroup lsp_document_highlight + autocmd! * + autocmd CursorHold lua vim.lsp.buf.document_highlight() + autocmd CursorMoved lua vim.lsp.buf.clear_references() + augroup END + ]], false) + end +end -" Candidate list item number limit -call deoplete#custom#option('max_list', 15) +local lspconfig = require("lspconfig") +lspconfig.pyls.setup { + on_attach = on_attach, + settings = { + pyls = { + plugins = { + flake8 = {enabled = false}, + pylint = {enabled = true, executable = "pylint"}, + pyflakes = {enabled = false}, + pycodestyle = {enabled = false}, + jedi_completion = {fuzzy = true}, + pyls_isort = {enabled = true}, + pyls_mypy = {enabled = true} + } + } + } +} -" The number of processes used for the deoplete parallel feature. -call deoplete#custom#option('num_processes', 8) +-- set up ccls, see https://github.com/neovim/nvim-lspconfig/blob/master/CONFIG.md#ccls +lspconfig.ccls.setup { + on_attach = on_attach, + root_dir = lspconfig.util.root_pattern("compile_commands.json", ".ccls", ".git"), + init_options = { + highlight = { lsRanges = true } + } +} -" The delay for completion after input, measured in milliseconds. -call deoplete#custom#option('auto_complete_delay', 100) +-- set up vim-language-server +lspconfig.vimls.setup{} -" lower case can also match upper case (upper case are hard to type!) -call deoplete#custom#option({'camel_case': v:true,}) +-- Change diagnostic signs. +vim.fn.sign_define('LspDiagnosticsSignError', { text = "✗", texthl = "LspDiagnosticsDefaultError" }) +vim.fn.sign_define('LspDiagnosticsSignWarning', { text = "!", texthl = "LspDiagnosticsDefaultWarning" }) +vim.fn.sign_define('LspDiagnosticsSignInformation', { text = "", texthl = "LspDiagnosticsDefaultInformation" }) +vim.fn.sign_define('LspDiagnosticsSignHint', { text = "", texthl = "LspDiagnosticsDefaultHint" }) +EOF -"""""""""""""""""""""""""""" vim-lsp settings"""""""""""""""""""""""""" -" whether to enable diagnostics for vim-lsp (we may want to use ALE for other -" plugins for that. -let g:lsp_diagnostics_enabled = 1 +" nvim-compe settings +lua << EOF +require'compe'.setup { + enabled = true; + autocomplete = true; + debug = false; + min_length = 1; + preselect = 'enable'; + throttle_time = 80; + source_timeout = 200; + incomplete_delay = 400; + max_abbr_width = 100; + max_kind_width = 100; + max_menu_width = 100; + documentation = true; -" show diagnostic signs -let g:lsp_diagnostics_signs_enabled = 1 -let g:lsp_diagnostics_signs_error = {'text': '✗'} -let g:lsp_diagnostics_signs_warning = {'text': '!'} -let g:lsp_diagnostics_signs_information = {'text': '!'} -let g:lsp_diagnostics_highlights_enabled = 0 + source = { + path = true; + buffer = true; + calc = true; + nvim_lsp = true; + nvim_lua = true; + vsnip = false; + ultisnips = true; + }; +} -" Do not use virtual text, they are far too obstrusive. -let g:lsp_diagnostics_virtual_text_enabled = 0 -" whether to echo a diagnostic message on statusline at cursor position -let g:lsp_diagnostics_echo_cursor = 1 -" Whether to show diagnostic in floating window -let g:lsp_diagnostics_float_cursor = 0 -let g:lsp_diagnostics_float_delay = 100 +vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with( + vim.lsp.diagnostic.on_publish_diagnostics, { + underline = false, + virtual_text = false, + signs = true, + update_in_insert = false, + } +) -let g:lsp_preview_max_width = 80 -let g:lsp_work_done_progress_enabled = 1 +vim.o.completeopt = "menuone,noselect" +EOF -" set up pyls for vim-lsp -if executable('pyls') - " pip install python-language-server - augroup pyls_setup - autocmd! - autocmd User lsp_setup call lsp#register_server({ - \ 'name': 'pyls', - \ 'cmd': {server_info->['pyls']}, - \ 'allowlist': ['python'], - \ 'workspace_config': { - \ 'pyls': - \ { - \ 'plugins': {'flake8': {'enabled': v:false}, - \ 'pylint': {'enabled': v:true, 'executable': 'pylint'}, - \ 'pyflakes': {'enabled': v:false}, - \ 'pycodestyle': {'enabled': v:false}, - \ 'jedi_completion': {'fuzzy': v:true}, - \ 'pyls_isort': {'enabled': v:true}, - \ 'pyls_mypy': {'enabled': v:true} - \ } - \ } - \ }}) - augroup END -endif - -if executable('ccls') - augroup ccls_setup - autocmd! - au User lsp_setup call lsp#register_server({ - \ 'name': 'ccls', - \ 'cmd': {server_info->['ccls']}, - \ 'root_uri': {server_info->lsp#utils#path_to_uri( - \ lsp#utils#find_nearest_parent_file_directory( - \ lsp#utils#get_buffer_path(), ['.ccls', 'compile_commands.json', '.git/']))}, - \ 'initialization_options': { - \ 'highlight': { 'lsRanges' : v:true }, - \ 'cache': {'directory': stdpath('cache') . '/ccls' }, - \ }, - \ 'whitelist': ['c', 'cpp', 'objc', 'objcpp', 'cc'], - \ }) - augroup END -endif - -if executable('vim-language-server') - augroup LspVim - autocmd! - autocmd User lsp_setup call lsp#register_server({ - \ 'name': 'vim-language-server', - \ 'cmd': {server_info->['vim-language-server', '--stdio']}, - \ 'whitelist': ['vim'], - \ 'initialization_options': { - \ 'vimruntime': $VIMRUNTIME, - \ 'runtimepath': &rtp, - \ }, - \ 'suggest': { - \ 'fromRuntimepath': v:true - \ }}) - augroup END -endif - -function! s:on_lsp_buffer_enabled() abort - if exists('+tagfunc') | setlocal tagfunc=lsp#tagfunc | endif - nmap gd (lsp-definition) - nmap gr (lsp-references) - nmap gi (lsp-implementation) - nmap gt (lsp-type-definition) - nmap rn (lsp-rename) - nmap [g (lsp-previous-diagnostic) - nmap ]g (lsp-next-diagnostic) - nmap K (lsp-hover) -endfunction - -augroup lsp_install - au! - " call s:on_lsp_buffer_enabled only for languages that has the server registered. - autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled() -augroup END +" nvim-comple mappings +inoremap compe#complete() +inoremap compe#confirm('') +inoremap compe#close('') +inoremap compe#scroll({ 'delta': +4 }) +inoremap compe#scroll({ 'delta': -4 }) """""""""""""""""""""""""UltiSnips settings""""""""""""""""""" " Trigger configuration. Do not use if you use YouCompleteMe @@ -773,9 +752,6 @@ endif """"""""""""""""""""""""unicode.vim settings"""""""""""""""""""""""""""""" nmap ga (UnicodeGA) - -""""""""""""""""""""""""deoplete-emoji settings"""""""""""""""""""""""""""" -call deoplete#custom#source('emoji', 'converters', ['converter_emoji']) "}} "{{ text objects @@ -804,11 +780,6 @@ if ( g:is_win || g:is_mac ) && executable('latex') autocmd FileType tex call SetServerName() augroup END - " Deoplete configurations for autocompletion to work - call deoplete#custom#var('omni', 'input_patterns', { - \ 'tex': g:vimtex#re#deoplete - \ }) - let g:vimtex_compiler_latexmk = { \ 'build_dir' : 'build', \ } @@ -889,8 +860,8 @@ let g:airline#extensions#gutentags#enabled = 1 " Do not show search index in statusline since it is shown on command line let g:airline#extensions#anzu#enabled = 0 -" Enable vim-airline extension for vim-lsp -let g:airline#extensions#lsp#enabled = 1 +" Enable vim-airline extension for nvim-lsp +let g:airline#extensions#nvimlsp#enabled = 1 " Skip empty sections if there are nothing to show, " extracted from https://vi.stackexchange.com/a/9637/15292