dotfiles

Clean dotfiles
git clone git://git.dimitrijedobrota.com/dotfiles.git
Log | Files | Refs

commit 4bcd34d6cb5e97a19a8f5c0555768d3a906cae51
parent 12c7d9a891ba310648f5efcb70135946ba887ca6
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Fri, 21 Jul 2023 14:18:47 +0200

Configure VIM language server with clangd

Diffstat:
M.gitignore | 1+
A.xdg/config/vim/clangd.vim | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
M.xdg/config/vim/vimrc | 16++++++++++++++++
3 files changed, 102 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -6,6 +6,7 @@ .ssh .xdg/cache +.xdg/config/vim/bundle .xdg/config/wget-hsts .xdg/data .xdg/state diff --git a/.xdg/config/vim/clangd.vim b/.xdg/config/vim/clangd.vim @@ -0,0 +1,85 @@ +" Set up VIM language server using clangd + +if executable('clangd') + augroup lsp_clangd + autocmd! + autocmd User lsp_setup call lsp#register_server({ + \ 'name': 'clangd', + \ 'cmd': {server_info->['clangd']}, + \ 'whitelist': ['c', 'cpp', 'objc', 'objcpp'], + \ }) + autocmd FileType c setlocal omnifunc=lsp#complete + autocmd FileType cpp setlocal omnifunc=lsp#complete + autocmd FileType objc setlocal omnifunc=lsp#complete + autocmd FileType objcpp setlocal omnifunc=lsp#complete + augroup end +endif + +function! s:on_lsp_buffer_enabled() abort + setlocal omnifunc=lsp#complete + setlocal signcolumn=yes + if exists('+tagfunc') | setlocal tagfunc=lsp#tagfunc | endif + nmap <buffer> gd <plug>(lsp-definition) + nmap <buffer> gs <plug>(lsp-document-symbol-search) + nmap <buffer> gf <plug>(lsp-document-format) + nmap <buffer> gD <plug>(lsp-document-diagnostics) + nmap <buffer> gS <plug>(lsp-workspace-symbol-search) + nmap <buffer> gR <plug>(lsp-references) + nmap <buffer> gi <plug>(lsp-implementation) + nmap <buffer> gt <plug>(lsp-type-definition) + nmap <buffer> <leader>rn <plug>(lsp-rename) + nmap <buffer> [g <plug>(lsp-previous-diagnostic) + nmap <buffer> ]g <plug>(lsp-next-diagnostic) + nmap <buffer> K <plug>(lsp-hover) + nnoremap <buffer> <expr><c-f> lsp#scroll(+4) + nnoremap <buffer> <expr><c-d> lsp#scroll(-4) + + let g:lsp_format_sync_timeout = 1000 + autocmd! BufWritePre *.rs,*.go call execute('LspDocumentFormatSync') + + " refer to doc to add more commands +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 + +" set foldmethod=expr +" \ foldexpr=lsp#ui#vim#folding#foldexpr() +" \ foldtext=lsp#ui#vim#folding#foldtext() + +" Key bindings for vim-lsp. +nn <silent> <M-d> :LspDefinition<cr> +nn <silent> <M-r> :LspReferences<cr> +nn <f2> :LspRename<cr> +nn <silent> <M-a> :LspWorkspaceSymbol<cr> +nn <silent> <M-l> :LspDocumentSymbol<cr> + +highlight lspReference ctermfg=red guifg=red ctermbg=NONE guibg=green + +autocmd User asyncomplete_setup call asyncomplete#register_source( + \ asyncomplete#sources#clang#get_source_options()) + +" Key bindings for asyncomplete +inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>" +inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>" +inoremap <expr> <cr> pumvisible() ? asyncomplete#close_popup() : "\<cr>" + +let g:asyncomplete_auto_popup = 0 + +function! s:check_back_space() abort + let col = col('.') - 1 + return !col || getline('.')[col - 1] =~ '\s' +endfunction + +inoremap <silent><expr> <TAB> + \ pumvisible() ? "\<C-n>" : + \ <SID>check_back_space() ? "\<TAB>" : + \ asyncomplete#force_refresh() +inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>" + +let g:asyncomplete_auto_completeopt = 0 +set completeopt=menuone,noinsert,noselect,preview +autocmd! CompleteDone * if pumvisible() == 0 | pclose | endif diff --git a/.xdg/config/vim/vimrc b/.xdg/config/vim/vimrc @@ -14,7 +14,21 @@ set undodir=$XDG_STATE_HOME/vim/undo | call mkdir(&undodir, 'p') set viewdir=$XDG_STATE_HOME/vim/view | call mkdir(&viewdir, 'p') if !has('nvim') | set viminfofile=$XDG_STATE_HOME/vim/viminfo | endif + set nocompatible +filetype off + +set rtp+=$XDG_CONFIG_HOME/vim/bundle/Vundle.vim +call vundle#begin('$XDG_CONFIG_HOME/vim/bundle') +Plugin 'prabirshrestha/async.vim' +Plugin 'prabirshrestha/vim-lsp' + +Plugin 'prabirshrestha/asyncomplete.vim' +Plugin 'prabirshrestha/asyncomplete-lsp.vim' +Plugin 'keremc/asyncomplete-clang.vim' + +call vundle#end() +filetype plugin indent on set ruler set number @@ -28,3 +42,5 @@ set incsearch set hlsearch set list listchars=tab:\ \ ,trail:. + +source <sfile>:h/clangd.vim