summaryrefslogtreecommitdiff
path: root/autoload
diff options
context:
space:
mode:
Diffstat (limited to 'autoload')
-rw-r--r--autoload/ale/code_action.vim20
-rw-r--r--autoload/ale/completion.vim2
-rw-r--r--autoload/ale/fix.vim29
-rw-r--r--autoload/ale/organize_imports.vim2
-rw-r--r--autoload/ale/rename.vim4
-rw-r--r--autoload/ale/util.vim41
6 files changed, 62 insertions, 36 deletions
diff --git a/autoload/ale/code_action.vim b/autoload/ale/code_action.vim
index 0af1bb70..60c3bbef 100644
--- a/autoload/ale/code_action.vim
+++ b/autoload/ale/code_action.vim
@@ -1,7 +1,7 @@
" Author: Jerko Steiner <jerko.steiner@gmail.com>
" Description: Code action support for LSP / tsserver
-function! ale#code_action#HandleCodeAction(code_action) abort
+function! ale#code_action#HandleCodeAction(code_action, should_save) abort
let l:current_buffer = bufnr('')
let l:changes = a:code_action.changes
@@ -17,11 +17,14 @@ function! ale#code_action#HandleCodeAction(code_action) abort
for l:file_code_edit in l:changes
call ale#code_action#ApplyChanges(
- \ l:file_code_edit.fileName, l:file_code_edit.textChanges)
+ \ l:file_code_edit.fileName,
+ \ l:file_code_edit.textChanges,
+ \ a:should_save,
+ \ )
endfor
endfunction
-function! ale#code_action#ApplyChanges(filename, changes) abort
+function! ale#code_action#ApplyChanges(filename, changes, should_save) abort
let l:current_buffer = bufnr('')
" The buffer is used to determine the fileformat, if available.
let l:buffer = bufnr(a:filename)
@@ -106,10 +109,17 @@ function! ale#code_action#ApplyChanges(filename, changes) abort
call remove(l:lines, -1)
endif
- call ale#util#Writefile(l:buffer, l:lines, a:filename)
+ if a:should_save
+ call ale#util#Writefile(l:buffer, l:lines, a:filename)
+ else
+ call ale#util#SetBufferContents(l:buffer, l:lines)
+ endif
if l:is_current_buffer
- call ale#util#Execute(':e!')
+ if a:should_save
+ call ale#util#Execute(':e!')
+ endif
+
call setpos('.', [0, l:pos[0], l:pos[1], 0])
endif
endfunction
diff --git a/autoload/ale/completion.vim b/autoload/ale/completion.vim
index 80684a30..2b5756e4 100644
--- a/autoload/ale/completion.vim
+++ b/autoload/ale/completion.vim
@@ -823,7 +823,7 @@ function! ale#completion#HandleUserData(completed_item) abort
endif
for l:code_action in get(l:user_data, 'codeActions', [])
- call ale#code_action#HandleCodeAction(l:code_action)
+ call ale#code_action#HandleCodeAction(l:code_action, v:false)
endfor
endfunction
diff --git a/autoload/ale/fix.vim b/autoload/ale/fix.vim
index dad9e2bc..69817b36 100644
--- a/autoload/ale/fix.vim
+++ b/autoload/ale/fix.vim
@@ -4,40 +4,15 @@ call ale#Set('fix_on_save_ignore', {})
" Vim doesn't let you modify hidden buffers.
function! ale#fix#ApplyQueuedFixes(buffer) abort
let l:data = get(g:ale_fix_buffer_data, a:buffer, {'done': 0})
- let l:has_bufline_api = exists('*deletebufline') && exists('*setbufline')
- if !l:data.done || (!l:has_bufline_api && a:buffer isnot bufnr(''))
+ if !l:data.done || (!ale#util#HasBuflineApi() && a:buffer isnot bufnr(''))
return
endif
call remove(g:ale_fix_buffer_data, a:buffer)
if l:data.changes_made
- " 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(l:data.output), 'substitute(v:val, ''\r\+$'', '''', '''')')
- \ : l:data.output
- 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 = len(l:data.lines_before)
-
- 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
+ let l:new_lines = ale#util#SetBufferContents(a:buffer, l:data.output)
if l:data.should_save
if a:buffer is bufnr('')
diff --git a/autoload/ale/organize_imports.vim b/autoload/ale/organize_imports.vim
index bc9b829e..e89c832c 100644
--- a/autoload/ale/organize_imports.vim
+++ b/autoload/ale/organize_imports.vim
@@ -15,7 +15,7 @@ function! ale#organize_imports#HandleTSServerResponse(conn_id, response) abort
call ale#code_action#HandleCodeAction({
\ 'description': 'Organize Imports',
\ 'changes': l:file_code_edits,
- \})
+ \}, v:false)
endfunction
function! s:OnReady(linter, lsp_details) abort
diff --git a/autoload/ale/rename.vim b/autoload/ale/rename.vim
index 02b7b579..fbd6c2ad 100644
--- a/autoload/ale/rename.vim
+++ b/autoload/ale/rename.vim
@@ -80,7 +80,7 @@ function! ale#rename#HandleTSServerResponse(conn_id, response) abort
call ale#code_action#HandleCodeAction({
\ 'description': 'rename',
\ 'changes': l:changes,
- \})
+ \}, v:true)
endfunction
function! ale#rename#HandleLSPResponse(conn_id, response) abort
@@ -134,7 +134,7 @@ function! ale#rename#HandleLSPResponse(conn_id, response) abort
call ale#code_action#HandleCodeAction({
\ 'description': 'rename',
\ 'changes': l:changes,
- \})
+ \}, v:true)
endif
endfunction
diff --git a/autoload/ale/util.vim b/autoload/ale/util.vim
index 3ca9ea08..ee62af28 100644
--- a/autoload/ale/util.vim
+++ b/autoload/ale/util.vim
@@ -476,3 +476,44 @@ endfunction
function! ale#util#Input(message, value) abort
return input(a:message, a:value)
endfunction
+
+function! ale#util#HasBuflineApi() abort
+ return exists('*deletebufline') && exists('*setbufline')
+endfunction
+
+" Sets buffer contents to lines
+function! ale#util#SetBufferContents(buffer, lines) abort
+ let l:has_bufline_api = ale#util#HasBuflineApi()
+
+ 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