aboutsummaryrefslogtreecommitdiff
path: root/autoload/lsp_settings.vim
blob: bbb0fc9db49b65bb99f144eec67e78974d977e83 (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
let s:settings_dir = expand('<sfile>:h:h') . '/settings'
let s:checkers_dir = expand('<sfile>:h:h') . '/checkers'
let s:servers_dir = expand('<sfile>:h:h') . '/servers'
let s:installer_dir = expand('<sfile>:h:h') . '/installer'
let s:root_dir = expand('<sfile>:h:h')

let s:settings = json_decode(join(readfile(expand('<sfile>:h:h') . '/settings.json'), "\n"))
call remove(s:settings, '$schema')

let s:ftmap = {}

function! lsp_settings#installer_dir() abort
  return s:installer_dir
endfunction

function! lsp_settings#servers_dir() abort
  let l:path = fnamemodify(get(g:, 'lsp_settings_servers_dir', s:servers_dir), ':p')
  if has('win32')
     let l:path = substitute(l:path, '/', '\', 'g')
  endif
  return substitute(l:path, '[\/]$', '', '')
endfunction

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

function! s:vim_lsp_installer(ft, ...) abort
  let l:ft = tolower(get(split(a:ft, '\.'), 0, ''))
  let l:ft = empty(l:ft) ? '_' : l:ft
  if !has_key(s:settings, l:ft)
    return []
  endif
  let l:server = s:settings[l:ft]
  if empty(l:server)
    return []
  endif
  if l:ft !=# '_'
    let l:server += s:settings['_']
  endif
  let l:found = {}
  for l:conf in l:server
    let l:missing = 0
    for l:require in l:conf.requires
      if !lsp_settings#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:name = get(a:000, 0, '')
  for l:conf in l:server
    if !empty(l:name) && l:conf.command != l:name
      continue
    endif
    let l:command = printf('%s/install-%s', s:installer_dir, l:conf.command)
    if has('win32')
      let l:command = substitute(l:command, '/', '\', 'g') . '.cmd'
    else
      let l:command = l:command . '.sh'
    endif
    if lsp_settings#executable(l:command)
      return [l:conf.command, l:command]
    endif
  endfor
  return []
endfunction

function! lsp_settings#server_config(name) abort
  for l:ft in sort(keys(s:settings))
    for l:conf in s:settings[l:ft]
      if l:conf.command ==# a:name && has_key(l:conf, 'config')
        return l:conf['config']
      endif
    endfor
  endfor
  return {}
endfunction

function! lsp_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, '*')
      if type(a:default) ==# v:t_func
        return a:default(a:name, a:key)
      endif
      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)
    if type(a:default) ==# v:t_func
      return a:default(a:name, a:key)
    endif
    return a:default
  endif
  if type(l:config[a:key]) == v:t_func
    return l:config[a:key](a:name, a:key)
  endif
  return l:config[a:key]
endfunction

function! lsp_settings#exec_path(cmd) abort
  let l:paths = []
  if has('win32')
    for l:path in split($PATH, ';')
      if l:path[len(l:path) - 1:] == "\\"
        call add(l:paths, l:path[:len(l:path)-2])
      elseif !empty(l:path)
        call add(l:paths, l:path)
      endif
    endfor
  else
    let l:paths = split($PATH, ':')
  endif
  let l:paths = join(l:paths, ',')
  let l:path = globpath(l:paths, a:cmd, 1)
  if !has('win32')
    if !empty(l:path)
      return lsp_settings#utils#first_one(l:path)
    endif
  else
    for l:ext in ['.exe', '.cmd', '.bat']
      let l:path = globpath(l:paths, a:cmd . l:ext, 1)
      if !empty(l:path)
        return lsp_settings#utils#first_one(l:path)
      endif
    endfor
  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 .= lsp_settings#servers_dir() . '/' . a:cmd
  if !has('win32')
    return lsp_settings#utils#first_one(globpath(l:paths, a:cmd, 1))
  endif
  for l:ext in ['.exe', '.cmd', '.bat']
    let l:path = globpath(l:paths, a:cmd . l:ext, 1)
    if !empty(l:path)
      return lsp_settings#utils#first_one(l:path)
    endif
  endfor
  return ''
endfunction

function! lsp_settings#root_path(name) abort
  let l:patterns = get(a:000, 0, [])
  return lsp#utils#find_nearest_parent_file_directory(lsp#utils#get_buffer_path(), extend(l:patterns, g:lsp_settings_root_markers))
endfunction

function! lsp_settings#root_uri(name) abort
  let l:patterns = lsp_settings#get(a:name, 'root_uri_patterns', [])
  if empty(l:patterns)
    for l:ft in sort(keys(s:settings))
      for l:conf in s:settings[l:ft]
        if l:conf.command ==# a:name && has_key(l:conf, 'root_uri_patterns')
          let l:patterns = l:conf['root_uri_patterns']
          break
        endif
      endfor
    endfor
  endif

  let l:dir = lsp#utils#find_nearest_parent_file_directory(lsp#utils#get_buffer_path(), extend(l:patterns, g:lsp_settings_root_markers))
  if empty(l:dir)
    return lsp#utils#get_default_root_uri()
  endif
  return lsp#utils#path_to_uri(l:dir)
endfunction

function! lsp_settings#autocd(server_info) abort
  if !has_key(a:server_info, 'root_uri')
    return
  endif
  if type(a:server_info['root_uri']) ==# v:t_func
    let l:root_uri = a:server_info['root_uri'](a:server_info)
  else
    let l:root_uri = a:server_info['root_uri']
  endif
  let l:path = lsp#utils#uri_to_path(l:root_uri)
  if isdirectory(l:path)
    exe 'cd' l:path
  endif
endfunction

function! lsp_settings#settings() abort
  return s:settings
endfunction

function! lsp_settings#complete_uninstall(arglead, cmdline, cursorpos) abort
  let l:installers = []
  for l:ft in sort(keys(s:settings))
    for l:conf in s:settings[l:ft]
      if !isdirectory(lsp_settings#servers_dir() . '/' . l:conf.command)
        continue
      endif
      call add(l:installers, l:conf.command)
    endfor
  endfor
  return filter(uniq(l:installers), 'stridx(v:val, a:arglead) == 0')
endfunction

function! lsp_settings#complete_install(arglead, cmdline, cursorpos) abort
  let l:installers = []

  for l:ft in split(&filetype . '.', '\.', 1)
    let l:ft = tolower(empty(l:ft) ? '_' : l:ft)
    if !has_key(s:settings, l:ft)
      continue
    endif
    for l:conf in s:settings[l:ft]
      let l:missing = 0
      for l:require in l:conf.requires
        if !executable(l:require)
          let l:missing = 1
          break
        endif
      endfor
      if l:missing !=# 0
        continue
      endif
      let l:command = printf('%s/install-%s', s:installer_dir, l:conf.command)
      if has('win32')
        let l:command = substitute(l:command, '/', '\', 'g') . '.cmd'
      else
        let l:command = l:command . '.sh'
      endif
      if executable(l:command)
        call add(l:installers, l:conf.command)
      endif
    endfor
  endfor
  return filter(uniq(sort(l:installers)), 'stridx(v:val, a:arglead) == 0')
endfunction

function! s:vim_lsp_uninstall_server(command) abort
  if !lsp_settings#utils#valid_name(a:command)
    call lsp_settings#utils#error('Invalid server name')
    return
  endif
  call lsp_settings#utils#msg('Uninstalling ' . a:command)
  let l:server_install_dir = lsp_settings#servers_dir() . '/' . a:command
  if !isdirectory(l:server_install_dir)
    call lsp_settings#utils#error('Server not found')
    return
  endif
  call delete(l:server_install_dir, 'rf')
  call lsp_settings#utils#msg('Uninstalled ' . a:command)
endfunction

" neovim passes third argument as 'exit' while vim passes only 2 arguments
function! s:vim_lsp_install_server_post(command, job, code, ...) abort
  if a:code != 0
    return
  endif
  if lsp_settings#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
  call lsp_settings#utils#msg('Installed ' . a:command)
endfunction

function! s:vim_lsp_install_server(ft, command, bang) abort
  if !empty(a:command) && !lsp_settings#utils#valid_name(a:command)
    call lsp_settings#utils#error('Invalid server name')
    return
  endif
  let l:entry = s:vim_lsp_installer(a:ft, a:command)
  if empty(l:entry)
    call lsp_settings#utils#error('Server not found')
    return
  endif
  if empty(a:bang) && confirm(printf('Install %s ?', l:entry[0]), "&Yes\n&Cancel") ==# 2
    return
  endif
  let l:server_install_dir = lsp_settings#servers_dir() . '/' . l:entry[0]
  if isdirectory(l:server_install_dir)
    call lsp_settings#utils#msg('Uninstalling ' . l:entry[0])
    call delete(l:server_install_dir, 'rf')
  endif
  call mkdir(l:server_install_dir, 'p')
  call lsp_settings#utils#msg('Installing ' . l:entry[0])
  if has('nvim')
    split new
    call termopen(l:entry[1], {'cwd': l:server_install_dir, 'on_exit': function('s:vim_lsp_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:vim_lsp_install_server_post', [l:entry[0]])})
    endif
  endif
endfunction

function! s:vim_lsp_settings_suggest(ft) abort
  let l:entry = s:vim_lsp_installer(a:ft)
  if empty(l:entry)
    return
  endif

  redraw!
  echohl Directory
  echomsg 'Please do :LspInstallServer to enable Language Server ' . l:entry[0]
  echohl None
endfunction

function! s:vim_lsp_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:vim_lsp_load_or_suggest_delay(ft) abort
  call s:vim_lsp_load_or_suggest(a:ft)
  "if get(g:, 'vim_lsp_settings_filetype_no_delays', 0)
  "  return s:vim_lsp_load_or_suggest(a:ft)
  "endif
  "call timer_start(0, {timer -> [s:vim_lsp_load_or_suggest(a:ft), execute('doautocmd BufReadPost')]})
endfunction

function! s:vim_lsp_load_or_suggest(ft) abort
  if (a:ft !=# '_' && &filetype !=# a:ft) || !has_key(s:settings, a:ft)
    return
  endif

  call lsp_settings#profile#load_local()

  if get(g:, 'lsp_loaded', 0)
    for l:server in s:settings[a:ft]
      let l:config = lsp_settings#server_config(l:server.command)
      let l:refresh_pattern = get(l:config, 'refresh_pattern', '')
      if !empty(l:refresh_pattern)
        let b:asyncomplete_refresh_pattern = l:refresh_pattern
      endif
    endfor
  endif

  if get(s:ftmap, a:ft, 0)
    return
  endif
  let l:group_name = lsp_settings#utils#group_name(a:ft)
  exe 'augroup' l:group_name
    autocmd!
  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:default = get(g:, 'lsp_settings_' . a:ft, '')

  let l:found = 0
  let l:disabled = 0

  for l:server in s:settings[a:ft]
    if lsp_settings#get(l:server.command, 'disabled', get(l:server, 'disabled', 0))
      let l:disabled += 1
      continue
    endif

    if type(l:default) ==# v:t_list
      if len(l:default) ># 0 && index(l:default, l:server.command) == -1
        continue
      endif
    elseif type(l:default) ==# v:t_string
      if !empty(l:default) && l:default != l:server.command
        continue
      endif
    else
      continue
    endif

    let l:command = lsp_settings#get(l:server.command, 'cmd', [])
    if empty(l:command) && !lsp_settings#executable(l:server.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

      " If default server is specified as list, continue to look next.
      if type(l:default) !=# v:t_list
        break
      endif
    endif
  endfor

  delcommand LspRegisterServer

  if l:disabled == 0 && l:found ==# 0
    if a:ft !=# '_'
      call s:vim_lsp_settings_suggest(a:ft)
    endif
  else
    if exists('#User#lsp_setup') !=# 0
      doautocmd User lsp_setup
    endif
  endif
endfunction

function! lsp_settings#clear() abort
  let s:ftmap = {}
endfunction

function! lsp_settings#init() 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' lsp_settings#utils#group_name(l:ft)
      autocmd!
      exe 'autocmd FileType' l:ft 'call' printf("s:vim_lsp_load_or_suggest_delay('%s')", l:ft)
    augroup END
  endfor
  augroup vim_lsp_suggest
    autocmd!
    autocmd BufNewFile,BufRead * call s:vim_lsp_suggest_plugin()
  augroup END
  command! -nargs=? -complete=customlist,lsp_settings#complete_uninstall LspUninstallServer call s:vim_lsp_uninstall_server(<q-args>)
  command! -bang -nargs=? -complete=customlist,lsp_settings#complete_install LspInstallServer call s:vim_lsp_install_server(&l:filetype, <q-args>, '<bang>')
  call s:vim_lsp_load_or_suggest('_')
endfunction