summaryrefslogtreecommitdiff
path: root/autoload/ale/code_action.vim
diff options
context:
space:
mode:
Diffstat (limited to 'autoload/ale/code_action.vim')
-rw-r--r--autoload/ale/code_action.vim80
1 files changed, 29 insertions, 51 deletions
diff --git a/autoload/ale/code_action.vim b/autoload/ale/code_action.vim
index 917190be..65db6246 100644
--- a/autoload/ale/code_action.vim
+++ b/autoload/ale/code_action.vim
@@ -16,13 +16,12 @@ endfunction
function! ale#code_action#HandleCodeAction(code_action, options) abort
let l:current_buffer = bufnr('')
let l:changes = a:code_action.changes
- let l:should_save = get(a:options, 'should_save')
for l:file_code_edit in l:changes
call ale#code_action#ApplyChanges(
\ l:file_code_edit.fileName,
\ l:file_code_edit.textChanges,
- \ l:should_save,
+ \ a:options,
\)
endfor
endfunction
@@ -63,29 +62,29 @@ function! s:ChangeCmp(left, right) abort
return 0
endfunction
-function! ale#code_action#ApplyChanges(filename, changes, should_save) abort
- let l:current_buffer = bufnr('')
+function! ale#code_action#ApplyChanges(filename, changes, options) abort
+ let l:should_save = get(a:options, 'should_save')
+ let l:conn_id = get(a:options, 'conn_id')
+
+ let l:orig_buffer = bufnr('')
+
" The buffer is used to determine the fileformat, if available.
let l:buffer = bufnr(a:filename)
- let l:is_current_buffer = l:buffer > 0 && l:buffer == l:current_buffer
- if l:buffer > 0
- let l:lines = getbufline(l:buffer, 1, '$')
-
- " Add empty line if there's trailing newline, like readfile() does.
- if getbufvar(l:buffer, '&eol')
- let l:lines += ['']
- endif
- else
- let l:lines = readfile(a:filename, 'b')
+ if l:buffer != l:orig_buffer
+ call ale#util#Execute('silent edit ' . a:filename)
+ let l:buffer = bufnr('')
endif
- if l:is_current_buffer
- let l:pos = getpos('.')[1:2]
- else
- let l:pos = [1, 1]
+ let l:lines = getbufline(l:buffer, 1, '$')
+
+ " Add empty line if there's trailing newline, like readfile() does.
+ if getbufvar(l:buffer, '&eol')
+ let l:lines += ['']
endif
+ let l:pos = getpos('.')[1:2]
+
" Changes have to be sorted so we apply them from bottom-to-top
for l:code_edit in reverse(sort(copy(a:changes), function('s:ChangeCmp')))
let l:line = l:code_edit.start.line
@@ -155,46 +154,25 @@ function! ale#code_action#ApplyChanges(filename, changes, should_save) abort
endif
endfor
- if l:buffer > 0
- " Make sure ale#util#{Writefile,SetBufferContents} add trailing
- " newline if and only if it should be added.
- if l:lines[-1] is# '' && getbufvar(l:buffer, '&eol')
- call remove(l:lines, -1)
- else
- call setbufvar(l:buffer, '&eol', 0)
- endif
- elseif exists('+fixeol') && &fixeol && l:lines[-1] is# ''
- " Not in buffer, ale#util#Writefile can't check &eol and always adds
- " newline if &fixeol: remove to prevent double trailing newline.
+ " Make sure to add a trailing newline if and only if it should be added.
+ if l:lines[-1] is# '' && getbufvar(l:buffer, '&eol')
call remove(l:lines, -1)
- endif
-
- if a:should_save || l:buffer < 0
- call ale#util#Writefile(l:buffer, l:lines, a:filename)
else
- call ale#util#SetBufferContents(l:buffer, l:lines)
+ call setbufvar(l:buffer, '&eol', 0)
endif
- if l:is_current_buffer
- if a:should_save
- call ale#util#Execute(':e!')
- endif
+ call ale#util#SetBufferContents(l:buffer, l:lines)
- call setpos('.', [0, l:pos[0], l:pos[1], 0])
+ call ale#lsp#NotifyForChanges(l:conn_id, l:buffer)
+
+ if l:should_save
+ call ale#util#Execute('silent w!')
endif
- if a:should_save && l:buffer > 0 && !l:is_current_buffer
- " Set up a one-time use event that will delete itself to reload the
- " buffer next time it's entered to view the changes made to it.
- execute 'augroup ALECodeActionReloadGroup' . l:buffer
- autocmd!
-
- execute printf(
- \ 'autocmd BufEnter <buffer=%d>'
- \ . ' call ale#code_action#ReloadBuffer()',
- \ l:buffer
- \)
- augroup END
+ call setpos('.', [0, l:pos[0], l:pos[1], 0])
+
+ if l:orig_buffer != l:buffer && bufexists(l:orig_buffer)
+ call ale#util#Execute('silent buf ' . string(l:orig_buffer))
endif
endfunction