1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-03-15 19:45:46 +08:00

221 lines
6.9 KiB
VimL
Raw Normal View History

2017-12-23 22:02:15 +08:00
"=============================================================================
" lsp.vim --- language server protocol wallpaper
2021-09-19 22:23:23 +08:00
" Copyright (c) 2016-2021 Wang Shidong & Contributors
2017-12-23 22:02:15 +08:00
" Author: Seong Yong-ju < @sei40kr >
" URL: https://spacevim.org
2018-02-15 22:25:03 +08:00
" License: GPLv3
2017-12-23 22:02:15 +08:00
"=============================================================================
scriptencoding utf-8
2021-10-05 15:13:10 +08:00
if exists('s:NVIM_VERSION')
finish
endif
let s:NVIM_VERSION = SpaceVim#api#import('neovim#version')
2021-10-05 15:13:10 +08:00
if (has('nvim-0.5.0') && s:NVIM_VERSION.is_release_version()) || has('nvim-0.6.0')
" use neovim built-in lsp
function! SpaceVim#lsp#reg_server(ft, cmds) abort
lua require("spacevim.lsp").register(
\ require("spacevim").eval("a:ft"),
\ require("spacevim").eval("a:cmds")
\ )
endfunction
function! SpaceVim#lsp#show_doc() abort
lua vim.lsp.buf.hover()
2021-10-05 15:13:10 +08:00
endfunction
function! SpaceVim#lsp#go_to_def() abort
lua vim.lsp.buf.definition()
endfunction
function! SpaceVim#lsp#go_to_declaration() abort
lua vim.lsp.buf.declaration()
endfunction
function! SpaceVim#lsp#rename() abort
" @todo add float prompt api
" lua vim.lsp.buf.rename(require('spacevim.api.input').float_prompt())
lua vim.lsp.buf.rename()
endfunction
function! SpaceVim#lsp#references() abort
lua vim.lsp.buf.references()
2021-10-05 15:13:10 +08:00
endfunction
function! SpaceVim#lsp#go_to_typedef() abort
endfunction
function! SpaceVim#lsp#refactor() abort
endfunction
function! SpaceVim#lsp#go_to_impl() abort
lua vim.lsp.buf.implementation()
endfunction
function! SpaceVim#lsp#show_line_diagnostics() abort
lua vim.lsp.diagnostic.show_line_diagnostics()
endfunction
function! SpaceVim#lsp#list_workspace_folder() abort
lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
endfunction
function! SpaceVim#lsp#add_workspace_folder() abort
lua vim.lsp.buf.add_workspace_folder()
endfunction
function! SpaceVim#lsp#remove_workspace_folder() abort
lua vim.lsp.buf.remove_workspace_folder()
endfunction
2021-10-05 15:13:10 +08:00
elseif SpaceVim#layers#isLoaded('autocomplete') && get(g:, 'spacevim_autocomplete_method') ==# 'coc'
" use coc.nvim
let s:coc_language_servers = {}
let s:coc_language_servers_key_id_map = {}
function! SpaceVim#lsp#reg_server(ft, cmds) abort
" coc.nvim doesn't support key values containing dots
" See https://github.com/neoclide/coc.nvim/issues/323
" Since a:cmds[0], i.e. the language server command can be a full path,
" which can potentially contain dots, we just take it's last part, if any
" dots are present.
"
" Clearly, with this implementation, an edge case could be the following
"
" [layers.override_cmd]
" c = ['/home/user/.local/.bin/ccls', '--log-file=/tmp/ccls.log']
" cpp = ['/home/user/local/.bin/ccls', '--log-file=/tmp/ccls.log']
"
" the last part `bin/ccls` is the same, whereas the commands are not
" actually the same.
" We need to keep an id to distinguish among conflicting keys.
2019-01-28 22:12:37 +08:00
if stridx(a:cmds[0], '.') >= 0
let l:key = split(a:cmds[0], "\\.")[-1]
else
let l:key = a:cmds[0]
endif
for id in range(get(s:coc_language_servers_key_id_map, l:key, 0))
if has_key(s:coc_language_servers, l:key . id) && s:coc_language_servers[l:key . id].command ==# a:cmds[0]
call add(s:coc_language_servers[l:key . id].filetypes, a:ft)
return
endif
endfor
let s:coc_language_servers_key_id_map[l:key] = get(s:coc_language_servers_key_id_map, l:key, 0)
let s:coc_language_servers[l:key . s:coc_language_servers_key_id_map[l:key]] = {
\'command': a:cmds[0],
\'args': a:cmds[1:],
\'filetypes': [a:ft]
\}
let s:coc_language_servers_key_id_map[l:key] = s:coc_language_servers_key_id_map[l:key] + 1
2019-01-28 22:12:37 +08:00
augroup spacevim_lsp_layer
autocmd!
autocmd! User CocNvimInit :call coc#config("languageserver", s:coc_language_servers)
augroup END
endfunction
function! SpaceVim#lsp#show_doc() abort
2019-01-28 22:12:37 +08:00
call CocActionAsync('doHover')
endfunction
function! SpaceVim#lsp#go_to_def() abort
call CocAction('jumpDefinition')
endfunction
function! SpaceVim#lsp#go_to_declaration() abort
call CocAction('jumpDeclaration')
endfunction
function! SpaceVim#lsp#go_to_typedef() abort
call CocAction('jumpTypeDefinition')
endfunction
function! SpaceVim#lsp#go_to_impl() abort
call CocAction('jumpImplementation')
endfunction
function! SpaceVim#lsp#refactor() abort
call CocActionAsync('refactor')
endfunction
function! SpaceVim#lsp#rename() abort
2019-01-28 22:12:37 +08:00
call CocActionAsync('rename')
endfunction
function! SpaceVim#lsp#references() abort
call CocAction('jumpReferences')
endfunction
2021-10-05 15:13:10 +08:00
elseif has('nvim-0.4.3')
2017-12-23 22:02:15 +08:00
function! SpaceVim#lsp#show_doc() abort
2021-10-05 15:13:10 +08:00
lua require('lsp.plugin')
\ .client.request('textDocument/hover',
\ {}, require('spacevim.lsp').hover_callback)
2017-12-23 22:02:15 +08:00
endfunction
function! SpaceVim#lsp#go_to_def() abort
2021-10-05 15:13:10 +08:00
lua require('lsp.plugin')
\ .client.request('textDocument/hover',
\ {}, require('spacevim.lsp').hover_callback)
2017-12-23 22:02:15 +08:00
endfunction
function! SpaceVim#lsp#go_to_typedef() abort
call LanguageClient_textDocument_typeDefinition()
endfunction
function! SpaceVim#lsp#go_to_impl() abort
call LanguageClient_textDocument_implementation()
endfunction
2017-12-23 22:02:15 +08:00
function! SpaceVim#lsp#rename() abort
call LanguageClient_textDocument_rename()
endfunction
function! SpaceVim#lsp#references() abort
call LanguageClient_textDocument_references()
endfunction
function! SpaceVim#lsp#go_to_declaration() abort
call LanguageClient_textDocument_declaration()
endfunction
2021-10-05 15:13:10 +08:00
function! SpaceVim#lsp#documentSymbol() abort
call LanguageClient_textDocument_documentSymbol()
endfunction
function! SpaceVim#lsp#refactor() abort
" @todo languageclient do not support refactor
endfunction
2021-10-05 15:13:10 +08:00
elseif has('nvim')
2017-12-23 22:02:15 +08:00
else
" use vim-lsp
function! SpaceVim#lsp#reg_server(ft, cmds) abort
2019-01-28 22:12:37 +08:00
exe 'au User lsp_setup call lsp#register_server({'
\ . "'name': '" . a:ft . "-lsp',"
2019-01-28 22:12:37 +08:00
\ . "'cmd': {server_info -> " . string(a:cmds) . '},'
2017-12-23 22:37:52 +08:00
\ . "'whitelist': ['" . a:ft . "' ],"
2019-01-28 22:12:37 +08:00
\ . '})'
exe 'autocmd FileType ' . a:ft . ' setlocal omnifunc=lsp#complete'
2017-12-23 22:02:15 +08:00
endfunction
2017-12-23 22:02:15 +08:00
function! SpaceVim#lsp#show_doc() abort
LspHover
endfunction
function! SpaceVim#lsp#go_to_def() abort
LspDefinition
endfunction
2021-10-05 15:13:10 +08:00
function! SpaceVim#lsp#go_to_declaration() abort
LspDeclaration
endfunction
2017-12-23 22:02:15 +08:00
function! SpaceVim#lsp#rename() abort
LspRename
endfunction
function! SpaceVim#lsp#references() abort
LspReferences
endfunction
2021-10-05 15:13:10 +08:00
function! SpaceVim#lsp#go_to_typedef() abort
LspPeekTypeDefinition
endfunction
function! SpaceVim#lsp#refactor() abort
LspCodeAction refactor
endfunction
function! SpaceVim#lsp#go_to_impl() abort
LspImplementation
endfunction
2017-12-23 22:02:15 +08:00
endif
" vi: et sw=2 cc=80