aboutsummaryrefslogtreecommitdiff
path: root/vim/bundle/syntastic/plugin/syntastic/cursor.vim
blob: f59d87b6f535bb6f2b9236ec88313d7500dddaef (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
if exists('g:loaded_syntastic_notifier_cursor') || !exists('g:loaded_syntastic_plugin')
    finish
endif
let g:loaded_syntastic_notifier_cursor = 1

let g:SyntasticCursorNotifier = {}

" Public methods {{{1

function! g:SyntasticCursorNotifier.New() abort " {{{2
    let newObj = copy(self)
    return newObj
endfunction " }}}2

function! g:SyntasticCursorNotifier.enabled() abort " {{{2
    return syntastic#util#var('echo_current_error')
endfunction " }}}2

function! g:SyntasticCursorNotifier.refresh(loclist) abort " {{{2
    if self.enabled() && !a:loclist.isEmpty()
        call syntastic#log#debug(g:_SYNTASTIC_DEBUG_NOTIFICATIONS, 'cursor: refresh')
        let b:syntastic_private_messages = copy(a:loclist.messages(bufnr('')))
        let b:syntastic_private_line = -1
        let b:syntastic_cursor_columns = a:loclist.getCursorColumns()
        autocmd! syntastic CursorMoved
        autocmd syntastic CursorMoved * call SyntasticRefreshCursor()
    endif
endfunction " }}}2

" @vimlint(EVL103, 1, a:loclist)
function! g:SyntasticCursorNotifier.reset(loclist) abort " {{{2
    call syntastic#log#debug(g:_SYNTASTIC_DEBUG_NOTIFICATIONS, 'cursor: reset')
    autocmd! syntastic CursorMoved
    unlet! b:syntastic_private_messages
    let b:syntastic_private_line = -1
endfunction " }}}2
" @vimlint(EVL103, 0, a:loclist)

" }}}1

" Private functions {{{1

function! SyntasticRefreshCursor() abort " {{{2
    if !exists('b:syntastic_private_messages') || empty(b:syntastic_private_messages)
        " file not checked
        return
    endif

    if !exists('b:syntastic_private_line')
        let b:syntastic_private_line = -1
    endif
    let l = line('.')
    let current_messages = get(b:syntastic_private_messages, l, {})

    if !exists('b:syntastic_cursor_columns')
        let b:syntastic_cursor_columns = g:syntastic_cursor_columns
    endif

    if b:syntastic_cursor_columns
        let c = virtcol('.')
        if !exists('b:syntastic_private_idx')
            let b:syntastic_private_idx = -1
        endif

        if s:_is_same_index(l, b:syntastic_private_line, c, b:syntastic_private_idx, current_messages)
            return
        else
            let b:syntastic_private_line = l
        endif

        if !empty(current_messages)
            let b:syntastic_private_idx = s:_find_index(c, current_messages)
            call syntastic#util#wideMsg(current_messages[b:syntastic_private_idx].text)
        else
            let b:syntastic_private_idx = -1
            echo
        endif
    else
        if l == b:syntastic_private_line
            return
        endif
        let b:syntastic_private_line = l

        if !empty(current_messages)
            call syntastic#util#wideMsg(current_messages[0].text)
        else
            echo
        endif
    endif
endfunction " }}}2

" }}}1

" Utilities {{{1

function! s:_is_same_index(line, old_line, column, idx, messages) abort " {{{2
    if a:old_line >= 0 && a:line == a:old_line && a:idx >= 0
        if len(a:messages) <= 1
            return 1
        endif

        if a:messages[a:idx].scol <= a:column || a:idx == 0
            if a:idx == len(a:messages) - 1 || a:column < a:messages[a:idx + 1].scol
                return 1
            else
                return 0
            endif
        else
            return 0
        endif
    else
        return 0
    endif
endfunction " }}}2

function! s:_find_index(column, messages) abort " {{{2
    let max = len(a:messages) - 1
    if max == 0
        return 0
    endif
    let min = 0

    " modified binary search: assign index 0 to columns to the left of the first error
    while min < max - 1
        let mid = (min + max) / 2
        if a:column < a:messages[mid].scol
            let max = mid
        else
            let min = mid
        endif
    endwhile

    return a:column < a:messages[max].scol ? min : max
endfunction " }}}2

" }}}1

" vim: set sw=4 sts=4 et fdm=marker: