robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1--- @brief 2--- Directory listing for `:edit <dir>`. 3 4local api =vim . api 5 6local M ={} 7 8--- An entry rendered as one line in a listing buffer. 9---@class (internal) nvim.dir.Entry 10---@field name string 11---@field dir boolean 12 13--- Handler for `Provider.list`; call once with either an error or entries. 14---@alias (private) nvim.dir.ListHandler fun(err: string?, entries: nvim.dir.Entry[]?) 15 16--- Source adapter that provides entries and listing actions. 17---@class (private) nvim.dir.Provider 18--- Produce entries for this listing. 19---@field list fun(buf: integer, name: string, cb: nvim.dir.ListHandler) 20--- Open an entry from the listing. 21---@field open fun(buf: integer, name: string, entry: nvim.dir.Entry) 22--- Open the parent listing. 23---@field open_parent fun(buf: integer, name: string) 24--- Run provider-specific buffer setup after a successful open. 25---@field init? fun(buf: integer, name: string) 26 27---@class (private) nvim.dir.State 28---@field gen integer 29---@field err? string 30---@field provider? nvim.dir.Provider 31 32local listing_group =api . nvim_create_augroup ( 'nvim.dir.listing' , { clear =false } ) 33 34---@param err any 35local function notify_error ( err ) 36vim . notify ( 'dir: ' .. tostring ( err ), vim . log . levels . ERROR ) 37end 38 39---@param buf integer 40---@param options [string, any][] 41---@return boolean 42local function set_buf_options ( buf , options ) 43for _ , option in ipairs ( options ) do 44if not api . nvim_buf_is_valid ( buf ) then 45return false 46end 47api . nvim_set_option_value ( option [ 1 ], option [ 2 ], { buf =buf } ) 48end 49return api . nvim_buf_is_valid ( buf ) 50end 51 52---@param name string 53---@return string 54local function encode_name ( name ) 55return ( name : gsub ( '\n' , '\0' )) 56end 57 58---@param entry nvim.dir.Entry 59---@return string 60local function entry_line ( entry ) 61return encode_name ( entry . name ) .. ( entry . dir and '/' or '' ) 62end 63 64---@param entry nvim.dir.Entry 65local function select_entry ( entry ) 66local line =entry_line ( entry ) 67vim . fn . search ( [[\C\m^\V]] .. vim . fn . escape ( line , [[\]] ) .. [[\m$]] , 'cw' ) 68end 69 70---@param buf integer 71---@param name string 72---@param entries nvim.dir.Entry[] 73---@return boolean 74local function render_entries ( buf , name , entries ) 75local lines ={} ---@type string[] 76for i , entry in ipairs ( entries ) do 77lines [ i ] =entry_line ( entry ) 78end 79 80if 81not set_buf_options ( buf , { 82{ 'modeline' , false } , 83{ 'buftype' , 'nowrite' } , 84{ 'buflisted' , true } , 85{ 'swapfile' , false } , 86{ 'readonly' , false } , 87{ 'modifiable' , true } , 88} ) 89then 90return false 91end 92api . nvim_buf_call ( buf , function () 93api . nvim_cmd ( { 94cmd ='file' , 95args ={ name } , 96mods ={ keepalt =true } , 97magic ={ file =false , bar =false } , 98} , {} ) 99end ) 100if not api . nvim_buf_is_valid ( buf ) then 101return false 102end 103api . nvim_buf_set_lines ( buf , 0 , - 1 , false , lines ) 104if not api . nvim_buf_is_valid ( buf ) then 105return false 106end 107return set_buf_options ( buf , { 108{ 'modified' , false } , 109{ 'readonly' , true } , 110{ 'modifiable' , false } , 111} ) 112end 113 114--- Stop tracking a listing and remove its autocmds. 115---@param buf integer 116local function close_listing ( buf ) 117if not api . nvim_buf_is_valid ( buf ) then 118return 119end 120vim . b [ buf ]. nvim_dir =nil 121api . nvim_clear_autocmds ( { group =listing_group , buffer =buf } ) 122end 123 124---@param buf integer 125---@return nvim.dir.State? 126local function get_state ( buf ) 127if not api . nvim_buf_is_valid ( buf ) then 128return nil 129end 130local state =vim . b [ buf ]. nvim_dir 131return type ( state ) == 'table' and state or nil 132end 133 134---@param buf integer 135---@return nvim.dir.Entry? 136local function current_entry ( buf ) 137local state =get_state ( buf ) 138if not state or not state . provider or api . nvim_get_current_buf () ~= buf then 139return nil 140end 141local line =api . nvim_get_current_line () 142if line == '' then 143return nil 144end 145local dir =line : sub ( - 1 ) == '/' 146if dir then 147line =line : sub ( 1 , - 2 ) 148end 149if line == '' then 150return nil 151end 152return { name =line : gsub ( '%z' , '\n' ), dir =dir } 153end 154 155---@type fun(buf: integer, name: string, provider: nvim.dir.Provider, restore_view?: table, setup?: boolean, select?: nvim.dir.Entry) 156local load 157 158---@param buf integer 159---@param provider nvim.dir.Provider 160local function reload ( buf , provider ) 161local state =get_state ( buf ) 162if not state or not state . provider then 163return 164end 165local restore_view =api . nvim_get_current_buf () == buf and vim . fn . winsaveview () or nil 166load ( buf , api . nvim_buf_get_name ( buf ), provider , restore_view ) 167end 168 169---@param buf integer 170---@param lhs string 171---@param plug string 172---@param force? boolean 173function M . _map ( buf , lhs , plug , force ) 174api . nvim_buf_call ( buf , function () 175if ( force or vim . fn . mapcheck ( lhs , 'n' ) == '' ) and vim . fn . hasmapto ( plug , 'n' ) == 0 then 176vim . keymap . set ( 'n' , lhs , plug , { buffer =buf , silent =true } ) 177end 178end ) 179end 180 181--- Let handlers reshape the rendered listing. Unlocks the buffer for the duration and 182--- restores it afterwards, so a handler can sort, filter, or delete lines with ordinary 183--- commands. 184---@param buf integer 185---@return boolean 186local function exec_render_autocmd ( buf ) 187if not set_buf_options ( buf , { { 'readonly' , false } , { 'modifiable' , true } } ) then 188return false 189end 190-- Errors in handlers are reported by the autocmd machinery; swallow them here so the 191-- buffer is never left writable. 192pcall ( api . nvim_buf_call , buf , function () 193api . nvim_exec_autocmds ( 'User' , { pattern ='DirReadPost' , modeline =false } ) 194end ) 195return set_buf_options ( buf , { 196{ 'modified' , false } , 197{ 'readonly' , true } , 198{ 'modifiable' , false } , 199} ) 200end 201 202---@param buf integer 203local function setup_render_autocmds ( buf ) 204api . nvim_clear_autocmds ( { group =listing_group , buffer =buf } ) 205api . nvim_create_autocmd ( 'BufReadCmd' , { 206group =listing_group , 207buffer =buf , 208nested =true , 209desc ='Reload directory listing' , 210callback =function () 211M . _reload ( buf ) 212end , 213} ) 214end 215 216--- Request entries and render the current list generation. 217---@param buf integer 218---@param name string 219---@param provider nvim.dir.Provider 220---@param restore_view? table 221---@param setup? boolean 222---@param select? nvim.dir.Entry 223function load ( buf , name , provider , restore_view , setup , select ) 224local state =get_state ( buf ) or { gen =0 } 225local list_gen =state . gen + 1 226state . gen =list_gen 227vim . b [ buf ]. nvim_dir =state 228-- Render an empty listing (retried by "R" or :edit) if the initial load fails, 229-- but preserve an existing listing on reload failure. 230local first_render =state . provider == nil 231local done =false 232 233---@param err string? 234---@param entries nvim.dir.Entry[]? 235local function on_list ( err , entries ) 236if done then 237return 238end 239done =true 240local current_state =get_state ( buf ) 241if not current_state or current_state . gen ~= list_gen then 242return 243end 244if err ~= nil then 245notify_error ( err ) 246if not first_render then 247current_state . err =err 248vim . b [ buf ]. nvim_dir =current_state 249return 250end 251entries ={} 252end 253---@cast entries nvim.dir.Entry[] 254 255if not render_entries ( buf , name , entries ) then 256if first_render then 257close_listing ( buf ) 258end 259return 260end 261current_state . err , current_state . provider =err , provider 262vim . b [ buf ]. nvim_dir =current_state 263 264-- Runs before the cursor is placed, so handlers may reorder entries. 265if not exec_render_autocmd ( buf ) then 266return 267end 268if restore_view and api . nvim_get_current_buf () == buf then 269vim . fn . winrestview ( restore_view ) 270end 271if select and api . nvim_get_current_buf () == buf then 272select_entry ( select ) 273end 274 275if not setup then 276return 277end 278 279setup_render_autocmds ( buf ) 280M . _map ( buf , '<CR>' , '<Plug>(nvim-dir-open)' , true ) 281M . _map ( buf , '-' , '<Plug>(nvim-dir-up)' ) 282M . _map ( buf , 'R' , '<Plug>(nvim-dir-reload)' , true ) 283if provider . init then 284provider . init ( buf , name ) 285end 286end 287 288local ok , call_err =pcall ( provider . list , buf , name , on_list ) ---@type boolean, any 289if not ok then 290if done then 291-- The handler already ran and would ignore this error: propagate it. 292error ( call_err , 0 ) 293end 294-- Route provider exceptions through the list handler so failures share one cleanup path. 295on_list ( tostring ( call_err ), nil ) 296end 297end 298 299--- Open {buf} as a listing named {name} using {provider}. 300---@param buf integer 301---@param name string 302---@param provider nvim.dir.Provider 303---@param select? nvim.dir.Entry 304function M . open ( buf , name , provider , select ) 305buf =vim . _resolve_bufnr ( buf ) 306local state =get_state ( buf ) 307local restore_view =state 308and state . provider 309and api . nvim_get_current_buf () == buf 310and vim . fn . winsaveview () 311or nil 312load ( buf , name , provider , restore_view , true , select ) 313end 314 315---@param buf integer 316---@return nvim.dir.Provider? 317local function get_provider ( buf ) 318local state =get_state ( buf ) 319return state and state . provider or nil 320end 321 322---@param cmd? 'split'|'vsplit' 323function M . _open_entry ( cmd ) 324local buf =api . nvim_get_current_buf () 325local provider =get_provider ( buf ) 326local entry =current_entry ( buf ) 327if not provider or not entry then 328return 329end 330local name =api . nvim_buf_get_name ( buf ) 331local win =cmd and api . nvim_get_current_win () 332local ok , err =pcall ( function () 333if cmd then 334api . nvim_cmd ( { cmd =cmd } , {} ) 335end 336provider . open ( buf , name , entry ) 337end ) 338if win and api . nvim_win_is_valid ( win ) then 339api . nvim_set_current_win ( win ) 340end 341if not ok then 342error ( err , 0 ) 343end 344end 345 346function M . _open_parent () 347local buf =api . nvim_get_current_buf () 348local provider =get_provider ( buf ) 349if provider then 350provider . open_parent ( buf , api . nvim_buf_get_name ( buf )) 351return 352end 353-- Keep the global `-` mapping useful from regular file buffers. 354require ( 'nvim.dir.fs' ). open_parent_path ( api . nvim_buf_get_name ( buf )) 355end 356 357--- Reload the existing listing with its current buffer name and provider. 358--- Replace the lines and restore the view only after a successful list; preserve the existing 359--- listing on failure. Do not rerun provider initialization or reinstall listing handlers. 360---@param buf? integer 361function M . _reload ( buf ) 362buf =vim . _resolve_bufnr ( buf ) 363local provider =get_provider ( buf ) 364if provider then 365reload ( buf , provider ) 366end 367end 368 369---@param buf integer 370---@param path string 371function M . try_open ( buf , path ) 372buf =vim . _resolve_bufnr ( buf ) 373local fs_provider =require ( 'nvim.dir.fs' ) 374if fs_provider . is_navigating () or path == '' then 375return 376end 377if vim . b [ buf ]. nvim_dir ~= nil then 378return 379end 380if vim . bo [ buf ]. buftype ~= '' then 381return 382end 383if vim . bo [ buf ]. filetype == 'netrw' or vim . b [ buf ]. netrw_curdir ~= nil then 384return 385end 386 387if vim . fn . isdirectory ( path ) == 0 then 388return 389end 390M . open ( buf , fs_provider . normalize ( path ), fs_provider ) 391end 392 393function M . handle_startup_dirs () 394for _ , win in ipairs ( api . nvim_tabpage_list_wins ( 0 )) do 395if api . nvim_win_is_valid ( win ) then 396api . nvim_win_call ( win , function () 397local buf =api . nvim_get_current_buf () 398M . try_open ( buf , api . nvim_buf_get_name ( buf )) 399end ) 400end 401end 402end 403 404return M