robin/artio.nvim
minimal ui2 fuzzy finder for Neovim
git clone https://git.robinwobin.dev/robin/artio.nvim
54b0625
main
1local View =require ( "artio.view" ) 2 3---@alias artio.Picker.item { id: integer, v: any, text: string, icon?: string, icon_hl?: string, hls?: artio.Picker.hl[] } 4---@alias artio.Picker.match { [1]: integer, [2]: integer[], [3]: integer } item, positions, score 5---@alias artio.Picker.matches table<integer, artio.Picker.match> id: match 6---@alias artio.Picker.sorter fun(lst: artio.Picker.item[], input: string): artio.Picker.matches 7---@alias artio.Picker.hl [[integer, integer], string] 8---@alias artio.Picker.action fun(self: artio.Picker) 9 10---@class artio.Picker.config 11---@field items artio.Picker.item[]|string[] 12---@field fn artio.Picker.sorter 13---@field on_close fun(text: string, idx: integer) 14---@field get_items? fun(input: string): artio.Picker.item[] 15---@field format_item? fun(item: any): string 16---@field preview_item? fun(item: any):{buf?:integer, pos?:[integer,integer], pos_end?:[integer,integer]} 17---@field get_icon? fun(item: artio.Picker.item): string, string 18---@field hl_item? fun(item: artio.Picker.item): artio.Picker.hl[] 19---@field on_quit? fun() 20---@field live? boolean 21---@field prompt? string 22---@field defaulttext? string 23---@field prompttext? string 24---@field opts? artio.config.opts 25---@field win? artio.config.win 26---@field actions? table<string, artio.Picker.action> 27---@field mappings? table<string, 'up'|'down'|'accept'|'cancel'|'togglepreview'|string> 28 29---@class artio.Picker : artio.Picker.config 30---@field co thread? 31---@field thread function 32---@field input string 33---@field liveinput? string 34---@field idx integer 1-indexed 35---@field matches artio.Picker.match[] 36---@field marked table<integer, true|nil> 37---@field live boolean 38---@field opts artio.config.opts 39---@field win artio.config.win 40local Picker ={} 41Picker . __index =Picker 42 43---@type artio.Picker? 44Picker . active_picker =nil 45 46---@param props artio.Picker.config 47function Picker : new ( props ) 48vim . validate ( "Picker.items" , props . items , function ( v ) 49if not v then 50return props . get_items and type ( props . get_items ) == "function" 51end 52return type ( v ) == "table" and vim . islist ( v ) 53end ) 54vim . validate ( "Picker:fn" , props . fn , "function" ) 55vim . validate ( "Picker:on_close" , props . on_close , "function" ) 56 57local t =vim . tbl_deep_extend ( "force" , { 58closed =false , 59prompt ="" , 60input =nil , 61liveinput =nil , 62idx =0 , 63matches ={} , 64marked ={} , 65} , require ( "artio.config" ). get (), props ) 66 67if not t . prompttext then 68t . prompttext =t . opts . prompt_title and ( "%s %s" ): format ( t . prompt , t . opts . promptprefix ) or t . opts . promptprefix 69end 70 71t . live =vim . F . if_nil ( t . live , t . get_items ~= nil ) 72 73if t . live then 74t . input ="" 75t . liveinput =t . defaulttext or "" 76else 77t . input =t . defaulttext or "" 78t . liveinput ="" 79end 80 81return setmetatable ( t , Picker ) 82end 83 84---@enum artio.Picker.Action 85local action_enum ={ 86accept =0 , 87cancel =1 , 88} 89 90function Picker : open () 91local ui2 =require ( "vim._core.ui2" ) 92 93if Picker . active_picker and Picker . active_picker ~= self then 94Picker . active_picker : close ( true ) 95end 96Picker . active_picker =self 97 98self . view =View : new ( self ) 99 100ui2 . check_targets () 101self . thread =coroutine . wrap ( function () 102-- self:getitems("") 103-- self.items = self.items or {} 104 105self . view : open () 106 107self : initkeymaps () 108 109self . co =coroutine . running () 110 111vim . api . nvim_exec_autocmds ( "User" , { pattern ="ArtioEnter" } ) 112 113local result =coroutine . yield () 114 115self : close () 116 117while true do 118if result == action_enum . cancel or result ~= action_enum . accept then 119if self . on_quit then 120self . on_quit () 121end 122break 123end 124 125local current =self . matches [ self . idx ] and self . matches [ self . idx ][ 1 ] 126if not current then 127break 128end 129 130local item =self . items [ current ] 131if item then 132self . on_close ( item . v , item . id ) 133end 134 135break 136end 137 138vim . api . nvim_exec_autocmds ( "User" , { pattern ="ArtioLeave" } ) 139end ) 140self . thread () 141end 142 143function Picker : resume () 144if not self . closed then 145return 146end 147self . closed =false 148 149self : open () 150end 151 152---@param free? boolean 153function Picker : close ( free ) 154if self . closed then 155return 156end 157 158if self . view then 159self . view : close () 160end 161 162self : delkeymaps () 163 164self . closed =true 165 166if free then 167self : free () 168end 169end 170 171function Picker : free () 172if self == nil then 173return 174end 175self . items =nil 176self . matches =nil 177self . marked =nil 178self =nil 179vim . schedule ( function () 180collectgarbage ( "collect" ) 181end ) 182end 183 184function Picker : initkeymaps () 185local ui2 =require ( "vim._core.ui2" ) 186 187---@type vim.keymap.set.Opts 188local opts ={ buf =ui2 . bufs . cmd } 189 190if self . actions then 191vim . iter ( pairs ( self . actions )): each ( function ( k , v ) 192vim . keymap . set ( "i" , ( "<Plug>(artio-action-%s)" ): format ( k ), v , opts ) 193end ) 194end 195if self . mappings then 196vim . iter ( pairs ( self . mappings )): each ( function ( k , v ) 197vim . keymap . set ( "i" , k , ( "<Plug>(artio-action-%s)" ): format ( v ), opts ) 198end ) 199end 200end 201 202function Picker : delkeymaps () 203local ui2 =require ( "vim._core.ui2" ) 204 205local keymaps =vim . api . nvim_buf_get_keymap ( ui2 . bufs . cmd , "i" ) 206 207vim . iter ( ipairs ( keymaps )): each ( function ( _ , v ) 208if v . lhs : match ( "^<Plug>(artio-action-" ) or ( v . rhs and v . rhs : match ( "^<Plug>(artio-action-" )) then 209vim . api . nvim_buf_del_keymap ( ui2 . bufs . cmd , "i" , v . lhs ) 210end 211end ) 212end 213 214function Picker : accept () 215self . thread ( action_enum . accept ) 216end 217 218function Picker : cancel () 219self . thread ( action_enum . cancel ) 220end 221 222function Picker : fix () 223self . idx =math . max ( self . idx , self . opts . preselect and 1 or 0 ) 224self . idx =math . min ( self . idx , # self . matches ) 225end 226 227local function item_is_structured ( item ) 228return type ( item ) == "table" and item . id and item . v and item . text 229end 230 231function Picker : getitems ( input ) 232if ( self . items == nil or self . live ) and self . get_items then 233self . items =self . get_items ( input ) 234end 235 236if # self . items > 0 and not item_is_structured ( self . items [ 1 ]) then 237self . items =vim 238. iter ( ipairs ( self . items )) 239: map ( function ( i , v ) 240local text 241if self . format_item and vim . is_callable ( self . format_item ) then 242text =self . format_item ( v ) 243end 244 245return { 246id =i , 247v =v , 248text =text or v , 249} 250end ) 251: totable () 252end 253end 254 255---@param input? string 256function Picker : getmatches ( input ) 257assert ( coroutine . running (), "Picker:getmatches needs to be called from a coroutine" ) 258 259if not input then 260input =self . live and self . liveinput or self . input 261end 262self : getitems ( input ) 263assert ( self . items ~= nil , "Picker.items cannot be nil after Picker:getitems" ) 264 265-- if live, ignore sorting 266if self . live then 267self . matches =self : getallmatches () 268return 269end 270 271self . matches =vim . tbl_values ( self . fn ( self . items , input )) 272table . sort ( self . matches , function ( a , b ) 273return a [ 3 ] > b [ 3 ] 274end ) 275end 276 277---@return artio.Picker.match[] 278function Picker : getallmatches () 279return vim 280. iter ( ipairs ( self . items )) 281: map ( function ( _ , v ) 282return { v . id , {} , 0 } 283end ) 284: totable () 285end 286 287---@param idx integer 288---@param yes? boolean 289function Picker : mark ( idx , yes ) 290self . marked [ idx ] =yes == nil and true or yes 291end 292 293---@return integer[] 294function Picker : getmarked () 295return vim 296. iter ( pairs ( self . marked )) 297: map ( function ( k , v ) 298return v and k or nil 299end ) 300: totable () 301end 302 303---@param idx? integer index in items 304---@return artio.Picker.item? 305function Picker : getcurrent ( idx ) 306if not idx then 307local i =self . idx 308idx =self . matches [ i ] and self . matches [ i ][ 1 ] 309end 310if not idx then 311return 312end 313 314return self . items [ idx ] 315end 316 317function Picker : togglelive () 318-- check if live can be toggled 319if not self . get_items then 320return 321end 322 323-- reset fuzzy search when enabling live search 324if not self . live then 325self . input ="" 326end 327self . live =not self . live 328end 329 330return Picker