diff options
-rw-r--r-- | autoload/ale/code_action.vim | 2 | ||||
-rw-r--r-- | autoload/ale/fix.vim | 39 | ||||
-rw-r--r-- | autoload/ale/util.vim | 37 |
3 files changed, 39 insertions, 39 deletions
diff --git a/autoload/ale/code_action.vim b/autoload/ale/code_action.vim index 466c023a..60c3bbef 100644 --- a/autoload/ale/code_action.vim +++ b/autoload/ale/code_action.vim @@ -112,7 +112,7 @@ function! ale#code_action#ApplyChanges(filename, changes, should_save) abort if a:should_save call ale#util#Writefile(l:buffer, l:lines, a:filename) else - call ale#fix#SetBufferContents(l:buffer, l:lines) + call ale#util#SetBufferContents(l:buffer, l:lines) endif if l:is_current_buffer diff --git a/autoload/ale/fix.vim b/autoload/ale/fix.vim index 81f5a6d6..d2c1cb98 100644 --- a/autoload/ale/fix.vim +++ b/autoload/ale/fix.vim @@ -12,7 +12,7 @@ function! ale#fix#ApplyQueuedFixes(buffer) abort call remove(g:ale_fix_buffer_data, a:buffer) if l:data.changes_made - let l:new_lines = ale#fix#SetBufferContents(a:buffer, l:data.output) + let l:new_lines = ale#util#SetBufferContents(a:buffer, l:data.output) if l:data.should_save if a:buffer is bufnr('') @@ -46,43 +46,6 @@ function! ale#fix#ApplyQueuedFixes(buffer) abort endif endfunction -" Sets buffer contents to lines -function! ale#fix#SetBufferContents(buffer, lines) abort - let l:has_bufline_api = exists('*deletebufline') && exists('*setbufline') - - if !l:has_bufline_api && a:buffer isnot bufnr('') - return - endif - - " If the file is in DOS mode, we have to remove carriage returns from - " the ends of lines before calling setline(), or we will see them - " twice. - let l:new_lines = getbufvar(a:buffer, '&fileformat') is# 'dos' - \ ? map(copy(a:lines), 'substitute(v:val, ''\r\+$'', '''', '''')') - \ : a:lines - let l:first_line_to_remove = len(l:new_lines) + 1 - - " Use a Vim API for setting lines in other buffers, if available. - if l:has_bufline_api - call setbufline(a:buffer, 1, l:new_lines) - call deletebufline(a:buffer, l:first_line_to_remove, '$') - " Fall back on setting lines the old way, for the current buffer. - else - let l:old_line_length = line('$') - - if l:old_line_length >= l:first_line_to_remove - let l:save = winsaveview() - silent execute - \ l:first_line_to_remove . ',' . l:old_line_length . 'd_' - call winrestview(l:save) - endif - - call setline(1, l:new_lines) - endif - - return l:new_lines -endfunction - function! ale#fix#ApplyFixes(buffer, output) abort let l:data = g:ale_fix_buffer_data[a:buffer] let l:data.output = a:output diff --git a/autoload/ale/util.vim b/autoload/ale/util.vim index 99cd856a..b781971c 100644 --- a/autoload/ale/util.vim +++ b/autoload/ale/util.vim @@ -480,3 +480,40 @@ endfunction function! ale#util#Input(message, value) abort return input(a:message, a:value) endfunction + +" Sets buffer contents to lines +function! ale#util#SetBufferContents(buffer, lines) abort + let l:has_bufline_api = exists('*deletebufline') && exists('*setbufline') + + if !l:has_bufline_api && a:buffer isnot bufnr('') + return + endif + + " If the file is in DOS mode, we have to remove carriage returns from + " the ends of lines before calling setline(), or we will see them + " twice. + let l:new_lines = getbufvar(a:buffer, '&fileformat') is# 'dos' + \ ? map(copy(a:lines), 'substitute(v:val, ''\r\+$'', '''', '''')') + \ : a:lines + let l:first_line_to_remove = len(l:new_lines) + 1 + + " Use a Vim API for setting lines in other buffers, if available. + if l:has_bufline_api + call setbufline(a:buffer, 1, l:new_lines) + call deletebufline(a:buffer, l:first_line_to_remove, '$') + " Fall back on setting lines the old way, for the current buffer. + else + let l:old_line_length = line('$') + + if l:old_line_length >= l:first_line_to_remove + let l:save = winsaveview() + silent execute + \ l:first_line_to_remove . ',' . l:old_line_length . 'd_' + call winrestview(l:save) + endif + + call setline(1, l:new_lines) + endif + + return l:new_lines +endfunction |