robin/artio.nvim
minimal ui2 fuzzy finder for Neovim
git clone https://git.robinwobin.dev/robin/artio.nvim
8fb9b8a
main
1local function lzrq ( modname ) 2return setmetatable ( {} , { 3__index =function ( _ , key ) 4return require ( modname )[ key ] 5end , 6} ) 7end 8 9local artio =lzrq ( "artio" ) 10local config =lzrq ( "artio.config" ) 11local utils =lzrq ( "artio.utils" ) 12 13local function extend ( t1 , ... ) 14return vim . tbl_deep_extend ( "force" , t1 , ... ) 15end 16 17local builtins ={} 18 19builtins . builtins =function ( props ) 20props =props or {} 21 22return artio . generic ( 23vim . tbl_keys ( builtins ), 24extend ( { 25prompt ="builtins" , 26on_close =function ( fname , _ ) 27if not builtins [ fname ] then 28return 29end 30 31artio . schedule ( builtins [ fname ]) 32end , 33} , props ) 34) 35end 36 37---@class artio.picker.generic.fs.Props : artio.Picker.config 38---@field base_dir? string 39 40local findprg =vim . fn . executable ( "fd" ) == 1 and "fd -H -p -a -t f --color=never --" 41or "find . -type f -iregex '.*$*.*'" 42 43---@class artio.picker.files.Props : artio.picker.generic.fs.Props 44---@field findprg? string 45 46---@param props? artio.picker.files.Props 47builtins . files =function ( props ) 48props =props or {} 49props . findprg =props . findprg or findprg 50 51local base_dir =props . base_dir or vim . fn . getcwd ( 0 ) 52 53return artio . pick ( extend ( { 54prompt ="files" , 55live =false , 56get_items =utils . make_cmd ( props . findprg , { 57cwd =base_dir , 58} ), 59fn =artio . sorter , 60on_close =function ( text , _ ) 61artio . schedule ( function () 62utils . edit ( text ) 63end ) 64end , 65format_item =function ( item ) 66return vim . fs . relpath ( base_dir , item ) or item 67end , 68get_icon =config . get (). opts . use_icons and function ( item ) 69return require ( "mini.icons" ). get ( "file" , item . v ) 70end or nil , 71preview_item =function ( item ) 72return { buf =vim . fn . bufadd ( item ) } 73end , 74actions =extend ( 75{} , 76utils . make_fileactions ( function ( item ) 77return vim . fn . bufnr ( item . v , true ) 78end ), 79utils . make_setqflistaction ( function ( item ) 80return { filename =item . v } 81end ) 82), 83} , props )) 84end 85 86---@class artio.picker.grep.Props : artio.picker.generic.fs.Props 87---@field grepprg? string 88 89---@param props? artio.picker.grep.Props 90builtins . grep =function ( props ) 91props =props or {} 92props . grepprg =props . grepprg or vim . o . grepprg 93 94local base_dir =props . base_dir or vim . fn . getcwd ( 0 ) 95local ui2 =require ( "vim._core.ui2" ) 96local grepcmd =utils . make_cmd ( props . grepprg , { 97cwd =base_dir , 98} ) 99 100return artio . pick ( extend ( { 101prompt ="grep" , 102live =true , 103get_items =function ( input ) 104if input == "" then 105return {} 106end 107 108local lines , err =grepcmd ( input ) 109if err then 110return vim 111. iter ( ipairs ( lines )) 112: map ( function ( i , l ) 113return { 114id =i , 115v ={} , 116text =l , 117} 118end ) 119: totable () 120end 121 122local co =coroutine . running () 123assert ( co ) 124 125coroutine . wrap ( vim . schedule_wrap ( function () 126vim . fn . setloclist ( ui2 . wins . cmd , {} , " " , { 127title ="grep[" .. input .. "]" , 128lines =lines , 129efm =vim . o . grepformat , 130nr ="$" , 131} ) 132local results =vim 133. iter ( ipairs ( vim . fn . getloclist ( ui2 . wins . cmd ))) 134: map ( function ( i , locitem ) 135local name =vim . fs . abspath ( vim . fn . bufname ( locitem . bufnr )) 136return { 137id =i , 138v ={ name , locitem . lnum , locitem . col } , 139text =( "%s:%d:%d:%s" ): format ( vim . fs . relpath ( base_dir , name ), locitem . lnum , locitem . col , locitem . text ), 140} 141end ) 142: totable () 143coroutine . resume ( co , results ) 144end ))() 145 146return coroutine . yield () 147end , 148fn =artio . sorter , 149on_close =function ( item , _ ) 150artio . schedule ( function () 151local win =vim . api . nvim_get_current_win () 152utils . edit ( item [ 1 ], { win =win } ) 153vim . api . nvim_win_set_cursor ( win , { item [ 2 ], item [ 3 ] } ) 154end ) 155end , 156preview_item =function ( item ) 157return { buf =vim . fn . bufadd ( item [ 1 ]), pos ={ item [ 2 ], 0 } } 158end , 159get_icon =config . get (). opts . use_icons and function ( item ) 160return item . v [ 1 ] and require ( "mini.icons" ). get ( "file" , item . v [ 1 ]) or nil 161end or nil , 162hl_item =utils . hl_qfitem , 163actions =extend ( 164{} , 165utils . make_setqflistaction ( function ( item ) 166return { filename =item . v [ 1 ], lnum =item . v [ 2 ], col =item . v [ 3 ], text =item . text } 167end ) 168), 169} , props )) 170end 171 172local function find_oldfiles () 173return vim 174. iter ( vim . v . oldfiles ) 175: filter ( function ( v ) 176return vim . uv . fs_stat ( v ) --[[@as boolean]] 177end ) 178: totable () 179end 180 181builtins . oldfiles =function ( props ) 182props =props or {} 183local lst =find_oldfiles () 184 185return artio . generic ( 186lst , 187extend ( { 188prompt ="oldfiles" , 189on_close =function ( text , _ ) 190artio . schedule ( function () 191utils . edit ( text ) 192end ) 193end , 194get_icon =config . get (). opts . use_icons and function ( item ) 195return require ( "mini.icons" ). get ( "file" , item . v ) 196end or nil , 197preview_item =function ( item ) 198return { buf =vim . fn . bufadd ( item ) } 199end , 200actions =extend ( 201{} , 202utils . make_setqflistaction ( function ( item ) 203return { filename =item . v } 204end ) 205), 206} , props ) 207) 208end 209 210builtins . buffergrep =function ( props ) 211props =props or {} 212local win =vim . api . nvim_get_current_win () 213local buf =vim . api . nvim_win_get_buf ( win ) 214local n =vim . api . nvim_buf_line_count ( buf ) 215local lst ={} ---@type integer[] 216for i =1 , n do 217lst [ # lst + 1 ] =i 218end 219 220local pad =# tostring ( lst [ # lst ]) 221 222return artio . generic ( 223lst , 224extend ( { 225prompt ="buffergrep" , 226on_close =function ( row , _ ) 227vim . schedule ( function () 228vim . api . nvim_win_set_cursor ( win , { row , 0 } ) 229end ) 230end , 231format_item =function ( row ) 232return vim . api . nvim_buf_get_lines ( buf , row - 1 , row , true )[ 1 ] 233end , 234preview_item =function ( row ) 235return { buf =buf , pos ={ row , 0 } } 236end , 237get_icon =function ( row ) 238local v =tostring ( row . v ) 239return ( "%s%s" ): format (( " " ): rep ( pad - # v ), v ) 240end , 241actions =extend ( 242{} , 243utils . make_setqflistaction ( function ( item ) 244return { filename =vim . api . nvim_buf_get_name ( buf ), lnum =item . v } 245end ) 246), 247} , props ) 248) 249end 250 251local function find_helptags () 252local buf =vim . api . nvim_create_buf ( false , true ) 253vim . bo [ buf ]. buftype ="help" 254local tags =vim . api . nvim_buf_call ( buf , function () 255return vim . fn . taglist ( ".*" ) 256end ) 257vim . api . nvim_buf_delete ( buf , { force =true } ) 258return vim . tbl_map ( function ( t ) 259return t . name 260end , tags ) 261end 262 263builtins . helptags =function ( props ) 264props =props or {} 265local lst =find_helptags () 266 267return artio . generic ( 268lst , 269extend ( { 270prompt ="helptags" , 271on_close =function ( text , _ ) 272vim . schedule ( function () 273vim . cmd . help ( text ) 274end ) 275end , 276preview_item =function ( tag ) 277local buf =vim . api . nvim_create_buf ( false , true ) 278 279vim . bo [ buf ]. bufhidden ="wipe" 280vim . bo [ buf ]. buftype ="help" 281 282vim . _with ( { buf =buf } , function () 283vim . cmd . help ( tag ) 284end ) 285 286return { buf =buf } 287end , 288} , props ) 289) 290end 291 292local function find_buffers () 293return vim 294. iter ( vim . api . nvim_list_bufs ()) 295: filter ( function ( bufnr ) 296return vim . api . nvim_buf_is_valid ( bufnr ) and vim . bo [ bufnr ]. buflisted 297end ) 298: totable () 299end 300 301builtins . buffers =function ( props ) 302props =props or {} 303local lst =find_buffers () 304 305return artio . generic ( 306lst , 307vim . tbl_extend ( "force" , { 308prompt ="buffers" , 309format_item =function ( bufnr ) 310return vim . api . nvim_buf_get_name ( bufnr ) 311end , 312on_close =function ( bufnr , _ ) 313vim . schedule ( function () 314vim . cmd . buffer ( bufnr ) 315end ) 316end , 317get_icon =config . get (). opts . use_icons and function ( item ) 318return require ( "mini.icons" ). get ( "file" , vim . api . nvim_buf_get_name ( item . v )) 319end or nil , 320preview_item =function ( item ) 321return { buf =item } 322end , 323} , props ) 324) 325end 326 327---@param currentfile string 328---@param item string 329---@return integer score 330local function matchproximity ( currentfile , item ) 331currentfile =vim . fs . abspath ( currentfile ) 332local parts =vim . split ( currentfile , "/" , { trimempty =true } ) 333item =vim . fs . abspath ( item ) 334 335return vim . iter ( ipairs ( vim . split ( item , "/" , { trimempty =true } ))): fold ( 0 , function ( score , i , part ) 336if part == parts [ i ] then 337return score + 50 338end 339return score 340end ) 341end 342 343--- uses the regular files picker as a base 344--- - boosts items in the bufferlist 345--- - proportionally boosts items that match closely to the current file in proximity within the filesystem 346builtins . smart =function ( props ) 347props =props or {} 348local currentbuf =vim . fn . bufnr ( "%" ) 349local currentfile =vim . api . nvim_buf_get_name ( currentbuf ) 350currentfile =vim . fs . abspath ( currentfile ) 351local alternatebuf =vim . fn . bufnr ( "#" ) 352local lbufnr =vim . fn . bufnr ( "$" ) 353local bufnr_len =# ( string . format ( "%d" , lbufnr )) 354 355props . findprg =props . findprg or findprg 356local base_dir =vim . fn . getcwd ( 0 ) 357 358local recentlst =vim 359. iter ( find_buffers ()) 360: map ( function ( buf ) 361local v =vim . api . nvim_buf_get_name ( buf ) 362return { path =vim . fs . abspath ( v ), buf =buf } 363end ) 364: totable () 365 366return artio . pick ( extend ( { 367prompt ="smart" , 368live =false , 369get_items =function ( _ ) 370local lst =utils . make_cmd ( props . findprg , { 371cwd =base_dir , 372} )() 373 374local results =vim 375. iter ( { recentlst , lst } ) 376: flatten ( 1 ) 377: map ( function ( x ) 378if type ( x ) == "string" then 379x ={ path =x } 380end 381return x 382end ) 383: map ( function ( x ) 384if x . buf and x . buf == currentbuf then 385x . current =true 386elseif x . buf and x . buf == alternatebuf then 387x . alt =true 388end 389return x 390end ) 391: totable () 392 393return vim . list . unique ( results , function ( x ) 394return x . path 395end ) 396end , 397fn =artio . mergesorters ( 398"base" , 399-- use default sorter but only display buffer files if input is empty 400function ( l , input ) 401if # input == 0 then 402return vim 403. iter ( l ) 404: map ( function ( item ) 405if not item . v . buf then 406return 407end 408return { item . id , {} , 0 } 409end ) 410: totable () 411end 412 413return artio . sorter ( l , input ) 414end , 415-- boost files that are open as buffers 416function ( l , _ ) 417return vim 418. iter ( l ) 419: map ( function ( item ) 420if not item . v . buf then 421return 422end 423return { item . id , {} , 100 } 424end ) 425: totable () 426end , 427-- boost files that are close in proximity to the current file 428function ( l , _ ) 429return vim 430. iter ( l ) 431: map ( function ( item ) 432return { item . id , {} , matchproximity ( currentfile , item . v . path ) } 433end ) 434: totable () 435end 436), 437on_close =function ( v , _ ) 438artio . schedule ( function () 439utils . edit ( v . path ) 440end ) 441end , 442format_item =function ( v ) 443local path =vim . fs . relpath ( base_dir , v . path ) or v . path 444local ind =v . current and "%" or v . alt and "#" or " " 445return v . buf and string . format ( "%s (%0" .. bufnr_len .. "d) %s" , ind , v . buf , path ) 446or string . format ( "%s " .. string . rep ( " " , bufnr_len + 2 ) .. " %s" , ind , path ) 447end , 448hl_item =function ( _ ) 449return { 450{ { 0 , 2 } , "Special" } , 451{ { 2 , 3 } , "Delimiter" } , 452{ { 3 , 3 + bufnr_len } , "Number" } , 453{ { 3 + bufnr_len , 3 + bufnr_len + 1 } , "NonText" } , 454} 455end , 456get_icon =config . get (). opts . use_icons and function ( item ) 457return require ( "mini.icons" ). get ( "file" , item . v . path ) 458end or nil , 459preview_item =function ( v ) 460return { buf =vim . fn . bufadd ( v . path ) } 461end , 462actions =extend ( 463{} , 464utils . make_fileactions ( function ( item ) 465return vim . fn . bufnr ( item . v . path , true ) 466end ), 467utils . make_setqflistaction ( function ( item ) 468return { filename =item . v . path } 469end ) 470), 471} , props )) 472end 473 474builtins . colorschemes =function ( props ) 475props =props or {} 476local files =vim . api . nvim_get_runtime_file ( "colors/*.{vim,lua}" , true ) 477local lst =vim . tbl_map ( function ( f ) 478return vim . fs . basename ( f ): gsub ( "%.[^.]+$" , "" ) 479end , files ) 480 481local current =vim . g . colors_name 482local bg =vim . o . background 483 484return artio . generic ( 485lst , 486extend ( { 487prompt ="colorschemes" , 488on_close =function ( text , _ ) 489vim . schedule ( function () 490vim . cmd . colorscheme ( text ) 491end ) 492end , 493on_quit =function () 494-- reset colorscheme 495vim . schedule ( function () 496if vim . g . colors_name ~= current then 497vim . cmd . colorscheme ( current ) 498end 499end ) 500end , 501preview_item =function ( item ) 502vim . cmd . colorscheme ( item ) 503vim . o . background =bg 504 505return {} 506end , 507} , props ) 508) 509end 510 511builtins . highlights =function ( props ) 512props =props or {} 513local hlout =vim . split ( vim . api . nvim_exec2 ( [[ highlight ]] , { output =true } ). output , "\n" , { trimempty =true } ) 514 515local maxw =0 516 517local hls =vim 518. iter ( hlout ) 519: map ( function ( hl ) 520local sp =string . find ( hl , "%s" , 1 ) 521maxw =sp > maxw and sp or maxw 522return { hl : sub ( 1 , sp - 1 ), hl } 523end ) 524: fold ( {} , function ( t , hl ) 525local pad =math . max ( 1 , math . min ( 20 , maxw ) - # hl [ 1 ] + 1 ) 526t [ hl [ 1 ]] =string . gsub ( hl [ 2 ], "%s+" , ( " " ): rep ( pad ), 1 ) 527return t 528end ) 529 530return artio . generic ( 531vim . tbl_keys ( hls ), 532extend ( { 533prompt ="highlights" , 534on_close =function ( hlname , _ ) 535vim . schedule ( function () 536vim . cmd (( "verbose hi %s" ): format ( hlname )) 537end ) 538end , 539format_item =function ( hlname ) 540return hls [ hlname ] 541end , 542hl_item =function ( hlname ) 543local x_start , x_end =string . find ( hlname . text , "%sxxx" ) 544 545return { 546{ { 0 , # hlname . v } , hlname . v } , 547{ { x_start , x_end } , hlname . v } , 548} 549end , 550} , props ) 551) 552end 553 554---@private 555---@param severity vim.diagnostic.Severity 556---@return string 557local function get_severity_hl ( severity ) 558if severity == vim . diagnostic . severity . ERROR then 559return "DiagnosticError" 560elseif severity == vim . diagnostic . severity . WARN then 561return "DiagnosticWarn" 562elseif severity == vim . diagnostic . severity . INFO then 563return "DiagnosticInfo" 564elseif severity == vim . diagnostic . severity . HINT then 565return "DiagnosticHint" 566end 567return "" 568end 569 570---@class artio.picker.diagnostics.Props : artio.Picker.config 571---@field buf? integer defaults to workspace 572 573---@param props? artio.picker.diagnostics.Props 574builtins . diagnostics =function ( props ) 575props =props or {} 576local lst =vim . diagnostic . get ( props . buf ) 577 578return artio . generic ( 579lst , 580extend ( { 581prompt ="diagnostics" , 582format_item =function ( item ) 583local text =item . message 584if item . code then 585text =( "%s [%s]" ): format ( text , item . code ) 586end 587return ( "%d:%d :: %s" ): format ( item . end_lnum , item . end_col , text ) 588end , 589on_close =function ( item , _ ) 590vim . schedule ( function () 591local win =vim . fn . bufwinid ( item . bufnr ) 592if win < 0 then 593vim . api . nvim_win_set_buf ( 0 , item . bufnr ) 594win =0 595end 596vim . api . nvim_set_current_win ( win ) 597vim . api . nvim_win_set_cursor ( win , { item . end_lnum + 1 , item . end_col } ) 598end ) 599end , 600hl_item =function ( item ) 601return { 602{ { 0 , # item . text } , get_severity_hl ( item . v . severity ) } , 603} 604end , 605get_icon =function ( item ) 606if item . v . severity == vim . diagnostic . severity . ERROR then 607return "E" , get_severity_hl ( item . v . severity ) 608elseif item . v . severity == vim . diagnostic . severity . WARN then 609return "W" , get_severity_hl ( item . v . severity ) 610elseif item . v . severity == vim . diagnostic . severity . INFO then 611return "I" , get_severity_hl ( item . v . severity ) 612elseif item . v . severity == vim . diagnostic . severity . HINT then 613return "H" , get_severity_hl ( item . v . severity ) 614end 615return " " 616end , 617} , props ) 618) 619end 620 621---@param props? artio.picker.diagnostics.Props 622builtins . diagnostics_buffer =function ( props ) 623props =props or {} 624props . buf =props . buf or vim . api . nvim_get_current_buf () 625return builtins . diagnostics ( props ) 626end 627 628---@class artio.picker.keymaps.Props : artio.Picker.config 629---@field modes? string[] defaults to all 630 631---@param props? artio.picker.keymaps.Props 632builtins . keymaps =function ( props ) 633props =props or {} 634props . modes =props . modes or { "n" , "i" , "c" , "v" , "x" , "s" , "o" , "t" , "l" } 635 636---@type vim.api.keyset.get_keymap[] 637local lst =vim . iter ( props . modes ): fold ( {} , function ( keymaps , mode ) 638vim . iter ( vim . api . nvim_get_keymap ( mode )): each ( function ( km ) 639keymaps [ # keymaps + 1 ] =km 640end ) 641return keymaps 642end ) 643 644return artio . generic ( 645lst , 646extend ( { 647prompt ="keymaps" , 648format_item =function ( km ) 649return ( "%s %s %s | %s" ): format ( km . mode , km . lhs , km . rhs , km . desc ) 650end , 651---@param km vim.api.keyset.get_keymap 652on_close =function ( km , _ ) 653vim . schedule ( function () 654local out =vim . api . nvim_cmd ( { 655cmd =( "%smap" ): format ( km . mode ), 656args ={ km . lhs } , 657} , { 658output =true , 659} ) 660vim . ( out ) 661end ) 662end , 663} , props ) 664) 665end 666 667builtins . commands =function ( props ) 668props =props or {} 669local lst =vim . api . nvim_get_commands ( {} ) 670 671return artio . generic ( 672vim . tbl_values ( lst ), 673extend ( { 674prompt ="commands" , 675---@param item vim.api.keyset.command_info 676format_item =function ( item ) 677return item . name 678end , 679---@param cmd vim.api.keyset.command_info 680on_close =function ( cmd , _ ) 681local nargs =vim . F . npcall ( tonumber , cmd . nargs ) 682local fmt =( nargs and nargs > 0 ) and ":%s " or ":%s" 683 684artio . schedule ( function () 685vim . api . nvim_feedkeys ( string . format ( fmt , cmd . name ), "n" , false ) 686end ) 687end , 688hl_item =function ( item ) 689return { 690{ { 0 , # item . v . name } , "@function.macro.vim" } , 691} 692end , 693} , props ) 694) 695end 696 697builtins . quickfix =function ( props ) 698props =props or {} 699 700local qfid =vim . fn . getqflist ( { id =0 } ). id 701local qflist =vim . fn . getqflist ( { id =qfid , items =0 } ). items 702 703return artio . generic ( 704vim 705. iter ( ipairs ( qflist )) 706: map ( function ( _ , item ) 707item . name =vim . fn . bufname ( item . bufnr ) 708return item 709end ) 710: totable (), 711extend ( { 712prompt ="quickfix" , 713on_close =function ( _ , idx ) 714artio . schedule ( function () 715vim . cmd ( [[copen]] ) 716local win =vim . fn . getqflist ( { id =qfid , winid =0 } ). winid 717vim . api . nvim_win_set_cursor ( win , { idx , 0 } ) 718end ) 719end , 720format_item =function ( item ) 721return string . format ( "%s:%d:%d:%s" , item . name , item . lnum , item . col , item . text ) 722end , 723preview_item =function ( item ) 724return { buf =item . bufnr , pos ={ item . lnum , item . col } , pos_end ={ item . end_lnum , item . end_col } } 725end , 726get_icon =config . get (). opts . use_icons and function ( item ) 727return require ( "mini.icons" ). get ( "file" , item . v . name ) 728end or nil , 729hl_item =utils . hl_qfitem , 730actions =extend ( 731{} , 732utils . make_setqflistaction ( function ( item ) 733return item . v 734end ) 735), 736} , props ) 737) 738end 739 740builtins . document_symbols =function ( props ) 741props =props or {} 742 743local buf =vim . api . nvim_get_current_buf () 744local win =vim . api . nvim_get_current_win () 745 746vim . lsp . buf . document_symbol ( { 747on_list =function ( what ) 748local lst =what . items 749 750return artio . select ( lst , { 751prompt ="document_symbols" , 752format_item =function ( v ) 753return v . text 754end , 755preview_item =function ( v ) 756return { 757buf =buf , 758pos ={ v . lnum , v . col } , 759pos_end ={ v . end_lnum , v . end_col } , 760} 761end , 762} , function ( v , _ ) 763if not v then 764return 765end 766 767vim . api . nvim_win_set_cursor ( win , { v . lnum , v . col } ) 768end , props ) 769end , 770} ) 771end 772 773return builtins