summaryrefslogtreecommitdiff
path: root/autoload/ale/virtualtext.vim
blob: 4fb8a31f968834567f56b20b6b250b8eec935f4d (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
scriptencoding utf-8
" Author: w0rp <devw0rp@gmail.com>
" Author: Luan Santos <cfcluan@gmail.com>
" Description: Shows lint message for the current line as virtualtext, if any

" Controls the milliseconds delay before showing a message.
let g:ale_virtualtext_delay = get(g:, 'ale_virtualtext_delay', 10)
let s:cursor_timer = -1
let s:last_pos = [0, 0, 0]
let s:has_virt_text = 0
let s:emulate_virt = 0

if has('nvim-0.3.2')
    let s:ns_id = nvim_create_namespace('ale')
    let s:has_virt_text = 1
elseif has('textprop') && has('popupwin')
    let s:has_virt_text = 1
    let s:emulate_virt = !has('patch-9.0.0297')
    let s:hl_list = []

    if s:emulate_virt
        call prop_type_add('ale', {})
        let s:last_virt = -1
    endif
endif

function! ale#virtualtext#Clear(buf) abort
    if !s:has_virt_text
        return
    endif

    if has('nvim')
        call nvim_buf_clear_namespace(a:buf, s:ns_id, 0, -1)
    else
        if s:emulate_virt && s:last_virt != -1
            call prop_remove({'type': 'ale'})
            call popup_close(s:last_virt)
            let s:last_virt = -1
        elseif !empty(s:hl_list)
            call prop_remove({
            \ 'types': s:hl_list,
            \ 'all': 1,
            \ 'bufnr': a:buf})
        endif
    endif
endfunction

function! ale#virtualtext#ShowMessage(message, hl_group, buf, line) abort
    if !s:has_virt_text || !bufexists(str2nr(a:buf))
        return
    endif

    let l:prefix = get(g:, 'ale_virtualtext_prefix', '> ')
    let l:msg = l:prefix.trim(substitute(a:message, '\n', ' ', 'g'))

    if has('nvim')
        call nvim_buf_set_virtual_text(a:buf, s:ns_id, a:line-1, [[l:msg, a:hl_group]], {})
    elseif s:emulate_virt
        let l:left_pad = col('$')
        call prop_add(a:line, l:left_pad, {
        \ 'type': 'ale',
        \})
        let s:last_virt = popup_create(l:msg, {
        \ 'line': -1,
        \ 'padding': [0, 0, 0, 1],
        \ 'mask': [[1, 1, 1, 1]],
        \ 'textprop': 'ale',
        \ 'highlight': a:hl_group,
        \ 'fixed': 1,
        \ 'wrap': 0,
        \ 'zindex': 2
        \})
    else
        let type = prop_type_get(a:hl_group)

        if type == {}
            call add(s:hl_list, a:hl_group)
            call prop_type_add(a:hl_group, {'highlight': a:hl_group})
        endif

        call prop_add(a:line, 0, {
        \ 'type': a:hl_group,
        \ 'text': ' ' . l:msg,
        \ 'bufnr': a:buf
        \})
    endif
endfunction

function! s:StopCursorTimer() abort
    if s:cursor_timer != -1
        call timer_stop(s:cursor_timer)
        let s:cursor_timer = -1
    endif
endfunction

function! ale#virtualtext#GetHlGroup(type, style) abort
    if a:type is# 'E'
        if a:style is# 'style'
            return 'ALEVirtualTextStyleError'
        else
            return 'ALEVirtualTextError'
        endif
    elseif a:type is# 'W'
        if a:style is# 'style'
            return 'ALEVirtualTextStyleWarning'
        else
            return 'ALEVirtualTextWarning'
        endif
    else
        return 'ALEVirtualTextInfo'
    endif
endfunction

function! ale#virtualtext#ShowCursorWarning(...) abort
    if g:ale_virtualtext_cursor isnot# 'current' && g:ale_virtualtext_cursor != 1
        return
    endif

    let l:buffer = bufnr('')

    if mode(1) isnot# 'n'
        return
    endif

    if ale#ShouldDoNothing(l:buffer)
        return
    endif

    let [l:info, l:loc] = ale#util#FindItemAtCursor(l:buffer)

    call ale#virtualtext#Clear(l:buffer)

    if !empty(l:loc)
        let l:msg = l:loc.text
        let l:type = get(l:loc, 'type', 'E')
        let l:style = get(l:loc, 'sub_type', '')
        let l:hl_group = ale#virtualtext#GetHlGroup(l:type, l:style)
        call ale#virtualtext#ShowMessage(l:msg, l:hl_group, l:buffer, line('.'))
    endif
endfunction

function! ale#virtualtext#ShowCursorWarningWithDelay() abort
    let l:buffer = bufnr('')

    if g:ale_virtualtext_cursor isnot# 'current' && g:ale_virtualtext_cursor != 1
        return
    endif

    if mode(1) isnot# 'n'
        return
    endif

    call s:StopCursorTimer()

    let l:pos = getpos('.')[0:2]

    " Check the current buffer, line, and column number against the last
    " recorded position. If the position has actually changed, *then*
    " we should show something. Otherwise we can end up doing processing
    " the show message far too frequently.
    if l:pos != s:last_pos
        let l:delay = ale#Var(l:buffer, 'virtualtext_delay')

        let s:last_pos = l:pos
        let s:cursor_timer = timer_start(
        \   l:delay,
        \   function('ale#virtualtext#ShowCursorWarning')
        \)
    endif
endfunction

function! ale#virtualtext#SetTexts(buf, loclist) abort
    if !has('nvim') && s:emulate_virt
        return
    endif

    call ale#virtualtext#Clear(a:buf)

    for l in a:loclist
        if l['bufnr'] != a:buf
            continue
        endif

        let hl = ale#virtualtext#GetHlGroup(l['type'], get(l, 'sub_type', ''))
        call ale#virtualtext#ShowMessage(l['text'], hl, a:buf, l['lnum'])
    endfor
endfunction