robin/artio.nvim
minimal ui2 fuzzy finder for Neovim
git clone https://git.robinwobin.dev/robin/artio.nvim
b608206
main
1local function lzrq ( modname ) 2return setmetatable ( {} , { 3__index =function ( _ , key ) 4return require ( modname )[ key ] 5end , 6} ) 7end 8 9local config =lzrq ( "artio.config" ) 10 11local artio ={} 12 13---@param cfg? artio.config 14artio . setup =function ( cfg ) 15cfg =cfg or {} 16config . set ( cfg ) 17end 18 19---@param a integer[] 20---@param ... integer[] 21---@return integer[] 22local function mergehl ( a , ... ) 23local hl_lists ={ a , ... } 24 25local t =vim . iter ( hl_lists ): fold ( {} , function ( hls , hl_list ) 26for i =1 , # hl_list do 27hls [ hl_list [ i ]] =true 28end 29return hls 30end ) 31return vim . tbl_keys ( t ) 32end 33 34---@param a artio.Picker.match 35---@param b artio.Picker.match 36---@return artio.Picker.match 37local function mergematches ( a , b ) 38return { a [ 1 ], mergehl ( a [ 2 ], b [ 2 ]), a [ 3 ] + b [ 3 ] } 39end 40 41---@param strat 'combine'|'intersect'|'base' 42--- combine: 43--- a, b -> a + ab + b 44--- intersect: 45--- a, b -> ab 46--- base: 47--- a, b -> a + ab 48---@param a artio.Picker.sorter 49---@param ... artio.Picker.sorter 50---@return artio.Picker.sorter 51function artio . mergesorters ( strat , a , ... ) 52local sorters ={ a , ... } ---@type artio.Picker.sorter[] 53 54return function ( lst , input ) 55return vim . iter ( ipairs ( sorters )): fold ( {} , function ( oldmatches , it , sorter ) 56---@type artio.Picker.matches 57local newmatches =sorter ( lst , input ) 58 59return vim 60. iter ( pairs ( newmatches )) 61: fold ( strat == "intersect" and {} or oldmatches , function ( matches , idx , newmatch ) 62local oldmatch =oldmatches [ idx ] 63if oldmatch then 64local next =mergematches ( oldmatch , newmatch ) 65matches [ idx ] =next 66elseif strat == "combine" or it == 1 then 67matches [ idx ] =newmatch 68end 69return matches 70end ) 71end ) 72end 73end 74 75---@type artio.Picker.sorter 76artio . fuzzy_sorter =function ( lst , input ) 77if not lst or # lst == 0 then 78return {} 79end 80 81if not input or # input == 0 then 82return vim . iter ( lst ): fold ( {} , function ( acc , v ) 83acc [ v . id ] ={ v . id , {} , 0 } 84return acc 85end ) 86end 87 88local matches =vim . fn . matchfuzzypos ( lst , input , { key ="text" } ) 89 90local items ={} 91for i =1 , # matches [ 1 ] do 92items [ matches [ 1 ][ i ]. id ] ={ matches [ 1 ][ i ]. id , matches [ 2 ][ i ], matches [ 3 ][ i ] } 93end 94return items 95end 96 97---@type artio.Picker.sorter 98artio . pattern_sorter =function ( lst , input ) 99local match =string . match ( input , "^/[^/]*/" ) 100local pattern =match and string . match ( match , "^/([^/]*)/$" ) 101 102return vim . iter ( lst ): fold ( {} , function ( acc , v ) 103if pattern and not string . match ( v . text , pattern ) then 104return acc 105end 106 107acc [ v . id ] ={ v . id , {} , 0 } 108return acc 109end ) 110end 111 112--- the default sorter provides support for pattern matching. a `/.../` match 113--- at the start of the input will limit the fuzzy sorter to items matching the 114--- pattern. if you want to use `/.../` in your fuzzy matches, make sure to 115--- escape it by starting the input with an empty space (` /.../`). fuzzy 116--- sorting will be done on the input with the pattern removed. 117---@type artio.Picker.sorter 118artio . sorter =artio . mergesorters ( "intersect" , artio . pattern_sorter , function ( lst , input ) 119input =string . gsub ( input , "^/[^/]*/" , "" ) 120return artio . fuzzy_sorter ( lst , input ) 121end ) 122 123---@generic T 124---@param items T[] Arbitrary items 125---@param opts vim.ui.select.Opts Additional options 126---@param on_choice fun(item: T|nil, idx: integer|nil) 127---@param start_opts? artio.Picker.config 128artio . select =function ( items , opts , on_choice , start_opts ) 129return artio . generic ( 130items , 131vim . tbl_deep_extend ( 132"force" , 133{ 134on_close =function ( _ , idx ) 135artio . schedule ( function () 136on_choice ( items [ idx ], idx ) 137end ) 138end , 139on_quit =function () 140artio . schedule ( function () 141on_choice ( nil , nil ) 142end ) 143end , 144} , 145opts or {} , -- opts.prompt, opts.format_item 146start_opts or {} 147) 148) 149end 150 151---@generic T 152---@param items T[] 153---@param props artio.Picker.config 154artio . generic =function ( items , props ) 155return artio . pick ( vim . tbl_deep_extend ( "force" , { 156fn =artio . sorter , 157items =items , 158} , props )) 159end 160 161---@param ... artio.Picker.config 162artio . pick =function ( ... ) 163local Picker =require ( "artio.picker" ) 164return Picker : new ( ... ): open () 165end 166 167---@param f fun(): any 168artio . schedule =function ( f ) 169local Picker =require ( "artio.picker" ) 170local current =Picker . active_picker 171if not current or not current . closed then 172return 173end 174 175vim . schedule ( function () 176vim . defer_fn ( f , 10 ) 177vim . wait ( 1000 , function () 178return coroutine . status ( current . co ) == "dead" 179end ) 180end ) 181end 182 183artio . resume =function () 184local picker =require ( "artio.picker" ). active_picker 185if not picker or not picker . closed then 186return 187end 188picker : resume () 189end 190 191---@param fn artio.Picker.action 192---@param scheduled_fn? artio.Picker.action 193artio . wrap =function ( fn , scheduled_fn ) 194return function () 195local Picker =require ( "artio.picker" ) 196local current =Picker . active_picker 197if not current or current . closed then 198return 199end 200 201-- whether to accept key inputs 202if coroutine . status ( current . co ) ~= "suspended" then 203return 204end 205 206pcall ( fn , current ) 207 208if scheduled_fn == nil then 209return 210end 211artio . schedule ( function () 212pcall ( scheduled_fn , current ) 213end ) 214end 215end 216 217return artio