From cf14d0aa53e9b51f4987dbfd3913183901bb2805 Mon Sep 17 00:00:00 2001 From: w0rp Date: Sat, 26 Jan 2019 19:33:52 +0000 Subject: #2132 Unify temporary file management in command.vim --- ale_linters/ada/gcc.vim | 2 +- ale_linters/cs/mcsc.vim | 2 +- ale_linters/cuda/nvcc.vim | 2 +- ale_linters/elixir/mix.vim | 2 +- ale_linters/erlang/erlc.vim | 2 +- ale_linters/java/javac.vim | 2 +- ale_linters/thrift/thrift.vim | 2 +- ale_linters/verilog/verilator.vim | 2 +- ale_linters/vim/ale_custom_linting_rules.vim | 2 +- autoload/ale/command.vim | 135 ++++++++++++++++++++++++++- autoload/ale/engine.vim | 94 +++---------------- autoload/ale/fix.vim | 70 +++----------- autoload/ale/lsp_linter.vim | 2 +- doc/ale.txt | 52 +++++------ test/test_format_command.vader | 42 +++++---- test/test_sandbox_execution.vader | 2 +- test/test_temporary_file_management.vader | 44 +++++---- 17 files changed, 247 insertions(+), 212 deletions(-) diff --git a/ale_linters/ada/gcc.vim b/ale_linters/ada/gcc.vim index d6f973ae..7b0c4809 100644 --- a/ale_linters/ada/gcc.vim +++ b/ale_linters/ada/gcc.vim @@ -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 output file name must match the source file name (except for the " 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' " -gnatc: Check syntax and semantics only (no code generation attempted) diff --git a/ale_linters/cs/mcsc.vim b/ale_linters/cs/mcsc.vim index 1561661e..a169297c 100644 --- a/ale_linters/cs/mcsc.vim +++ b/ale_linters/cs/mcsc.vim @@ -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. - 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 " temporary file. diff --git a/ale_linters/cuda/nvcc.vim b/ale_linters/cuda/nvcc.vim index f4442cb8..db789ff1 100644 --- a/ale_linters/cuda/nvcc.vim +++ b/ale_linters/cuda/nvcc.vim @@ -7,7 +7,7 @@ call ale#Set('cuda_nvcc_options', '-std=c++11') function! ale_linters#cuda#nvcc#GetCommand(buffer) abort " Unused: use ale#util#nul_file " 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' \ . ale#Pad(ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer))) \ . ale#Pad(ale#Var(a:buffer, 'cuda_nvcc_options')) diff --git a/ale_linters/elixir/mix.vim b/ale_linters/elixir/mix.vim index dc3c1818..59cfe505 100644 --- a/ale_linters/elixir/mix.vim +++ b/ale_linters/elixir/mix.vim @@ -32,7 +32,7 @@ endfunction function! ale_linters#elixir#mix#GetCommand(buffer) abort 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') \ ? 'set MIX_BUILD_PATH=' . ale#Escape(l:temp_dir) . ' &&' diff --git a/ale_linters/erlang/erlc.vim b/ale_linters/erlang/erlc.vim index 0bdb4dea..81ba128b 100644 --- a/ale_linters/erlang/erlc.vim +++ b/ale_linters/erlang/erlc.vim @@ -4,7 +4,7 @@ let g:ale_erlang_erlc_options = get(g:, 'ale_erlang_erlc_options', '') function! ale_linters#erlang#erlc#GetCommand(buffer) abort 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) \ . ' ' . ale#Var(a:buffer, 'erlang_erlc_options') diff --git a/ale_linters/java/javac.vim b/ale_linters/java/javac.vim index 63dcdd94..2aa01b4f 100644 --- a/ale_linters/java/javac.vim +++ b/ale_linters/java/javac.vim @@ -73,7 +73,7 @@ function! ale_linters#java#javac#GetCommand(buffer, import_paths) abort endif " 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 " relative paths correctly. diff --git a/ale_linters/thrift/thrift.vim b/ale_linters/thrift/thrift.vim index 36a8656e..2f2086c9 100644 --- a/ale_linters/thrift/thrift.vim +++ b/ale_linters/thrift/thrift.vim @@ -16,7 +16,7 @@ function! ale_linters#thrift#thrift#GetCommand(buffer) abort let l:generators = ['cpp'] endif - let l:output_dir = ale#engine#CreateDirectory(a:buffer) + let l:output_dir = ale#command#CreateDirectory(a:buffer) return '%e' \ . ale#Pad(join(map(copy(l:generators), "'--gen ' . v:val"))) diff --git a/ale_linters/verilog/verilator.vim b/ale_linters/verilog/verilator.vim index 83d5f59d..18d99043 100644 --- a/ale_linters/verilog/verilator.vim +++ b/ale_linters/verilog/verilator.vim @@ -10,7 +10,7 @@ function! ale_linters#verilog#verilator#GetCommand(buffer) abort let l:filename = ale#util#Tempname() . '_verilator_linted.v' " 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, '$') call ale#util#Writefile(a:buffer, l:lines, l:filename) diff --git a/ale_linters/vim/ale_custom_linting_rules.vim b/ale_linters/vim/ale_custom_linting_rules.vim index 3da44206..3abe5d4b 100644 --- a/ale_linters/vim/ale_custom_linting_rules.vim +++ b/ale_linters/vim/ale_custom_linting_rules.vim @@ -25,7 +25,7 @@ endfunction function! ale_linters#vim#ale_custom_linting_rules#GetCommand(buffer) abort 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:lines = getbufline(a:buffer, 1, '$') diff --git a/autoload/ale/command.vim b/autoload/ale/command.vim index 6c56d518..6944f170 100644 --- a/autoload/ale/command.vim +++ b/autoload/ale/command.vim @@ -1,6 +1,121 @@ " Author: w0rp -" Description: Special command formatting for creating temporary files and -" passing buffer filenames easily. +" Description: Functions for formatting command strings, running commands, and +" 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 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 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... " %s -> with the current filename " %t -> with the name of an unused file in a temporary directory " %% -> 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: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') endif - if l:command =~# '%t' + if a:input isnot v:false && l:command =~# '%t' " Create a temporary filename, / " The file itself will not be created by this function. 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) 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] endfunction diff --git a/autoload/ale/engine.vim b/autoload/ale/engine.vim index ace123dc..c3313512 100644 --- a/autoload/ale/engine.vim +++ b/autoload/ale/engine.vim @@ -74,15 +74,11 @@ function! ale#engine#InitBufferInfo(buffer) abort " job_list will hold the list of job IDs " active_linter_list will hold the list of active linter names " 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] = { \ 'job_list': [], \ 'active_linter_list': [], \ 'active_other_sources_list': [], \ 'loclist': [], - \ 'temporary_file_list': [], - \ 'temporary_directory_list': [], \} return 1 @@ -104,73 +100,25 @@ endfunction " Register a temporary file to be managed with the ALE engine for " a current job run. function! ale#engine#ManageFile(buffer, filename) abort - call ale#engine#InitBufferInfo(a:buffer) - call add(g:ale_buffer_info[a:buffer].temporary_file_list, a:filename) + " TODO: Emit deprecation warning here later. + call ale#command#ManageFile(a:buffer, a:filename) endfunction " Same as the above, but manage an entire directory. function! ale#engine#ManageDirectory(buffer, directory) abort - call ale#engine#InitBufferInfo(a:buffer) - call add(g:ale_buffer_info[a:buffer].temporary_directory_list, a:directory) + " TODO: Emit deprecation warning here later. + call ale#command#ManageDirectory(a:buffer, a:directory) endfunction function! ale#engine#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#engine#ManageFile(a:buffer, l:temporary_file) - - return l:temporary_file + " TODO: Emit deprecation warning here later. + return ale#command#CreateFile(a:buffer) endfunction " Create a new temporary directory and manage it in one go. function! ale#engine#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#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 + " TODO: Emit deprecation warning here later. + return ale#command#CreateDirectory(a:buffer) endfunction 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 " 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. silent doautocmd User ALELintPost @@ -472,26 +420,8 @@ endfunction " Given part of a command, replace any % with %%, so that no characters in " the string will be replaced with filenames, etc. function! ale#engine#EscapeCommandPart(command_part) abort - return substitute(a:command_part, '%', '%%', 'g') -endfunction - -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 + " TODO: Emit deprecation warning here later. + return ale#command#EscapeCommandPart(a:command_part) endfunction " Run a job. @@ -517,7 +447,7 @@ function! s:RunJob(options) abort \ l:executable, \ l:command, \ l:read_buffer, - \ function('s:CreateTemporaryFileForJob'), + \ v:null, \) if l:file_created diff --git a/autoload/ale/fix.vim b/autoload/ale/fix.vim index 59502933..b13878eb 100644 --- a/autoload/ale/fix.vim +++ b/autoload/ale/fix.vim @@ -66,11 +66,17 @@ function! ale#fix#ApplyQueuedFixes() abort endfunction 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.output = a: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) let l:lines = getbufline(a:buffer, 1, '$') @@ -83,13 +89,6 @@ function! ale#fix#ApplyFixes(buffer, output) abort 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, " so try and apply the fixes to the current buffer. call ale#fix#ApplyQueuedFixes() @@ -146,50 +145,6 @@ function! s:HandleExit(job_id, exit_code) abort \}) 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 let l:buffer = a:options.buffer let l:command = a:options.command @@ -223,7 +178,7 @@ function! s:RunJob(options) abort \ '', \ l:command, \ l:read_buffer, - \ function('s:CreateTemporaryFileForJob', [l:input]), + \ l:input, \) 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) 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. - call ale#fix#RemoveManagedFiles(a:buffer) + call ale#command#RemoveManagedFiles(a:buffer) call ale#fix#InitBufferData(a:buffer, a:fixing_flag) silent doautocmd User ALEFixPre diff --git a/autoload/ale/lsp_linter.vim b/autoload/ale/lsp_linter.vim index d92dae7e..306a2cf1 100644 --- a/autoload/ale/lsp_linter.vim +++ b/autoload/ale/lsp_linter.vim @@ -182,7 +182,7 @@ function! ale#lsp_linter#StartLSP(buffer, linter) abort let l:command = ale#linter#GetCommand(a:buffer, a:linter) " 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:ready = ale#lsp#StartProgram(l:conn_id, l:executable, l:command) endif diff --git a/doc/ale.txt b/doc/ale.txt index ac71ab83..ddc5a8a4 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -2598,26 +2598,26 @@ ale#Queue(delay, [linting_flag, buffer_number]) *ale#Queue()* 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 - 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. It is advised to only call this function from a callback function for 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 - |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 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 `%%` instead. This function can be used to escape strings which are @@ -2626,23 +2626,7 @@ ale#engine#EscapeCommandPart(command_part) *ale#engine#EscapeCommandPart()* specially. -ale#engine#GetLoclist(buffer) *ale#engine#GetLoclist()* - - Given a buffer number, this function will return the list of problems - reported by ALE for a given buffer in the format accepted by |setqflist()|. - - A reference to the buffer's list of problems will be returned. The list must - be copied before applying |map()| or |filter()|. - - -ale#engine#IsCheckingBuffer(buffer) *ale#engine#IsCheckingBuffer()* - - Given a buffer number, returns `1` when ALE is busy checking that buffer. - - This function can be used for status lines, tab names, etc. - - -ale#engine#ManageFile(buffer, filename) *ale#engine#ManageFile()* +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 @@ -2657,20 +2641,36 @@ ale#engine#ManageFile(buffer, filename) *ale#engine#ManageFile()* 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. + manages directories separetly with the |ale#command#ManageDirectory| function. -ale#engine#ManageDirectory(buffer, directory) *ale#engine#ManageDirectory()* +ale#command#ManageDirectory(buffer, directory) *ale#command#ManageDirectory()* - Like |ale#engine#ManageFile()|, but directories and all of their contents + 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#engine#ManageFile()| instead for deleting single + It is advised to use |ale#command#ManageFile()| instead for deleting single files. +ale#engine#GetLoclist(buffer) *ale#engine#GetLoclist()* + + Given a buffer number, this function will return the list of problems + reported by ALE for a given buffer in the format accepted by |setqflist()|. + + A reference to the buffer's list of problems will be returned. The list must + be copied before applying |map()| or |filter()|. + + +ale#engine#IsCheckingBuffer(buffer) *ale#engine#IsCheckingBuffer()* + + Given a buffer number, returns `1` when ALE is busy checking that buffer. + + This function can be used for status lines, tab names, etc. + + ale#fix#registry#Add(name, func, filetypes, desc, [aliases]) *ale#fix#registry#Add()* diff --git a/test/test_format_command.vader b/test/test_format_command.vader index 119c7138..15435326 100644 --- a/test/test_format_command.vader +++ b/test/test_format_command.vader @@ -8,7 +8,9 @@ Before: AssertEqual 'dummy.txt', fnamemodify(a:filename, ':t') 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) endfunction @@ -17,17 +19,18 @@ After: unlet! g:match delfunction CheckTempFile - delfunction TestCreateFunc + + runtime autoload/ale/command.vim Execute(FormatCommand should do nothing to basic command strings): AssertEqual \ ['', '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): AssertEqual \ ['', '% %%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): AssertEqual @@ -36,10 +39,10 @@ Execute(FormatCommand should convert %s to the current filename): \ 'foo ' . ale#Escape(expand('%:p')) . ' bar ' . ale#Escape(expand('%:p')), \ 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): - 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]) @@ -52,17 +55,22 @@ Execute(FormatCommand should convert %t to a new temporary filename): " The two temporary filenames formatted in should be the same. 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): AssertEqual \ 1, - \ ale#command#FormatCommand(bufnr('%'), '', 'foo %t', 0, function('TestCreateFunc'))[2] + \ ale#command#FormatCommand(bufnr('%'), '', 'foo %t', 0, v:null)[2] AssertEqual \ 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): - 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]) @@ -79,30 +87,30 @@ Execute(FormatCommand should replace %e with the escaped executable): if has('win32') AssertEqual \ ['', 'foo foo', 0], - \ ale#command#FormatCommand(bufnr('%'), 'foo', '%e %e', 0, function('TestCreateFunc')) + \ ale#command#FormatCommand(bufnr('%'), 'foo', '%e %e', 0, v:null) AssertEqual \ ['', '"foo bar"', 0], - \ ale#command#FormatCommand(bufnr('%'), 'foo bar', '%e', 0, function('TestCreateFunc')) + \ ale#command#FormatCommand(bufnr('%'), 'foo bar', '%e', 0, v:null) AssertEqual \ ['', '%e %e', 0], - \ ale#command#FormatCommand(bufnr('%'), '', '%e %e', 0, function('TestCreateFunc')) + \ ale#command#FormatCommand(bufnr('%'), '', '%e %e', 0, v:null) else AssertEqual \ ['', '''foo'' ''foo''', 0], - \ ale#command#FormatCommand(bufnr('%'), 'foo', '%e %e', 0, function('TestCreateFunc')) + \ ale#command#FormatCommand(bufnr('%'), 'foo', '%e %e', 0, v:null) AssertEqual \ ['', '''foo bar''', 0], - \ ale#command#FormatCommand(bufnr('%'), 'foo bar', '%e', 0, function('TestCreateFunc')) + \ ale#command#FormatCommand(bufnr('%'), 'foo bar', '%e', 0, v:null) AssertEqual \ ['', '%e %e', 0], - \ ale#command#FormatCommand(bufnr('%'), '', '%e %e', 0, function('TestCreateFunc')) + \ ale#command#FormatCommand(bufnr('%'), '', '%e %e', 0, v:null) endif Execute(EscapeCommandPart should escape all percent signs): AssertEqual '%%s %%t %%%% %%s %%t %%%%', ale#engine#EscapeCommandPart('%s %t %% %s %t %%') 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]) @@ -110,7 +118,7 @@ Execute(EscapeCommandPart should pipe in temporary files appropriately): Assert !empty(g:match), 'No match found! Result was: ' . g:result[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]) diff --git a/test/test_sandbox_execution.vader b/test/test_sandbox_execution.vader index 5a4974ba..cf994ce8 100644 --- a/test/test_sandbox_execution.vader +++ b/test/test_sandbox_execution.vader @@ -59,7 +59,7 @@ Execute(ALE shouldn't blow up if file cleanup happens in a sandbox): \ 'temporary_file_list': ['/tmp/foo'], \ '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/bar'], g:ale_buffer_info[3].temporary_directory_list diff --git a/test/test_temporary_file_management.vader b/test/test_temporary_file_management.vader index 4847706a..b25da7ac 100644 --- a/test/test_temporary_file_management.vader +++ b/test/test_temporary_file_management.vader @@ -13,21 +13,21 @@ Before: " We are registering a temporary file, so we should delete it. let g:filename = tempname() 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 " the whole thing. let g:directory = tempname() call mkdir(g:directory) 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 " shouldn't delete it. let g:preserved_directory = tempname() call mkdir(g:preserved_directory) 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 endfunction @@ -42,6 +42,7 @@ Before: \ 'callback': 'TestCallback', \ 'command_callback': 'TestCommandCallback', \}) + call ale#command#ClearData() After: Restore @@ -58,6 +59,7 @@ After: delfunction TestCommandCallback delfunction TestCallback call ale#linter#Reset() + call ale#command#ClearData() Given foobar (Some imaginary filetype): 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: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('%')) - let b:dir = ale#engine#CreateDirectory(bufnr('%')) - let b:dir2 = ale#engine#CreateDirectory(bufnr('%')) + let b:dir = ale#command#CreateDirectory(bufnr('%')) + let b:dir2 = ale#command#CreateDirectory(bufnr('%')) 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: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): - let g:ale_buffer_info = {} - call ale#engine#ManageFile(bufnr(''), '/foo/bar') +Execute(ale#command#ManageFile should add the file even if the buffer info hasn't be set yet): + call ale#command#ManageFile(bufnr(''), '/foo/bar') + AssertEqual - \ ['/foo/bar'], - \ g:ale_buffer_info[bufnr('')].temporary_file_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') -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 - \ ['/foo/bar'], - \ g:ale_buffer_info[bufnr('')].temporary_directory_list + \ { + \ bufnr(''): { + \ 'file_list': [], + \ 'directory_list': ['/foo/bar'], + \ }, + \ }, + \ ale#command#GetData() -- cgit v1.2.3