robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1-- Filesystem backend. 2 3local api =vim . api 4local fs =vim . fs 5 6local M ={} 7 8local navigating =false 9 10---@param path string 11---@return string 12function M . normalize ( path ) 13return fs . normalize ( fs . abspath ( path ), { plain =true } ) 14end 15 16---@return boolean 17function M . is_navigating () 18return navigating 19end 20 21---@param path string 22local function edit ( path ) 23navigating =true 24local ok , err =pcall ( api . nvim_cmd , { 25cmd ='edit' , 26args ={ path } , 27mods ={ keepalt =vim . b . nvim_dir ~= nil } , 28magic ={ file =false , bar =false } , 29} ) 30navigating =false 31if not ok then 32error ( err , 0 ) 33end 34end 35 36---@param path string 37---@param select? nvim.dir.Entry 38local function navigate ( path , select ) 39edit ( path ) 40local buf =api . nvim_get_current_buf () 41local dir =M . normalize ( api . nvim_buf_get_name ( buf )) 42if vim . fn . isdirectory ( dir ) == 0 then 43return 44end 45 46require ( 'nvim.dir' ). open ( buf , dir , M , select ) 47end 48 49---@param path string 50function M . open_parent_path ( path ) 51if path == '' or path : find ( '^%a[%w+.-]*://' ) then 52navigate ( '.' ) 53return 54end 55path =M . normalize ( path ) 56navigate ( fs . dirname ( path ), { name =fs . basename ( path ), dir =vim . fn . isdirectory ( path ) == 1 } ) 57end 58 59---@param _ integer 60---@param path string 61---@param cb fun(err?: string, entries?: nvim.dir.Entry[]) 62function M . list ( _ , path , cb ) 63local entries ={} ---@type nvim.dir.Entry[] 64for name , type , err in fs . dir ( path , { err =true , plain =true } ) do 65if err then 66cb ( err ) 67return 68end 69if type == 'link' and vim . fn . isdirectory ( fs . joinpath ( path , name )) == 1 then 70type ='directory' 71end 72entries [ # entries + 1 ] ={ 73name =name , 74dir =type == 'directory' , 75} 76end 77table . sort ( entries , function ( a , b ) 78if a . dir ~= b . dir then 79return a . dir 80end 81return a . name < b . name 82end ) 83cb ( nil , entries ) 84end 85 86---@param _ integer 87---@param path string 88---@param entry nvim.dir.Entry 89function M . open ( _ , path , entry ) 90navigate ( fs . joinpath ( path , entry . name )) 91end 92 93---@param _ integer 94---@param path string 95function M . open_parent ( _ , path ) 96M . open_parent_path ( path ) 97end 98 99---@param buf integer 100---@param path string 101function M . init ( buf , path ) 102if api . nvim_get_option_value ( 'filetype' , { buf =buf } ) ~= 'directory' then 103api . nvim_set_option_value ( 'filetype' , 'directory' , { buf =buf } ) 104end 105local dir =require ( 'nvim.dir' ) 106vim . keymap . set ( 'n' , '<Plug>(nvim-dir-vsplit)' , function () 107dir . _open_entry ( 'vsplit' ) 108end , { buffer =buf , silent =true , desc ='Open directory entry in a vertical split' } ) 109vim . keymap . set ( 'n' , '<Plug>(nvim-dir-split)' , function () 110dir . _open_entry ( 'split' ) 111end , { buffer =buf , silent =true , desc ='Open directory entry in a horizontal split' } ) 112dir . _map ( buf , 'a' , '<Plug>(nvim-dir-vsplit)' ) 113dir . _map ( buf , 'o' , '<Plug>(nvim-dir-split)' ) 114api . nvim_buf_call ( buf , function () 115pcall ( api . nvim_cmd , { 116cmd ='bcd' , 117args ={ path } , 118magic ={ file =false , bar =false } , 119} ) 120end ) 121end 122 123return M