-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
code_action: Fix EOL at EOF corner cases while performing no changes
Instead of always removing the trailing empty line and hoping it wasn't supposed to be there, only remove it when we know ale#util#Writefile will add a trailing newline, and additionally tell ale#util#Writefile to not add that newline if any change explicitly deleted that newline. (Unfortunately with a:should_save=0, adding a trailing newline that wasn't there results in noeol and empty line at end of buffer, because for some reason setbufvar(l:buffer, '&eol', 1) doesn't work in buffers that started as noeol. This is likely a bug in vim itself.) Includes a minor fix that prevents invoking ale#util#SetBufferContents without a buffer. Not sure whether that might really happen anywhere. (test_code_action_python.vader depends on two incorrect behaviours cancelling each other, so it's temporarily adjusted to pass)
- Loading branch information
Showing
4 changed files
with
94 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
Before: | ||
Save &fixeol | ||
set nofixeol | ||
|
||
Save &fileformats | ||
set fileformats=unix | ||
|
||
" two files, one accessed through a buffer, the other using write/readfile only | ||
let g:files = [tempname(), tempname()] | ||
|
||
function! TestChanges(contents, changes, mode) abort | ||
let l:file = g:files[a:mode is 'file' ? 0 : 1] | ||
call writefile(split(a:contents, '\n', 1), l:file, 'bS') | ||
if a:mode isnot 'file' | ||
execute 'edit ' . l:file | ||
endif | ||
call ale#code_action#ApplyChanges(l:file, a:changes, a:mode isnot 'buffer') | ||
if a:mode is 'buffer' | ||
execute 'write ' . l:file | ||
endif | ||
return join(readfile(l:file, 'b'), "\n") | ||
endfunction! | ||
|
||
function! MkPos(line, offset) abort | ||
return {'line': a:line, 'offset': a:offset} | ||
endfunction! | ||
|
||
function! MkDelete(start, end) abort | ||
return {'start': a:start, 'end': a:end, 'newText': ''} | ||
endfunction! | ||
|
||
After: | ||
for g:file in g:files | ||
if bufnr(g:file) != -1 | ||
execute ':bp! | :bd! ' . bufnr(g:file) | ||
endif | ||
if filereadable(g:file) | ||
call delete(g:file) | ||
endif | ||
endfor | ||
unlet! g:files g:file | ||
|
||
unlet! g:mode | ||
|
||
delfunction TestChanges | ||
delfunction MkPos | ||
delfunction MkDelete | ||
|
||
Restore | ||
|
||
Execute(Preserve (no)eol at eof): | ||
for g:mode in ['save', 'file', 'buffer'] | ||
Log g:mode | ||
AssertEqual "noeol", TestChanges("noeol", [], g:mode) | ||
AssertEqual "eol\n", TestChanges("eol\n", [], g:mode) | ||
AssertEqual "eols\n\n", TestChanges("eols\n\n", [], g:mode) | ||
endfor | ||
|
||
" there doesn't seem to be a way to tell if a buffer is empty or contains one | ||
" empty line :-( | ||
AssertEqual "", TestChanges("", [], 'file') | ||
|
||
Execute(Respect fixeol): | ||
set fixeol | ||
for g:mode in ['save', 'file', 'buffer'] | ||
Log g:mode | ||
AssertEqual "noeol\n", TestChanges("noeol", [], g:mode) | ||
AssertEqual "eol\n", TestChanges("eol\n", [], g:mode) | ||
endfor | ||
|
||
Execute(Del eol at eof): | ||
for g:mode in ['save', 'file', 'buffer'] | ||
Log g:mode | ||
AssertEqual "deleol", TestChanges("deleolx\n", [MkDelete(MkPos(1, 7), MkPos(2, 1))], g:mode) | ||
endfor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters