aboutsummaryrefslogtreecommitdiff
path: root/plugin/lsp_setup.vim
blob: 8145708086ee11943a1a69dacf6dab6564bfccf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
  if !has_key(s:settings, &filetype)
    return ''
  endif
  let l:setting = s:settings[&filetype]
  if empty(l:setting)
    return ''
  endif
  let l:found = {}
  for l:conf in l:setting
    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
  let l:command = s:vimlsp_settings_get(l:setting[0].command, 'cmd', l:setting[0].command)
  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 ''
  endif
  return l:command
endfunction

function! s:vimlsp_install_server() abort
  let l:command = s:vimlsp_installer()
  exe 'terminal' l:command
endfunction

function! s:vimlsp_settings_suggest() abort
  if empty(s:vimlsp_installer())
    return
  endif
  echomsg printf("If you want to enable Language Server, please do :LspInstallServer")
  command! -buffer LspInstallServer call s:vimlsp_install_server()
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)
    let l:found = 0
    if empty(s:settings[l:ft])
      continue
    endif
    for l:server in s:settings[l:ft]
      if s:executable(s:vimlsp_settings_get(l:server.command, 'cmd', l:server.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
      exe printf('augroup vimlsp_suggest_%s', l:ft)
        au!
        exe printf('autocmd FileType %s call s:vimlsp_settings_suggest()', l:ft)
      augroup END
    elseif !empty(s:vimlsp_installer())
      command! -buffer LspInstallServer call s:vimlsp_install_server()
    endif
  endfor
endfunction

call s:vimlsp_setting()