]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/loclist_jumping.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / autoload / ale / loclist_jumping.vim
1 " Author: w0rp <devw0rp@gmail.com>
2 " Description: This file implements functions for jumping around in a file
3 "   based on ALE's internal loclist.
4
5 " Search for the nearest line either before or after the current position
6 " in the loclist. The argument 'wrap' can be passed to enable wrapping
7 " around the end of the list.
8 "
9 " If there are no items or we have hit the end with wrapping off, an empty
10 " List will be returned, otherwise a pair of [line_number, column_number] will
11 " be returned.
12 function! ale#loclist_jumping#FindNearest(direction, wrap, ...) abort
13     let l:buffer = bufnr('')
14     let l:pos = getpos('.')
15     let l:info = get(g:ale_buffer_info, bufnr('%'), {'loclist': []})
16     " Copy the list and filter to only the items in this buffer.
17     let l:loclist = filter(copy(l:info.loclist), 'v:val.bufnr == l:buffer')
18     let l:search_item = {'bufnr': l:buffer, 'lnum': l:pos[1], 'col': l:pos[2]}
19
20     if a:0 > 0
21         let l:filter = a:1
22     else
23         let l:filter = 'any'
24     endif
25
26     if a:0 > 1
27         let l:subtype_filter = a:2
28     else
29         let l:subtype_filter = 'any'
30     endif
31
32     " When searching backwards, so we can find the next smallest match.
33     if a:direction is# 'before'
34         call reverse(l:loclist)
35     endif
36
37     " Look for items before or after the current position.
38     for l:item in l:loclist
39         " Compare the cursor with a item where the column number is bounded,
40         " such that it's possible for the cursor to actually be on the given
41         " column number, without modifying the cursor number we return. This
42         " will allow us to move through matches, but still let us move the
43         " cursor to a line without changing the column, in some cases.
44         let l:cmp_value = ale#util#LocItemCompare(
45         \   {
46         \       'bufnr': l:buffer,
47         \       'lnum': l:item.lnum,
48         \       'col': min([
49         \           max([l:item.col, 1]),
50         \           max([len(getline(l:item.lnum)), 1]),
51         \       ]),
52         \   },
53         \   l:search_item
54         \)
55
56         if (l:filter is# 'any' || l:filter is# l:item.type)
57         \&& (
58         \   l:subtype_filter is# 'any'
59         \   || l:subtype_filter is# get(l:item, 'sub_type', '')
60         \)
61
62             if a:direction is# 'before' && l:cmp_value < 0
63                 return [l:item.lnum, l:item.col]
64             endif
65
66             if a:direction is# 'after' && l:cmp_value > 0
67                 return [l:item.lnum, l:item.col]
68             endif
69         endif
70     endfor
71
72     " If we found nothing, and the wrap option is set to 1, then we should
73     " wrap around the list of warnings/errors
74     if a:wrap
75         for l:item in l:loclist
76             if (l:filter is# 'any' || l:filter is# l:item.type)
77             \&& (
78             \   l:subtype_filter is# 'any'
79             \   || l:subtype_filter is# get(l:item, 'sub_type', '')
80             \)
81                 return [l:item.lnum, l:item.col]
82             endif
83         endfor
84     endif
85
86     return []
87 endfunction
88
89 " As before, find the nearest match, but position the cursor at it.
90 function! ale#loclist_jumping#Jump(direction, ...) abort
91     if a:0 > 0
92         let l:wrap = a:1
93     else
94         let l:wrap = 0
95     endif
96
97     if a:0 > 1
98         let l:filter = a:2
99     else
100         let l:filter = 'any'
101     endif
102
103     if a:0 > 2
104         let l:subtype_filter = a:3
105     else
106         let l:subtype_filter = 'any'
107     endif
108
109     let l:nearest = ale#loclist_jumping#FindNearest(a:direction,
110     \   l:wrap, l:filter, l:subtype_filter)
111
112     if !empty(l:nearest)
113         normal! m`
114         call cursor([l:nearest[0], max([l:nearest[1], 1])])
115     endif
116 endfunction
117
118 function! ale#loclist_jumping#WrapJump(direction, sargs) abort
119     let [l:args, l:rest] = ale#args#Parse(['error', 'warning', 'info', 'wrap',
120     \                                      'style', 'nostyle'], a:sargs)
121
122     let l:wrap = 0
123     let l:type_filter = 'any'
124     let l:subtype_filter = 'any'
125
126     if get(l:args, 'wrap', 'nil') is# ''
127         let l:wrap = 1
128     endif
129
130     if get(l:args, 'error', 'nil') is# ''
131         let l:type_filter = 'E'
132     elseif get(l:args, 'warning', 'nil') is# ''
133         let l:type_filter = 'W'
134     elseif get(l:args, 'info', 'nil') is# ''
135         let l:type_filter = 'I'
136     endif
137
138     if get(l:args, 'nostyle', 'nil') is# ''
139         let l:subtype_filter = 'style'
140     elseif get(l:args, 'style', 'nil') is# ''
141         let l:subtype_filter = ''
142     endif
143
144     call ale#loclist_jumping#Jump(a:direction, l:wrap, l:type_filter,
145     \                             l:subtype_filter)
146 endfunction
147
148 function! ale#loclist_jumping#JumpToIndex(index) abort
149     let l:buffer = bufnr('')
150     let l:info = get(g:ale_buffer_info, l:buffer, {'loclist': []})
151     let l:loclist = filter(copy(l:info.loclist), 'v:val.bufnr == l:buffer')
152
153     if empty(l:loclist)
154         return
155     endif
156
157     let l:item = l:loclist[a:index]
158
159     if !empty(l:item)
160         normal! m`
161         call cursor([l:item.lnum, l:item.col])
162     endif
163 endfunction