summaryrefslogtreecommitdiff
path: root/plugin/ale.vim
blob: 92c1562e3ed5b413356e3d3dfdf6edef6d70873e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
" Author: w0rp <devw0rp@gmail.com>
" Description: Main entry point for the plugin: sets up prefs and autocommands
"   Preferences can be set in vimrc files and so on to configure ale

" Sanity Checks

if exists('g:loaded_ale_dont_use_this_in_other_plugins_please')
    finish
endif

" Set a special flag used only by this plugin for preventing doubly
" loading the script.
let g:loaded_ale_dont_use_this_in_other_plugins_please = 1

" A flag for detecting if the required features are set.
if has('nvim')
    let s:has_features = has('timers')
else
    " Check if Job and Channel functions are available, instead of the
    " features. This works better on old MacVim versions.
    let s:has_features = has('timers') && exists('*job_start') && exists('*ch_close_in')
endif

if !s:has_features
    " Only output a warning if editing some special files.
    if index(['', 'gitcommit'], &filetype) == -1
        echoerr 'ALE requires NeoVim >= 0.1.5 or Vim 8 with +timers +job +channel'
        echoerr 'Please update your editor appropriately.'
    endif

    " Stop here, as it won't work.
    finish
endif

" Add the after directory to the runtimepath
let &runtimepath .= ',' . expand('<sfile>:p:h:h') . '/after'

" Set this flag so that other plugins can use it, like airline.
let g:loaded_ale = 1

" Set the TMPDIR environment variable if it is not set automatically.
" This can automatically fix some environments.
if has('unix') && empty($TMPDIR)
    let $TMPDIR = '/tmp'
endif

" This flag can be set to 0 to disable emitting conflict warnings.
let g:ale_emit_conflict_warnings = get(g:, 'ale_emit_conflict_warnings', 1)

" This global variable is used internally by ALE for tracking information for
" each buffer which linters are being run against.
let g:ale_buffer_info = {}

" User Configuration

" This option prevents ALE autocmd commands from being run for particular
" filetypes which can cause issues.
let g:ale_filetype_blacklist = ['nerdtree', 'unite', 'tags']

" This Dictionary configures which linters are enabled for which languages.
let g:ale_linters = get(g:, 'ale_linters', {})

" This Dictionary configures which functions will be used for fixing problems.
let g:ale_fixers = get(g:, 'ale_fixers', {})

" This Dictionary allows users to set up filetype aliases for new filetypes.
let g:ale_linter_aliases = get(g:, 'ale_linter_aliases', {})

" This flag can be set with a number of milliseconds for delaying the
" execution of a linter when text is changed. The timeout will be set and
" cleared each time text is changed, so repeated edits won't trigger the
" jobs for linting until enough time has passed after editing is done.
let g:ale_lint_delay = get(g:, 'ale_lint_delay', 200)

" This flag can be set to 'never' to disable linting when text is changed.
" This flag can also be set to 'insert' or 'normal' to lint when text is
" changed only in insert or normal mode respectively.
let g:ale_lint_on_text_changed = get(g:, 'ale_lint_on_text_changed', 'always')

" This flag can be set to 1 to enable linting when leaving insert mode.
let g:ale_lint_on_insert_leave = get(g:, 'ale_lint_on_insert_leave', 0)

" This flag can be set to 0 to disable linting when the buffer is entered.
let g:ale_lint_on_enter = get(g:, 'ale_lint_on_enter', 1)

" This flag can be set to 1 to enable linting when a buffer is written.
let g:ale_lint_on_save = get(g:, 'ale_lint_on_save', 1)

" This flag can be set to 1 to enable linting when the filetype is changed.
let g:ale_lint_on_filetype_changed = get(g:, 'ale_lint_on_filetype_changed', 1)

" This flag may be set to 0 to disable ale. After ale is loaded, :ALEToggle
" should be used instead.
let g:ale_enabled = get(g:, 'ale_enabled', 1)

" These flags dictates if ale uses the quickfix or the loclist (loclist is the
" default, quickfix overrides loclist).
let g:ale_set_loclist = get(g:, 'ale_set_loclist', 1)
let g:ale_set_quickfix = get(g:, 'ale_set_quickfix', 0)

" This flag dictates if ale open the configured loclist
let g:ale_open_list = get(g:, 'ale_open_list', 0)

" This flag dictates if ale keeps open loclist even if there is no error in loclist
let g:ale_keep_list_window_open = get(g:, 'ale_keep_list_window_open', 0)

" This flag can be set to 0 to disable setting signs.
" This is enabled by default only if the 'signs' feature exists.
let g:ale_set_signs = get(g:, 'ale_set_signs', has('signs'))

" This flag can be set to 0 to disable setting error highlights.
let g:ale_set_highlights = get(g:, 'ale_set_highlights', has('syntax'))

" These variables dictate what sign is used to indicate errors and warnings.
call ale#Set('sign_error', '>>')
call ale#Set('sign_style_error', g:ale_sign_error)
call ale#Set('sign_warning', '--')
call ale#Set('sign_style_warning', g:ale_sign_warning)
call ale#Set('sign_info', g:ale_sign_warning)

" This variable sets an offset which can be set for sign IDs.
" This ID can be changed depending on what IDs are set for other plugins.
" The dummy sign will use the ID exactly equal to the offset.
let g:ale_sign_offset = get(g:, 'ale_sign_offset', 1000000)

" This flag can be set to 1 to keep sign gutter always open
let g:ale_sign_column_always = get(g:, 'ale_sign_column_always', 0)

" String format for the echoed message
" A %s is mandatory
" It can contain 2 handlers: %linter%, %severity%
let g:ale_echo_msg_format = get(g:, 'ale_echo_msg_format', '%s')

" Strings used for severity in the echoed message
let g:ale_echo_msg_error_str = get(g:, 'ale_echo_msg_error_str', 'Error')
let g:ale_echo_msg_warning_str = get(g:, 'ale_echo_msg_warning_str', 'Warning')

" This flag can be set to 0 to disable echoing when the cursor moves.
let g:ale_echo_cursor = get(g:, 'ale_echo_cursor', 1)

" String format for statusline
" Its a list where:
" * The 1st element is for errors
" * The 2nd element is for warnings
" * The 3rd element is when there are no errors
let g:ale_statusline_format = get(g:, 'ale_statusline_format',
\   ['%d error(s)', '%d warning(s)', 'OK']
\)

" This flag can be set to 0 to disable warnings for trailing whitespace
let g:ale_warn_about_trailing_whitespace =
\   get(g:, 'ale_warn_about_trailing_whitespace', 1)

" A flag for controlling the maximum size of the command history to store.
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! ALEInitAuGroups() abort
    " This value used to be a Boolean as a Number, and is now a String.
    let l:text_changed = '' . g:ale_lint_on_text_changed

    augroup ALERunOnTextChangedGroup
        autocmd!
        if g:ale_enabled
            if l:text_changed ==? 'always' || l:text_changed ==# '1'
                autocmd TextChanged,TextChangedI * call ale#Queue(g:ale_lint_delay)
            elseif l:text_changed ==? 'normal'
                autocmd TextChanged * call ale#Queue(g:ale_lint_delay)
            elseif l:text_changed ==? 'insert'
                autocmd TextChangedI * call ale#Queue(g:ale_lint_delay)
            endif
        endif
    augroup END

    augroup ALERunOnEnterGroup
        autocmd!
        if g:ale_enabled && g:ale_lint_on_enter
            autocmd BufEnter,BufRead * call ale#Queue(300, 'lint_file')
        endif
    augroup END

    augroup ALERunOnFiletypeChangeGroup
        autocmd!
        if g:ale_enabled && g:ale_lint_on_filetype_changed
            " Set the filetype after a buffer is opened or read.
            autocmd BufEnter,BufRead * let b:ale_original_filetype = &filetype
            " Only start linting if the FileType actually changes after
            " opening a buffer. The FileType will fire when buffers are opened.
            autocmd FileType *
            \   if has_key(b:, 'ale_original_filetype')
            \   && b:ale_original_filetype !=# expand('<amatch>')
            \|      call ale#Queue(300, 'lint_file')
            \|  endif
        endif
    augroup END

    augroup ALERunOnSaveGroup
        autocmd!
        if g:ale_enabled && g:ale_lint_on_save
            autocmd BufWrite * call ale#Queue(0, 'lint_file')
        endif
    augroup END

    augroup ALERunOnInsertLeave
        autocmd!
        if g:ale_enabled && g:ale_lint_on_insert_leave
            autocmd InsertLeave * call ale#Queue(0, 'lint_file')
        endif
    augroup END

    augroup ALECursorGroup
        autocmd!
        if g:ale_enabled && g:ale_echo_cursor
            autocmd CursorMoved,CursorHold * call ale#cursor#EchoCursorWarningWithDelay()
            " Look for a warning to echo as soon as we leave Insert mode.
            " The script's position variable used when moving the cursor will
            " not be changed here.
            autocmd InsertLeave * call ale#cursor#EchoCursorWarning()
        endif
    augroup END

    if !g:ale_enabled
        augroup! ALERunOnTextChangedGroup
        augroup! ALERunOnEnterGroup
        augroup! ALERunOnSaveGroup
        augroup! ALERunOnInsertLeave
        augroup! ALECursorGroup
    endif
endfunction

function! s:ALEToggle() abort
    let g:ale_enabled = !get(g:, 'ale_enabled')

    if g:ale_enabled
        " Lint immediately, including running linters against the file.
        call ale#Queue(0, 'lint_file')
    else
        " Make sure the buffer number is a number, not a string,
        " otherwise things can go wrong.
        for l:buffer in map(keys(g:ale_buffer_info), 'str2nr(v:val)')
            " Stop jobs and delete stored buffer data
            call ale#cleanup#Buffer(l:buffer)
            " Clear signs, loclist, quicklist
            call ale#engine#SetResults(l:buffer, [])
        endfor

        " Remove highlights for the current buffer now.
        if g:ale_set_highlights
            call ale#highlight#UpdateHighlights()
        endif
    endif

    call ALEInitAuGroups()
endfunction

call ALEInitAuGroups()

" Define commands for moving through warnings and errors.
command! -bar ALEPrevious :call ale#loclist_jumping#Jump('before', 0)
command! -bar ALEPreviousWrap :call ale#loclist_jumping#Jump('before', 1)
command! -bar ALENext :call ale#loclist_jumping#Jump('after', 0)
command! -bar ALENextWrap :call ale#loclist_jumping#Jump('after', 1)

" A command for showing error details.
command! -bar ALEDetail :call ale#cursor#ShowCursorDetail()

" Define commands for turning ALE on or off.
command! -bar ALEToggle :call s:ALEToggle()
command! -bar ALEEnable :if !g:ale_enabled | ALEToggle | endif
command! -bar ALEDisable :if g:ale_enabled | ALEToggle | endif

" A command for linting manually.
command! -bar ALELint :call ale#Queue(0, 'lint_file')

" Define a command to get information about current filetype.
command! -bar ALEInfo :call ale#debugging#Info()
" The same, but copy output to your clipboard.
command! -bar ALEInfoToClipboard :call ale#debugging#InfoToClipboard()

" Fix problems in files.
command! -bar ALEFix :call ale#fix#Fix()
" Suggest registered functions to use for fixing problems.
command! -bar ALEFixSuggest :call ale#fix#registry#Suggest(&filetype)

" <Plug> mappings for commands
nnoremap <silent> <Plug>(ale_previous) :ALEPrevious<Return>
nnoremap <silent> <Plug>(ale_previous_wrap) :ALEPreviousWrap<Return>
nnoremap <silent> <Plug>(ale_next) :ALENext<Return>
nnoremap <silent> <Plug>(ale_next_wrap) :ALENextWrap<Return>
nnoremap <silent> <Plug>(ale_toggle) :ALEToggle<Return>
nnoremap <silent> <Plug>(ale_lint) :ALELint<Return>
nnoremap <silent> <Plug>(ale_detail) :ALEDetail<Return>
nnoremap <silent> <Plug>(ale_fix) :ALEFix<Return>

" Housekeeping

augroup ALECleanupGroup
    autocmd!
    " Clean up buffers automatically when they are unloaded.
    autocmd BufUnload * call ale#cleanup#Buffer(expand('<abuf>'))
augroup END

" Backwards Compatibility

function! ALELint(delay) abort
    call ale#Queue(a:delay)
endfunction

function! ALEGetStatusLine() abort
    return ale#statusline#Status()
endfunction