- Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathchooser.vim
135 lines (122 loc) · 4 KB
/
chooser.vim
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
" items should be a list of dictionary items with the following keys:
" label (optional) The string to display
" value The corresponding value passed to the callback
" items may also be a raw list of strings. They will be treated as values
function!arduino#chooser#Choose(title, raw_items, callback) abort
letitems= []
let dict_type =type({})
for item ina:raw_items
iftype(item) == dict_type
calladd(items, item)
else
calladd(items, {'value': item})
endif
endfor
ifget(g:, 'arduino_nvim_select_enabled', 0)
callluaeval("require('arduino').select_shim(_A[1], _A[2], _A[3])", [a:title, items, a:callback])
elseifget(g:, 'arduino_telescope_enabled', 0)
callluaeval("require('arduino.telescope').choose('".a:title."', _A, '".a:callback."')", items)
elseifg:arduino_ctrlp_enabled
let ext_data =get(g:ctrlp_ext_vars, s:ctrlp_idx)
let ext_data.lname =a:title
lets:ctrlp_list=items
lets:ctrlp_callback=a:callback
callctrlp#init(s:ctrlp_id)
elseifg:arduino_fzf_enabled
lets:fzf_counter+=1
callfzf#run({
\ 'source': s:ConvertItemsToLabels(items),
\ 'sink': s:mk_fzf_callback(a:callback),
\ 'options': '--prompt="'.a:title.': "'
\ })
else
let labels =map(copy(s:ConvertItemsToLabels(items)), {i, l->
\ i < 9
\ ? ''.(i+1).') '.l
\ : (i+1).') '.l
\ })
let labels = ["" . a:title] + labels
let choice =inputlist(labels)
if choice > 0
callcall(a:callback, [items[choice-1]['value']])
endif
endif
endfunction
function!s:ConvertItemsToLabels(items) abort
let longest =1
for item ina:items
ifhas_key(item, 'label')
let longest =max([longest, strchars(item['label'])])
endif
endfor
returnmap(copy(a:items), 's:ChooserItemLabel(v:val, ' . longest . ')')
endfunction
function!s:ChooserItemLabel(item, ...) abort
let pad_amount =a:0 ? a:1 : 0
ifhas_key(a:item, 'label')
letlabel=a:item['label']
let spacing =1+max([pad_amount -strchars(label), 0])
returnlabel . repeat('', spacing) . '[' . a:item['value'] . ']'
endif
returna:item['value']
endfunction
function!s:ChooserValueFromLabel(label) abort
" The label may be in the format 'label [value]'.
" If so, we need to parse out the value
let groups =matchlist(a:label, '\[\(.*\)\]$')
ifempty(groups)
returna:label
else
return groups[1]
endif
endfunction
" Ctrlp extension {{{1
ifexists('g:ctrlp_ext_vars')
if!exists('g:arduino_ctrlp_enabled')
letg:arduino_ctrlp_enabled=1
endif
lets:ctrlp_idx=len(g:ctrlp_ext_vars)
calladd(g:ctrlp_ext_vars, {
\ 'init': 'arduino#chooser#ctrlp_GetData()',
\ 'accept': 'arduino#chooser#ctrlp_Callback',
\ 'lname': 'arduino',
\ 'sname': 'arduino',
\ 'type': 'line',
\ })
lets:ctrlp_id=g:ctrlp_builtins+len(g:ctrlp_ext_vars)
else
letg:arduino_ctrlp_enabled=0
endif
function!arduino#chooser#ctrlp_GetData() abort
returns:ConvertItemsToLabels(s:ctrlp_list)
endfunction
function!arduino#chooser#ctrlp_Callback(mode, str) abort
callctrlp#exit()
let value =s:ChooserValueFromLabel(a:str)
callcall(s:ctrlp_callback, [value])
endfunction
" fzf extension {{{1
if!exists('g:arduino_fzf_enabled')
ifexists("*fzf#run")
letg:arduino_fzf_enabled=1
else
letg:arduino_fzf_enabled=0
endif
endif
lets:fzf_counter=0
function!s:fzf_leave(callback, item)
callfunction(a:callback)(a:item)
lets:fzf_counter-=1
endfunction
function!s:mk_fzf_callback(callback)
return { item -> s:fzf_leave(a:callback, s:ChooserValueFromLabel(item)) }
endfunction
" telescope extension {{{1
if!exists('g:arduino_telescope_enabled') &&has('nvim')
letg:arduino_telescope_enabled=luaeval("pcall(require, 'telescope')")
endif
" neovim vim.ui.select {{{1
if!exists('g:arduino_nvim_select_enabled') &&has('nvim')
letg:arduino_nvim_select_enabled=luaeval('(vim.ui and vim.ui.select) ~= nil')
endif
" vim:fen:fdm=marker:fmr={{{,}}}