Skip to content

Commit 38bdd83

Browse files
committed
Add a telescope option chooser plugin
1 parent 1031a5d commit 38bdd83

File tree

2 files changed

+80
-21
lines changed

2 files changed

+80
-21
lines changed

autoload/arduino.vim

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ 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
8487
call arduino#ReloadBoards()
8588
endfunction
8689

@@ -255,7 +258,7 @@ function! arduino#GetBoards() abort
255258
let linesplit = split(line, '=')
256259
let name = linesplit[1]
257260
let board = meta.package . ':' . meta.arch . ':' . board
258-
if index(boards, board) == -1 && !has_key(seen, board)
261+
if !has_key(seen, board)
259262
let seen[board] = 1
260263
call add(boards, {
261264
\ 'label': name,
@@ -337,17 +340,21 @@ function! arduino#GetProgrammers() abort
337340
let programmers = []
338341
if g:arduino_use_cli
339342
let data = s:get_json_output('arduino-cli board details ' . g:arduino_board . ' --list-programmers --format json')
340-
for entry in data['programmers']
341-
call add(programmers, {
342-
\ 'label': entry['name'],
343-
\ 'value': entry['id'],
344-
\ })
345-
endfor
346-
" I'm running into some issues with 3rd party boards (e.g. adafruit:avr:gemma) where the programmer list is empty. If so, fall back to the hardware directory method
347-
if !empty(programmers)
348-
return sort(programmers, 's:ChooserItemOrder')
343+
if has_key(data, 'programmers')
344+
for entry in data['programmers']
345+
call add(programmers, {
346+
\ 'label': entry['name'],
347+
\ 'value': entry['id'],
348+
\ })
349+
endfor
350+
" I'm running into some issues with 3rd party boards (e.g. adafruit:avr:gemma) where the programmer list is empty. If so, fall back to the hardware directory method
351+
if !empty(programmers)
352+
return sort(programmers, 's:ChooserItemOrder')
353+
endif
349354
endif
350355
endif
356+
357+
let seen = {}
351358
for [dir,meta] in items(s:hardware_dirs)
352359
if !isdirectory(dir)
353360
continue
@@ -361,9 +368,15 @@ function! arduino#GetProgrammers() abort
361368
if line =~? '^[^.]*\.name=.*$'
362369
let linesplit = split(line, '\.')
363370
let programmer = linesplit[0]
371+
let linesplit = split(line, '=')
372+
let name = linesplit[1]
364373
let prog = meta.package . ':' . programmer
365-
if index(programmers, prog) == -1
366-
call add(programmers, prog)
374+
if !has_key(seen, prog)
375+
let seen[prog] = 1
376+
call add(programmers, {
377+
\ 'label': name,
378+
\ 'value': prog
379+
\ })
367380
endif
368381
endif
369382
endfor
@@ -396,7 +409,7 @@ function! arduino#ChoosePort(...) abort
396409
if empty(ports)
397410
echoerr "No likely serial ports detected!"
398411
else
399-
call arduino#Choose('Port', ports, 'arduino#SelectPort')
412+
call arduino#Choose('Select Port', ports, 'arduino#SelectPort')
400413
endif
401414
endfunction
402415

@@ -415,7 +428,7 @@ function! arduino#ChooseBoard(...) abort
415428
return
416429
endif
417430
let boards = arduino#GetBoards()
418-
call arduino#Choose('Arduino Board', boards, 'arduino#SelectBoard')
431+
call arduino#Choose('Select Board', boards, 'arduino#SelectBoard')
419432
endfunction
420433

421434
" Callback from board selection. Sets the board and prompts for any options
@@ -428,7 +441,8 @@ function! arduino#SelectBoard(board) abort
428441
\ 'opts': {},
429442
\ 'active_option': '',
430443
\}
431-
call arduino#ChooseBoardOption()
444+
" Have to delay this to give the previous chooser UI time to clear
445+
call timer_start(10, {tid -> arduino#ChooseBoardOption()})
432446
endfunction
433447

434448
" Prompt user for the next unselected board option
@@ -459,7 +473,7 @@ function! arduino#ChooseProgrammer(...) abort
459473
return
460474
endif
461475
let programmers = arduino#GetProgrammers()
462-
call arduino#Choose('Arduino Programmer', programmers, 'arduino#SetProgrammer')
476+
call arduino#Choose('Select Programmer', programmers, 'arduino#SetProgrammer')
463477
endfunction
464478

465479
function! arduino#SetProgrammer(programmer) abort
@@ -659,7 +673,9 @@ function! arduino#Choose(title, raw_items, callback) abort
659673
endif
660674
endfor
661675

662-
if g:arduino_ctrlp_enabled
676+
if g:arduino_telescope_enabled
677+
call luaeval("require('arduino.telescope').choose('".a:title."', _A, '".a:callback."')", items)
678+
elseif g:arduino_ctrlp_enabled
663679
let ext_data = get(g:ctrlp_ext_vars, s:ctrlp_idx)
664680
let ext_data.lname = a:title
665681
let s:ctrlp_list = items
@@ -673,11 +689,10 @@ function! arduino#Choose(title, raw_items, callback) abort
673689
\ 'options': '--prompt="'.a:title.': "'
674690
\ })
675691
else
676-
let labels = s:ConvertItemsToLabels(items)
677-
call map(labels, {i, l ->
692+
let labels = map(copy(items), {i, v ->
678693
\ i < 9
679-
\ ? ' '.(i+1).') '.l
680-
\ : (i+1).') '.l
694+
\ ? ' '.(i+1).') '.v.label
695+
\ : (i+1).') '.v.label
681696
\ })
682697
let labels = [" " . a:title] + labels
683698
let choice = inputlist(labels)

lua/arduino/telescope.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
local themes = require('telescope.themes')
2+
local actions = require('telescope.actions')
3+
local pickers = require('telescope.pickers')
4+
local finders = require('telescope.finders')
5+
local conf = require('telescope.config').values
6+
7+
local M = {}
8+
9+
local entry_maker = function(item)
10+
return {
11+
display = item.label or item.value,
12+
ordinal = item.value,
13+
value = item.value,
14+
}
15+
end
16+
17+
M.choose = function(title, items, callback)
18+
local opts = themes.get_dropdown{
19+
previewer = false,
20+
}
21+
pickers.new(opts, {
22+
prompt_title = title,
23+
finder = finders.new_table {
24+
results = items,
25+
entry_maker = entry_maker,
26+
},
27+
sorter = conf.generic_sorter(opts),
28+
attach_mappings = function(prompt_bufnr)
29+
actions.select_default:replace(function()
30+
local selection = actions.get_selected_entry()
31+
actions.close(prompt_bufnr)
32+
if type(callback) == "string" then
33+
vim.call(callback, selection.value)
34+
else
35+
callback(selection.value)
36+
end
37+
end)
38+
39+
return true
40+
end
41+
}):find()
42+
end
43+
44+
return M

0 commit comments

Comments
 (0)