aboutsummaryrefslogtreecommitdiff
path: root/plugin/lsp_settings.vim
blob: 72a8729734d112c6170bfaeec905a3199c546867 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
if exists('g:loaded_lsp_settings') || !exists('*json_encode') || !has('lambda')
  finish
endif
let g:loaded_lsp_settings= 1

let g:lsp_settings_root_markers = get(g:, 'lsp_settings_root_markers', [
      \ '.git/',
      \ '.svn/',
      \ '.hg/',
      \ '.bzr/'
      \ ])

let s:settings_dir = expand('<sfile>:h:h').'/settings'
let s:checkers_dir = expand('<sfile>:h:h').'/checkers'
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"))
let s:ftmap = {}

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:servers_dir = get(g:, 'lsp_settings_servers_dir', s:servers_dir)
  let l:paths .= ',' . l:servers_dir . '/' . a:cmd
  if !has('win32')
    let l:found = globpath(l:paths, a:cmd)
    return !empty(l:found)
  endif
  for l:ext in ['.exe', '.cmd', '.bat']
    if !empty(globpath(l:paths, a:cmd . l:ext))
      return 1
    endif
  endfor
  return 0
endfunction

function! s:vimlsp_installer() abort
  let l:ft = tolower(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

" neovim passes third argument as 'exit' while vim passes only 2 arguments
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()
  if empty(l:entry)
    return
  endif
  let l:servers_dir = get(g:, 'lsp_settings_servers_dir', s:servers_dir)
  let l:server_install_dir = l:servers_dir . '/' . l:entry[0]
  if isdirectory(l:server_install_dir)
    call delete(l:server_install_dir, 'rf')
  endif
  call mkdir(l:server_install_dir, 'p')
  if has('nvim')
    split new
    call termopen(l:entry[1], {'cwd': l:server_install_dir, 'on_exit': function('s:vimlsp_install_server_post', [l:entry[0]])}) | startinsert
  else
    let l:bufnr = term_start(l:entry[1], {'cwd': l:server_install_dir})
    let l:job = term_getjob(l:bufnr)
    if l:job != v:null
      call job_setoptions(l:job, {'exit_cb': function('s:vimlsp_install_server_post', [l:entry[0]])})
    endif
  endif
endfunction

function! s:vimlsp_settings_suggest() abort
  if empty(s:vimlsp_installer())
    return
  endif
  if exists(':LspInstallServer') !=# 2
    redraw
    echohl Directory
    echomsg 'Please do :LspInstallServer to enable Language Server'
    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
  augroup vimlsp_suggest
    au!
    autocmd BufNewFile,BufRead * call s:vimlsp_suggest_plugin()
  augroup END
endfunction

function! s:vimlsp_suggest_plugin() abort
  if &ft != ''
    return
  endif
  let l:ext = expand('%:e')
  for l:ft in keys(s:settings)
    for l:server in s:settings[l:ft]
      if !has_key(l:server, 'vim-plugin')
        continue
      endif
      if index(l:server['vim-plugin']['extensions'], l:ext) == -1
        continue
      endif
      redraw
      echohl Directory
      echomsg printf('Please install vim-plugin "%s" to enable Language Server', l:server['vim-plugin']['name'])
      echohl None
      return
    endfor
  endfor
endfunction

function! s:vimlsp_load_or_suggest(ft) abort
  if get(s:ftmap, a:ft, 0)
    return
  endif
  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]
    if s:vimlsp_settings_get(l:server.command, 'disabled', 0)
      continue
    endif
    let l:default = get(g:, 'lsp_settings_' . a:ft, '')
    if !empty(l:default) && l:default != l:server.command
      continue
    endif
    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:checkers_dir, l:server.command)
      if !filereadable(l:script) || has_key(l:server, 'fallback')
        continue
      endif
      let l:server['fallback'] = ''
      try
        exe 'source' l:script
        let l:command = LspCheckCommand()
        let l:server['fallback'] = l:command
      catch
      finally
        if exists('*LspCheckCommand')
          delfunction LspCheckCommand
        endif
        if empty(l:server['fallback'])
          continue
        endif
      endtry
    endif
    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
      let s:ftmap[a:ft] = 1
      break
    endif
  endfor

  if l:found ==# 0
    call s:vimlsp_settings_suggest()
  else
    doautocmd User lsp_setup
    if exists(':LspInstallServer') !=# 2
      command! -buffer LspInstallServer call s:vimlsp_install_server()
    endif
  endif

  if exists(':LspRegisterServer') !=# 2
    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()