From bf45ab6d8d4dd3a97905eeaa4610757cf69c956c Mon Sep 17 00:00:00 2001 From: w0rp Date: Mon, 17 Oct 2016 23:26:19 +0100 Subject: [PATCH] Add a function for waiting for linters to complete, and add a test which checks that linting updates the loclist. --- .eslintrc.js | 11 ++++++++ autoload/ale/engine.vim | 36 +++++++++++++++++++++++++ test/test_linting_updates_loclist.vader | 17 ++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 .eslintrc.js create mode 100644 test/test_linting_updates_loclist.vader diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 00000000..adcb2513 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,11 @@ +module.exports = { + parserOptions: { + ecmaVersion: 6, + sourceType: "module", + }, + rules: { + semi: 'error', + 'space-infix-ops': 'warn', + radix: 'error', + } +} diff --git a/autoload/ale/engine.vim b/autoload/ale/engine.vim index c530c8a3..2df97385 100644 --- a/autoload/ale/engine.vim +++ b/autoload/ale/engine.vim @@ -254,3 +254,39 @@ function! ale#engine#Invoke(buffer, linter) abort endif endif endfunction + +" This function can be called with a timeout to wait for all jobs to finish. +" If the jobs to not finish in the given number of milliseconds, +" an exception will be thrown. +" +" The time taken will be a very rough approximation, and more time may be +" permitted than is specified. +function! ale#engine#WaitForJobs(deadline) abort + let l:time_ticked = 0 + let l:job_list = [] + + for l:job_id in keys(s:job_info_map) + call add(l:job_list, s:job_info_map[l:job_id].linter.job) + endfor + + let l:should_wait_more = 1 + + while l:should_wait_more + let l:should_wait_more = 0 + + for l:job in l:job_list + if job_status(l:job) ==# 'run' + if l:time_ticked > a:deadline + " Stop waiting after a timeout, so we don't wait forever. + throw 'Jobs did not complete on time!' + endif + + " Wait another 10 milliseconds + let l:time_ticked += 10 + let l:should_wait_more = 1 + sleep 10ms + break + endif + endfor + endwhile +endfunction diff --git a/test/test_linting_updates_loclist.vader b/test/test_linting_updates_loclist.vader new file mode 100644 index 00000000..9c39c30c --- /dev/null +++ b/test/test_linting_updates_loclist.vader @@ -0,0 +1,17 @@ +Given javascript (Some JavaScript with problems): + var y = 3+3; + var y = 3 + +Before: + let g:ale_buffer_loclist_map = {} + let g:expected_data = {bufnr('%'): [{'lnum': 1, 'bufnr': bufnr('%'), 'vcol': 0, 'linter_name': 'eslint', 'nr': -1, 'type': 'W', 'col': 10, 'text': 'Infix operators must be spaced. (space-infix-ops)'}, {'lnum': 2, 'bufnr': bufnr('%'), 'vcol': 0, 'linter_name': 'eslint', 'nr': -1, 'type': 'E', 'col': 10, 'text': 'Missing semicolon. (semi)'}]} + +After: + let g:ale_buffer_loclist_map = {} + unlet g:expected_data + +Execute(The loclist should be updated after linting is done): + call ale#Lint() + call ale#engine#WaitForJobs(2000) + + AssertEqual g:expected_data, g:ale_buffer_loclist_map