From 06e7f2195ef6375be32a63f98b5e46070708a315 Mon Sep 17 00:00:00 2001 From: w0rp Date: Sat, 21 Nov 2020 19:00:53 +0000 Subject: [PATCH] Fix #3332 - Modify everything for rename/actions ALE now just modifies every open buffer for rename and actions, and sets up a one-time use BufEnter event to reload buffers that are changed so you don't have to think about what to do with changed buffers. --- autoload/ale/code_action.vim | 42 ++++++++++++++++++++++-------------- autoload/ale/rename.vim | 5 +---- doc/ale.txt | 6 ------ plugin/ale.vim | 2 +- test/test_rename.vader | 6 +++--- 5 files changed, 31 insertions(+), 30 deletions(-) diff --git a/autoload/ale/code_action.vim b/autoload/ale/code_action.vim index 6b808b34..69d40933 100644 --- a/autoload/ale/code_action.vim +++ b/autoload/ale/code_action.vim @@ -1,28 +1,24 @@ " Author: Jerko Steiner " Description: Code action support for LSP / tsserver +function! ale#code_action#ReloadBuffer() abort + let l:buffer = bufnr('') + + execute 'augroup ALECodeActionReloadGroup' . l:buffer + autocmd! + augroup END + + silent! execute 'augroup! ALECodeActionReloadGroup' . l:buffer + + call ale#util#Execute(':e!') +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') - 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') - if !l:force_save - call ale#util#Execute('echom ''Aborting action, file is unsaved''') - - return - endif - else - call add(l:safe_changes, l:file_code_edit) - endif - endfor - - for l:file_code_edit in l:safe_changes call ale#code_action#ApplyChanges( \ l:file_code_edit.fileName, \ l:file_code_edit.textChanges, @@ -161,6 +157,20 @@ function! ale#code_action#ApplyChanges(filename, changes, should_save) abort call setpos('.', [0, l:pos[0], l:pos[1], 0]) 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 ' + \ . ' call ale#code_action#ReloadBuffer()', + \ l:buffer + \) + augroup END + endif endfunction function! s:UpdateCursor(cursor, start, end, offset) abort diff --git a/autoload/ale/rename.vim b/autoload/ale/rename.vim index 0d074c24..9030618e 100644 --- a/autoload/ale/rename.vim +++ b/autoload/ale/rename.vim @@ -85,7 +85,6 @@ function! ale#rename#HandleTSServerResponse(conn_id, response) abort \ }, \ { \ 'should_save': 1, - \ 'force_save': get(l:options, 'force_save'), \ }, \) endfunction @@ -118,7 +117,6 @@ function! ale#rename#HandleLSPResponse(conn_id, response) abort \ }, \ { \ 'should_save': 1, - \ 'force_save': get(l:options, 'force_save'), \ }, \) endif @@ -177,7 +175,7 @@ function! s:ExecuteRename(linter, options) abort call ale#lsp_linter#StartLSP(l:buffer, a:linter, l:Callback) endfunction -function! ale#rename#Execute(options) abort +function! ale#rename#Execute() abort let l:lsp_linters = [] for l:linter in ale#linter#Get(&filetype) @@ -205,7 +203,6 @@ function! ale#rename#Execute(options) abort 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 diff --git a/doc/ale.txt b/doc/ale.txt index 71caf0e5..a64f86f0 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -3145,12 +3145,6 @@ ALERename *ALERename* The symbol where the cursor is resting will be the symbol renamed, and a prompt will open to request a new name. - ALE will refuse to complete a rename operation if there are files to modify - which have not yet been saved in Vim. If the command is run with a bang - (`:ALERename!`), all warnings will be suppressed, and files that are still - open in Vim and not saved will be ignored and left in a state where symbols - in those files will not be updated. - ALECodeAction *ALECodeAction* diff --git a/plugin/ale.vim b/plugin/ale.vim index 2398956e..1fde9df1 100644 --- a/plugin/ale.vim +++ b/plugin/ale.vim @@ -245,7 +245,7 @@ command! -bar ALEComplete :call ale#completion#GetCompletions('ale-manual') command! -bar ALEImport :call ale#completion#Import() " Rename symbols using tsserver and LSP -command! -bar -bang ALERename :call ale#rename#Execute({'force_save': '' is# '!'}) +command! -bar -bang ALERename :call ale#rename#Execute() " Apply code actions to a range. command! -bar -range ALECodeAction :call ale#codefix#Execute() diff --git a/test/test_rename.vader b/test/test_rename.vader index 2e8b746e..5bc655f4 100644 --- a/test/test_rename.vader +++ b/test/test_rename.vader @@ -269,7 +269,7 @@ Execute(tsserver rename requests should be sent): \ }] \ ], \ g:message_list - AssertEqual {'42': {'old_name': 'somelongerline', 'new_name': 'a-new-name', 'force_save': 0}}, + AssertEqual {'42': {'old_name': 'somelongerline', 'new_name': 'a-new-name'}}, \ ale#rename#GetMap() Given python(Some Python file): @@ -470,7 +470,7 @@ Execute(LSP rename requests should be sent): let b:ale_linters = ['pyls'] call setpos('.', [bufnr(''), 1, 5, 0]) - ALERename! + ALERename " We shouldn't register the callback yet. AssertEqual '''''', string(g:Callback) @@ -500,5 +500,5 @@ Execute(LSP rename requests should be sent): \ ], \ g:message_list - AssertEqual {'42': {'old_name': 'foo', 'new_name': 'a-new-name', 'force_save': 1}}, + AssertEqual {'42': {'old_name': 'foo', 'new_name': 'a-new-name'}}, \ ale#rename#GetMap()