Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ALERename: Fix an issue with unlisted buffers #3782

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions autoload/ale/code_action.vim
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ 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)
let l:is_current_buffer = l:buffer > 0 && l:buffer == l:current_buffer
" Buffer might be unlisted after calling :bd on it, we want to behave
" as if these buffers don't exist because they cannot be read from or
" modified anymore..
let l:is_opened = l:buffer > 0 && buflisted(l:buffer)
let l:is_current_buffer = l:is_opened && l:buffer == l:current_buffer

if l:buffer > 0
if l:is_opened
let l:lines = getbufline(l:buffer, 1, '$')

" Add empty line if there's trailing newline, like readfile() does.
Expand Down Expand Up @@ -155,7 +159,7 @@ function! ale#code_action#ApplyChanges(filename, changes, should_save) abort
endif
endfor

if l:buffer > 0
if l:is_opened
" 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')
Expand Down Expand Up @@ -183,7 +187,7 @@ 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
if a:should_save && l:is_opened && !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
Expand Down
43 changes: 41 additions & 2 deletions test/test_code_action.vader
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ Before:

After:
" Close the extra buffers if we opened it.
if bufnr(g:file1) != -1
if bufnr(g:file1) != -1 && buflisted(bufnr(g:file1))
execute ':bp! | :bd! ' . bufnr(g:file1)
endif
if bufnr(g:file2) != -1
if bufnr(g:file2) != -1 && buflisted(bufnr(g:file2))
execute ':bp! | :bd! ' . bufnr(g:file2)
endif

Expand Down Expand Up @@ -252,6 +252,45 @@ Execute(Current buffer contents will be reloaded):
\] + g:test.text, getbufline(g:test.buffer, 1, '$')


Execute(Unlisted buffer contents will be modified correctly):
let g:test.text = [
\ 'class Name {',
\ ' value: string',
\ '}',
\]
call writefile(g:test.text, g:file1, 'S')

execute 'edit ' . g:file1
let g:test.buffer = bufnr(g:file1)

execute 'bd'
AssertEqual bufnr(g:file1), g:test.buffer

call ale#code_action#HandleCodeAction(
\ {
\ 'changes': [{
\ 'fileName': g:file1,
\ 'textChanges': [{
\ 'start': {
\ 'line': 1,
\ 'offset': 1,
\ },
\ 'end': {
\ 'line': 1,
\ 'offset': 1,
\ },
\ 'newText': "type A: string\ntype B: number\n",
\ }],
\ }]
\ },
\ {'should_save': 1},
\)

AssertEqual [
\ 'type A: string',
\ 'type B: number',
\] + g:test.text + [''], readfile(g:file1, 'b')

# Tests for cursor repositioning. In comments `=` designates change range, and
# `C` cursor position

Expand Down