From eac0a41ae1aba77e1d017833425fa8132130df13 Mon Sep 17 00:00:00 2001 From: w0rp Date: Thu, 16 Feb 2017 23:18:57 +0000 Subject: [PATCH] #254 Add an option for logging the output of commands --- autoload/ale/debugging.vim | 18 ++++++++++ autoload/ale/engine.vim | 5 +++ autoload/ale/history.vim | 31 ++++++++++++---- doc/ale.txt | 16 +++++++++ plugin/ale.vim | 3 ++ test/test_ale_info.vader | 64 ++++++++++++++++++++++++++++++++++ test/test_history_saving.vader | 14 ++++++++ 7 files changed, 144 insertions(+), 7 deletions(-) diff --git a/autoload/ale/debugging.vim b/autoload/ale/debugging.vim index 1ca7736..60c1b37 100644 --- a/autoload/ale/debugging.vim +++ b/autoload/ale/debugging.vim @@ -76,6 +76,24 @@ function! s:EchoCommandHistory() abort endif echom '(' . l:status_message . ') ' . string(l:item.command) + + if g:ale_history_log_output && has_key(l:item, 'output') + if empty(l:item.output) + echom '' + echom '<<>>' + echom '' + else + echom '' + echom '<<>>' + + for l:line in l:item.output + echom l:line + endfor + + echom '<<>>' + echom '' + endif + endif endfor endfunction diff --git a/autoload/ale/engine.vim b/autoload/ale/engine.vim index a6b9df3..ac8028a 100644 --- a/autoload/ale/engine.vim +++ b/autoload/ale/engine.vim @@ -214,6 +214,11 @@ function! s:HandleExit(job) abort return endif + " Log the output of the command for ALEInfo if we should. + if g:ale_history_enabled && g:ale_history_log_output + call ale#history#RememberOutput(l:buffer, l:job_id, l:output[:]) + endif + let l:linter_loclist = ale#util#GetFunction(l:linter.callback)(l:buffer, l:output) " Make some adjustments to the loclists to fix common problems. diff --git a/autoload/ale/history.vim b/autoload/ale/history.vim index f52c1d7..78703be 100644 --- a/autoload/ale/history.vim +++ b/autoload/ale/history.vim @@ -25,17 +25,34 @@ function! ale#history#Add(buffer, status, job_id, command) abort let g:ale_buffer_info[a:buffer].history = l:history endfunction -" Set an exit code for a command which finished. -function! ale#history#SetExitCode(buffer, job_id, exit_code) abort +function! s:FindHistoryItem(buffer, job_id) abort " Search backwards to find a matching job ID. IDs might be recycled, " so finding the last one should be good enough. for l:obj in reverse(g:ale_buffer_info[a:buffer].history[:]) if l:obj.job_id == a:job_id - " If we find a match, then set the code and status, and stop here. - let l:obj.exit_code = a:exit_code - let l:obj.status = 'finished' - - return + return l:obj endif endfor + + return {} +endfunction + +" Set an exit code for a command which finished. +function! ale#history#SetExitCode(buffer, job_id, exit_code) abort + let l:obj = s:FindHistoryItem(a:buffer, a:job_id) + + if !empty(l:obj) + " If we find a match, then set the code and status. + let l:obj.exit_code = a:exit_code + let l:obj.status = 'finished' + endif +endfunction + +" Set the output for a command which finished. +function! ale#history#RememberOutput(buffer, job_id, output) abort + let l:obj = s:FindHistoryItem(a:buffer, a:job_id) + + if !empty(l:obj) + let l:obj.output = a:output + endif endfunction diff --git a/doc/ale.txt b/doc/ale.txt index 0922abc..f21983b 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -199,6 +199,22 @@ g:ale_history_enabled *g:ale_history_enabled* This option can be disabled if storing a command history is not desired. +g:ale_history_log_output *g:ale_history_log_output* + + Type: |Number| + Default: `0` + + When set to `1`, ALE will store the output of commands which have completed + successfully in the command history, and the output will be displayed when + using |ALEInfo|. + + |g:ale_history_enabled| must be set to `1` for this output to be stored or + printed. + + ALE will likely consume a lot of memory if this option is on, so it should + only be used for debugging problems with linters. + + g:ale_keep_list_window_open *g:ale_keep_list_window_open* Type: |Number| diff --git a/plugin/ale.vim b/plugin/ale.vim index 321bff8..fd598d4 100644 --- a/plugin/ale.vim +++ b/plugin/ale.vim @@ -137,6 +137,9 @@ let g:ale_max_buffer_history_size = get(g:, 'ale_max_buffer_history_size', 20) " A flag for enabling or disabling the command history. let g:ale_history_enabled = get(g:, 'ale_history_enabled', 1) +" A flag for storing the full output of commands in the history. +let g:ale_history_log_output = get(g:, 'ale_history_log_output', 0) + function! s:ALEInitAuGroups() abort augroup ALERunOnTextChangedGroup autocmd! diff --git a/test/test_ale_info.vader b/test/test_ale_info.vader index 92025f2..ecea899 100644 --- a/test/test_ale_info.vader +++ b/test/test_ale_info.vader @@ -41,6 +41,7 @@ After: unlet! g:globals_string unlet! g:command_header let g:ale_buffer_info = {} + let g:ale_history_log_output = 0 unlet! g:ale_testft_testlinter1_foo unlet! g:ale_testft_testlinter1_bar unlet! g:ale_testft2_testlinter2_foo @@ -218,3 +219,66 @@ Execute (ALEInfo command history should print exit codes correctly): \ '(finished - exit code 1) [''/bin/bash'', ''\c'', ''last command'']', \ ], "\n"), \ g:output + +Given testft.testft2 (Empty buffer with two filetypes): +Execute (ALEInfo command history should print command output if logging is on): + let g:ale_history_log_output = 1 + + let g:ale_buffer_info[bufnr('%')] = { + \ 'history': [ + \ { + \ 'status': 'finished', + \ 'exit_code': 0, + \ 'job_id': 347, + \ 'command': 'first command', + \ 'output': ['some', 'first command output'], + \ }, + \ { + \ 'status': 'finished', + \ 'exit_code': 1, + \ 'job_id': 347, + \ 'command': ['/bin/bash', '\c', 'last command'], + \ 'output': ['different second command output'], + \ }, + \ { + \ 'status': 'finished', + \ 'exit_code': 0, + \ 'job_id': 347, + \ 'command': 'command with no output', + \ 'output': [], + \ }, + \ ], + \} + + call ale#linter#Define('testft', g:testlinter1) + call ale#linter#Define('testft2', g:testlinter2) + redir => g:output + silent ALEInfo + redir END + AssertEqual + \ join([ + \ '', + \ ' Current Filetype: testft.testft2', + \ 'Available Linters: [''testlinter1'', ''testlinter2'']', + \ ' Enabled Linters: [''testlinter1'', ''testlinter2'']', + \ ' Linter Variables:', + \ g:globals_string . g:command_header, + \ '(finished - exit code 0) ''first command''', + \ '', + \ '<<>>', + \ 'some', + \ 'first command output', + \ '<<>>', + \ '', + \ '(finished - exit code 1) [''/bin/bash'', ''\c'', ''last command'']', + \ '', + \ '<<>>', + \ 'different second command output', + \ '<<>>', + \ '', + \ '(finished - exit code 0) ''command with no output''', + \ '', + \ '<<>>', + \ '', + \ ], "\n"), + \ g:output diff --git a/test/test_history_saving.vader b/test/test_history_saving.vader index 23b9170..303a02f 100644 --- a/test/test_history_saving.vader +++ b/test/test_history_saving.vader @@ -17,6 +17,7 @@ Before: After: let g:ale_history_enabled = 1 + let g:ale_history_log_output = 0 unlet g:history let g:ale_buffer_info = {} let g:ale_max_buffer_history_size = 20 @@ -52,6 +53,19 @@ Execute(History should be not set when disabled): AssertEqual 0, len(g:ale_buffer_info[bufnr('%')].history) +Execute(History should include command output if logging is enabled): + AssertEqual 'foobar', &filetype + + let g:ale_history_log_output = 1 + + call ale#Lint() + call ale#engine#WaitForJobs(2000) + + let g:history = g:ale_buffer_info[bufnr('%')].history + + AssertEqual 1, len(g:history) + AssertEqual ['command history test'], g:history[0].output + Execute(History items should be popped after going over the max): let g:ale_buffer_info[1] = { \ 'history': map(range(20), '{''status'': ''started'', ''job_id'': v:val, ''command'': ''foobar''}'),