@@ -28,16 +28,21 @@ endfunction
2828" apply
2929"
3030function ! s: apply (path , text_edits) abort
31- let l: current_bufname = bufname (' %' )
32- let l: current_position = s: Position .cursor ()
31+ let l: current_bufnr = bufnr (' %' )
32+ for l: bufnr in range (1 , bufnr (' $' ))
33+ if index ([fnamemodify (a: path , ' :p' ),fnameescape (fnamemodify (a: path , ' :p' ))], bufname (l: bufnr )) != -1
34+ let l: target_bufnr = l: bufnr
35+ break
36+ endif
37+ endfor
38+ if ! exists (' l:target_bufnr' )
39+ let l: target_bufnr = s: Buffer .ensure (a: path )
40+ endif
3341
34- let l: target_bufnr = s: _switch (a: path )
35- call s: _substitute (l: target_bufnr , a: text_edits , l: current_position )
36- let l: current_bufnr = s: _switch (l: current_bufname )
42+ let l: current_position = s: Position .cursor ()
43+ call s: Buffer .do (l: target_bufnr , { - > s: _substitute (l: target_bufnr , a: text_edits , l: current_position ) })
3744
38- if l: current_bufnr == l: target_bufnr
39- call cursor (s: Position .lsp_to_vim (' %' , l: current_position ))
40- endif
45+ call cursor (s: Position .lsp_to_vim (' %' , l: current_position ))
4146endfunction
4247
4348"
@@ -167,18 +172,3 @@ function! s:_fix_text_edits(bufnr, text_edits) abort
167172 return [l: fixeol , l: text_edits ]
168173endfunction
169174
170- "
171- " _switch
172- "
173- function ! s: _switch (path ) abort
174- let l: curr = bufnr (' %' )
175- let l: next = filereadable (a: path ) ? bufnr (fnameescape (a: path )) : bufnr (a: path )
176- if l: next >= 0
177- if l: curr != l: next
178- execute printf (' noautocmd keepalt keepjumps %sbuffer!' , l: next )
179- endif
180- else
181- execute printf (' noautocmd keepalt keepjumps edit! %s' , fnameescape (a: path ))
182- endif
183- return bufnr (' %' )
184- endfunction
0 commit comments