Skip to content

Commit 6eee2bf

Browse files
mattnprabirshrestha
authored andcommitted
Check textDocumentSync
1 parent ec38d8f commit 6eee2bf

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

autoload/lsp.vim

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,17 +405,27 @@ endfunction
405405

406406
let s:file_content = {}
407407

408-
function! s:text_changes(buf) abort
409-
if has_key(s:file_content, a:buf)
408+
function! s:text_changes(server_name, buf) abort
409+
let l:sync_kind = lsp#capabilities#get_text_document_change_sync_kind(a:server_name)
410+
411+
" When syncKind is None, return null for contentChanges.
412+
if l:sync_kind == 0
413+
return v:null
414+
endif
415+
416+
" When syncKind is Incremental and previous content is saved.
417+
if l:sync_kind == 2 && has_key(s:file_content, a:buf)
418+
" compute diff
410419
let l:old_content = get(s:file_content, a:buf, [])
411420
let l:new_content = getbufline(a:buf, 1, '$')
412421
let l:changes = lsp#utils#diff#compute(l:old_content, l:new_content)
413422
let s:file_content[a:buf] = l:new_content
414-
else
415-
let l:new_content = getbufline(a:buf, 1, '$')
416-
let l:changes = {'text': join(l:new_content, "\n")}
417-
let s:file_content[a:buf] = l:new_content
423+
return [l:changes]
418424
endif
425+
426+
let l:new_content = getbufline(a:buf, 1, '$')
427+
let l:changes = {'text': join(l:new_content, "\n")}
428+
let s:file_content[a:buf] = l:new_content
419429
return [l:changes]
420430
endfunction
421431

@@ -442,7 +452,7 @@ function! s:ensure_changed(buf, server_name, cb) abort
442452
\ 'method': 'textDocument/didChange',
443453
\ 'params': {
444454
\ 'textDocument': s:get_text_document_identifier(a:buf, l:buffer_info),
445-
\ 'contentChanges': s:text_changes(a:buf),
455+
\ 'contentChanges': s:text_changes(a:server_name, a:buf),
446456
\ }
447457
\ })
448458

autoload/lsp/capabilities.vim

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,23 @@ function! lsp#capabilities#get_text_document_save_registration_options(server_na
6969
endif
7070
return [0, { 'includeText': 0 }]
7171
endfunction
72+
73+
" supports_did_change (boolean)
74+
function! lsp#capabilities#get_text_document_change_sync_kind(server_name) abort
75+
let l:capabilities = lsp#get_server_capabilities(a:server_name)
76+
if !empty(l:capabilities) && has_key(l:capabilities, 'textDocumentSync')
77+
if type(l:capabilities['textDocumentSync']) == type({})
78+
if has_key(l:capabilities['textDocumentSync'], 'change') && type(l:capabilities['textDocumentSync']) == type(1)
79+
let l:val = l:capabilities['textDocumentSync']['change']
80+
return l:val >= 0 && l:val <= 2 ? l:val : 1
81+
else
82+
return 1
83+
endif
84+
elseif type(l:capabilities['textDocumentSync']) == type(1)
85+
return l:capabilities['textDocumentSync']
86+
else
87+
return 1
88+
endif
89+
endif
90+
return 1
91+
endfunction

0 commit comments

Comments
 (0)