[+] improve vim, beta

1. add filter callback;
  1.1. partially
    added recording of pressed keys;
  1.2. TODO,
    handle esape to close the window;
    handle to select the entry;
    handle hjkl, arrows
    to change the selection;
    handle pressed keys otherwise, ideally
    after i has been pressed,
    or handle hjkl after escape has been presed;
    to restrict entries to the matching pattern
    visualize current filter, and ins/normal mode
    in the popup title;
This commit is contained in:
Siarhei Siniak 2025-10-15 21:48:30 +03:00
parent 656464fe38
commit f59e0bbe6c

@ -44,6 +44,8 @@ class FastSelect:
self._buffer_frequency : dict[int, int] = dict() self._buffer_frequency : dict[int, int] = dict()
self._buffer_last_used : dict[int, int] = dict() self._buffer_last_used : dict[int, int] = dict()
self._filter_pattern : Optional[str] = None
self._queue : collections.deque[Callable[[], None]] = collections.deque() self._queue : collections.deque[Callable[[], None]] = collections.deque()
self._lock = threading.Lock() self._lock = threading.Lock()
@ -160,6 +162,8 @@ augroup END
) -> Optional[int]: ) -> Optional[int]:
logger.info(dict(msg='started')) logger.info(dict(msg='started'))
self._filter_pattern = ''
self._options = options self._options = options
self._option_id = asyncio.Future[int]() self._option_id = asyncio.Future[int]()
@ -209,6 +213,22 @@ augroup END
result.add_done_callback(future_dump_exception) result.add_done_callback(future_dump_exception)
def on_filter_key(self, key: str) -> None:
logger.info(dict(msg='got key', key=key))
try:
key_str = key.decode('utf-8')
except:
return 0
if not key_str.isprintable():
return 0
with self._lock:
self._filter_pattern += key_str
return 1
async def _on_buf_enter( async def _on_buf_enter(
self, self,
buf_number: int, buf_number: int,
@ -242,6 +262,12 @@ augroup END
'popup_callback', 'popup_callback',
).capitalize() ).capitalize()
filter_name = '{}_{}_{}'.format(
MODULE_NAME,
type(self).__name__.lower(),
'popup_filter',
).capitalize()
if int(vim.eval('exists("{}")'.format(callback_name))) == 1: if int(vim.eval('exists("{}")'.format(callback_name))) == 1:
logger.warning(dict(msg='callback already defined, %s' % callback_name)) logger.warning(dict(msg='callback already defined, %s' % callback_name))
@ -257,6 +283,14 @@ augroup END
callback_name=callback_name, callback_name=callback_name,
)) ))
vim.command(r"""
function! {filter_name}(win_id, key)
return py3eval('online_fxreader_pr34_vim.beta.FastSelect.singleton().on_filter_key(key)', #{key: a:key})
endfunction
""".replace(
'{filter_name}', filter_name,
))
logger.info(dict(msg='before popup')) logger.info(dict(msg='before popup'))
popup_menu = vim.Function('popup_menu') popup_menu = vim.Function('popup_menu')
@ -268,6 +302,7 @@ augroup END
{ {
'title': 'Select a file', 'title': 'Select a file',
'callback': callback_name, 'callback': callback_name,
'filter': filter_name,
'wrap': 1, 'wrap': 1,
'maxwidth': 80, 'maxwidth': 80,
'close': 'button', 'close': 'button',