summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorw0rp <w0rp@users.noreply.github.com>2020-08-14 00:32:28 +0100
committerGitHub <noreply@github.com>2020-08-14 00:32:28 +0100
commit2b2403a20d59ea4207be01ca308ec43a90e9453f (patch)
tree0be0f3936739e09a772d9ab5c89de97669e2460e
parent8151e3e8facfc0474ac18177fc1a3047326788d5 (diff)
parent15d590ee5e6779b6dad2640cdb55abc9d357bbe9 (diff)
downloadale-2b2403a20d59ea4207be01ca308ec43a90e9453f.zip
Merge pull request #3144 from jamescdavis/dont_append_newline_when_noeol
don't append a newline to temp file when buffer is noeol and nofixeol is set
-rw-r--r--autoload/ale/util.vim5
-rw-r--r--test/test_writefile_function.vader46
2 files changed, 50 insertions, 1 deletions
diff --git a/autoload/ale/util.vim b/autoload/ale/util.vim
index bb9c1961..1f396377 100644
--- a/autoload/ale/util.vim
+++ b/autoload/ale/util.vim
@@ -423,7 +423,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')
+