Files
vim-lsp-settings-mirror/plugin/lsp_settings.vim
Yasuhiro Matsumoto 38c4e67140 Check command exists
2019-12-29 00:21:32 +09:00

197 lines
5.2 KiB
VimL

if exists('g:loaded_lsp_settings')
finish
endif
let g:loaded_lsp_settings= 1
let s:settings_dir = expand('<sfile>:h:h').'/settings'
let s:installer_dir = expand('<sfile>:h:h').'/installer'
let s:servers_dir = expand('<sfile>:h:h').'/servers'
let s:settings = json_decode(join(readfile(expand('<sfile>:h:h').'/settings.json'), "\n"))
function! s:executable(cmd) abort
if executable(a:cmd)
return 1
endif
let l:paths = get(g:, 'lsp_settings_extra_paths', '')
if type(l:paths) == type([])
let l:paths = join(l:paths, ',')
endif
let l:paths .= ',' . s:servers_dir . '/' . a:cmd
if !has('win32')
return !empty(globpath(l:paths, a:cmd))
endif
if !empty(globpath(l:paths, a:cmd . '.exe'))
return 1
endif
if !empty(globpath(l:paths, a:cmd . '.cmd'))
return 1
endif
if !empty(globpath(l:paths, a:cmd . '.bat'))
return 1
endif
return 0
endfunction
function! s:vimlsp_installer() abort
let l:ft = split(&filetype, '\.')[0]
if !has_key(s:settings, l:ft)
return []
endif
let l:server = s:settings[l:ft]
if empty(l:server)
return []
endif
let l:found = {}
for l:conf in l:server
let l:missing = 0
for l:require in l:conf.requires
if !s:executable(l:require)
let l:missing = 1
break
endif
endfor
if l:missing ==# 0
let l:found = l:conf
break
endif
endfor
if empty(l:found)
return []
endif
for l:conf in l:server
let l:command = s:vimlsp_settings_get(l:conf.command, 'cmd', l:conf.command)
if type(l:command) == type([])
let l:command = l:command[0]
endif
let l:command = printf('%s/install-%s', s:installer_dir, l:command)
if has('win32')
let l:command = substitute(l:command, '/', '\', 'g') . '.cmd'
else
let l:command = l:command . '.sh'
endif
if s:executable(l:command)
return [l:conf.command, l:command]
endif
endfor
return []
endfunction
function! s:vimlsp_install_server_post(command, job, code) abort
if a:code != 0
return
endif
if s:executable(a:command)
let l:script = printf('%s/%s.vim', s:settings_dir, a:command)
if filereadable(l:script)
if has('patch-8.1.1113')
command! -nargs=1 LspRegisterServer autocmd User lsp_setup ++once call lsp#register_server(<args>)
else
command! -nargs=1 LspRegisterServer autocmd User lsp_setup call lsp#register_server(<args>)
endif
exe 'source' l:script
delcommand LspRegisterServer
doautocmd User lsp_setup
endif
endif
endfunction
function! s:vimlsp_install_server() abort
let l:entry = s:vimlsp_installer()
exe 'terminal' l:entry[1]
let l:job = term_getjob(bufnr('%'))
if l:job != v:null
call job_setoptions(l:job, {'exit_cb': function('s:vimlsp_install_server_post', [l:entry[0]])})
endif
endfunction
function! s:vimlsp_settings_suggest() abort
if empty(s:vimlsp_installer())
return
endif
if !exists(':LspInstallServer')
redraw
echohl Directory
echomsg 'If you want to enable Language Server, please do :LspInstallServer'
echohl None
command! -buffer LspInstallServer call s:vimlsp_install_server()
endif
endfunction
function! s:vimlsp_settings_get(name, key, default) abort
let l:config = get(g:, 'lsp_settings', {})
if !has_key(l:config, a:name)
if !has_key(l:config, '*')
return a:default
endif
let l:config = l:config['*']
else
let l:config = l:config[a:name]
endif
if !has_key(l:config, a:key)
return a:default
endif
return l:config[a:key]
endfunction
function! s:vimlsp_setting() abort
for l:ft in keys(s:settings)
if has_key(g:, 'lsp_settings_whitelist') && index(g:lsp_settings_whitelist, l:ft) == -1 || empty(s:settings[l:ft])
continue
endif
exe 'augroup' s:load_or_suggest_group_name(l:ft)
au!
exe 'autocmd FileType' l:ft 'call s:vimlsp_load_or_suggest(' string(l:ft) ')'
augroup END
endfor
endfunction
function! s:vimlsp_load_or_suggest(ft) abort
let l:group_name = s:load_or_suggest_group_name(a:ft)
exe 'augroup' l:group_name
au!
augroup END
exe 'augroup!' l:group_name
if has('patch-8.1.1113')
command! -nargs=1 LspRegisterServer autocmd User lsp_setup ++once call lsp#register_server(<args>)
else
command! -nargs=1 LspRegisterServer autocmd User lsp_setup call lsp#register_server(<args>)
endif
let l:found = 0
for l:server in s:settings[a:ft]
let l:command = s:vimlsp_settings_get(l:server.command, 'cmd', l:server.command)
if type(l:command) == type([])
let l:command = l:command[0]
endif
if s:executable(l:command)
let l:script = printf('%s/%s.vim', s:settings_dir, l:server.command)
if filereadable(l:script)
exe 'source' l:script
let l:found += 1
break
endif
endif
endfor
if l:found ==# 0
call s:vimlsp_settings_suggest()
else
doautocmd User lsp_setup
if !exists(':LspInstallServer')
command! -buffer LspInstallServer call s:vimlsp_install_server()
endif
endif
if !exists(':LspRegisterServer')
delcommand LspRegisterServer
endif
endfunction
function! s:load_or_suggest_group_name(ft) abort
return printf('vimlsp_suggest_%s', a:ft)
endfunction
call s:vimlsp_setting()