Refactor history management functions into their own file

This commit is contained in:
w0rp 2017-02-16 21:18:03 +00:00
parent 434ff01f59
commit 3a2286a1b8
4 changed files with 39 additions and 37 deletions

View File

@ -9,30 +9,6 @@
" output: The array of lines for the output of the job.
let s:job_info_map = {}
function! ale#engine#AddToHistory(buffer, status, job_id, command) abort
if g:ale_max_buffer_history_size <= 0
" Don't save anything if the history isn't a positive number.
let g:ale_buffer_info[a:buffer].history = []
return
endif
let l:history = g:ale_buffer_info[a:buffer].history
" Remove the first item if we hit the max history size.
if len(l:history) >= g:ale_max_buffer_history_size
let l:history = l:history[1:]
endif
call add(l:history, {
\ 'status': a:status,
\ 'job_id': a:job_id,
\ 'command': a:command,
\})
let g:ale_buffer_info[a:buffer].history = l:history
endfunction
function! s:GetJobID(job) abort
if has('nvim')
"In NeoVim, job values are just IDs.
@ -471,7 +447,7 @@ function! s:RunJob(options) abort
" Add the job to the list of jobs, so we can track them.
call add(g:ale_buffer_info[l:buffer].job_list, l:job)
let l:status = 'ran'
let l:status = 'started'
let l:job_id = s:GetJobID(l:job)
" Store the ID for the job in the map to read back again.
let s:job_info_map[l:job_id] = {
@ -482,7 +458,7 @@ function! s:RunJob(options) abort
\}
endif
call ale#engine#AddToHistory(l:buffer, l:status, l:job_id, l:command)
call ale#history#Add(l:buffer, l:status, l:job_id, l:command)
endfunction
" Determine which commands to run for a link in a command chain, or

26
autoload/ale/history.vim Normal file
View File

@ -0,0 +1,26 @@
" Author: w0rp <devw0rp@gmail.com>
" Description: Tools for managing command history
"
function! ale#history#Add(buffer, status, job_id, command) abort
if g:ale_max_buffer_history_size <= 0
" Don't save anything if the history isn't a positive number.
let g:ale_buffer_info[a:buffer].history = []
return
endif
let l:history = g:ale_buffer_info[a:buffer].history
" Remove the first item if we hit the max history size.
if len(l:history) >= g:ale_max_buffer_history_size
let l:history = l:history[1:]
endif
call add(l:history, {
\ 'status': a:status,
\ 'job_id': a:job_id,
\ 'command': a:command,
\})
let g:ale_buffer_info[a:buffer].history = l:history
endfunction

View File

@ -169,8 +169,8 @@ Given testft.testft2 (Empty buffer with two filetypes):
Execute (ALEInfo should return command history):
let g:ale_buffer_info[bufnr('%')] = {
\ 'history': [
\ {'status': 'ran', 'job_id': 347, 'command': 'first command'},
\ {'status': 'ran', 'job_id': 347, 'command': ['/bin/bash', '\c', 'last command']},
\ {'status': 'started', 'job_id': 347, 'command': 'first command'},
\ {'status': 'started', 'job_id': 347, 'command': ['/bin/bash', '\c', 'last command']},
\ ],
\}
@ -187,7 +187,7 @@ Execute (ALEInfo should return command history):
\ ' Enabled Linters: [''testlinter1'', ''testlinter2'']',
\ ' Linter Variables:',
\ g:globals_string . g:command_header,
\ '(ran) ''first command''',
\ '(ran) [''/bin/bash'', ''\c'', ''last command'']',
\ '(started) ''first command''',
\ '(started) [''/bin/bash'', ''\c'', ''last command'']',
\ ], "\n"),
\ g:output

View File

@ -36,21 +36,21 @@ Execute(History should be set when commands are run):
AssertEqual 1, len(g:history)
AssertEqual ['status', 'job_id', 'command'], keys(g:history[0])
AssertEqual ['/bin/bash', '-c', 'echo command history test'], g:history[0].command
AssertEqual 'ran', g:history[0].status
AssertEqual 'started', g:history[0].status
" The Job ID will change each time, but we can check the type.
AssertEqual type(1), type(g:history[0].job_id)
Execute(History items should be popped after going over the max):
let g:ale_buffer_info[1] = {
\ 'history': map(range(20), '{''status'': ''ran'', ''job_id'': v:val, ''command'': ''foobar''}'),
\ 'history': map(range(20), '{''status'': ''started'', ''job_id'': v:val, ''command'': ''foobar''}'),
\}
call ale#engine#AddToHistory(1, 'ran', 347, 'last command')
call ale#history#Add(1, 'started', 347, 'last command')
AssertEqual
\ (
\ map(range(1, 19), '{''status'': ''ran'', ''job_id'': v:val, ''command'': ''foobar''}')
\ + [{'status': 'ran', 'job_id': 347, 'command': 'last command'}]
\ map(range(1, 19), '{''status'': ''started'', ''job_id'': v:val, ''command'': ''foobar''}')
\ + [{'status': 'started', 'job_id': 347, 'command': 'last command'}]
\ ),
\ g:ale_buffer_info[1].history
@ -58,12 +58,12 @@ Execute(Nothing should be added to history if the size is too low):
let g:ale_max_buffer_history_size = 0
let g:ale_buffer_info[1] = {'history': []}
call ale#engine#AddToHistory(1, 'ran', 347, 'last command')
call ale#history#Add(1, 'started', 347, 'last command')
AssertEqual [], g:ale_buffer_info[1].history
let g:ale_max_buffer_history_size = -2
call ale#engine#AddToHistory(1, 'ran', 347, 'last command')
call ale#history#Add(1, 'started', 347, 'last command')
AssertEqual [], g:ale_buffer_info[1].history