Make confirm dialog take arbitrary keys
Confirm now takes the list of options, list of keys to select them and the default value. Returned values are always a 1-based index into the list (like SelectFromList) or -1 to mean esc/ctrl-c. This uses a nice popup dialog in vim and a crappy input on neovim.
This commit is contained in:
parent
6b74e584d5
commit
154e727b96
6 changed files with 82 additions and 28 deletions
|
|
@ -82,29 +82,52 @@ endfunction
|
|||
|
||||
function! vimspector#internal#neopopup#Confirm( confirm_id,
|
||||
\ text,
|
||||
\ default_value ) abort
|
||||
\ options,
|
||||
\ default_value,
|
||||
\ keys ) abort
|
||||
|
||||
" Neovim doesn't have an equivalent of popup_dialog, and it's way too much
|
||||
" effort to write one, so we just use confirm().
|
||||
let result = confirm( a:text, '&Yes &No &Default', 3 )
|
||||
let prompt = a:text
|
||||
for opt in a:options
|
||||
let prompt .= ' ' . opt
|
||||
endfor
|
||||
let prompt .= ': '
|
||||
|
||||
" Annoyingly we can't use confirm() here because for some reason it doesn't
|
||||
" render properly in a channel callback. So we use input() and mimic dialog
|
||||
" behaviour.
|
||||
try
|
||||
let result = input( prompt, a:keys[ a:default_value - 1 ] )
|
||||
catch /.*/
|
||||
let result = -1
|
||||
endtry
|
||||
|
||||
" Map the results to what popup_menu_filter would return (ok s:ConfirmCallback
|
||||
" in popup.vim)
|
||||
if result == 2
|
||||
" No is represented as 0
|
||||
let result = 0
|
||||
elseif result == 0
|
||||
if result == ''
|
||||
" User pressed ESC/ctrl-c
|
||||
let result = -1
|
||||
elseif result == 3
|
||||
" Default
|
||||
let result = a:default_value
|
||||
else
|
||||
let index = 1
|
||||
for k in a:keys
|
||||
if k ==? result
|
||||
let result = index
|
||||
break
|
||||
endif
|
||||
let index += 2
|
||||
endfor
|
||||
|
||||
if index >= len( a:keys )
|
||||
let result = -1
|
||||
endif
|
||||
endif
|
||||
|
||||
py3 __import__( 'vimspector', fromlist = [ 'utils' ] ).utils.ConfirmCallback(
|
||||
\ int( vim.eval( 'a:confirm_id' ) ),
|
||||
\ int( vim.eval( 'result' ) ) )
|
||||
endfunction
|
||||
|
||||
" Boilerplate {{{
|
||||
let &cpoptions=s:save_cpo
|
||||
unlet s:save_cpo
|
||||
|
|
|
|||
|
|
@ -42,13 +42,10 @@ function! s:UpdatePopup( id )
|
|||
call popup_settext( a:id, buf )
|
||||
endfunction
|
||||
|
||||
function! s:YesNoDefaultFilter( default_value, id, key ) abort
|
||||
function! s:ConfirmKeyFilter( keys, id, key ) abort
|
||||
if a:key ==# "\<CR>"
|
||||
call popup_close( a:id, s:current_selection + 1 )
|
||||
return 1
|
||||
elseif a:key ==# 'D' || a:key ==# 'd'
|
||||
call popup_close( a:id, a:default_value )
|
||||
return 1
|
||||
elseif index( [ "\<Tab>", "\<Right>" ], a:key ) >= 0
|
||||
let s:current_selection = ( s:current_selection + 1 ) % len( s:selections )
|
||||
call s:UpdatePopup( a:id )
|
||||
|
|
@ -58,9 +55,19 @@ function! s:YesNoDefaultFilter( default_value, id, key ) abort
|
|||
\ ? len( s:selections ) - 1: s:current_selection - 1
|
||||
call s:UpdatePopup( a:id )
|
||||
return 1
|
||||
elseif a:key ==# "\<Esc>" || a:key ==# "\<C-c>"
|
||||
call popup_close( a:id, -1 )
|
||||
return 1
|
||||
endif
|
||||
|
||||
return popup_filter_yesno( a:id, a:key )
|
||||
let index = 1
|
||||
for key in a:keys
|
||||
if a:key ==? key
|
||||
call popup_close( a:id, index )
|
||||
return 1
|
||||
endif
|
||||
let index += 1
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
function! s:ConfirmCallback( confirm_id, id, result ) abort
|
||||
|
|
@ -90,7 +97,8 @@ function! vimspector#internal#popup#Confirm(
|
|||
\ confirm_id,
|
||||
\ text,
|
||||
\ options,
|
||||
\ default_value ) abort
|
||||
\ default_value,
|
||||
\ keys ) abort
|
||||
|
||||
silent! call prop_type_add( 'VimspectorSelectedItem', {
|
||||
\ 'highlight': 'PMenuSel'
|
||||
|
|
@ -112,7 +120,7 @@ function! vimspector#internal#popup#Confirm(
|
|||
|
||||
let config = {
|
||||
\ 'callback': function( 's:ConfirmCallback', [ a:confirm_id ] ),
|
||||
\ 'filter': function( 's:YesNoDefaultFilter', [ a:default_value ] ),
|
||||
\ 'filter': function( 's:ConfirmKeyFilter', [ a:keys ] ),
|
||||
\ 'mapping': v:false,
|
||||
\ }
|
||||
let config = vimspector#internal#popup#SetBorderChars( config )
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue