Skip to content

Commit d1282b8

Browse files
authored
Merge pull request #738 from nickspoons/update-on-change
Update buffer on change
2 parents e33c7ce + 5edf9e2 commit d1282b8

File tree

10 files changed

+91
-87
lines changed

10 files changed

+91
-87
lines changed

autoload/OmniSharp/actions/buffer.vim

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
let s:save_cpo = &cpoptions
22
set cpoptions&vim
33

4-
" Optional arguments:
5-
" - callback: funcref to be called after the response is returned (synchronously
4+
" Synchronize the buffer contents with the server. By default, contents are only
5+
" sent when there have been changes since the last run.
6+
" Optional argument: A dict containing the following optional items:
7+
" Callback: funcref to be called after the response is returned (synchronously
68
" or asynchronously)
7-
" - initializing: flag indicating that this is the first request for this buffer
8-
" - sendBuffer: flag indicating that the buffer contents should be sent,
9+
" Initializing: flag indicating that this is the first request for this buffer
10+
" SendBuffer: flag indicating that the buffer contents should be sent,
911
" regardless of &modified status or b:changedtick
1012
function! OmniSharp#actions#buffer#Update(...) abort
11-
let cb = a:0 && type(a:1) == type(function('tr')) ? { 'Callback': a:1 } : {}
12-
let initializing = a:0 > 1 && a:2 is 1
13-
let sendBuffer = initializing || (a:0 > 2 ? a:3 : 0)
13+
let opts = a:0 ? a:1 : {}
14+
let opts.Initializing = get(opts, 'Initializing', 0)
15+
let opts.SendBuffer = opts.Initializing || get(opts, 'SendBuffer', 0)
1416
if bufname('%') ==# '' || OmniSharp#FugitiveCheck() | return | endif
1517
let lasttick = get(b:, 'OmniSharp_UpdateChangeTick', -1)
16-
if initializing || sendBuffer || b:changedtick != lasttick
18+
if opts.SendBuffer || b:changedtick != lasttick
1719
let b:OmniSharp_UpdateChangeTick = b:changedtick
1820
if g:OmniSharp_server_stdio
19-
let opts = {
20-
\ 'ResponseHandler': function('s:StdioUpdateRH', [cb]),
21-
\ 'Initializing': initializing
21+
let requestOpts = {
22+
\ 'ResponseHandler': function('s:StdioUpdateRH', [opts]),
23+
\ 'Initializing': opts.Initializing
2224
\}
23-
call OmniSharp#stdio#Request('/updatebuffer', opts)
25+
call OmniSharp#stdio#Request('/updatebuffer', requestOpts)
2426
else
2527
if !OmniSharp#IsServerRunning() | return | endif
2628
call OmniSharp#py#Eval('updateBuffer()')
2729
call OmniSharp#py#CheckForError()
28-
if has_key(cb, 'Callback')
29-
call cb.Callback()
30+
if has_key(opts, 'Callback')
31+
call opts.Callback()
3032
endif
3133
endif
3234
endif

autoload/OmniSharp/actions/diagnostics.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,13 @@ endfunction
5151
" directly from autoload/ale/sources/OmniSharp.vim so requires a full autoload
5252
" function name.
5353
function! OmniSharp#actions#diagnostics#StdioCheck(bufnr, Callback) abort
54+
" OmniSharp#actions#buffer#Update only updates the server state when the
55+
" buffer has been modified since the last server update
56+
call OmniSharp#actions#buffer#Update()
5457
let opts = {
5558
\ 'ResponseHandler': function('s:StdioCheckRH', [a:Callback]),
5659
\ 'BufNum': a:bufnr,
60+
\ 'SendBuffer': 0,
5761
\ 'ReplayOnLoad': 1
5862
\}
5963
call OmniSharp#stdio#Request('/codecheck', opts)

autoload/OmniSharp/actions/signature.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function! s:StdioSignatureHelpRH(Callback, seq, opts, response) abort
9797
if has_key(a:opts, 'ForCompleteMethod') && !g:OmniSharp_want_snippet
9898
" Because of our 'falsified' request with an extra '(', re-synchronise the
9999
" server's version of the buffer with the actual buffer contents.
100-
call OmniSharp#actions#buffer#Update(0, 0, 1)
100+
call OmniSharp#actions#buffer#Update({'SendBuffer': 1})
101101
endif
102102
call a:Callback(a:response.Body)
103103
endfunction

autoload/OmniSharp/buffer.vim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ function! OmniSharp#buffer#Initialize(job, bufnr, command, opts) abort
1010
let a:job.pending_requests[a:bufnr][a:command] = a:opts
1111
if has_key(OmniSharp#GetHost(a:bufnr), 'initializing') | return | endif
1212
let host.initializing = 1
13-
let Callback = function('s:CBInitialize', [a:job, a:bufnr, host])
14-
call OmniSharp#actions#buffer#Update(Callback, 1)
13+
call OmniSharp#actions#buffer#Update({
14+
\ 'Callback': function('s:CBInitialize', [a:job, a:bufnr, host]),
15+
\ 'Initializing': 1
16+
\})
1517
endfunction
1618

1719
function! s:CBInitialize(job, bufnr, host) abort

autoload/OmniSharp/stdio.vim

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,6 @@ function! OmniSharp#stdio#Request(command, opts) abort
174174
\ fnamemodify(bufname(bufnr), ':p'))
175175
let send_buffer = get(a:opts, 'SendBuffer', 1)
176176
endif
177-
let lines = getbufline(bufnr, 1, '$')
178-
if has_key(a:opts, 'OverrideBuffer')
179-
let lines[a:opts.OverrideBuffer.LineNr - 1] = a:opts.OverrideBuffer.Line
180-
let cnum = a:opts.OverrideBuffer.Col
181-
endif
182-
let tmp = join(lines, '')
183-
" Unique string separator which must not exist in the buffer
184-
let sep = '@' . matchstr(reltimestr(reltime()), '\v\.@<=\d+') . '@'
185-
while stridx(tmp, sep) >= 0
186-
let sep = '@' . matchstr(reltimestr(reltime()), '\v\.@<=\d+') . '@'
187-
endwhile
188-
let buffer = join(lines, sep)
189177

190178
let body = {
191179
\ 'Arguments': {
@@ -195,10 +183,23 @@ function! OmniSharp#stdio#Request(command, opts) abort
195183
\ }
196184
\}
197185
if send_buffer
198-
let body.Arguments.Buffer = buffer
186+
let lines = getbufline(bufnr, 1, '$')
187+
if has_key(a:opts, 'OverrideBuffer')
188+
let lines[a:opts.OverrideBuffer.LineNr - 1] = a:opts.OverrideBuffer.Line
189+
let cnum = a:opts.OverrideBuffer.Col
190+
endif
191+
let tmp = join(lines, '')
192+
" Unique string separator which must not exist in the buffer
193+
let sep = '@' . matchstr(reltimestr(reltime()), '\v\.@<=\d+') . '@'
194+
while stridx(tmp, sep) >= 0
195+
let sep = '@' . matchstr(reltimestr(reltime()), '\v\.@<=\d+') . '@'
196+
endwhile
197+
let body.Arguments.Buffer = join(lines, sep)
198+
else
199+
let sep = ''
199200
endif
200201

201-
call s:Request(job, body, a:command, a:opts, sep)
202+
call s:Request(job, body, a:command, a:opts, sep, bufnr)
202203

203204
if has_key(a:opts, 'ReplayOnLoad')
204205
let replay_opts = filter(copy(a:opts), 'v:key !=# "ReplayOnLoad"')
@@ -209,10 +210,10 @@ function! OmniSharp#stdio#Request(command, opts) abort
209210
endfunction
210211

211212
function! OmniSharp#stdio#RequestGlobal(job, command, opts) abort
212-
call s:Request(a:job, {}, a:command, a:opts)
213+
call s:Request(a:job, {}, a:command, a:opts, '', -1)
213214
endfunction
214215

215-
function! s:Request(job, body, command, opts, ...) abort
216+
function! s:Request(job, body, command, opts, sep, bufnr) abort
216217
call OmniSharp#log#Log(a:job, 'Request: ' . a:command, 1)
217218

218219
let a:body['Command'] = a:command
@@ -221,14 +222,14 @@ function! s:Request(job, body, command, opts, ...) abort
221222
if has_key(a:opts, 'Parameters')
222223
call extend(a:body.Arguments, a:opts.Parameters, 'force')
223224
endif
224-
let sep = a:0 ? a:1 : ''
225-
if sep !=# ''
226-
let encodedBody = substitute(json_encode(a:body), sep, '\\r\\n', 'g')
225+
if a:sep !=# ''
226+
let encodedBody = substitute(json_encode(a:body), a:sep, '\\r\\n', 'g')
227227
else
228228
let encodedBody = json_encode(a:body)
229229
endif
230230

231231
let s:requests[s:nextseq] = {
232+
\ 'BufNum': a:bufnr,
232233
\ 'Command': a:command,
233234
\ 'Seq': s:nextseq,
234235
\ 'StartTime': reltime()

autoload/OmniSharp/util.vim

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ function! OmniSharp#util#CheckCapabilities() abort
123123
endif
124124

125125
if !s:capable
126-
" Clear BufEnter and InsertLeave autocmds
127-
silent! autocmd! OmniSharp_HighlightTypes
128126
" Clear plugin integration autocmds
129127
silent! autocmd! OmniSharp_Integrations
130128
endif

autoload/ale/sources/OmniSharp.vim

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
function! ale#sources#OmniSharp#WantResults(buffer) abort
2-
let g:OmniSharp_diagnostics_requested = 1
1+
function! ale#sources#OmniSharp#WantResults() abort
2+
if !g:OmniSharp_server_stdio | return | endif
3+
let bufnr = g:ale_want_results_buffer
4+
if getbufvar(bufnr, '&filetype') !=# 'cs' | return | endif
35
if OmniSharp#FugitiveCheck() | return | endif
4-
call ale#other_source#StartChecking(a:buffer, 'OmniSharp')
5-
let opts = { 'BufNum': a:buffer }
6+
let g:OmniSharp_diagnostics_requested = 1
7+
call ale#other_source#StartChecking(bufnr, 'OmniSharp')
8+
let opts = { 'BufNum': bufnr }
69
let Callback = function('ale#sources#OmniSharp#ProcessResults', [opts])
7-
call OmniSharp#actions#diagnostics#StdioCheck(a:buffer, Callback)
10+
call OmniSharp#actions#diagnostics#StdioCheck(bufnr, Callback)
811
endfunction
912

1013
function! ale#sources#OmniSharp#ProcessResults(opts, locations) abort

ftplugin/cs/OmniSharp.vim

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,54 @@ if !OmniSharp#util#CheckCapabilities() | finish | endif
33
if get(b:, 'OmniSharp_ftplugin_loaded', 0) | finish | endif
44
let b:OmniSharp_ftplugin_loaded = 1
55

6+
if get(g:, 'OmniSharp_start_server', 0)
7+
call OmniSharp#StartServerIfNotRunning()
8+
endif
9+
10+
call OmniSharp#actions#buffer#Update()
11+
if g:OmniSharp_highlighting
12+
call OmniSharp#actions#highlight#Buffer()
13+
endif
14+
615
augroup OmniSharp_FileType
716
autocmd! * <buffer>
817

18+
autocmd BufEnter <buffer>
19+
\ if !pumvisible() |
20+
\ call OmniSharp#actions#buffer#Update({'SendBuffer': 1}) |
21+
\ endif
922
autocmd BufLeave <buffer>
1023
\ if !pumvisible() |
1124
\ call OmniSharp#actions#buffer#Update() |
1225
\ endif
1326

1427
autocmd CompleteDone <buffer> call OmniSharp#actions#complete#ExpandSnippet()
28+
29+
autocmd TextChanged <buffer> call OmniSharp#actions#buffer#Update()
30+
31+
autocmd BufEnter <buffer>
32+
\ if g:OmniSharp_highlighting |
33+
\ call OmniSharp#actions#highlight#Buffer() |
34+
\ endif
35+
autocmd InsertLeave,TextChanged <buffer>
36+
\ if g:OmniSharp_highlighting >= 2 |
37+
\ call OmniSharp#actions#highlight#Buffer() |
38+
\ endif
39+
autocmd TextChangedI <buffer>
40+
\ if g:OmniSharp_highlighting >= 3 |
41+
\ call OmniSharp#actions#highlight#Buffer() |
42+
\ endif
43+
if exists('##TextChangedP')
44+
autocmd TextChangedP <buffer>
45+
\ if g:OmniSharp_highlighting >= 3 |
46+
\ call OmniSharp#actions#highlight#Buffer() |
47+
\ endif
48+
endif
49+
1550
augroup END
1651

1752
setlocal omnifunc=OmniSharp#Complete
1853

19-
if get(g:, 'OmniSharp_start_server', 0)
20-
call OmniSharp#StartServerIfNotRunning()
21-
endif
22-
2354
command! -buffer -bar OmniSharpRestartAllServers call OmniSharp#RestartAllServers()
2455
command! -buffer -bar OmniSharpRestartServer call OmniSharp#RestartServer()
2556
command! -buffer -bar -nargs=? -complete=file OmniSharpStartServer call OmniSharp#StartServer(<q-args>)

plugin/OmniSharp.vim

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -50,46 +50,8 @@ command! -bar -nargs=? OmniSharpInstall call OmniSharp#Install(<f-args>)
5050
command! -bar -nargs=? OmniSharpOpenLog call OmniSharp#log#Open(<q-args>)
5151
command! -bar -bang OmniSharpStatus call OmniSharp#Status(<bang>0)
5252

53-
" Initialise automatic type and interface highlighting
5453
" Preserve backwards compatibility with older version g:OmniSharp_highlight_types
5554
let g:OmniSharp_highlighting = get(g:, 'OmniSharp_highlighting', get(g:, 'OmniSharp_highlight_types', 2))
56-
if g:OmniSharp_highlighting
57-
augroup OmniSharp_HighlightTypes
58-
autocmd!
59-
autocmd BufEnter *.cs,*.csx
60-
\ if !pumvisible() && OmniSharp#util#CheckCapabilities() |
61-
\ call OmniSharp#actions#highlight#Buffer() |
62-
\ endif
63-
64-
if g:OmniSharp_highlighting >= 2
65-
autocmd InsertLeave,TextChanged *.cs,*.csx
66-
\ if OmniSharp#util#CheckCapabilities() |
67-
\ call OmniSharp#actions#highlight#Buffer() |
68-
\ endif
69-
endif
70-
71-
if g:OmniSharp_highlighting >= 3
72-
autocmd TextChangedI *.cs,*.csx
73-
\ if OmniSharp#util#CheckCapabilities() |
74-
\ call OmniSharp#actions#highlight#Buffer() |
75-
\ endif
76-
77-
if exists('##TextChangedP')
78-
autocmd TextChangedP *.cs,*.csx
79-
\ if OmniSharp#util#CheckCapabilities() |
80-
\ call OmniSharp#actions#highlight#Buffer() |
81-
\ endif
82-
endif
83-
endif
84-
augroup END
85-
endif
86-
87-
function! s:ALEWantResults() abort
88-
if !g:OmniSharp_server_stdio | return | endif
89-
if getbufvar(g:ale_want_results_buffer, '&filetype') ==# 'cs'
90-
call ale#sources#OmniSharp#WantResults(g:ale_want_results_buffer)
91-
endif
92-
endfunction
9355

9456
augroup OmniSharp_Integrations
9557
autocmd!
@@ -113,7 +75,7 @@ augroup OmniSharp_Integrations
11375
\})
11476

11577
" Listen for ALE requests
116-
autocmd User ALEWantResults call s:ALEWantResults()
78+
autocmd User ALEWantResults call ale#sources#OmniSharp#WantResults()
11779
augroup END
11880

11981
if !exists('g:OmniSharp_selector_ui')

test/utils/async-helper.vader

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ Execute (set up async helper):
3131
if has_key(b:, 'OmniSharp_UpdateChangeTick')
3232
unlet b:OmniSharp_UpdateChangeTick
3333
endif
34-
call OmniSharpTestAwait('OmniSharp#actions#buffer#Update', [])
34+
call OmniSharpTestAwait('OmniSharp#actions#buffer#Update',
35+
\ [{'Callback': function('s:CallbackStopWaiting')}])
3536
endfunction
3637

3738
function! OmniSharpTestCleanupBuffer() abort

0 commit comments

Comments
 (0)