From 15d590ee5e6779b6dad2640cdb55abc9d357bbe9 Mon Sep 17 00:00:00 2001 From: "James C. Davis" Date: Thu, 30 Apr 2020 03:43:17 -0400 Subject: [PATCH] fix: don't append newline when buffer is noeol and nofixeol --- autoload/ale/util.vim | 5 +++- test/test_writefile_function.vader | 46 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/autoload/ale/util.vim b/autoload/ale/util.vim index ee62af28..1f716a13 100644 --- a/autoload/ale/util.vim +++ b/autoload/ale/util.vim @@ -418,7 +418,10 @@ function! ale#util#Writefile(buffer, lines, filename) abort \ ? map(copy(a:lines), 'substitute(v:val, ''\r*$'', ''\r'', '''')') \ : a:lines - call writefile(l:corrected_lines, a:filename, 'S') " no-custom-checks + " Set binary flag if buffer doesn't have eol and nofixeol to avoid appending newline + let l:flags = !getbufvar(a:buffer, '&eol') && exists('+fixeol') && !&fixeol ? 'bS' : 'S' + + call writefile(l:corrected_lines, a:filename, l:flags) " no-custom-checks endfunction if !exists('s:patial_timers') diff --git a/test/test_writefile_function.vader b/test/test_writefile_function.vader index 811d59e8..53a88331 100644 --- a/test/test_writefile_function.vader +++ b/test/test_writefile_function.vader @@ -69,3 +69,49 @@ Execute(Unix file lines should be written as normal): AssertEqual \ ['first', 'second', 'third', ''], \ readfile(g:new_line_test_file, 'b') + +Execute(Newline at end of file should be preserved even when nofixeol): + call ale#test#SetFilename(g:new_line_test_file) + + setlocal buftype= + noautocmd :w + noautocmd :e! ++ff=unix + set eol + set nofixeol + + call ale#util#Writefile(bufnr(''), getline(1, '$'), g:new_line_test_file) + + AssertEqual + \ ['first', 'second', 'third', ''], + \ readfile(g:new_line_test_file, 'b') + +Execute(Newline should not be appended on write when noeol and nofixeol): + call ale#test#SetFilename(g:new_line_test_file) + + setlocal buftype= + noautocmd :w + noautocmd :e! ++ff=unix + set noeol + set nofixeol + + call ale#util#Writefile(bufnr(''), getline(1, '$'), g:new_line_test_file) + + AssertEqual + \ ['first', 'second', 'third'], + \ readfile(g:new_line_test_file, 'b') + +Execute(Newline should be appended on write when noeol and fixeol): + call ale#test#SetFilename(g:new_line_test_file) + + setlocal buftype= + noautocmd :w + noautocmd :e! ++ff=unix + set noeol + set fixeol + + call ale#util#Writefile(bufnr(''), getline(1, '$'), g:new_line_test_file) + + AssertEqual + \ ['first', 'second', 'third', ''], + \ readfile(g:new_line_test_file, 'b') +