Skip to content

Commit 4468bbc

Browse files
add lsp#_new_command() and lsp#_last_command() (#648)
* add lsp#_new_command() and lsp#_last_command() * remove log * fix rename
1 parent 44e0823 commit 4468bbc

File tree

2 files changed

+48
-41
lines changed

2 files changed

+48
-41
lines changed

autoload/lsp.vim

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
let s:enabled = 0
22
let s:already_setup = 0
33
let s:servers = {} " { lsp_id, server_info, init_callbacks, init_result, buffers: { path: { changed_tick } }
4-
4+
let s:last_command_id = 0
55
let s:notification_callbacks = [] " { name, callback }
66

77
" This hold previous content for each language servers to make
@@ -914,3 +914,12 @@ endfunction
914914
function! lsp#server_complete(lead, line, pos) abort
915915
return filter(sort(keys(s:servers)), 'stridx(v:val, a:lead)==0 && has_key(s:servers[v:val], "init_result")')
916916
endfunction
917+
918+
function! lsp#_new_command() abort
919+
let s:last_command_id += 1
920+
return s:last_command_id
921+
endfunction
922+
923+
function! lsp#_last_command() abort
924+
return s:last_command_id
925+
endfunction

autoload/lsp/ui/vim.vim

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
let s:last_req_id = 0
2-
31
function! s:not_supported(what) abort
42
return lsp#utils#error(a:what.' not supported for '.&filetype)
53
endfunction
64

75
function! lsp#ui#vim#implementation(in_preview) abort
86
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_implementation_provider(v:val)')
9-
let s:last_req_id = s:last_req_id + 1
7+
let l:command_id = lsp#_new_command()
108
call setqflist([])
119

1210
if len(l:servers) == 0
1311
call s:not_supported('Retrieving implementation')
1412
return
1513
endif
16-
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_req_id': s:last_req_id, 'jump_if_one': 1, 'in_preview': a:in_preview }
14+
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_command_id': l:command_id, 'jump_if_one': 1, 'in_preview': a:in_preview }
1715
for l:server in l:servers
1816
call lsp#send_request(l:server, {
1917
\ 'method': 'textDocument/implementation',
@@ -30,14 +28,14 @@ endfunction
3028

3129
function! lsp#ui#vim#type_definition(in_preview) abort
3230
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_type_definition_provider(v:val)')
33-
let s:last_req_id = s:last_req_id + 1
31+
let l:command_id = lsp#_new_command()
3432
call setqflist([])
3533

3634
if len(l:servers) == 0
3735
call s:not_supported('Retrieving type definition')
3836
return
3937
endif
40-
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_req_id': s:last_req_id, 'jump_if_one': 1, 'in_preview': a:in_preview }
38+
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_command_id': l:command_id, 'jump_if_one': 1, 'in_preview': a:in_preview }
4139
for l:server in l:servers
4240
call lsp#send_request(l:server, {
4341
\ 'method': 'textDocument/typeDefinition',
@@ -54,13 +52,13 @@ endfunction
5452

5553
function! lsp#ui#vim#type_hierarchy() abort
5654
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_type_hierarchy_provider(v:val)')
57-
let s:last_req_id = s:last_req_id + 1
55+
let l:command_id = lsp#_new_command()
5856

5957
if len(l:servers) == 0
6058
call s:not_supported('Retrieving type hierarchy')
6159
return
6260
endif
63-
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_req_id': s:last_req_id }
61+
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_command_id': l:command_id }
6462
" direction 0 children, 1 parent, 2 both
6563
for l:server in l:servers
6664
call lsp#send_request(l:server, {
@@ -80,15 +78,15 @@ endfunction
8078

8179
function! lsp#ui#vim#declaration(in_preview) abort
8280
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_declaration_provider(v:val)')
83-
let s:last_req_id = s:last_req_id + 1
81+
let l:command_id = lsp#_new_command()
8482
call setqflist([])
8583

8684
if len(l:servers) == 0
8785
call s:not_supported('Retrieving declaration')
8886
return
8987
endif
9088

91-
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_req_id': s:last_req_id, 'jump_if_one': 1, 'in_preview': a:in_preview }
89+
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_command_id': l:command_id, 'jump_if_one': 1, 'in_preview': a:in_preview }
9290
for l:server in l:servers
9391
call lsp#send_request(l:server, {
9492
\ 'method': 'textDocument/declaration',
@@ -105,15 +103,15 @@ endfunction
105103

106104
function! lsp#ui#vim#definition(in_preview) abort
107105
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_definition_provider(v:val)')
108-
let s:last_req_id = s:last_req_id + 1
106+
let l:command_id = lsp#_new_command()
109107
call setqflist([])
110108

111109
if len(l:servers) == 0
112110
call s:not_supported('Retrieving definition')
113111
return
114112
endif
115113

116-
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_req_id': s:last_req_id, 'jump_if_one': 1, 'in_preview': a:in_preview }
114+
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_command_id': l:command_id, 'jump_if_one': 1, 'in_preview': a:in_preview }
117115
for l:server in l:servers
118116
call lsp#send_request(l:server, {
119117
\ 'method': 'textDocument/definition',
@@ -130,11 +128,11 @@ endfunction
130128

131129
function! lsp#ui#vim#references() abort
132130
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_references_provider(v:val)')
133-
let s:last_req_id = s:last_req_id + 1
131+
let l:command_id = lsp#_new_command()
134132

135133
call setqflist([])
136134

137-
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_req_id': s:last_req_id, 'jump_if_one': 0, 'in_preview': 0 }
135+
let l:ctx = { 'counter': len(l:servers), 'list':[], 'last_command_id': l:command_id, 'jump_if_one': 0, 'in_preview': 0 }
138136
if len(l:servers) == 0
139137
call s:not_supported('Retrieving references')
140138
return
@@ -169,7 +167,7 @@ function! s:rename(server, new_name, pos) abort
169167
\ 'position': a:pos,
170168
\ 'newName': a:new_name,
171169
\ },
172-
\ 'on_notification': function('s:handle_workspace_edit', [a:server, s:last_req_id, 'rename']),
170+
\ 'on_notification': function('s:handle_workspace_edit', [a:server, lsp#_last_command(), 'rename']),
173171
\ })
174172

175173
echo ' ... Renaming ...'
@@ -183,7 +181,7 @@ function! lsp#ui#vim#rename() abort
183181
let l:prepare_support = 0
184182
endif
185183

186-
let s:last_req_id = s:last_req_id + 1
184+
let l:command_id = lsp#_new_command()
187185

188186
if len(l:servers) == 0
189187
call s:not_supported('Renaming')
@@ -200,7 +198,7 @@ function! lsp#ui#vim#rename() abort
200198
\ 'textDocument': lsp#get_text_document_identifier(),
201199
\ 'position': lsp#get_position(),
202200
\ },
203-
\ 'on_notification': function('s:handle_rename_prepare', [l:server, s:last_req_id, 'rename_prepare']),
201+
\ 'on_notification': function('s:handle_rename_prepare', [l:server, l:command_id, 'rename_prepare']),
204202
\ })
205203
return
206204
endif
@@ -210,7 +208,7 @@ endfunction
210208

211209
function! s:document_format(sync) abort
212210
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_document_formatting_provider(v:val)')
213-
let s:last_req_id = s:last_req_id + 1
211+
let l:command_id = lsp#_new_command()
214212

215213
if len(l:servers) == 0
216214
call s:not_supported('Document formatting')
@@ -230,7 +228,7 @@ function! s:document_format(sync) abort
230228
\ },
231229
\ },
232230
\ 'sync': a:sync,
233-
\ 'on_notification': function('s:handle_text_edit', [l:server, s:last_req_id, 'document format']),
231+
\ 'on_notification': function('s:handle_text_edit', [l:server, l:command_id, 'document format']),
234232
\ })
235233
endfunction
236234

@@ -290,7 +288,7 @@ endfunction
290288

291289
function! s:document_format_range(sync, type) abort
292290
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_document_range_formatting_provider(v:val)')
293-
let s:last_req_id = s:last_req_id + 1
291+
let l:command_id = lsp#_new_command()
294292

295293
if len(l:servers) == 0
296294
call s:not_supported('Document range formatting')
@@ -318,7 +316,7 @@ function! s:document_format_range(sync, type) abort
318316
\ },
319317
\ },
320318
\ 'sync': a:sync,
321-
\ 'on_notification': function('s:handle_text_edit', [l:server, s:last_req_id, 'range format']),
319+
\ 'on_notification': function('s:handle_text_edit', [l:server, l:command_id, 'range format']),
322320
\ })
323321
endfunction
324322

@@ -336,7 +334,7 @@ endfunction
336334

337335
function! lsp#ui#vim#workspace_symbol() abort
338336
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_workspace_symbol_provider(v:val)')
339-
let s:last_req_id = s:last_req_id + 1
337+
let l:command_id = lsp#_new_command()
340338

341339
call setqflist([])
342340

@@ -353,7 +351,7 @@ function! lsp#ui#vim#workspace_symbol() abort
353351
\ 'params': {
354352
\ 'query': l:query,
355353
\ },
356-
\ 'on_notification': function('s:handle_symbol', [l:server, s:last_req_id, 'workspaceSymbol']),
354+
\ 'on_notification': function('s:handle_symbol', [l:server, l:command_id, 'workspaceSymbol']),
357355
\ })
358356
endfor
359357

@@ -362,7 +360,7 @@ endfunction
362360

363361
function! lsp#ui#vim#document_symbol() abort
364362
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_document_symbol_provider(v:val)')
365-
let s:last_req_id = s:last_req_id + 1
363+
let l:command_id = lsp#_new_command()
366364

367365
call setqflist([])
368366

@@ -377,7 +375,7 @@ function! lsp#ui#vim#document_symbol() abort
377375
\ 'params': {
378376
\ 'textDocument': lsp#get_text_document_identifier(),
379377
\ },
380-
\ 'on_notification': function('s:handle_symbol', [l:server, s:last_req_id, 'documentSymbol']),
378+
\ 'on_notification': function('s:handle_symbol', [l:server, l:command_id, 'documentSymbol']),
381379
\ })
382380
endfor
383381

@@ -413,7 +411,7 @@ endfunction
413411
" https://microsoft.github.io/language-server-protocol/specification#textDocument_codeAction
414412
function! lsp#ui#vim#code_action() abort
415413
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_code_action_provider(v:val)')
416-
let s:last_req_id = s:last_req_id + 1
414+
let l:command_id = lsp#_new_command()
417415
let l:diagnostic = lsp#ui#vim#diagnostics#get_diagnostics_under_cursor()
418416

419417
if len(l:servers) == 0
@@ -445,15 +443,15 @@ function! lsp#ui#vim#code_action() abort
445443
\ 'only': ['', 'quickfix', 'refactor', 'refactor.extract', 'refactor.inline', 'refactor.rewrite', 'source', 'source.organizeImports'],
446444
\ },
447445
\ },
448-
\ 'on_notification': function('s:handle_code_action', [l:server, s:last_req_id, 'codeAction']),
446+
\ 'on_notification': function('s:handle_code_action', [l:server, l:command_id, 'codeAction']),
449447
\ })
450448
endfor
451449

452450
echo 'Retrieving code actions ...'
453451
endfunction
454452

455-
function! s:handle_symbol(server, last_req_id, type, data) abort
456-
if a:last_req_id != s:last_req_id
453+
function! s:handle_symbol(server, last_command_id, type, data) abort
454+
if a:last_command_id != lsp#_last_command()
457455
return
458456
endif
459457

@@ -474,8 +472,8 @@ function! s:handle_symbol(server, last_req_id, type, data) abort
474472
endif
475473
endfunction
476474

477-
function! s:handle_location(ctx, server, type, data) abort "ctx = {counter, list, jump_if_one, last_req_id, in_preview}
478-
if a:ctx['last_req_id'] != s:last_req_id
475+
function! s:handle_location(ctx, server, type, data) abort "ctx = {counter, list, jump_if_one, last_command_id, in_preview}
476+
if a:ctx['last_command_id'] != lsp#_last_command()
479477
return
480478
endif
481479

@@ -523,8 +521,8 @@ function! s:handle_location(ctx, server, type, data) abort "ctx = {counter, list
523521
endif
524522
endfunction
525523

526-
function! s:handle_rename_prepare(server, last_req_id, type, data) abort
527-
if a:last_req_id != s:last_req_id
524+
function! s:handle_rename_prepare(server, last_command_id, type, data) abort
525+
if a:last_command_id != lsp#_last_command()
528526
return
529527
endif
530528

@@ -554,8 +552,8 @@ function! s:handle_rename_prepare(server, last_req_id, type, data) abort
554552
call timer_start(1, {x->s:rename(a:server, input('new name: ', l:name), l:range['start'])})
555553
endfunction
556554

557-
function! s:handle_workspace_edit(server, last_req_id, type, data) abort
558-
if a:last_req_id != s:last_req_id
555+
function! s:handle_workspace_edit(server, last_command_id, type, data) abort
556+
if a:last_command_id != lsp#_last_command()
559557
return
560558
endif
561559

@@ -569,8 +567,8 @@ function! s:handle_workspace_edit(server, last_req_id, type, data) abort
569567
echo 'Renamed'
570568
endfunction
571569

572-
function! s:handle_text_edit(server, last_req_id, type, data) abort
573-
if a:last_req_id != s:last_req_id
570+
function! s:handle_text_edit(server, last_command_id, type, data) abort
571+
if a:last_command_id != lsp#_last_command()
574572
return
575573
endif
576574

@@ -584,7 +582,7 @@ function! s:handle_text_edit(server, last_req_id, type, data) abort
584582
redraw | echo 'Document formatted'
585583
endfunction
586584

587-
function! s:handle_code_action(server, last_req_id, type, data) abort
585+
function! s:handle_code_action(server, last_command_id, type, data) abort
588586
if lsp#client#is_error(a:data['response'])
589587
call lsp#utils#error('Failed to '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
590588
return
@@ -615,8 +613,8 @@ function! s:handle_code_action(server, last_req_id, type, data) abort
615613
endif
616614
endfunction
617615

618-
function! s:handle_type_hierarchy(ctx, server, type, data) abort "ctx = {counter, list, last_req_id}
619-
if a:ctx['last_req_id'] != s:last_req_id
616+
function! s:handle_type_hierarchy(ctx, server, type, data) abort "ctx = {counter, list, last_command_id}
617+
if a:ctx['last_command_id'] != lsp#_last_command()
620618
return
621619
endif
622620

0 commit comments

Comments
 (0)