robin/artio.nvim
minimal ui2 fuzzy finder for Neovim
git clone https://git.robinwobin.dev/robin/artio.nvim
8fb9b8a
main
1local uv =vim . uv 2 3local utils ={} 4 5---@param path string 6---@param ctx? vim.context.mods 7function utils . edit ( path , ctx ) 8local f =function () 9vim . api . nvim_cmd ( { 10cmd ="edit" , 11args ={ path } , 12magic ={ file =false , bar =false } , 13} , {} ) 14end 15 16if ctx then 17return vim . _with ( ctx , f ) 18end 19 20return f () 21end 22 23local function parsechunks ( chunks ) 24local lines ={} 25 26for _ , chunk in ipairs ( chunks ) do 27for _ , line in ipairs ( vim . split ( chunk , "\n" , { trimempty =true } )) do 28table . insert ( lines , line ) 29end 30end 31 32return lines 33end 34 35---@param prg? string 36---@param opts? table 37---@return fun(arg?: string): string[] 38function utils . make_cmd ( prg , opts ) 39---@async 40---@param arg? string 41---@return string[] 42return function ( arg ) 43if not prg then 44return {} 45end 46local cmd , n =prg , nil 47if arg and # arg > 0 then 48arg =string . format ( "'%s'" , arg ) 49cmd , n =prg : gsub ( "%$%*" , arg ) 50if n == 0 then 51cmd =( "%s %s" ): format ( prg , arg ) 52end 53end 54 55local chunks ={} 56local err_chunks ={} 57 58local stdout =uv . new_pipe () 59local stderr =uv . new_pipe () 60 61local co =coroutine . running () 62assert ( co , "utils.make_cmd needs to be run inside a coroutine" ) 63 64uv . spawn ( 65vim . o . shell , 66vim . tbl_extend ( "keep" , { 67stdio ={ nil , stdout , stderr } , 68args ={ vim . o . shellcmdflag , cmd } , 69} , opts or {} ), 70function ( code , signal ) 71if code == 0 then 72local lines =parsechunks ( chunks ) 73coroutine . resume ( co , lines ) 74return 75end 76local err =( "error while running shell cmd %s (%d)" ): format ( signal , code ) 77table . insert ( err_chunks , 1 , err ) 78local lines =parsechunks ( err_chunks ) 79coroutine . resume ( co , lines , true ) 80end 81) 82 83---@diagnostic disable-next-line: param-type-mismatch 84uv . read_start ( stdout , function ( err , data ) 85assert ( not err , err ) 86if data then 87table . insert ( chunks , data ) 88end 89end ) 90 91---@diagnostic disable-next-line: param-type-mismatch 92uv . read_start ( stderr , function ( err , data ) 93assert ( not err , err ) 94if data then 95table . insert ( err_chunks , data ) 96end 97end ) 98 99return coroutine . yield () 100end 101end 102 103---@param fn fun(item: artio.Picker.item): vim.quickfix.entry 104---@return artio.Picker.action 105function utils . make_setqflist ( fn ) 106return require ( "artio" ). wrap ( function ( self ) 107if # ( vim . tbl_keys ( self . marked )) > 0 then 108vim . fn . setqflist ( vim 109. iter ( ipairs ( self : getmarked ())) 110: map ( function ( _ , id ) 111local item =self . items [ id ] 112local qfitem =fn ( item ) 113return qfitem 114end ) 115: totable ()) 116self : cancel () 117return 118end 119 120vim . fn . setqflist ( vim 121. iter ( ipairs ( self . matches )) 122: map ( function ( _ , match ) 123local item =self . items [ match [ 1 ]] 124local qfitem =fn ( item ) 125return qfitem 126end ) 127: totable ()) 128self : cancel () 129end , function ( _ ) 130vim . cmd . copen () 131end ) 132end 133 134---@param fn fun(item: artio.Picker.item): vim.quickfix.entry 135---@return table<string, artio.Picker.action> 136function utils . make_setqflistaction ( fn ) 137return { 138setqflist =utils . make_setqflist ( fn ), 139} 140end 141 142---@param fn fun(item: artio.Picker.item): integer 143---@return table<string, artio.Picker.action> 144function utils . make_fileactions ( fn ) 145return { 146split =require ( "artio" ). wrap ( function ( self ) 147self : cancel () 148end , function ( self ) 149local item =self : getcurrent () 150if not item then 151return 152end 153local buf =fn ( item ) 154vim . api . nvim_open_win ( buf , true , { win =- 1 , vertical =false } ) 155end ), 156vsplit =require ( "artio" ). wrap ( function ( self ) 157self : cancel () 158end , function ( self ) 159local item =self : getcurrent () 160if not item then 161return 162end 163local buf =fn ( item ) 164vim . api . nvim_open_win ( buf , true , { win =- 1 , vertical =true } ) 165end ), 166tabnew =require ( "artio" ). wrap ( function ( self ) 167self : cancel () 168end , function ( self ) 169local item =self : getcurrent () 170if not item then 171return 172end 173local buf =fn ( item ) 174vim . api . nvim_cmd ( { 175cmd ="split" , 176args ={ ( "+%dbuf" ): format ( buf ) } , 177---@diagnostic disable-next-line: missing-fields 178mods ={ 179tab =1 , 180silent =true , 181} , 182} , { 183output =false , 184} ) 185end ), 186} 187end 188 189function utils . hl_qfitem ( item ) 190local name_end =string . find ( item . text , ":" ) 191if not name_end then 192return 193end 194name_end =name_end - 1 195local lnum_end =string . find ( item . text , ":" , name_end + 2 ) 196if not lnum_end then 197return 198end 199lnum_end =lnum_end - 1 200local col_end =string . find ( item . text , ":" , lnum_end + 2 ) 201if not col_end then 202return 203end 204col_end =col_end - 1 205 206return { 207{ { 0 , name_end } , "Title" } , 208{ { name_end , name_end + 1 } , "NonText" } , 209{ { name_end + 1 , lnum_end } , "Number" } , 210{ { lnum_end , lnum_end + 1 } , "NonText" } , 211{ { lnum_end + 1 , col_end } , "Number" } , 212{ { col_end , col_end + 1 } , "NonText" } , 213} 214end 215 216return utils