Skip to content
This repository was archived by the owner on Aug 24, 2020. It is now read-only.

Commit 92d63e1

Browse files
committed
Support autocommands when LSP commands fail
This commit resolves issue 207. "Definition or references" can be implemented with the following: ```vim autocmd User lsp_definition_failed LspReferences ``` I want to fall back to ctags when `LspDefinition` fails. ```vim autocmd User lsp_definition_failed execute "tjump " . expand("<cword>") ``` Only commands/functions defined in `autoload/lsp/ui/vim.vim` are experimentally suppported.
1 parent 415f6e6 commit 92d63e1

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

autoload/lsp.vim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ augroup _lsp_silent_
2929
autocmd User lsp_float_opened silent
3030
autocmd User lsp_float_closed silent
3131
autocmd User lsp_buffer_enabled silent
32+
autocmd User lsp_implementation_failed silent
33+
autocmd User lsp_type_definition_failed silent
34+
autocmd User lsp_declaration_failed silent
35+
autocmd User lsp_definition_failed silent
36+
autocmd User lsp_references_failed silent
37+
autocmd User lsp_rename_failed silent
38+
autocmd User lsp_document_format_failed silent
39+
autocmd User lsp_range_format_failed silent
40+
autocmd User lsp_workspace_symbol_failed silent
41+
autocmd User lsp_document_symbol_failed silent
42+
autocmd User lsp_code_action_failed silent
3243
augroup END
3344

3445
function! lsp#log_verbose(...) abort

autoload/lsp/ui/vim.vim

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function! lsp#ui#vim#implementation(in_preview) abort
99

1010
if len(l:servers) == 0
1111
call s:not_supported('Retrieving implementation')
12+
call s:after_failure('implementation')
1213
return
1314
endif
1415
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_command_id': l:command_id, 'jump_if_one': 1, 'in_preview': a:in_preview }
@@ -33,6 +34,7 @@ function! lsp#ui#vim#type_definition(in_preview) abort
3334

3435
if len(l:servers) == 0
3536
call s:not_supported('Retrieving type definition')
37+
call s:after_failure('type definition')
3638
return
3739
endif
3840
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_command_id': l:command_id, 'jump_if_one': 1, 'in_preview': a:in_preview }
@@ -83,6 +85,7 @@ function! lsp#ui#vim#declaration(in_preview) abort
8385

8486
if len(l:servers) == 0
8587
call s:not_supported('Retrieving declaration')
88+
call s:after_failure('declaration')
8689
return
8790
endif
8891

@@ -108,6 +111,7 @@ function! lsp#ui#vim#definition(in_preview) abort
108111

109112
if len(l:servers) == 0
110113
call s:not_supported('Retrieving definition')
114+
call s:after_failure('definition')
111115
return
112116
endif
113117

@@ -135,6 +139,7 @@ function! lsp#ui#vim#references() abort
135139
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_command_id': l:command_id, 'jump_if_one': 0, 'in_preview': 0 }
136140
if len(l:servers) == 0
137141
call s:not_supported('Retrieving references')
142+
call s:after_failure('references')
138143
return
139144
endif
140145

@@ -185,6 +190,7 @@ function! lsp#ui#vim#rename() abort
185190

186191
if len(l:servers) == 0
187192
call s:not_supported('Renaming')
193+
call s:after_failure('rename')
188194
return
189195
endif
190196

@@ -212,6 +218,7 @@ function! s:document_format(sync) abort
212218

213219
if len(l:servers) == 0
214220
call s:not_supported('Document formatting')
221+
call s:after_failure('document format')
215222
return
216223
endif
217224

@@ -292,6 +299,7 @@ function! s:document_format_range(sync, type) abort
292299

293300
if len(l:servers) == 0
294301
call s:not_supported('Document range formatting')
302+
call s:after_failure('range format')
295303
return
296304
endif
297305

@@ -340,6 +348,7 @@ function! lsp#ui#vim#workspace_symbol() abort
340348

341349
if len(l:servers) == 0
342350
call s:not_supported('Retrieving workspace symbols')
351+
call s:after_failure('workspaceSymbol')
343352
return
344353
endif
345354

@@ -370,6 +379,7 @@ function! lsp#ui#vim#document_symbol() abort
370379

371380
if len(l:servers) == 0
372381
call s:not_supported('Retrieving symbols')
382+
call s:after_failure('documentSymbol')
373383
return
374384
endif
375385

@@ -393,6 +403,7 @@ function! s:handle_symbol(server, last_command_id, type, data) abort
393403

394404
if lsp#client#is_error(a:data['response'])
395405
call lsp#utils#error('Failed to retrieve '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
406+
call s:after_failure(a:type)
396407
return
397408
endif
398409

@@ -402,6 +413,7 @@ function! s:handle_symbol(server, last_command_id, type, data) abort
402413

403414
if empty(l:list)
404415
call lsp#utils#error('No ' . a:type .' found')
416+
call s:after_failure(a:type)
405417
else
406418
echo 'Retrieved ' . a:type
407419
botright copen
@@ -417,13 +429,15 @@ function! s:handle_location(ctx, server, type, data) abort "ctx = {counter, list
417429

418430
if lsp#client#is_error(a:data['response']) || !has_key(a:data['response'], 'result')
419431
call lsp#utils#error('Failed to retrieve '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
432+
call s:after_failure(a:type)
420433
else
421434
let a:ctx['list'] = a:ctx['list'] + lsp#utils#location#_lsp_to_vim_list(a:data['response']['result'])
422435
endif
423436

424437
if a:ctx['counter'] == 0
425438
if empty(a:ctx['list'])
426439
call lsp#utils#error('No ' . a:type .' found')
440+
call s:after_failure(a:type)
427441
else
428442
call lsp#utils#tagstack#_update()
429443

@@ -464,6 +478,7 @@ function! s:handle_rename_prepare(server, last_command_id, type, data) abort
464478

465479
if lsp#client#is_error(a:data['response'])
466480
call lsp#utils#error('Failed to retrieve '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
481+
call s:after_failure('rename')
467482
return
468483
endif
469484

@@ -495,6 +510,7 @@ function! s:handle_workspace_edit(server, last_command_id, type, data) abort
495510

496511
if lsp#client#is_error(a:data['response'])
497512
call lsp#utils#error('Failed to retrieve '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
513+
call s:after_failure(a:type)
498514
return
499515
endif
500516

@@ -510,6 +526,7 @@ function! s:handle_text_edit(server, last_command_id, type, data) abort
510526

511527
if lsp#client#is_error(a:data['response'])
512528
call lsp#utils#error('Failed to '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
529+
call s:after_failure(a:type)
513530
return
514531
endif
515532

@@ -584,6 +601,14 @@ function! s:get_treeitem_for_tree_hierarchy(Callback, object) dict abort
584601
call a:Callback('success', s:hierarchyitem_to_treeitem(a:object))
585602
endfunction
586603

604+
function! s:after_failure(type) abort
605+
execute 'doautocmd User ' . s:event_name_on_failure(a:type)
606+
endfunction
607+
608+
function! s:event_name_on_failure(type) abort
609+
return 'lsp_' . substitute(substitute(a:type, '\s', '_', 'g'), '\C[A-Z]', '\="_" . tolower(submatch(0))', 'g') . '_failed'
610+
endfunction
611+
587612
function! lsp#ui#vim#code_action() abort
588613
call lsp#ui#vim#code_action#do({
589614
\ 'sync': v:false,

doc/vim-lsp.txt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,57 @@ This autocommand is run after vim-lsp is enabled for the buffer. This event is
11831183
triggered imediately when the buffer is currently active. If the buffer is not
11841184
current active, the event will be triggered when the buffer will be active.
11851185

1186+
lsp_implementation_failed *lsp_implementation_failed*
1187+
1188+
This autocommand is run after |LspImplementation| and |LspPeekImplementation|
1189+
failed.
1190+
1191+
lsp_type_definition_failed *lsp_type_definition_failed*
1192+
1193+
This autocommand is run after |LspTypeDefinition| and |LspPeekTypeDefinition|
1194+
failed.
1195+
1196+
lsp_declaration_failed *lsp_declaration_failed*
1197+
1198+
This autocommand is run after |LspDeclaration| and |LspPeekDeclaration|
1199+
failed.
1200+
1201+
lsp_definition_failed *lsp_definition_failed*
1202+
1203+
This autocommand is run after |LspDefinition| and |LspPeekDefinition|
1204+
failed.
1205+
1206+
lsp_references_failed *lsp_references_failed*
1207+
1208+
This autocommand is run after |LspReferences|, |LspNextReference| and
1209+
|LspPreviousReference| failed.
1210+
1211+
lsp_rename_failed *lsp_rename_failed*
1212+
1213+
This autocommand is run after |LspRename| failed.
1214+
1215+
lsp_document_format_failed *lsp_document_format_failed*
1216+
1217+
This autocommand is run after |LspDocumentFormat| and |LspDocumentFormatSync|
1218+
failed.
1219+
1220+
lsp_range_format_failed *lsp_range_format_failed*
1221+
1222+
This autocommand is run after |LspDocumentRangeFormat| and
1223+
|LspDocumentRangeFormatSync| failed.
1224+
1225+
lsp_workspace_symbol_failed *lsp_workspace_symbol_failed*
1226+
1227+
This autocommand is run after |LspWorkspaceSymbol| failed.
1228+
1229+
lsp_document_symbol_failed *lsp_document_symbol_failed*
1230+
1231+
This autocommand is run after |LspDocumentSymbol| failed.
1232+
1233+
lsp_code_action_failed *lsp_code_action_failed*
1234+
1235+
This autocommand is run after |LspCodeAction| failed.
1236+
11861237

11871238
==============================================================================
11881239
Mappings *vim-lsp-mappings*

0 commit comments

Comments
 (0)