summaryrefslogtreecommitdiff
path: root/autoload/ale/sign.vim
blob: 21e063132fdb64969664fc34c04e60ae0d41ce91 (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
scriptencoding utf8
" Author: w0rp <devw0rp@gmail.com>
" Description: Draws error and warning signs into signcolumn

" This flag can be set to some integer to control the maximum number of signs
" that ALE will set.
let g:ale_max_signs = get(g:, 'ale_max_signs', -1)
" This flag can be set to 1 to enable changing the sign column colors when
" there are errors.
let g:ale_change_sign_column_color = get(g:, 'ale_change_sign_column_color', 0)
" These variables dictate what signs are used to indicate errors and warnings.
let g:ale_sign_error = get(g:, 'ale_sign_error', '>>')
let g:ale_sign_style_error = get(g:, 'ale_sign_style_error', g:ale_sign_error)
let g:ale_sign_warning = get(g:, 'ale_sign_warning', '--')
let g:ale_sign_style_warning = get(g:, 'ale_sign_style_warning', g:ale_sign_warning)
let g:ale_sign_info = get(g:, 'ale_sign_info', g:ale_sign_warning)
let g:ale_sign_priority = get(g:, 'ale_sign_priority', 30)
" 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)
let g:ale_sign_highlight_linenrs = get(g:, 'ale_sign_highlight_linenrs', 0)

let s:supports_sign_groups = has('nvim-0.4.2') || has('patch-8.1.614')

if !hlexists('ALEErrorSign')
    highlight link ALEErrorSign error
endif

if !hlexists('ALEStyleErrorSign')
    highlight link ALEStyleErrorSign ALEErrorSign
endif

if !hlexists('ALEWarningSign')
    highlight link ALEWarningSign todo
endif

if !hlexists('ALEStyleWarningSign')
    highlight link ALEStyleWarningSign ALEWarningSign
endif

if !hlexists('ALEInfoSign')
    highlight link ALEInfoSign ALEWarningSign
endif

if !hlexists('ALESignColumnWithErrors')
    highlight link ALESignColumnWithErrors error
endif

function! ale#sign#SetUpDefaultColumnWithoutErrorsHighlight() abort
    let l:verbose = &verbose
    set verbose=0
    let l:output = execute('highlight SignColumn', 'silent')
    let &verbose = l:verbose

    let l:highlight_syntax = join(split(l:output)[2:])
    let l:match = matchlist(l:highlight_syntax, '\vlinks to (.+)$')

    if !empty(l:match)
        execute 'highlight link ALESignColumnWithoutErrors ' . l:match[1]
    elseif l:highlight_syntax isnot# 'cleared'
        execute 'highlight ALESignColumnWithoutErrors ' . l:highlight_syntax
    endif
endfunction

if !hlexists('ALESignColumnWithoutErrors')
    call ale#sign#SetUpDefaultColumnWithoutErrorsHighlight()
endif

" Spaces and backslashes need to be escaped for signs.
function! s:EscapeSignText(sign_text) abort
    return substitute(substitute(a:sign_text, ' *$', '', ''), '\\\| ', '\\\0', 'g')
endfunction

" Signs show up on the left for error markers.
execute 'sign define ALEErrorSign text=' . s:EscapeSignText(g:ale_sign_error)
\   . ' texthl=ALEErrorSign linehl=ALEErrorLine'
execute 'sign define ALEStyleErrorSign text=' .  s:EscapeSignText(g:ale_sign_style_error)
\   . ' texthl=ALEStyleErrorSign linehl=ALEErrorLine'
execute 'sign define ALEWarningSign text=' . s:EscapeSignText(g:ale_sign_warning)
\   . ' texthl=ALEWarningSign linehl=ALEWarningLine'
execute 'sign define ALEStyleWarningSign text=' . s:EscapeSignText(g:ale_sign_style_warning)
\   . ' texthl=ALEStyleWarningSign linehl=ALEWarningLine'
execute 'sign define ALEInfoSign text=' . s:EscapeSignText(g:ale_sign_info)
\   . ' texthl=ALEInfoSign linehl=ALEInfoLine'
sign define ALEDummySign text=\  texthl=SignColumn

if g:ale_sign_highlight_linenrs && has('nvim-0.3.2')
    if !hlexists('ALEErrorSignLineNr')
        highlight link ALEErrorSignLineNr CursorLineNr
    endif

    if !hlexists('ALEStyleErrorSignLineNr')
        highlight link ALEStyleErrorSignLineNr CursorLineNr
    endif

    if !hlexists('ALEWarningSignLineNr')
        highlight link ALEWarningSignLineNr CursorLineNr
    endif

    if !hlexists('ALEStyleWarningSignLineNr')
        highlight link ALEStyleWarningSignLineNr CursorLineNr
    endif

    if !hlexists('ALEInfoSignLineNr')
        highlight link ALEInfoSignLineNr CursorLineNr
    endif

    sign define ALEErrorSign numhl=ALEErrorSignLineNr
    sign define ALEStyleErrorSign numhl=ALEStyleErrorSignLineNr
    sign define ALEWarningSign numhl=ALEWarningSignLineNr
    sign define ALEStyleWarningSign numhl=ALEStyleWarningSignLineNr
    sign define ALEInfoSign numhl=ALEInfoSignLineNr
endif

function! ale#sign#GetSignName(sublist) abort
    let l:priority = g:ale#util#style_warning_priority

    " Determine the highest priority item for the line.
    for l:item in a:sublist
        let l:item_priority = ale#util#GetItemPriority(l:item)

        if l:item_priority > l:priority
            let l:priority = l:item_priority
        endif
    endfor

    if l:priority is# g:ale#util#error_priority
        return 'ALEErrorSign'
    endif

    if l:priority is# g:ale#util#warning_priority
        return 'ALEWarningSign'
    endif

    if l:priority is# g:ale#util#style_error_priority
        return 'ALEStyleErrorSign'
    endif

    if l:priority is# g:ale#util#style_warning_priority
        return 'ALEStyleWarningSign'
    endif

    if l:priority is# g:ale#util#info_priority
        return 'ALEInfoSign'
    endif

    " Use the error sign for invalid severities.
    return 'ALEErrorSign'
endfunction

function! s:PriorityCmd() abort
    if s:supports_sign_groups
        return ' priority=' . g:ale_sign_priority . ' '
    else
        return ''
    endif
endfunction

function! s:GroupCmd() abort
    if s:supports_sign_groups
        return ' group=ale '
    else
        return ' '
    endif
endfunction

" Read sign data for a buffer to a list of lines.
function! ale#sign#ReadSigns(buffer) abort
    let l:output = execute(
    \   'sign place ' . s:GroupCmd() . s:PriorityCmd()
    \   . ' buffer=' . a:buffer
    \ )

    return split(l:output, "\n")
endfunction

function! ale#sign#ParsePattern() abort
    if s:supports_sign_groups
        " Matches output like :
        " line=4  id=1  group=ale  name=ALEErrorSign
        " строка=1  id=1000001  группа=ale  имя=ALEErrorSign
        " 行=1  識別子=1000001  グループ=ale  名前=ALEWarningSign
        " línea=12 id=1000001 grupo=ale  nombre=ALEWarningSign
        " riga=1 id=1000001  gruppo=ale   nome=ALEWarningSign
        " Zeile=235  id=1000001 Gruppe=ale  Name=ALEErrorSign
        let l:pattern = '\v^.*\=(\d+).*\=(\d+).*\=ale>.*\=(ALE[a-zA-Z]+Sign)'
    else
        " Matches output like :
        " line=4  id=1  name=ALEErrorSign
        " строка=1  id=1000001  имя=ALEErrorSign
        " 行=1  識別子=1000001  名前=ALEWarningSign
        " línea=12 id=1000001 nombre=ALEWarningSign
        " riga=1 id=1000001  nome=ALEWarningSign
        " Zeile=235  id=1000001  Name=ALEErrorSign
        let l:pattern = '\v^.*\=(\d+).*\=(\d+).*\=(ALE[a-zA-Z]+Sign)'
    endif

    return l:pattern
endfunction

" Given a buffer number, return a List of placed signs [line, id, group]
function! ale#sign#ParseSignsWithGetPlaced(buffer) abort
    let l:signs = sign_getplaced(a:buffer, { 'group': s:supports_sign_groups ? 'ale' : '' })[0].signs
    let l:result = []
    let l:is_dummy_sign_set = 0

    for l:sign in l:signs
        if l:sign['name'] is# 'ALEDummySign'
            let l:is_dummy_sign_set = 1
        else
            call add(l:result, [
            \   str2nr(l:sign['lnum']),
            \   str2nr(l:sign['id']),
            \   l:sign['name'],
            \])
        endif
    endfor

    return [l:is_dummy_sign_set, l:result]
endfunction

" Given a list of lines for sign output, return a List of [line, id, group]
function! ale#sign#ParseSigns(line_list) abort
    let l:pattern =ale#sign#ParsePattern()
    let l:result = []
    let l:is_dummy_sign_set = 0

    for l:line in a:line_list
        let l:match = matchlist(l:line, l:pattern)

        if len(l:match) > 0
            if l:match[3] is# 'ALEDummySign'
                let l:is_dummy_sign_set = 1
            else
                call add(l:result, [
                \   str2nr(l:match[1]),
                \   str2nr(l:match[2]),
                \   l:match[3],
                \])
            endif
        endif
    endfor

    return [l:is_dummy_sign_set, l:result]
endfunction

function! ale#sign#FindCurrentSigns(buffer) abort
    if exists('*sign_getplaced')
        return ale#sign#ParseSignsWithGetPlaced(a:buffer)
    else
        let l:line_list = ale#sign#ReadSigns(a:buffer)

        return ale#sign#ParseSigns(l:line_list)
    endif
endfunction

" Given a loclist, group the List into with one List per line.
function! s:GroupLoclistItems(buffer, loclist) abort
    let l:grouped_items = []
    let l:last_lnum = -1

    for l:obj in a:loclist
        if l:obj.bufnr != a:buffer
            continue
        endif

        " Create a new sub-List when we hit a new line.
        if l:obj.lnum != l:last_lnum
            call add(l:grouped_items, [])
        endif

        call add(l:grouped_items[-1], l:obj)
        let l:last_lnum = l:obj.lnum
    endfor

    return l:grouped_items
endfunction

function! s:UpdateLineNumbers(buffer, current_sign_list, loclist) abort
    let l:line_map = {}
    let l:line_numbers_changed = 0

    for [l:line, l:sign_id, l:name] in a:current_sign_list
        let l:line_map[l:sign_id] = l:line
    endfor

    for l:item in a:loclist
        if l:item.bufnr == a:buffer
            let l:lnum = get(l:line_map, get(l:item, 'sign_id', 0), 0)

            if l:lnum && l:item.lnum != l:lnum
                let l:item.lnum = l:lnum
                let l:line_numbers_changed = 1
            endif
        endif
    endfor

    " When the line numbers change, sort the list again
    if l:line_numbers_changed
        call sort(a:loclist, 'ale#util#LocItemCompare')
    endif
endfunction

function! s:BuildSignMap(buffer, current_sign_list, grouped_items) abort
    let l:max_signs = ale#Var(a:buffer, 'max_signs')

    if l:max_signs is 0
        let l:selected_grouped_items = []
    elseif type(l:max_signs) is v:t_number && l:max_signs > 0
        let l:selected_grouped_items = a:grouped_items[:l:max_signs - 1]
    else
        let l:selected_grouped_items = a:grouped_items
    endif

    let l:sign_map = {}
    let l:sign_offset = g:ale_sign_offset

    for [l:line, l:sign_id, l:name] in a:current_sign_list
        let l:sign_info = get(l:sign_map, l:line, {
        \   'current_id_list': [],
        \   'current_name_list': [],
        \   'new_id': 0,
        \   'new_name': '',
        \   'items': [],
        \})

        " Increment the sign offset for new signs, by the maximum sign ID.
        if l:sign_id > l:sign_offset
            let l:sign_offset = l:sign_id
        endif

        " Remember the sign names and IDs in separate Lists, so they are easy
        " to work with.
        call add(l:sign_info.current_id_list, l:sign_id)
        call add(l:sign_info.current_name_list, l:name)

        let l:sign_map[l:line] = l:sign_info
    endfor

    for l:group in l:selected_grouped_items
        let l:line = l:group[0].lnum
        let l:sign_info = get(l:sign_map, l:line, {
        \   'current_id_list': [],
        \   'current_name_list': [],
        \   'new_id': 0,
        \   'new_name': '',
        \   'items': [],
        \})

        let l:sign_info.new_name = ale#sign#GetSignName(l:group)
        let l:sign_info.items = l:group

        let l:index = index(
        \   l:sign_info.current_name_list,
        \   l:sign_info.new_name
        \)

        if l:index >= 0
            " We have a sign with this name already, so use the same ID.
            let l:sign_info.new_id = l:sign_info.current_id_list[l:index]
        else
            " This sign name replaces the previous name, so use a new ID.
            let l:sign_info.new_id = l:sign_offset + 1
            let l:sign_offset += 1
        endif

        let l:sign_map[l:line] = l:sign_info
    endfor

    return l:sign_map
endfunction

function! ale#sign#GetSignCommands(buffer, was_sign_set, sign_map) abort
    let l:command_list = []
    let l:is_dummy_sign_set = a:was_sign_set

    " Set the dummy sign if we need to.
    " The dummy sign is needed to keep the sign column open while we add
    " and remove signs.
    if !l:is_dummy_sign_set && (!empty(a:sign_map) || g:ale_sign_column_always)
        call add(l:command_list, 'sign place '
        \   .  g:ale_sign_offset
        \   . s:GroupCmd()
        \   . s:PriorityCmd()
        \   . ' line=1 name=ALEDummySign '
        \   . ' buffer=' . a:buffer
        \)
        let l:is_dummy_sign_set = 1
    endif

    " Place new items first.
    for [l:line_str, l:info] in items(a:sign_map)
        if l:info.new_id
            " Save the sign IDs we are setting back on our loclist objects.
            " These IDs will be used to preserve items which are set many times.
            for l:item in l:info.items
                let l:item.sign_id = l:info.new_id
            endfor

            if index(l:info.current_id_list, l:info.new_id) < 0
                call add(l:command_list, 'sign place '
                \   . (l:info.new_id)
                \   . s:GroupCmd()
                \   . s:PriorityCmd()
                \   . ' line=' . l:line_str
                \   . ' name=' . (l:info.new_name)
                \   . ' buffer=' . a:buffer
                \)
            endif
        endif
    endfor

    " Remove signs without new IDs.
    for l:info in values(a:sign_map)
        for l:current_id in l:info.current_id_list
            if l:current_id isnot l:info.new_id
                call add(l:command_list, 'sign unplace '
                \   . l:current_id
                \   . s:GroupCmd()
                \   . ' buffer=' . a:buffer
                \)
            endif
        endfor
    endfor

    " Remove the dummy sign to close the sign column if we need to.
    if l:is_dummy_sign_set && !g:ale_sign_column_always
        call add(l:command_list, 'sign unplace '
        \   . g:ale_sign_offset
        \   . s:GroupCmd()
        \   . ' buffer=' . a:buffer
        \)
    endif

    return l:command_list
endfunction

" This function will set the signs which show up on the left.
function! ale#sign#SetSigns(buffer, loclist) abort
    if !bufexists(str2nr(a:buffer))
        " Stop immediately when attempting to set signs for a buffer which
        " does not exist.
        return
    endif

    " Find the current markers
    let [l:is_dummy_sign_set, l:current_sign_list] =
    \   ale#sign#FindCurrentSigns(a:buffer)

    " Update the line numbers for items from before which may have moved.
    call s:UpdateLineNumbers(a:buffer, l:current_sign_list, a:loclist)

    " Group items after updating the line numbers.
    let l:grouped_items = s:GroupLoclistItems(a:buffer, a:loclist)

    " Build a map of current and new signs, with the lines as the keys.
    let l:sign_map = s:BuildSignMap(
    \   a:buffer,
    \   l:current_sign_list,
    \   l:grouped_items,
    \)

    let l:command_list = ale#sign#GetSignCommands(
    \   a:buffer,
    \   l:is_dummy_sign_set,
    \   l:sign_map,
    \)

    " Change the sign column color if the option is on.
    if g:ale_change_sign_column_color && !empty(a:loclist)
        highlight clear SignColumn
        highlight link SignColumn ALESignColumnWithErrors
    endif

    for l:command in l:command_list
        silent! execute l:command
    endfor

    " Reset the sign column color when there are no more errors.
    if g:ale_change_sign_column_color && empty(a:loclist)
        highlight clear SignColumn
        highlight link SignColumn ALESignColumnWithoutErrors
    endif
endfunction

" Remove all signs.
function! ale#sign#Clear() abort
    if s:supports_sign_groups
        sign unplace group=ale *
    else
        sign unplace *
    endif
endfunction