· 8 years ago · Jun 19, 2018, 04:56 AM
1" tags, tags/help sources for unite.vim
2" Version: 0.0.1
3" Last Change: 20 Oct 2010
4" Author: tsukkee <takayuki0510 at gmail.com>
5" Licence: The MIT License {{{
6" Permission is hereby granted, free of charge, to any person obtaining a copy
7" of this software and associated documentation files (the "Software"), to deal
8" in the Software without restriction, including without limitation the rights
9" to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10" copies of the Software, and to permit persons to whom the Software is
11" furnished to do so, subject to the following conditions:
12"
13" The above copyright notice and this permission notice shall be included in
14" all copies or substantial portions of the Software.
15"
16" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19" AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20" LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21" OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22" THE SOFTWARE.
23" }}}
24
25
26" define source
27function! unite#sources#tags#define()
28 return [s:source_tags, s:source_tags_help]
29endfunction
30
31
32" tags
33let s:source_tags = {
34\ 'name': 'tags',
35\ 'max_candidates': 30,
36\ 'is_volatile': 1,
37\ 'action_table': {},
38\ 'default_action': {'word': 'lookup'}
39\}
40function! s:source_tags.gather_candidates(args, candidate)
41 return s:gather_candidates(0, a:args, a:candidate)
42endfunction
43
44let s:action_table_tags = {}
45let s:action_table_tags.lookup = {
46\ 'is_selectable': 1
47\}
48function! s:action_table_tags.lookup.func(candidate)
49 execute "tag" a:candidate.word
50endfunction
51
52let s:action_table_tags.jump = {
53\ 'is_selectable': 1
54\}
55function! s:action_table_tags.jump.func(candidate)
56 execute "tjump" a:candidate.word
57endfunction
58
59let s:action_table_tags.select = {
60\ 'is_selectable': 1
61\}
62function! s:action_table_tags.select.func(candidate)
63 execute "tselect" a:candidate.word
64endfunction
65
66let s:action_table_tags.split = {
67\ 'is_selectable': 1
68\}
69function! s:action_table_tags.split.func(candidate)
70 execute "stjump" a:candidate.word
71endfunction
72
73let s:action_table_tags.preview = {
74\ 'is_selectable': 1,
75\ 'is_quit': 0
76\}
77function! s:action_table_tags.preview.func(candidate)
78 execute "ptjump" a:candidate.word
79endfunction
80
81let s:source_tags.action_table.word = s:action_table_tags
82
83
84" tags/help
85let s:source_tags_help = {
86\ 'name': 'tags/help',
87\ 'max_candidates': 30,
88\ 'action_table': {},
89\ 'default_action': {'word': 'lookup'}
90\}
91function! s:source_tags_help.gather_candidates(args, candidate)
92 return s:gather_candidates(1, a:args, a:candidate)
93endfunction
94
95let s:action_table_tags_help = {}
96let s:action_table_tags_help.lookup = {
97\ 'is_selectable': 1
98\}
99function! s:action_table_tags_help.lookup.func(candidate)
100 execute "help" a:candidate.word
101endfunction
102
103let s:source_tags_help.action_table.word = s:action_table_tags_help
104
105
106" gather
107function! s:gather_candidates(is_help, args, context)
108 let saved_buftype = &l:buftype
109 if a:is_help
110 let &l:buftype = "help"
111 endif
112
113 let result = []
114 let source_name = a:is_help ? 'tags/help' : 'tags'
115 let source_abbr = a:is_help ? '[help] ' : '[tag] '
116 for tagfile in s:unique(tagfiles())
117 for line in readfile(tagfile)
118 let [word, file] = split(line, "\t")[0:1]
119 if stridx(word, "!") != 0
120 call add(result, {
121 \ 'word': word,
122 \ 'abbr': source_abbr . word . (a:is_help ? '' : ' @' . file),
123 \ 'kind': 'word',
124 \ 'source': source_name,
125 \ 'is_insert': a:context.is_insert
126 \})
127 endif
128 endfor
129 endfor
130
131 let &l:buftype = saved_buftype
132
133 return result
134endfunction
135
136" unique
137function! s:unique(array)
138 if len(a:array) <= 1
139 return a:array
140 endif
141
142 let sorted = sort(a:array)
143 let result = [sorted[0]]
144 for item in sorted
145 if item != result[-1]
146 call add(result, item)
147 endif
148 endfor
149 return result
150endfunction