#2132 Unify temporary file management in command.vim

This commit is contained in:
w0rp 2019-01-26 19:33:52 +00:00
parent f12d312aa4
commit cf14d0aa53
No known key found for this signature in database
GPG Key ID: 0FC1ECAA8C81CD83
17 changed files with 256 additions and 221 deletions

View File

@ -12,7 +12,7 @@ function! ale_linters#ada#gcc#GetCommand(buffer) abort
" the .ali file may be created even if no code generation is attempted. " the .ali file may be created even if no code generation is attempted.
" The output file name must match the source file name (except for the " The output file name must match the source file name (except for the
" extension), so here we cannot use the null file as output. " extension), so here we cannot use the null file as output.
let l:tmp_dir = fnamemodify(ale#engine#CreateDirectory(a:buffer), ':p') let l:tmp_dir = fnamemodify(ale#command#CreateDirectory(a:buffer), ':p')
let l:out_file = l:tmp_dir . fnamemodify(bufname(a:buffer), ':t:r') . '.o' let l:out_file = l:tmp_dir . fnamemodify(bufname(a:buffer), ':t:r') . '.o'
" -gnatc: Check syntax and semantics only (no code generation attempted) " -gnatc: Check syntax and semantics only (no code generation attempted)

View File

@ -30,7 +30,7 @@ function! ale_linters#cs#mcsc#GetCommand(buffer) abort
" register temporary module target file with ale " register temporary module target file with ale
" register temporary module target file with ALE. " register temporary module target file with ALE.
let l:out = ale#engine#CreateFile(a:buffer) let l:out = ale#command#CreateFile(a:buffer)
" The code is compiled as a module and the output is redirected to a " The code is compiled as a module and the output is redirected to a
" temporary file. " temporary file.

View File

@ -7,7 +7,7 @@ call ale#Set('cuda_nvcc_options', '-std=c++11')
function! ale_linters#cuda#nvcc#GetCommand(buffer) abort function! ale_linters#cuda#nvcc#GetCommand(buffer) abort
" Unused: use ale#util#nul_file " Unused: use ale#util#nul_file
" let l:output_file = ale#util#Tempname() . '.ii' " let l:output_file = ale#util#Tempname() . '.ii'
" call ale#engine#ManageFile(a:buffer, l:output_file) " call ale#command#ManageFile(a:buffer, l:output_file)
return '%e -cuda' return '%e -cuda'
\ . ale#Pad(ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer))) \ . ale#Pad(ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer)))
\ . ale#Pad(ale#Var(a:buffer, 'cuda_nvcc_options')) \ . ale#Pad(ale#Var(a:buffer, 'cuda_nvcc_options'))

View File

@ -32,7 +32,7 @@ endfunction
function! ale_linters#elixir#mix#GetCommand(buffer) abort function! ale_linters#elixir#mix#GetCommand(buffer) abort
let l:project_root = ale#handlers#elixir#FindMixProjectRoot(a:buffer) let l:project_root = ale#handlers#elixir#FindMixProjectRoot(a:buffer)
let l:temp_dir = ale#engine#CreateDirectory(a:buffer) let l:temp_dir = ale#command#CreateDirectory(a:buffer)
let l:mix_build_path = has('win32') let l:mix_build_path = has('win32')
\ ? 'set MIX_BUILD_PATH=' . ale#Escape(l:temp_dir) . ' &&' \ ? 'set MIX_BUILD_PATH=' . ale#Escape(l:temp_dir) . ' &&'

View File

@ -4,7 +4,7 @@ let g:ale_erlang_erlc_options = get(g:, 'ale_erlang_erlc_options', '')
function! ale_linters#erlang#erlc#GetCommand(buffer) abort function! ale_linters#erlang#erlc#GetCommand(buffer) abort
let l:output_file = ale#util#Tempname() let l:output_file = ale#util#Tempname()
call ale#engine#ManageFile(a:buffer, l:output_file) call ale#command#ManageFile(a:buffer, l:output_file)
return 'erlc -o ' . ale#Escape(l:output_file) return 'erlc -o ' . ale#Escape(l:output_file)
\ . ' ' . ale#Var(a:buffer, 'erlang_erlc_options') \ . ' ' . ale#Var(a:buffer, 'erlang_erlc_options')

View File

@ -73,7 +73,7 @@ function! ale_linters#java#javac#GetCommand(buffer, import_paths) abort
endif endif
" Create .class files in a temporary directory, which we will delete later. " Create .class files in a temporary directory, which we will delete later.
let l:class_file_directory = ale#engine#CreateDirectory(a:buffer) let l:class_file_directory = ale#command#CreateDirectory(a:buffer)
" Always run javac from the directory the file is in, so we can resolve " Always run javac from the directory the file is in, so we can resolve
" relative paths correctly. " relative paths correctly.

View File

@ -16,7 +16,7 @@ function! ale_linters#thrift#thrift#GetCommand(buffer) abort
let l:generators = ['cpp'] let l:generators = ['cpp']
endif endif
let l:output_dir = ale#engine#CreateDirectory(a:buffer) let l:output_dir = ale#command#CreateDirectory(a:buffer)
return '%e' return '%e'
\ . ale#Pad(join(map(copy(l:generators), "'--gen ' . v:val"))) \ . ale#Pad(join(map(copy(l:generators), "'--gen ' . v:val")))

View File

@ -10,7 +10,7 @@ function! ale_linters#verilog#verilator#GetCommand(buffer) abort
let l:filename = ale#util#Tempname() . '_verilator_linted.v' let l:filename = ale#util#Tempname() . '_verilator_linted.v'
" Create a special filename, so we can detect it in the handler. " Create a special filename, so we can detect it in the handler.
call ale#engine#ManageFile(a:buffer, l:filename) call ale#command#ManageFile(a:buffer, l:filename)
let l:lines = getbufline(a:buffer, 1, '$') let l:lines = getbufline(a:buffer, 1, '$')
call ale#util#Writefile(a:buffer, l:lines, l:filename) call ale#util#Writefile(a:buffer, l:lines, l:filename)

View File

@ -25,7 +25,7 @@ endfunction
function! ale_linters#vim#ale_custom_linting_rules#GetCommand(buffer) abort function! ale_linters#vim#ale_custom_linting_rules#GetCommand(buffer) abort
let l:dir = s:GetALEProjectDir(a:buffer) let l:dir = s:GetALEProjectDir(a:buffer)
let l:temp_dir = ale#engine#CreateDirectory(a:buffer) let l:temp_dir = ale#command#CreateDirectory(a:buffer)
let l:temp_file = l:temp_dir . '/example.vim' let l:temp_file = l:temp_dir . '/example.vim'
let l:lines = getbufline(a:buffer, 1, '$') let l:lines = getbufline(a:buffer, 1, '$')

View File

@ -1,6 +1,121 @@
" Author: w0rp <devw0rp@gmail.com> " Author: w0rp <devw0rp@gmail.com>
" Description: Special command formatting for creating temporary files and " Description: Functions for formatting command strings, running commands, and
" passing buffer filenames easily. " managing files during linting and fixing cycles.
" This dictionary holds lists of files and directories to remove later.
if !exists('s:managed_data')
let s:managed_data = {}
endif
" Used to get the data in tests.
function! ale#command#GetData() abort
return deepcopy(s:managed_data)
endfunction
function! ale#command#ClearData() abort
let s:managed_data = {}
endfunction
function! ale#command#ManageFile(buffer, file) abort
if !has_key(s:managed_data, a:buffer)
let s:managed_data[a:buffer] = {'file_list': [], 'directory_list': []}
endif
call add(s:managed_data[a:buffer].file_list, a:file)
endfunction
function! ale#command#ManageDirectory(buffer, directory) abort
if !has_key(s:managed_data, a:buffer)
let s:managed_data[a:buffer] = {'file_list': [], 'directory_list': []}
endif
call add(s:managed_data[a:buffer].directory_list, a:directory)
endfunction
function! ale#command#CreateFile(buffer) abort
" This variable can be set to 1 in tests to stub this out.
if get(g:, 'ale_create_dummy_temporary_file')
return 'TEMP'
endif
let l:temporary_file = ale#util#Tempname()
call ale#command#ManageFile(a:buffer, l:temporary_file)
return l:temporary_file
endfunction
" Create a new temporary directory and manage it in one go.
function! ale#command#CreateDirectory(buffer) abort
" This variable can be set to 1 in tests to stub this out.
if get(g:, 'ale_create_dummy_temporary_file')
return 'TEMP_DIR'
endif
let l:temporary_directory = ale#util#Tempname()
" Create the temporary directory for the file, unreadable by 'other'
" users.
call mkdir(l:temporary_directory, '', 0750)
call ale#command#ManageDirectory(a:buffer, l:temporary_directory)
return l:temporary_directory
endfunction
function! ale#command#RemoveManagedFiles(buffer) abort
let l:info = get(s:managed_data, a:buffer, {})
if !empty(l:info)
\&& (
\ !exists('*ale#engine#IsCheckingBuffer')
\ || !ale#engine#IsCheckingBuffer(a:buffer)
\)
\&& (
\ !has_key(g:ale_fix_buffer_data, a:buffer)
\ || g:ale_fix_buffer_data[a:buffer].done
\)
" We can't delete anything in a sandbox, so wait until we escape from
" it to delete temporary files and directories.
if ale#util#InSandbox()
return
endif
" Delete files with a call akin to a plan `rm` command.
for l:filename in l:info.file_list
call delete(l:filename)
endfor
" Delete directories like `rm -rf`.
" Directories are handled differently from files, so paths that are
" intended to be single files can be set up for automatic deletion
" without accidentally deleting entire directories.
for l:directory in l:info.directory_list
call delete(l:directory, 'rf')
endfor
call remove(s:managed_data, a:buffer)
endif
endfunction
function! ale#command#CreateTempFile(buffer, temporary_file, input) abort
if empty(a:temporary_file)
" There is no file, so we didn't create anything.
return 0
endif
" Use an existing list of lines of input if we have it, or get the lines
" from the file.
let l:lines = a:input isnot v:null ? a:input : getbufline(a:buffer, 1, '$')
let l:temporary_directory = fnamemodify(a:temporary_file, ':h')
" Create the temporary directory for the file, unreadable by 'other'
" users.
call mkdir(l:temporary_directory, '', 0750)
" Automatically delete the directory later.
call ale#command#ManageDirectory(a:buffer, l:temporary_directory)
" Write the buffer out to a file.
call ale#util#Writefile(a:buffer, l:lines, a:temporary_file)
return 1
endfunction
function! s:TemporaryFilename(buffer) abort function! s:TemporaryFilename(buffer) abort
let l:filename = fnamemodify(bufname(a:buffer), ':t') let l:filename = fnamemodify(bufname(a:buffer), ':t')
@ -16,11 +131,17 @@ function! s:TemporaryFilename(buffer) abort
return ale#util#Tempname() . (has('win32') ? '\' : '/') . l:filename return ale#util#Tempname() . (has('win32') ? '\' : '/') . l:filename
endfunction endfunction
" Given part of a command, replace any % with %%, so that no characters in
" the string will be replaced with filenames, etc.
function! ale#command#EscapeCommandPart(command_part) abort
return substitute(a:command_part, '%', '%%', 'g')
endfunction
" Given a command string, replace every... " Given a command string, replace every...
" %s -> with the current filename " %s -> with the current filename
" %t -> with the name of an unused file in a temporary directory " %t -> with the name of an unused file in a temporary directory
" %% -> with a literal % " %% -> with a literal %
function! ale#command#FormatCommand(buffer, executable, command, pipe_file_if_needed, CreateTemporaryFileForJob) abort function! ale#command#FormatCommand(buffer, executable, command, pipe_file_if_needed, input) abort
let l:temporary_file = '' let l:temporary_file = ''
let l:command = a:command let l:command = a:command
@ -40,7 +161,7 @@ function! ale#command#FormatCommand(buffer, executable, command, pipe_file_if_ne
let l:command = substitute(l:command, '%s', '\=ale#Escape(l:filename)', 'g') let l:command = substitute(l:command, '%s', '\=ale#Escape(l:filename)', 'g')
endif endif
if l:command =~# '%t' if a:input isnot v:false && l:command =~# '%t'
" Create a temporary filename, <temp_dir>/<original_basename> " Create a temporary filename, <temp_dir>/<original_basename>
" The file itself will not be created by this function. " The file itself will not be created by this function.
let l:temporary_file = s:TemporaryFilename(a:buffer) let l:temporary_file = s:TemporaryFilename(a:buffer)
@ -58,7 +179,11 @@ function! ale#command#FormatCommand(buffer, executable, command, pipe_file_if_ne
let l:command = l:command . ' < ' . ale#Escape(l:temporary_file) let l:command = l:command . ' < ' . ale#Escape(l:temporary_file)
endif endif
let l:file_created = a:CreateTemporaryFileForJob(a:buffer, l:temporary_file) let l:file_created = ale#command#CreateTempFile(
\ a:buffer,
\ l:temporary_file,
\ a:input,
\)
return [l:temporary_file, l:command, l:file_created] return [l:temporary_file, l:command, l:file_created]
endfunction endfunction

View File

@ -74,15 +74,11 @@ function! ale#engine#InitBufferInfo(buffer) abort
" job_list will hold the list of job IDs " job_list will hold the list of job IDs
" active_linter_list will hold the list of active linter names " active_linter_list will hold the list of active linter names
" loclist holds the loclist items after all jobs have completed. " loclist holds the loclist items after all jobs have completed.
" temporary_file_list holds temporary files to be cleaned up
" temporary_directory_list holds temporary directories to be cleaned up
let g:ale_buffer_info[a:buffer] = { let g:ale_buffer_info[a:buffer] = {
\ 'job_list': [], \ 'job_list': [],
\ 'active_linter_list': [], \ 'active_linter_list': [],
\ 'active_other_sources_list': [], \ 'active_other_sources_list': [],
\ 'loclist': [], \ 'loclist': [],
\ 'temporary_file_list': [],
\ 'temporary_directory_list': [],
\} \}
return 1 return 1
@ -104,73 +100,25 @@ endfunction
" Register a temporary file to be managed with the ALE engine for " Register a temporary file to be managed with the ALE engine for
" a current job run. " a current job run.
function! ale#engine#ManageFile(buffer, filename) abort function! ale#engine#ManageFile(buffer, filename) abort
call ale#engine#InitBufferInfo(a:buffer) " TODO: Emit deprecation warning here later.
call add(g:ale_buffer_info[a:buffer].temporary_file_list, a:filename) call ale#command#ManageFile(a:buffer, a:filename)
endfunction endfunction
" Same as the above, but manage an entire directory. " Same as the above, but manage an entire directory.
function! ale#engine#ManageDirectory(buffer, directory) abort function! ale#engine#ManageDirectory(buffer, directory) abort
call ale#engine#InitBufferInfo(a:buffer) " TODO: Emit deprecation warning here later.
call add(g:ale_buffer_info[a:buffer].temporary_directory_list, a:directory) call ale#command#ManageDirectory(a:buffer, a:directory)
endfunction endfunction
function! ale#engine#CreateFile(buffer) abort function! ale#engine#CreateFile(buffer) abort
" This variable can be set to 1 in tests to stub this out. " TODO: Emit deprecation warning here later.
if get(g:, 'ale_create_dummy_temporary_file') return ale#command#CreateFile(a:buffer)
return 'TEMP'
endif
let l:temporary_file = ale#util#Tempname()
call ale#engine#ManageFile(a:buffer, l:temporary_file)
return l:temporary_file
endfunction endfunction
" Create a new temporary directory and manage it in one go. " Create a new temporary directory and manage it in one go.
function! ale#engine#CreateDirectory(buffer) abort function! ale#engine#CreateDirectory(buffer) abort
" This variable can be set to 1 in tests to stub this out. " TODO: Emit deprecation warning here later.
if get(g:, 'ale_create_dummy_temporary_file') return ale#command#CreateDirectory(a:buffer)
return 'TEMP_DIR'
endif
let l:temporary_directory = ale#util#Tempname()
" Create the temporary directory for the file, unreadable by 'other'
" users.
call mkdir(l:temporary_directory, '', 0750)
call ale#engine#ManageDirectory(a:buffer, l:temporary_directory)
return l:temporary_directory
endfunction
function! ale#engine#RemoveManagedFiles(buffer) abort
let l:info = get(g:ale_buffer_info, a:buffer, {})
" We can't delete anything in a sandbox, so wait until we escape from
" it to delete temporary files and directories.
if ale#util#InSandbox()
return
endif
" Delete files with a call akin to a plan `rm` command.
if has_key(l:info, 'temporary_file_list')
for l:filename in l:info.temporary_file_list
call delete(l:filename)
endfor
let l:info.temporary_file_list = []
endif
" Delete directories like `rm -rf`.
" Directories are handled differently from files, so paths that are
" intended to be single files can be set up for automatic deletion without
" accidentally deleting entire directories.
if has_key(l:info, 'temporary_directory_list')
for l:directory in l:info.temporary_directory_list
call delete(l:directory, 'rf')
endfor
let l:info.temporary_directory_list = []
endif
endfunction endfunction
function! s:GatherOutput(job_id, line) abort function! s:GatherOutput(job_id, line) abort
@ -321,7 +269,7 @@ function! ale#engine#SetResults(buffer, loclist) abort
" Automatically remove all managed temporary files and directories " Automatically remove all managed temporary files and directories
" now that all jobs have completed. " now that all jobs have completed.
call ale#engine#RemoveManagedFiles(a:buffer) call ale#command#RemoveManagedFiles(a:buffer)
" Call user autocommands. This allows users to hook into ALE's lint cycle. " Call user autocommands. This allows users to hook into ALE's lint cycle.
silent doautocmd <nomodeline> User ALELintPost silent doautocmd <nomodeline> User ALELintPost
@ -472,26 +420,8 @@ endfunction
" Given part of a command, replace any % with %%, so that no characters in " Given part of a command, replace any % with %%, so that no characters in
" the string will be replaced with filenames, etc. " the string will be replaced with filenames, etc.
function! ale#engine#EscapeCommandPart(command_part) abort function! ale#engine#EscapeCommandPart(command_part) abort
return substitute(a:command_part, '%', '%%', 'g') " TODO: Emit deprecation warning here later.
endfunction return ale#command#EscapeCommandPart(a:command_part)
function! s:CreateTemporaryFileForJob(buffer, temporary_file) abort
if empty(a:temporary_file)
" There is no file, so we didn't create anything.
return 0
endif
let l:temporary_directory = fnamemodify(a:temporary_file, ':h')
" Create the temporary directory for the file, unreadable by 'other'
" users.
call mkdir(l:temporary_directory, '', 0750)
" Automatically delete the directory later.
call ale#engine#ManageDirectory(a:buffer, l:temporary_directory)
" Write the buffer out to a file.
let l:lines = getbufline(a:buffer, 1, '$')
call ale#util#Writefile(a:buffer, l:lines, a:temporary_file)
return 1
endfunction endfunction
" Run a job. " Run a job.
@ -517,7 +447,7 @@ function! s:RunJob(options) abort
\ l:executable, \ l:executable,
\ l:command, \ l:command,
\ l:read_buffer, \ l:read_buffer,
\ function('s:CreateTemporaryFileForJob'), \ v:null,
\) \)
if l:file_created if l:file_created

View File

@ -66,11 +66,17 @@ function! ale#fix#ApplyQueuedFixes() abort
endfunction endfunction
function! ale#fix#ApplyFixes(buffer, output) abort function! ale#fix#ApplyFixes(buffer, output) abort
call ale#fix#RemoveManagedFiles(a:buffer)
let l:data = g:ale_fix_buffer_data[a:buffer] let l:data = g:ale_fix_buffer_data[a:buffer]
let l:data.output = a:output let l:data.output = a:output
let l:data.changes_made = l:data.lines_before != l:data.output let l:data.changes_made = l:data.lines_before != l:data.output
let l:data.done = 1
call ale#command#RemoveManagedFiles(a:buffer)
if !bufexists(a:buffer)
" Remove the buffer data when it doesn't exist.
call remove(g:ale_fix_buffer_data, a:buffer)
endif
if l:data.changes_made && bufexists(a:buffer) if l:data.changes_made && bufexists(a:buffer)
let l:lines = getbufline(a:buffer, 1, '$') let l:lines = getbufline(a:buffer, 1, '$')
@ -83,13 +89,6 @@ function! ale#fix#ApplyFixes(buffer, output) abort
endif endif
endif endif
if !bufexists(a:buffer)
" Remove the buffer data when it doesn't exist.
call remove(g:ale_fix_buffer_data, a:buffer)
endif
let l:data.done = 1
" We can only change the lines of a buffer which is currently open, " We can only change the lines of a buffer which is currently open,
" so try and apply the fixes to the current buffer. " so try and apply the fixes to the current buffer.
call ale#fix#ApplyQueuedFixes() call ale#fix#ApplyQueuedFixes()
@ -146,50 +145,6 @@ function! s:HandleExit(job_id, exit_code) abort
\}) \})
endfunction endfunction
function! ale#fix#ManageDirectory(buffer, directory) abort
call add(g:ale_fix_buffer_data[a:buffer].temporary_directory_list, a:directory)
endfunction
function! ale#fix#RemoveManagedFiles(buffer) abort
if !has_key(g:ale_fix_buffer_data, a:buffer)
return
endif
" We can't delete anything in a sandbox, so wait until we escape from
" it to delete temporary files and directories.
if ale#util#InSandbox()
return
endif
" Delete directories like `rm -rf`.
" Directories are handled differently from files, so paths that are
" intended to be single files can be set up for automatic deletion without
" accidentally deleting entire directories.
for l:directory in g:ale_fix_buffer_data[a:buffer].temporary_directory_list
call delete(l:directory, 'rf')
endfor
let g:ale_fix_buffer_data[a:buffer].temporary_directory_list = []
endfunction
function! s:CreateTemporaryFileForJob(input, buffer, temporary_file) abort
if empty(a:temporary_file)
" There is no file, so we didn't create anything.
return 0
endif
let l:temporary_directory = fnamemodify(a:temporary_file, ':h')
" Create the temporary directory for the file, unreadable by 'other'
" users.
call mkdir(l:temporary_directory, '', 0750)
" Automatically delete the directory later.
call ale#fix#ManageDirectory(a:buffer, l:temporary_directory)
" Write the buffer out to a file.
call ale#util#Writefile(a:buffer, a:input, a:temporary_file)
return 1
endfunction
function! s:RunJob(options) abort function! s:RunJob(options) abort
let l:buffer = a:options.buffer let l:buffer = a:options.buffer
let l:command = a:options.command let l:command = a:options.command
@ -223,7 +178,7 @@ function! s:RunJob(options) abort
\ '', \ '',
\ l:command, \ l:command,
\ l:read_buffer, \ l:read_buffer,
\ function('s:CreateTemporaryFileForJob', [l:input]), \ l:input,
\) \)
let l:command = ale#job#PrepareCommand(l:buffer, l:command) let l:command = ale#job#PrepareCommand(l:buffer, l:command)
@ -469,8 +424,13 @@ function! ale#fix#Fix(buffer, fixing_flag, ...) abort
call ale#job#Stop(l:job_id) call ale#job#Stop(l:job_id)
endfor endfor
" Mark the buffer as `done` so files can be removed.
if has_key(g:ale_fix_buffer_data, a:buffer)
let g:ale_fix_buffer_data[a:buffer].done = 1
endif
" Clean up any files we might have left behind from a previous run. " Clean up any files we might have left behind from a previous run.
call ale#fix#RemoveManagedFiles(a:buffer) call ale#command#RemoveManagedFiles(a:buffer)
call ale#fix#InitBufferData(a:buffer, a:fixing_flag) call ale#fix#InitBufferData(a:buffer, a:fixing_flag)
silent doautocmd <nomodeline> User ALEFixPre silent doautocmd <nomodeline> User ALEFixPre

View File

@ -182,7 +182,7 @@ function! ale#lsp_linter#StartLSP(buffer, linter) abort
let l:command = ale#linter#GetCommand(a:buffer, a:linter) let l:command = ale#linter#GetCommand(a:buffer, a:linter)
" Format the command, so %e can be formatted into it. " Format the command, so %e can be formatted into it.
let l:command = ale#command#FormatCommand(a:buffer, l:executable, l:command, 0, {-> 0})[1] let l:command = ale#command#FormatCommand(a:buffer, l:executable, l:command, 0, v:false)[1]
let l:command = ale#job#PrepareCommand(a:buffer, l:command) let l:command = ale#job#PrepareCommand(a:buffer, l:command)
let l:ready = ale#lsp#StartProgram(l:conn_id, l:executable, l:command) let l:ready = ale#lsp#StartProgram(l:conn_id, l:executable, l:command)
endif endif

View File

@ -2598,26 +2598,26 @@ ale#Queue(delay, [linting_flag, buffer_number]) *ale#Queue()*
is broken, or when developing ALE itself. is broken, or when developing ALE itself.
ale#engine#CreateDirectory(buffer) *ale#engine#CreateDirectory()* ale#command#CreateDirectory(buffer) *ale#command#CreateDirectory()*
Create a new temporary directory with a unique name, and manage that Create a new temporary directory with a unique name, and manage that
directory with |ale#engine#ManageDirectory()|, so it will be removed as soon directory with |ale#command#ManageDirectory()|, so it will be removed as soon
as possible. as possible.
It is advised to only call this function from a callback function for It is advised to only call this function from a callback function for
returning a linter command to run. returning a linter command to run.
ale#engine#CreateFile(buffer) *ale#engine#CreateFile()* ale#command#CreateFile(buffer) *ale#command#CreateFile()*
Create a new temporary file with a unique name, and manage that file with Create a new temporary file with a unique name, and manage that file with
|ale#engine#ManageFile()|, so it will be removed as soon as possible. |ale#command#ManageFile()|, so it will be removed as soon as possible.
It is advised to only call this function from a callback function for It is advised to only call this function from a callback function for
returning a linter command to run. returning a linter command to run.
ale#engine#EscapeCommandPart(command_part) *ale#engine#EscapeCommandPart()* ale#command#EscapeCommandPart(command_part) *ale#command#EscapeCommandPart()*
Given a |String|, return a |String| with all `%` characters replaced with Given a |String|, return a |String| with all `%` characters replaced with
`%%` instead. This function can be used to escape strings which are `%%` instead. This function can be used to escape strings which are
@ -2626,6 +2626,35 @@ ale#engine#EscapeCommandPart(command_part) *ale#engine#EscapeCommandPart()*
specially. specially.
ale#command#ManageFile(buffer, filename) *ale#command#ManageFile()*
Given a buffer number for a buffer currently running some linting tasks
and a filename, register a filename with ALE for automatic deletion after
linting is complete, or when Vim exits.
If Vim exits suddenly, ALE will try its best to remove temporary files, but
ALE cannot guarantee with absolute certainty that the files will be removed.
It is advised to create temporary files in the operating system's managed
temporary file directory, such as with |tempname()|.
Directory names should not be given to this function. ALE will only delete
files and symlinks given to this function. This is to prevent entire
directories from being accidentally deleted, say in cases of writing
`dir . '/' . filename` where `filename` is actually `''`, etc. ALE instead
manages directories separetly with the |ale#command#ManageDirectory| function.
ale#command#ManageDirectory(buffer, directory) *ale#command#ManageDirectory()*
Like |ale#command#ManageFile()|, but directories and all of their contents
will be deleted, akin to `rm -rf directory`, which could lead to loss of
data if mistakes are made. This command will also delete any temporary
filenames given to it.
It is advised to use |ale#command#ManageFile()| instead for deleting single
files.
ale#engine#GetLoclist(buffer) *ale#engine#GetLoclist()* ale#engine#GetLoclist(buffer) *ale#engine#GetLoclist()*
Given a buffer number, this function will return the list of problems Given a buffer number, this function will return the list of problems
@ -2642,35 +2671,6 @@ ale#engine#IsCheckingBuffer(buffer) *ale#engine#IsCheckingBuffer()*
This function can be used for status lines, tab names, etc. This function can be used for status lines, tab names, etc.
ale#engine#ManageFile(buffer, filename) *ale#engine#ManageFile()*
Given a buffer number for a buffer currently running some linting tasks
and a filename, register a filename with ALE for automatic deletion after
linting is complete, or when Vim exits.
If Vim exits suddenly, ALE will try its best to remove temporary files, but
ALE cannot guarantee with absolute certainty that the files will be removed.
It is advised to create temporary files in the operating system's managed
temporary file directory, such as with |tempname()|.
Directory names should not be given to this function. ALE will only delete
files and symlinks given to this function. This is to prevent entire
directories from being accidentally deleted, say in cases of writing
`dir . '/' . filename` where `filename` is actually `''`, etc. ALE instead
manages directories separetly with the |ale#engine#ManageDirectory| function.
ale#engine#ManageDirectory(buffer, directory) *ale#engine#ManageDirectory()*
Like |ale#engine#ManageFile()|, but directories and all of their contents
will be deleted, akin to `rm -rf directory`, which could lead to loss of
data if mistakes are made. This command will also delete any temporary
filenames given to it.
It is advised to use |ale#engine#ManageFile()| instead for deleting single
files.
ale#fix#registry#Add(name, func, filetypes, desc, [aliases]) ale#fix#registry#Add(name, func, filetypes, desc, [aliases])
*ale#fix#registry#Add()* *ale#fix#registry#Add()*

View File

@ -8,7 +8,9 @@ Before:
AssertEqual 'dummy.txt', fnamemodify(a:filename, ':t') AssertEqual 'dummy.txt', fnamemodify(a:filename, ':t')
endfunction endfunction
function! TestCreateFunc(buffer, temporary_file) abort runtime autoload/ale/command.vim
function! ale#command#CreateTempFile(buffer, temporary_file, input) abort
return !empty(a:temporary_file) return !empty(a:temporary_file)
endfunction endfunction
@ -17,17 +19,18 @@ After:
unlet! g:match unlet! g:match
delfunction CheckTempFile delfunction CheckTempFile
delfunction TestCreateFunc
runtime autoload/ale/command.vim
Execute(FormatCommand should do nothing to basic command strings): Execute(FormatCommand should do nothing to basic command strings):
AssertEqual AssertEqual
\ ['', 'awesome-linter do something', 0], \ ['', 'awesome-linter do something', 0],
\ ale#command#FormatCommand(bufnr('%'), '', 'awesome-linter do something', 0, function('TestCreateFunc')) \ ale#command#FormatCommand(bufnr('%'), '', 'awesome-linter do something', 0, v:null)
Execute(FormatCommand should handle %%, and ignore other percents): Execute(FormatCommand should handle %%, and ignore other percents):
AssertEqual AssertEqual
\ ['', '% %%d %%f %x %', 0], \ ['', '% %%d %%f %x %', 0],
\ ale#command#FormatCommand(bufnr('%'), '', '%% %%%d %%%f %x %', 0, function('TestCreateFunc')) \ ale#command#FormatCommand(bufnr('%'), '', '%% %%%d %%%f %x %', 0, v:null)
Execute(FormatCommand should convert %s to the current filename): Execute(FormatCommand should convert %s to the current filename):
AssertEqual AssertEqual
@ -36,10 +39,10 @@ Execute(FormatCommand should convert %s to the current filename):
\ 'foo ' . ale#Escape(expand('%:p')) . ' bar ' . ale#Escape(expand('%:p')), \ 'foo ' . ale#Escape(expand('%:p')) . ' bar ' . ale#Escape(expand('%:p')),
\ 0, \ 0,
\ ], \ ],
\ ale#command#FormatCommand(bufnr('%'), '', 'foo %s bar %s', 0, function('TestCreateFunc')) \ ale#command#FormatCommand(bufnr('%'), '', 'foo %s bar %s', 0, v:null)
Execute(FormatCommand should convert %t to a new temporary filename): Execute(FormatCommand should convert %t to a new temporary filename):
let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo %t bar %t', 0, function('TestCreateFunc')) let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo %t bar %t', 0, v:null)
call CheckTempFile(g:result[0]) call CheckTempFile(g:result[0])
@ -52,17 +55,22 @@ Execute(FormatCommand should convert %t to a new temporary filename):
" The two temporary filenames formatted in should be the same. " The two temporary filenames formatted in should be the same.
AssertEqual g:match[1], g:match[2] AssertEqual g:match[1], g:match[2]
Execute(FormatCommand should not convert %t to a new temporary filename when the input is given as v:false):
let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo %t bar %t', 0, v:false)
AssertEqual ['', 'foo %t bar %t', 0], g:result
Execute(FormatCommand should signal that files are created when temporary files are needed): Execute(FormatCommand should signal that files are created when temporary files are needed):
AssertEqual AssertEqual
\ 1, \ 1,
\ ale#command#FormatCommand(bufnr('%'), '', 'foo %t', 0, function('TestCreateFunc'))[2] \ ale#command#FormatCommand(bufnr('%'), '', 'foo %t', 0, v:null)[2]
AssertEqual AssertEqual
\ 0, \ 0,
\ ale#command#FormatCommand(bufnr('%'), '', 'foo %s', 0, function('TestCreateFunc'))[2] \ ale#command#FormatCommand(bufnr('%'), '', 'foo %s', 0, v:null)[2]
Execute(FormatCommand should let you combine %s and %t): Execute(FormatCommand should let you combine %s and %t):
let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo %t bar %s', 0, function('TestCreateFunc')) let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo %t bar %s', 0, v:null)
call CheckTempFile(g:result[0]) call CheckTempFile(g:result[0])
@ -79,30 +87,30 @@ Execute(FormatCommand should replace %e with the escaped executable):
if has('win32') if has('win32')
AssertEqual AssertEqual
\ ['', 'foo foo', 0], \ ['', 'foo foo', 0],
\ ale#command#FormatCommand(bufnr('%'), 'foo', '%e %e', 0, function('TestCreateFunc')) \ ale#command#FormatCommand(bufnr('%'), 'foo', '%e %e', 0, v:null)
AssertEqual AssertEqual
\ ['', '"foo bar"', 0], \ ['', '"foo bar"', 0],
\ ale#command#FormatCommand(bufnr('%'), 'foo bar', '%e', 0, function('TestCreateFunc')) \ ale#command#FormatCommand(bufnr('%'), 'foo bar', '%e', 0, v:null)
AssertEqual AssertEqual
\ ['', '%e %e', 0], \ ['', '%e %e', 0],
\ ale#command#FormatCommand(bufnr('%'), '', '%e %e', 0, function('TestCreateFunc')) \ ale#command#FormatCommand(bufnr('%'), '', '%e %e', 0, v:null)
else else
AssertEqual AssertEqual
\ ['', '''foo'' ''foo''', 0], \ ['', '''foo'' ''foo''', 0],
\ ale#command#FormatCommand(bufnr('%'), 'foo', '%e %e', 0, function('TestCreateFunc')) \ ale#command#FormatCommand(bufnr('%'), 'foo', '%e %e', 0, v:null)
AssertEqual AssertEqual
\ ['', '''foo bar''', 0], \ ['', '''foo bar''', 0],
\ ale#command#FormatCommand(bufnr('%'), 'foo bar', '%e', 0, function('TestCreateFunc')) \ ale#command#FormatCommand(bufnr('%'), 'foo bar', '%e', 0, v:null)
AssertEqual AssertEqual
\ ['', '%e %e', 0], \ ['', '%e %e', 0],
\ ale#command#FormatCommand(bufnr('%'), '', '%e %e', 0, function('TestCreateFunc')) \ ale#command#FormatCommand(bufnr('%'), '', '%e %e', 0, v:null)
endif endif
Execute(EscapeCommandPart should escape all percent signs): Execute(EscapeCommandPart should escape all percent signs):
AssertEqual '%%s %%t %%%% %%s %%t %%%%', ale#engine#EscapeCommandPart('%s %t %% %s %t %%') AssertEqual '%%s %%t %%%% %%s %%t %%%%', ale#engine#EscapeCommandPart('%s %t %% %s %t %%')
Execute(EscapeCommandPart should pipe in temporary files appropriately): Execute(EscapeCommandPart should pipe in temporary files appropriately):
let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo bar', 1, function('TestCreateFunc')) let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo bar', 1, v:null)
call CheckTempFile(g:result[0]) call CheckTempFile(g:result[0])
@ -110,7 +118,7 @@ Execute(EscapeCommandPart should pipe in temporary files appropriately):
Assert !empty(g:match), 'No match found! Result was: ' . g:result[1] Assert !empty(g:match), 'No match found! Result was: ' . g:result[1]
AssertEqual ale#Escape(g:result[0]), g:match[1] AssertEqual ale#Escape(g:result[0]), g:match[1]
let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo bar %t', 1, function('TestCreateFunc')) let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo bar %t', 1, v:null)
call CheckTempFile(g:result[0]) call CheckTempFile(g:result[0])

View File

@ -59,7 +59,7 @@ Execute(ALE shouldn't blow up if file cleanup happens in a sandbox):
\ 'temporary_file_list': ['/tmp/foo'], \ 'temporary_file_list': ['/tmp/foo'],
\ 'temporary_directory_list': ['/tmp/bar'], \ 'temporary_directory_list': ['/tmp/bar'],
\} \}
sandbox call ale#engine#RemoveManagedFiles(3) sandbox call ale#command#RemoveManagedFiles(3)
AssertEqual ['/tmp/foo'], g:ale_buffer_info[3].temporary_file_list AssertEqual ['/tmp/foo'], g:ale_buffer_info[3].temporary_file_list
AssertEqual ['/tmp/bar'], g:ale_buffer_info[3].temporary_directory_list AssertEqual ['/tmp/bar'], g:ale_buffer_info[3].temporary_directory_list

View File

@ -13,21 +13,21 @@ Before:
" We are registering a temporary file, so we should delete it. " We are registering a temporary file, so we should delete it.
let g:filename = tempname() let g:filename = tempname()
call writefile(['foo'], g:filename) call writefile(['foo'], g:filename)
call ale#engine#ManageFile(a:buffer, g:filename) call ale#command#ManageFile(a:buffer, g:filename)
" We are registering this directory appropriately, so we should delete " We are registering this directory appropriately, so we should delete
" the whole thing. " the whole thing.
let g:directory = tempname() let g:directory = tempname()
call mkdir(g:directory) call mkdir(g:directory)
call writefile(['foo'], g:directory . '/bar') call writefile(['foo'], g:directory . '/bar')
call ale#engine#ManageDirectory(a:buffer, g:directory) call ale#command#ManageDirectory(a:buffer, g:directory)
" We are registering this directory as temporary file, so we " We are registering this directory as temporary file, so we
" shouldn't delete it. " shouldn't delete it.
let g:preserved_directory = tempname() let g:preserved_directory = tempname()
call mkdir(g:preserved_directory) call mkdir(g:preserved_directory)
call writefile(['foo'], g:preserved_directory . '/bar') call writefile(['foo'], g:preserved_directory . '/bar')
call ale#engine#ManageFile(a:buffer, g:preserved_directory) call ale#command#ManageFile(a:buffer, g:preserved_directory)
return g:command return g:command
endfunction endfunction
@ -42,6 +42,7 @@ Before:
\ 'callback': 'TestCallback', \ 'callback': 'TestCallback',
\ 'command_callback': 'TestCommandCallback', \ 'command_callback': 'TestCommandCallback',
\}) \})
call ale#command#ClearData()
After: After:
Restore Restore
@ -58,6 +59,7 @@ After:
delfunction TestCommandCallback delfunction TestCommandCallback
delfunction TestCallback delfunction TestCallback
call ale#linter#Reset() call ale#linter#Reset()
call ale#command#ClearData()
Given foobar (Some imaginary filetype): Given foobar (Some imaginary filetype):
foo foo
@ -95,11 +97,11 @@ Execute(ALE should delete managed files when the buffer is removed):
Assert !isdirectory(g:directory), 'The temporary directory was not deleted' Assert !isdirectory(g:directory), 'The temporary directory was not deleted'
Assert isdirectory(g:preserved_directory), 'The tempoary directory was not kept' Assert isdirectory(g:preserved_directory), 'The tempoary directory was not kept'
Execute(ALE should create and delete directories for ale#engine#CreateDirectory()): Execute(ALE should create and delete directories for ale#command#CreateDirectory()):
call ale#engine#InitBufferInfo(bufnr('%')) call ale#engine#InitBufferInfo(bufnr('%'))
let b:dir = ale#engine#CreateDirectory(bufnr('%')) let b:dir = ale#command#CreateDirectory(bufnr('%'))
let b:dir2 = ale#engine#CreateDirectory(bufnr('%')) let b:dir2 = ale#command#CreateDirectory(bufnr('%'))
Assert isdirectory(b:dir), 'The directory was not created' Assert isdirectory(b:dir), 'The directory was not created'
@ -117,16 +119,26 @@ Execute(ALE should create and delete directories for ale#engine#CreateDirectory(
Assert !isdirectory(b:dir), 'The directory was not deleted' Assert !isdirectory(b:dir), 'The directory was not deleted'
Assert !isdirectory(b:dir2), 'The second directory was not deleted' Assert !isdirectory(b:dir2), 'The second directory was not deleted'
Execute(ale#engine#ManageFile should add the file even if the buffer info hasn't be set yet): Execute(ale#command#ManageFile should add the file even if the buffer info hasn't be set yet):
let g:ale_buffer_info = {} call ale#command#ManageFile(bufnr(''), '/foo/bar')
call ale#engine#ManageFile(bufnr(''), '/foo/bar')
AssertEqual
\ ['/foo/bar'],
\ g:ale_buffer_info[bufnr('')].temporary_file_list
Execute(ale#engine#ManageDirectory should add the directory even if the buffer info hasn't be set yet):
let g:ale_buffer_info = {}
call ale#engine#ManageDirectory(bufnr(''), '/foo/bar')
AssertEqual AssertEqual
\ ['/foo/bar'], \ {
\ g:ale_buffer_info[bufnr('')].temporary_directory_list \ bufnr(''): {
\ 'file_list': ['/foo/bar'],
\ 'directory_list': [],
\ },
\ },
\ ale#command#GetData()
Execute(ale#command#ManageDirectory should add the directory even if the buffer info hasn't be set yet):
call ale#command#ManageDirectory(bufnr(''), '/foo/bar')
AssertEqual
\ {
\ bufnr(''): {
\ 'file_list': [],
\ 'directory_list': ['/foo/bar'],
\ },
\ },
\ ale#command#GetData()