Skip to content

Commit 8c86ff3

Browse files
committed
Factor out chooser logic into new file
1 parent f0071ee commit 8c86ff3

File tree

2 files changed

+133
-135
lines changed

2 files changed

+133
-135
lines changed

autoload/arduino.vim

Lines changed: 5 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ function! arduino#InitializeConfig() abort
8181
elseif g:arduino_use_cli && !s:has_cli
8282
echoerr 'arduino-cli: command not found'
8383
endif
84-
if !exists('g:arduino_telescope_enabled')
85-
let g:arduino_telescope_enabled = luaeval("pcall(require, 'telescope')")
86-
endif
8784
call arduino#ReloadBoards()
8885
endfunction
8986

@@ -421,7 +418,7 @@ function! arduino#ChoosePort(...) abort
421418
if empty(ports)
422419
echoerr "No likely serial ports detected!"
423420
else
424-
call arduino#Choose('Select Port', ports, 'arduino#SelectPort')
421+
call arduino#chooser#Choose('Select Port', ports, 'arduino#SelectPort')
425422
endif
426423
endfunction
427424

@@ -440,7 +437,7 @@ function! arduino#ChooseBoard(...) abort
440437
return
441438
endif
442439
let boards = arduino#GetBoards()
443-
call arduino#Choose('Select Board', boards, 'arduino#SelectBoard')
440+
call arduino#chooser#Choose('Select Board', boards, 'arduino#SelectBoard')
444441
endfunction
445442

446443
" Callback from board selection. Sets the board and prompts for any options
@@ -463,7 +460,7 @@ function! arduino#ChooseBoardOption() abort
463460
for opt in available_opts
464461
if !has_key(s:callback_data.opts, opt.option)
465462
let s:callback_data.active_option = opt.option
466-
call arduino#Choose(opt.option_label, opt.values, 'arduino#SelectOption')
463+
call arduino#chooser#Choose(opt.option_label, opt.values, 'arduino#SelectOption')
467464
return
468465
endif
469466
endfor
@@ -485,7 +482,7 @@ function! arduino#ChooseProgrammer(...) abort
485482
return
486483
endif
487484
let programmers = arduino#GetProgrammers()
488-
call arduino#Choose('Select Programmer', programmers, 'arduino#SetProgrammer')
485+
call arduino#chooser#Choose('Select Programmer', programmers, 'arduino#SetProgrammer')
489486
endfunction
490487

491488
function! arduino#SetProgrammer(programmer) abort
@@ -630,97 +627,6 @@ function! s:get_json_output(cmd) abort
630627
return py3eval('json.loads(vim.eval("output_str"))')
631628
endfunction
632629

633-
let s:fzf_counter = 0
634-
function! s:fzf_leave(callback, item)
635-
call function(a:callback)(a:item)
636-
let s:fzf_counter -= 1
637-
endfunction
638-
function! s:mk_fzf_callback(callback)
639-
return { item -> s:fzf_leave(a:callback, s:ChooserValueFromLabel(item)) }
640-
endfunction
641-
642-
function! s:ConvertItemsToLabels(items) abort
643-
let longest = 1
644-
for item in a:items
645-
if has_key(item, 'label')
646-
let longest = max([longest, strchars(item['label'])])
647-
endif
648-
endfor
649-
return map(copy(a:items), 's:ChooserItemLabel(v:val, ' . longest . ')')
650-
endfunction
651-
652-
function! s:ChooserItemLabel(item, ...) abort
653-
let pad_amount = a:0 ? a:1 : 0
654-
if has_key(a:item, 'label')
655-
let label = a:item['label']
656-
let spacing = 1 + max([pad_amount - strchars(label), 0])
657-
return label . repeat(' ', spacing) . '[' . a:item['value'] . ']'
658-
endif
659-
return a:item['value']
660-
endfunction
661-
662-
function! s:ChooserValueFromLabel(label) abort
663-
" The label may be in the format 'label [value]'.
664-
" If so, we need to parse out the value
665-
let groups = matchlist(a:label, '\[\(.*\)\]$')
666-
if empty(groups)
667-
return a:label
668-
else
669-
return groups[1]
670-
endif
671-
endfunction
672-
673-
" items should be a list of dictionary items with the following keys:
674-
" label (optional) The string to display
675-
" value The corresponding value passed to the callback
676-
" items may also be a raw list of strings. They will be treated as values
677-
function! arduino#Choose(title, raw_items, callback) abort
678-
let items = []
679-
let dict_type = type({})
680-
for item in a:raw_items
681-
if type(item) == dict_type
682-
call add(items, item)
683-
else
684-
call add(items, {'value': item})
685-
endif
686-
endfor
687-
688-
if g:arduino_telescope_enabled
689-
call luaeval("require('arduino.telescope').choose('".a:title."', _A, '".a:callback."')", items)
690-
elseif g:arduino_ctrlp_enabled
691-
let ext_data = get(g:ctrlp_ext_vars, s:ctrlp_idx)
692-
let ext_data.lname = a:title
693-
let s:ctrlp_list = items
694-
let s:ctrlp_callback = a:callback
695-
call ctrlp#init(s:ctrlp_id)
696-
elseif g:arduino_fzf_enabled
697-
let s:fzf_counter += 1
698-
call fzf#run({
699-
\ 'source': s:ConvertItemsToLabels(items),
700-
\ 'sink': s:mk_fzf_callback(a:callback),
701-
\ 'options': '--prompt="'.a:title.': "'
702-
\ })
703-
else
704-
let labels = map(copy(s:ConvertItemsToLabels(items)), {i, l ->
705-
\ i < 9
706-
\ ? ' '.(i+1).') '.l
707-
\ : (i+1).') '.l
708-
\ })
709-
let labels = [" " . a:title] + labels
710-
let choice = inputlist(labels)
711-
if choice > 0
712-
call call(a:callback, [items[choice-1]['value']])
713-
endif
714-
endif
715-
endfunction
716-
717-
function! arduino#FindExecutable(name) abort
718-
let path = substitute(system('command -v ' . a:name), "\n*$", '', '')
719-
if empty(path) | return 0 | endif
720-
let abspath = resolve(path)
721-
return abspath
722-
endfunction
723-
724630
function! s:CacheLine(lines, varname) abort
725631
if exists(a:varname)
726632
let value = eval(a:varname)
@@ -733,7 +639,7 @@ function! arduino#GetArduinoDir() abort
733639
return g:arduino_dir
734640
endif
735641
let executable = arduino#GetArduinoExecutable()
736-
let arduino_cmd = arduino#FindExecutable(executable)
642+
let arduino_cmd = exepath(executable)
737643
let arduino_dir = fnamemodify(arduino_cmd, ':h')
738644
if s:OS == 'Darwin'
739645
let arduino_dir = fnamemodify(arduino_dir, ':h') . '/Java'
@@ -771,40 +677,4 @@ function! arduino#GetInfo() abort
771677
echo "CLI command : " . arduino#GetCLICompileCommand()
772678
endfunction
773679

774-
" Ctrlp extension {{{1
775-
if exists('g:ctrlp_ext_vars')
776-
if !exists('g:arduino_ctrlp_enabled')
777-
let g:arduino_ctrlp_enabled = 1
778-
endif
779-
let s:ctrlp_idx = len(g:ctrlp_ext_vars)
780-
call add(g:ctrlp_ext_vars, {
781-
\ 'init': 'arduino#ctrlp_GetData()',
782-
\ 'accept': 'arduino#ctrlp_Callback',
783-
\ 'lname': 'arduino',
784-
\ 'sname': 'arduino',
785-
\ 'type': 'line',
786-
\ })
787-
788-
let s:ctrlp_id = g:ctrlp_builtins + len(g:ctrlp_ext_vars)
789-
else
790-
let g:arduino_ctrlp_enabled = 0
791-
endif
792-
793-
function! arduino#ctrlp_GetData() abort
794-
return s:ConvertItemsToLabels(s:ctrlp_list)
795-
endfunction
796-
797-
function! arduino#ctrlp_Callback(mode, str) abort
798-
call ctrlp#exit()
799-
let value = s:ChooserValueFromLabel(a:str)
800-
call call(s:ctrlp_callback, [value])
801-
endfunction
802-
803-
" fzf extension {{{1
804-
if exists("*fzf#run") && !exists('g:arduino_fzf_enabled')
805-
let g:arduino_fzf_enabled = 1
806-
else
807-
let g:arduino_fzf_enabled = 0
808-
endif
809-
810680
" vim:fen:fdm=marker:fmr={{{,}}}

autoload/arduino/chooser.vim

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
" items should be a list of dictionary items with the following keys:
2+
" label (optional) The string to display
3+
" value The corresponding value passed to the callback
4+
" items may also be a raw list of strings. They will be treated as values
5+
function! arduino#chooser#Choose(title, raw_items, callback) abort
6+
let items = []
7+
let dict_type = type({})
8+
for item in a:raw_items
9+
if type(item) == dict_type
10+
call add(items, item)
11+
else
12+
call add(items, {'value': item})
13+
endif
14+
endfor
15+
16+
if g:arduino_telescope_enabled
17+
call luaeval("require('arduino.telescope').choose('".a:title."', _A, '".a:callback."')", items)
18+
elseif g:arduino_ctrlp_enabled
19+
let ext_data = get(g:ctrlp_ext_vars, s:ctrlp_idx)
20+
let ext_data.lname = a:title
21+
let s:ctrlp_list = items
22+
let s:ctrlp_callback = a:callback
23+
call ctrlp#init(s:ctrlp_id)
24+
elseif g:arduino_fzf_enabled
25+
let s:fzf_counter += 1
26+
call fzf#run({
27+
\ 'source': s:ConvertItemsToLabels(items),
28+
\ 'sink': s:mk_fzf_callback(a:callback),
29+
\ 'options': '--prompt="'.a:title.': "'
30+
\ })
31+
else
32+
let labels = map(copy(s:ConvertItemsToLabels(items)), {i, l ->
33+
\ i < 9
34+
\ ? ' '.(i+1).') '.l
35+
\ : (i+1).') '.l
36+
\ })
37+
let labels = [" " . a:title] + labels
38+
let choice = inputlist(labels)
39+
if choice > 0
40+
call call(a:callback, [items[choice-1]['value']])
41+
endif
42+
endif
43+
endfunction
44+
45+
function! s:ConvertItemsToLabels(items) abort
46+
let longest = 1
47+
for item in a:items
48+
if has_key(item, 'label')
49+
let longest = max([longest, strchars(item['label'])])
50+
endif
51+
endfor
52+
return map(copy(a:items), 's:ChooserItemLabel(v:val, ' . longest . ')')
53+
endfunction
54+
55+
function! s:ChooserItemLabel(item, ...) abort
56+
let pad_amount = a:0 ? a:1 : 0
57+
if has_key(a:item, 'label')
58+
let label = a:item['label']
59+
let spacing = 1 + max([pad_amount - strchars(label), 0])
60+
return label . repeat(' ', spacing) . '[' . a:item['value'] . ']'
61+
endif
62+
return a:item['value']
63+
endfunction
64+
65+
function! s:ChooserValueFromLabel(label) abort
66+
" The label may be in the format 'label [value]'.
67+
" If so, we need to parse out the value
68+
let groups = matchlist(a:label, '\[\(.*\)\]$')
69+
if empty(groups)
70+
return a:label
71+
else
72+
return groups[1]
73+
endif
74+
endfunction
75+
76+
" Ctrlp extension {{{1
77+
if exists('g:ctrlp_ext_vars')
78+
if !exists('g:arduino_ctrlp_enabled')
79+
let g:arduino_ctrlp_enabled = 1
80+
endif
81+
let s:ctrlp_idx = len(g:ctrlp_ext_vars)
82+
call add(g:ctrlp_ext_vars, {
83+
\ 'init': 'arduino#chooser#ctrlp_GetData()',
84+
\ 'accept': 'arduino#chooser#ctrlp_Callback',
85+
\ 'lname': 'arduino',
86+
\ 'sname': 'arduino',
87+
\ 'type': 'line',
88+
\ })
89+
90+
let s:ctrlp_id = g:ctrlp_builtins + len(g:ctrlp_ext_vars)
91+
else
92+
let g:arduino_ctrlp_enabled = 0
93+
endif
94+
95+
function! arduino#chooser#ctrlp_GetData() abort
96+
return s:ConvertItemsToLabels(s:ctrlp_list)
97+
endfunction
98+
99+
function! arduino#chooser#ctrlp_Callback(mode, str) abort
100+
call ctrlp#exit()
101+
let value = s:ChooserValueFromLabel(a:str)
102+
call call(s:ctrlp_callback, [value])
103+
endfunction
104+
105+
" fzf extension {{{1
106+
if !exists('g:arduino_fzf_enabled')
107+
if exists("*fzf#run")
108+
let g:arduino_fzf_enabled = 1
109+
else
110+
let g:arduino_fzf_enabled = 0
111+
endif
112+
endif
113+
114+
let s:fzf_counter = 0
115+
function! s:fzf_leave(callback, item)
116+
call function(a:callback)(a:item)
117+
let s:fzf_counter -= 1
118+
endfunction
119+
function! s:mk_fzf_callback(callback)
120+
return { item -> s:fzf_leave(a:callback, s:ChooserValueFromLabel(item)) }
121+
endfunction
122+
123+
" telescope extension {{{1
124+
if !exists('g:arduino_telescope_enabled')
125+
let g:arduino_telescope_enabled = luaeval("pcall(require, 'telescope')")
126+
endif
127+
128+
" vim:fen:fdm=marker:fmr={{{,}}}

0 commit comments

Comments
 (0)