summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ale_linters/verilog/verilator.vim3
-rw-r--r--autoload/ale/engine.vim3
-rw-r--r--autoload/ale/fix.vim2
-rw-r--r--autoload/ale/util.vim12
-rw-r--r--test/test_writefile_function.vader48
5 files changed, 65 insertions, 3 deletions
diff --git a/ale_linters/verilog/verilator.vim b/ale_linters/verilog/verilator.vim
index aa5e7047..9a0067e8 100644
--- a/ale_linters/verilog/verilator.vim
+++ b/ale_linters/verilog/verilator.vim
@@ -11,7 +11,8 @@ function! ale_linters#verilog#verilator#GetCommand(buffer) abort
" Create a special filename, so we can detect it in the handler.
call ale#engine#ManageFile(a:buffer, l:filename)
- call writefile(getbufline(a:buffer, 1, '$'), l:filename)
+ let l:lines = getbufline(a:buffer, 1, '$')
+ call ale#util#Writefile(a:buffer, l:lines, l:filename)
return 'verilator --lint-only -Wall -Wno-DECLFILENAME '
\ . ale#Var(a:buffer, 'verilog_verilator_options') .' '
diff --git a/autoload/ale/engine.vim b/autoload/ale/engine.vim
index a5a5f524..9b90e953 100644
--- a/autoload/ale/engine.vim
+++ b/autoload/ale/engine.vim
@@ -417,7 +417,8 @@ function! s:CreateTemporaryFileForJob(buffer, temporary_file) abort
" Automatically delete the directory later.
call ale#engine#ManageDirectory(a:buffer, l:temporary_directory)
" Write the buffer out to a file.
- call writefile(getbufline(a:buffer, 1, '$'), a:temporary_file)
+ let l:lines = getbufline(a:buffer, 1, '$')
+ call ale#util#Writefile(a:buffer, l:lines, a:temporary_file)
return 1
endfunction
diff --git a/autoload/ale/fix.vim b/autoload/ale/fix.vim
index 12cbea4d..73992125 100644
--- a/autoload/ale/fix.vim
+++ b/autoload/ale/fix.vim
@@ -154,7 +154,7 @@ function! s:CreateTemporaryFileForJob(buffer, temporary_file, input) abort
" Automatically delete the directory later.
call ale#fix#ManageDirectory(a:buffer, l:temporary_directory)
" Write the buffer out to a file.
- call writefile(a:input, a:temporary_file)
+ call ale#util#Writefile(a:buffer, a:input, a:temporary_file)
return 1
endfunction
diff --git a/autoload/ale/util.vim b/autoload/ale/util.vim
index f3146151..312f8f0f 100644
--- a/autoload/ale/util.vim
+++ b/autoload/ale/util.vim
@@ -187,3 +187,15 @@ function! ale#util#FuzzyJSONDecode(data, default) abort
return a:default
endtry
endfunction
+
+" Write a file, including carriage return characters for DOS files.
+"
+" The buffer number is required for determining the fileformat setting for
+" the buffer.
+function! ale#util#Writefile(buffer, lines, filename) abort
+ let l:corrected_lines = getbufvar(a:buffer, '&fileformat') ==# 'dos'
+ \ ? map(copy(a:lines), 'v:val . "\r"')
+ \ : a:lines
+
+ call writefile(l:corrected_lines, a:filename) " no-custom-checks
+endfunction
diff --git a/test/test_writefile_function.vader b/test/test_writefile_function.vader
new file mode 100644
index 00000000..4e4aab53
--- /dev/null
+++ b/test/test_writefile_function.vader
@@ -0,0 +1,48 @@
+Before:
+ call ale#test#SetDirectory('/testplugin/test')
+
+After:
+ noautocmd :e! ++ff=unix
+ setlocal buftype=nofile
+
+ if filereadable('.newline-test')
+ call delete('.newline-test')
+ endif
+
+ call ale#test#RestoreDirectory()
+
+Given(A file with Windows line ending characters):
+ first
+ second
+ third
+
+Execute(Carriage returns should be included for ale#util#Writefile):
+ call ale#test#SetFilename('.newline-test')
+
+ setlocal buftype=
+ noautocmd :w
+ noautocmd :e! ++ff=dos
+
+ call ale#util#Writefile(bufnr(''), getline(1, '$'), '.newline-test')
+
+ AssertEqual
+ \ ["first\r", "second\r", "third\r", ''],
+ \ readfile('.newline-test', 'b')
+ \
+Given(A file with Unix line ending characters):
+ first
+ second
+ third
+
+Execute(Unix file lines should be written as normal):
+ call ale#test#SetFilename('.newline-test')
+
+ setlocal buftype=
+ noautocmd :w
+ noautocmd :e! ++ff=unix
+
+ call ale#util#Writefile(bufnr(''), getline(1, '$'), '.newline-test')
+
+ AssertEqual
+ \ ['first', 'second', 'third', ''],
+ \ readfile('.newline-test', 'b')