Add ALEFixPre and ALEFixPost events

To run autocmd before and after every fix cycle.

Fixes #623 (`ALELintPre` was added in #1203).
This commit is contained in:
Martin Tournoij 2017-12-23 13:13:55 +00:00
parent 92e6e4d1ba
commit 302f69e933
No known key found for this signature in database
GPG key ID: A6258419189EE585
4 changed files with 28 additions and 5 deletions

View file

@ -502,15 +502,17 @@ Will give you:
### 5.viii. How can I execute some code when ALE starts or stops linting? ### 5.viii. How can I execute some code when ALE starts or stops linting?
ALE runs its own [autocmd](http://vimdoc.sourceforge.net/htmldoc/autocmd.html) ALE runs its own [autocmd](http://vimdoc.sourceforge.net/htmldoc/autocmd.html)
events whenever has a linter is started and has been successfully executed and events when a lint or fix cycle are started and stopped. These events can be
processed. These events can be used to call arbitrary functions before and after used to call arbitrary functions before and after ALE stops linting.
ALE stops linting.
```vim ```vim
augroup YourGroup augroup YourGroup
autocmd! autocmd!
autocmd User ALELintPre call YourFunction() autocmd User ALELintPre call YourFunction()
autocmd User ALELintPost call YourFunction() autocmd User ALELintPost call YourFunction()
autocmd User ALEFixPre call YourFunction()
autocmd User ALEFixPost call YourFunction()
augroup END augroup END
``` ```

View file

@ -54,6 +54,8 @@ function! ale#fix#ApplyQueuedFixes() abort
let l:should_lint = l:data.changes_made let l:should_lint = l:data.changes_made
endif endif
silent doautocmd <nomodeline> User ALEFixPost
" If ALE linting is enabled, check for problems with the file again after " If ALE linting is enabled, check for problems with the file again after
" fixing problems. " fixing problems.
if g:ale_enabled if g:ale_enabled
@ -463,6 +465,8 @@ function! ale#fix#Fix(...) abort
call ale#fix#RemoveManagedFiles(l:buffer) call ale#fix#RemoveManagedFiles(l:buffer)
call ale#fix#InitBufferData(l:buffer, l:fixing_flag) call ale#fix#InitBufferData(l:buffer, l:fixing_flag)
silent doautocmd <nomodeline> User ALEFixPre
call s:RunFixer({ call s:RunFixer({
\ 'buffer': l:buffer, \ 'buffer': l:buffer,
\ 'input': g:ale_fix_buffer_data[l:buffer].lines_before, \ 'input': g:ale_fix_buffer_data[l:buffer].lines_before,

View file

@ -2304,9 +2304,11 @@ b:ale_linted *b:ale_linted*
ALELintPre *ALELintPre-autocmd* ALELintPre *ALELintPre-autocmd*
ALELintPost *ALELintPost-autocmd* ALELintPost *ALELintPost-autocmd*
ALEFixPre *ALEFixPre-autocmd*
ALEFixPost *ALEFixPost-autocmd*
These |User| autocommands are triggered before and after every lint cycle. These |User| autocommands are triggered before and after every lint or fix
They can be used to update statuslines, send notifications, etc. cycle. They can be used to update statuslines, send notifications, etc.
The autocmd commands are run with |:silent|, so |:unsilent| is required for The autocmd commands are run with |:silent|, so |:unsilent| is required for
echoing messges. echoing messges.

View file

@ -17,6 +17,14 @@ Before:
\ 'testft': [], \ 'testft': [],
\} \}
let g:pre_success = 0
let g:post_success = 0
augroup VaderTest
autocmd!
autocmd User ALEFixPre let g:pre_success = 1
autocmd User ALEFixPost let g:post_success = 1
augroup end
if !has('win32') if !has('win32')
let &shell = '/bin/bash' let &shell = '/bin/bash'
endif endif
@ -171,6 +179,7 @@ After:
unlet! g:ale_emulate_job_failure unlet! g:ale_emulate_job_failure
unlet! b:ale_fixers unlet! b:ale_fixers
unlet! b:ale_fix_on_save unlet! b:ale_fix_on_save
augroup! VaderTest
delfunction AddCarets delfunction AddCarets
delfunction AddDollars delfunction AddDollars
delfunction DoNothing delfunction DoNothing
@ -664,3 +673,9 @@ Expect(The lines in the JSON should be used):
x x
y y
z z
Execute(ALEFix should apply autocmds):
let g:ale_fixers.testft = ['AddCarets']
ALEFix
AssertEqual g:pre_success, 1
AssertEqual g:post_success, 1