Last active
March 16, 2023 12:47
-
-
Save Bakudankun/2ccdf7f8f992ae2f7f5135d6d12bc948 to your computer and use it in GitHub Desktop.
Incremental fuzzy search for Vim/Neovim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function s:infs(...) | |
call setloclist(0, [], ' ', #{title: 'infs: ', quickfixtextfunc: function('s:infs_text')}) | |
lopen | |
wincmd p | |
redraw | |
augroup infs-input | |
autocmd CmdlineChanged @ call s:udpate_loclist(getcmdline()) | |
augroup END | |
call input('query: ', get(a:, '1', '')) | |
autocmd! infs-input | |
if getloclist(0)->len() > 0 | |
lopen | |
else | |
lclose | |
endif | |
endfunction | |
function s:udpate_loclist(query) abort | |
if getchar(1) !=# 0 | |
return | |
endif | |
const bufnr = bufnr() | |
call setloclist(0, [], 'r', #{ | |
\ title: 'infs: ' .. a:query, | |
\ items: getline(1, '$') | |
\ ->map({ i, v -> #{text: v, bufnr: bufnr, lnum: i + 1} }) | |
\ ->matchfuzzy(a:query, #{key: 'text'}) | |
\ }) | |
redraw | |
endfunction | |
function s:infs_text(info) abort | |
let items = getloclist(a:info.winid, #{id: a:info.id, items: 1}).items | |
return items[a:info.start_idx - 1 : a:info.end_idx - 1] | |
\ ->map({_, v -> printf('|%3d| %s', v.lnum, v.text->substitute('^\s*', '', ''))}) | |
endfunction | |
command -nargs=? Infs call s:infs(<f-args>) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install
Place infs.vim into
~/.vim/plugin
or~/.config/nvim/plugin
.Usage
:Infs [{initial query}]
License
CC0