#3332 Implement :ALERename! for ignoring errors

:ALERename! now ignores errors for files that cannot be modified, and
modifies all other files.
This commit is contained in:
w0rp
2020-10-15 21:56:21 +01:00
parent f384d61c3d
commit 477eb89793
11 changed files with 122 additions and 79 deletions

View File

@@ -1,26 +1,33 @@
" Author: Jerko Steiner <jerko.steiner@gmail.com>
" Description: Code action support for LSP / tsserver
function! ale#code_action#HandleCodeAction(code_action, should_save) abort
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')
let l:force_save = get(a:options, 'force_save')
let l:safe_changes = []
for l:file_code_edit in l:changes
let l:buf = bufnr(l:file_code_edit.fileName)
if l:buf != -1 && l:buf != l:current_buffer && getbufvar(l:buf, '&mod')
call ale#util#Execute('echom ''Aborting action, file is unsaved''')
if !l:force_save
call ale#util#Execute('echom ''Aborting action, file is unsaved''')
return
return
endif
else
call add(l:safe_changes, l:file_code_edit)
endif
endfor
for l:file_code_edit in l:changes
for l:file_code_edit in l:safe_changes
call ale#code_action#ApplyChanges(
\ l:file_code_edit.fileName,
\ l:file_code_edit.textChanges,
\ a:should_save,
\ )
\ l:file_code_edit.fileName,
\ l:file_code_edit.textChanges,
\ l:should_save,
\)
endfor
endfunction

View File

@@ -1006,7 +1006,7 @@ function! ale#completion#HandleUserData(completed_item) abort
\|| l:source is# 'ale-import'
\|| l:source is# 'ale-omnifunc'
for l:code_action in get(l:user_data, 'code_actions', [])
call ale#code_action#HandleCodeAction(l:code_action, v:false)
call ale#code_action#HandleCodeAction(l:code_action, {})
endfor
endif

View File

@@ -12,10 +12,13 @@ function! ale#organize_imports#HandleTSServerResponse(conn_id, response) abort
let l:file_code_edits = a:response.body
call ale#code_action#HandleCodeAction({
\ 'description': 'Organize Imports',
\ 'changes': l:file_code_edits,
\}, v:false)
call ale#code_action#HandleCodeAction(
\ {
\ 'description': 'Organize Imports',
\ 'changes': l:file_code_edits,
\ },
\ {}
\)
endfunction
function! s:OnReady(linter, lsp_details) abort

View File

@@ -33,9 +33,10 @@ function! ale#rename#HandleTSServerResponse(conn_id, response) abort
return
endif
let l:old_name = s:rename_map[a:response.request_seq].old_name
let l:new_name = s:rename_map[a:response.request_seq].new_name
call remove(s:rename_map, a:response.request_seq)
let l:options = remove(s:rename_map, a:response.request_seq)
let l:old_name = l:options.old_name
let l:new_name = l:options.new_name
if get(a:response, 'success', v:false) is v:false
let l:message = get(a:response, 'message', 'unknown')
@@ -77,10 +78,16 @@ function! ale#rename#HandleTSServerResponse(conn_id, response) abort
return
endif
call ale#code_action#HandleCodeAction({
\ 'description': 'rename',
\ 'changes': l:changes,
\}, v:true)
call ale#code_action#HandleCodeAction(
\ {
\ 'description': 'rename',
\ 'changes': l:changes,
\ },
\ {
\ 'should_save': 1,
\ 'force_save': get(l:options, 'force_save'),
\ },
\)
endfunction
function! s:getChanges(workspace_edit) abort
@@ -111,7 +118,7 @@ endfunction
function! ale#rename#HandleLSPResponse(conn_id, response) abort
if has_key(a:response, 'id')
\&& has_key(s:rename_map, a:response.id)
call remove(s:rename_map, a:response.id)
let l:options = remove(s:rename_map, a:response.id)
if !has_key(a:response, 'result')
call s:message('No rename result received from server')
@@ -156,14 +163,20 @@ function! ale#rename#HandleLSPResponse(conn_id, response) abort
\})
endfor
call ale#code_action#HandleCodeAction({
\ 'description': 'rename',
\ 'changes': l:changes,
\}, v:true)
call ale#code_action#HandleCodeAction(
\ {
\ 'description': 'rename',
\ 'changes': l:changes,
\ },
\ {
\ 'should_save': 1,
\ 'force_save': get(l:options, 'force_save'),
\ },
\)
endif
endfunction
function! s:OnReady(line, column, old_name, new_name, linter, lsp_details) abort
function! s:OnReady(line, column, options, linter, lsp_details) abort
let l:id = a:lsp_details.connection_id
if !ale#lsp#HasCapability(l:id, 'rename')
@@ -195,19 +208,16 @@ function! s:OnReady(line, column, old_name, new_name, linter, lsp_details) abort
\ l:buffer,
\ a:line,
\ a:column,
\ a:new_name
\ a:options.new_name
\)
endif
let l:request_id = ale#lsp#Send(l:id, l:message)
let s:rename_map[l:request_id] = {
\ 'new_name': a:new_name,
\ 'old_name': a:old_name,
\}
let s:rename_map[l:request_id] = a:options
endfunction
function! s:ExecuteRename(linter, old_name, new_name) abort
function! s:ExecuteRename(linter, options) abort
let l:buffer = bufnr('')
let [l:line, l:column] = getpos('.')[1:2]
@@ -215,12 +225,11 @@ function! s:ExecuteRename(linter, old_name, new_name) abort
let l:column = min([l:column, len(getline(l:line))])
endif
let l:Callback = function(
\ 's:OnReady', [l:line, l:column, a:old_name, a:new_name])
let l:Callback = function('s:OnReady', [l:line, l:column, a:options])
call ale#lsp_linter#StartLSP(l:buffer, a:linter, l:Callback)
endfunction
function! ale#rename#Execute() abort
function! ale#rename#Execute(options) abort
let l:lsp_linters = []
for l:linter in ale#linter#Get(&filetype)
@@ -245,6 +254,10 @@ function! ale#rename#Execute() abort
endif
for l:lsp_linter in l:lsp_linters
call s:ExecuteRename(l:lsp_linter, l:old_name, l:new_name)
call s:ExecuteRename(l:lsp_linter, {
\ 'old_name': l:old_name,
\ 'new_name': l:new_name,
\ 'force_save': get(a:options, 'force_save') is 1,
\})
endfor
endfunction