robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1--- @class (private) vim.undotree.tree.entry 2--- @field child integer[] 3--- @field time integer 4 5--- @alias vim.undotree.tree {[integer]: vim.undotree.tree.entry} 6 7local M ={} 8 9local ns =vim . api . nvim_create_namespace ( 'nvim.undotree' ) 10 11--- @param buf integer 12--- @return vim.fn.undotree.entry[] 13--- @return integer 14local function get_undotree_entries ( buf ) 15local undotree =vim . fn . undotree ( buf ) 16local entries =undotree . entries 17 18--Maybe: `:undo 0` and then `undotree` to get seq 0 time 19table . insert ( entries , 1 , { seq =0 , time =- 1 } ) 20 21return entries , undotree . seq_cur 22end 23 24--- @param ent vim.fn.undotree.entry[] 25--- @param _tree vim.undotree.tree? 26--- @param _last integer? 27--- @return vim.undotree.tree 28local function treefy ( ent , _tree , _last ) 29local tree =_tree or {} 30local last =_last or nil 31 32for idx , v in ipairs ( ent ) do 33local seq =v . seq 34 35if last then 36table . insert ( tree [ last ]. child , seq ) 37else 38assert ( idx == 1 and not _tree ) 39end 40 41tree [ seq ] ={ child ={} , time =v . time } 42if v . alt then 43assert ( last ) 44treefy ( v . alt , tree , last ) 45end 46last =seq 47end 48 49return tree 50end 51 52--- @class (private) vim.undotree.graph_line.base 53--- @field index integer 54--- @field node_count integer 55 56--- @class (private) vim.undotree.graph_line.node: vim.undotree.graph_line.base 57--- @field kind 'node'|'remove'|'nochange_remove' 58--- @field node integer 59 60--- @class (private) vim.undotree.graph_line.branch: vim.undotree.graph_line.base 61--- @field kind 'branch' 62--- @field node integer[] 63 64--- @class (private) vim.undotree.graph_line.remove_branch: vim.undotree.graph_line.base 65--- @field kind 'remove+branch' 66--- @field node integer 67--- @field index2 integer 68 69--- @alias vim.undotree.graph_line 70--- | vim.undotree.graph_line.node 71--- | vim.undotree.graph_line.branch 72--- | vim.undotree.graph_line.remove_branch 73 74--- @param tree vim.undotree.tree 75--- @return vim.undotree.graph_line[] 76local function tree_to_graph_lines ( tree ) 77--- @type vim.undotree.graph_line[] 78local graph_lines ={} 79 80--- @type (integer[]|integer)[] 81local nodes ={ 0 } 82 83while # nodes > 0 do 84local minseq =math . huge 85--- @type integer 86local index 87--- @type integer 88local node_index 89 90for k , v in ipairs ( nodes ) do 91if type ( v ) == 'table' then 92for i , j in ipairs ( v ) do 93if j < minseq then 94minseq =j 95index =k 96node_index =i 97end 98end 99elseif v < minseq then 100assert ( type ( v ) == 'number' ) 101minseq =v 102index =k 103end 104end 105 106local node =nodes [ index ] 107 108--- @param kind 'node'|'remove'|'branch'|'nochange_remove' 109local function add_graph_line ( kind ) 110table . insert ( graph_lines , { kind =kind , index =index , node_count =# nodes , node =node } ) 111end 112 113if type ( node ) == 'number' then 114add_graph_line ( 'node' ) 115 116local child =tree [ node ]. child 117if # child == 0 then 118if index ~= # nodes then 119add_graph_line ( 'remove' ) 120else 121add_graph_line ( 'nochange_remove' ) 122end 123 124table . remove ( nodes , index ) 125elseif # child == 1 then 126nodes [ index ] =child [ 1 ] 127else 128nodes [ index ] =child 129end 130else 131assert ( type ( node ) == 'table' ) 132 133add_graph_line ( 'branch' ) 134 135table . remove ( nodes , index ) 136if # node == 2 then 137--- @cast node [integer, integer] 138table . insert ( nodes , index , math . min ( unpack ( node ))) 139table . insert ( nodes , index , math . max ( unpack ( node ))) 140elseif # node > 2 then 141table . insert ( nodes , index , node [ node_index ]) 142table . insert ( nodes , index , node ) 143table . remove ( node , node_index ) 144end 145end 146end 147 148for k , v in ipairs ( graph_lines ) do 149local next_line =graph_lines [ k + 1 ] 150if v . kind == 'remove' and next_line and next_line . kind == 'branch' then 151v . kind ='remove+branch' 152v . index2 =next_line . index 153table . remove ( graph_lines , k + 1 ) 154end 155end 156 157return graph_lines 158end 159 160--- @param time integer 161--- @return string 162local function undo_fmt_time ( time ) 163if time == - 1 then 164return 'origin' 165end 166 167local diff =os . time () - time 168 169if diff >= 100 then 170if diff < ( 60 * 60 * 12 ) then 171return os . date ( '%H:%M:%S' , time ) --[[@as string]] 172else 173return os . date ( '%Y/%m/%d %H:%M:%S' , time ) --[[@as string]] 174end 175else 176return ( '%d second%s ago' ): format ( diff , diff == 1 and '' or 's' ) 177end 178end 179 180--- @param tree vim.undotree.tree 181--- @param graph_lines vim.undotree.graph_line[] 182--- @param buf integer 183--- @param meta {[integer]:integer} 184--- @param find_seq? integer 185--- @return integer? 186local function buf_apply_graph_lines ( tree , graph_lines , buf , meta , find_seq ) 187-- As in io-buffer, not vim-buffer 188local line_buffer ={} 189local extmark_buffer ={} 190 191--- @type integer? 192local found_seq 193 194for k , v in ipairs ( graph_lines ) do 195local is_last =k == # graph_lines 196 197--- @type string? 198local line 199if v . kind == 'node' then 200-- Work around tagged union narrowing: EmmyLuaLs/emmylua-analyzer-rust#1241. 201local seq =v . node --[[@as integer]] 202line =( '| ' ): rep ( v . index - 1 ) 203.. '*' 204.. ( ' |' ): rep ( v . node_count - v . index ) 205.. ' ' 206.. seq 207.. ' (' 208.. undo_fmt_time ( tree [ seq ]. time ) 209.. ')' 210elseif v . kind == 'remove' then 211line =( '| ' ): rep ( v . index - 1 ) .. ( ' /' ): rep ( v . node_count - v . index ) 212elseif v . kind == 'branch' then 213line =( '| ' ): rep ( v . index - 1 ) .. '|\\' .. ( ' \\' ): rep ( v . node_count - v . index ) 214elseif v . kind == 'remove+branch' then 215assert ( v . index2 ) 216if v . index2 < v . index then 217line =( '| ' ): rep ( v . index2 - 1 ) 218.. '|\\' 219.. ( ' \\' ): rep ( v . index - v . index2 - 1 ) 220.. ' ' 221.. ( ' |' ): rep ( v . node_count - v . index ) 222else 223line =( '| ' ): rep ( v . index - 1 ) 224.. ( ' /' ): rep ( v . index2 - v . index ) 225.. ' /|' 226.. ( ' |' ): rep ( v . node_count - v . index2 - 1 ) 227end 228elseif v . kind == 'nochange_remove' then 229line =nil 230else 231error 'unreachable' 232end 233 234if v . kind == 'node' then 235table . insert ( line_buffer , line ) 236table . insert ( meta , v . node ) 237 238if v . node == find_seq then 239found_seq =# meta 240end 241elseif line then 242table . insert ( extmark_buffer , { { line , 'Normal' } } ) 243end 244 245if next ( extmark_buffer ) and ( v . kind == 'node' or is_last ) then 246local row =vim . api . nvim_buf_line_count ( buf ) 247vim . api . nvim_buf_set_extmark ( buf , ns , row - 1 , 0 , { virt_lines =extmark_buffer } ) 248extmark_buffer ={} 249end 250 251if next ( line_buffer ) and ( v . kind ~= 'node' or is_last ) then 252vim . api . nvim_buf_set_lines ( buf , - 1 , - 1 , true , line_buffer ) 253 254if # line_buffer > 3 then 255local end_ =vim . api . nvim_buf_line_count ( buf ) - 1 256local start =end_ - # line_buffer + 3 257vim . api . nvim_buf_call ( buf , function () 258local w =vim . b [ buf ]. nvim_is_undotree 259if vim . api . nvim_win_is_valid ( w ) and vim . wo [ w ]. foldmethod == 'manual' then 260vim . cmd . fold { range ={ start , end_ } } 261end 262end ) 263end 264 265line_buffer ={} 266end 267end 268 269vim . api . nvim_buf_set_lines ( buf , 0 , 1 , true , {} ) 270 271return found_seq 272end 273 274---@param inbuf integer 275---@param outbuf integer 276---@return {[integer]:integer} 277local function draw ( inbuf , outbuf ) 278local entries , curseq =get_undotree_entries ( inbuf ) 279local tree =treefy ( entries ) 280local graph_lines =tree_to_graph_lines ( tree ) 281 282local meta ={} 283vim . bo [ outbuf ]. modifiable =true 284vim . api . nvim_buf_set_lines ( outbuf , 0 , - 1 , true , {} ) 285vim . api . nvim_buf_clear_namespace ( outbuf , ns , 0 , - 1 ) 286local curseq_line =buf_apply_graph_lines ( tree , graph_lines , outbuf , meta , curseq ) 287vim . bo [ outbuf ]. modifiable =false 288 289vim . schedule ( function () 290if vim . api . nvim_win_is_valid ( vim . b [ outbuf ]. nvim_is_undotree ) then 291vim . api . nvim_win_set_cursor ( vim . b [ outbuf ]. nvim_is_undotree , { assert ( curseq_line ), 0 } ) 292end 293end ) 294 295return meta 296end 297 298--- @class vim.undotree.opts 299--- @inlinedoc 300--- 301--- Buffer to draw the tree into. If omitted, a new buffer is created. 302--- @field bufnr integer? 303--- 304--- Window id to display the tree buffer in. If omitted, a new window is 305--- created with {command}. 306--- @field winid integer? 307--- 308--- Vimscript command to create the window. Default value is "30vnew". 309--- Only used when {winid} is nil. 310--- @field command string? 311--- 312--- Title of the window. If a function, it accepts the buffer number of the 313--- source buffer as its only argument and should return a string. 314--- @field title (string|fun(bufnr:integer):string|nil)? 315 316--- Open a window that displays a textual representation of the [undo-tree], or closes the window if 317--- it is already open. Can also be shown with `:Undotree`. [:Undotree]() 318--- 319--- While in the window, moving the cursor changes the undo. 320--- 321--- The plugin is not loaded by default; use `:packadd` to activate it: 322--- ``` 323--- :packadd nvim.undotree 324--- ``` 325--- 326--- @param opts vim.undotree.opts? 327--- @return boolean? Returns true if the window was already open, nil otherwise 328function M . open ( opts ) 329-- The following lines of code was copied from 330-- `vim.treesitter.dev.inspect_tree` and then modified to fit 331 332vim . validate ( 'opts' , opts , 'table' , true ) 333 334opts =opts or {} 335 336local buf =vim . api . nvim_get_current_buf () 337 338if vim . b [ buf ]. nvim_undotree then 339local w =vim . b [ buf ]. nvim_undotree 340if vim . api . nvim_win_is_valid ( w ) then 341vim . api . nvim_win_close ( w , true ) 342return true 343end 344elseif vim . b [ buf ]. nvim_is_undotree then 345local w =vim . b [ buf ]. nvim_is_undotree 346if vim . api . nvim_win_is_valid ( w ) then 347vim . api . nvim_win_close ( w , true ) 348return true 349end 350end 351 352local w =opts . winid 353if not w then 354vim . cmd ( opts . command or '30vnew' ) 355w =vim . api . nvim_get_current_win () 356end 357 358local b =opts . bufnr 359if b then 360vim . api . nvim_win_set_buf ( w , b ) 361else 362b =vim . api . nvim_win_get_buf ( w ) 363end 364 365vim . b [ buf ]. nvim_undotree =w 366vim . b [ b ]. nvim_is_undotree =w 367 368local title --- @type string? 369local opts_title =opts . title 370if not opts_title then 371local bufname =vim . api . nvim_buf_get_name ( buf ) 372title =string . format ( 'Undo tree for %s' , vim . fn . fnamemodify ( bufname , ':.' )) 373elseif type ( opts_title ) == 'function' then 374title =opts_title ( buf ) 375elseif type ( opts_title ) == 'string' then 376title =opts_title 377end 378 379assert ( type ( title ) == 'string' , 'Window title must be a string' ) 380vim . api . nvim_buf_set_name ( b , title ) 381 382vim . wo [ w ][ 0 ]. scrolloff =5 383vim . wo [ w ][ 0 ]. wrap =false 384vim . wo [ w ][ 0 ]. foldmethod ='manual' 385vim . wo [ w ][ 0 ]. foldenable =true 386vim . wo [ w ][ 0 ]. cursorline =true 387vim . bo [ b ]. buflisted =false 388vim . bo [ b ]. buftype ='nofile' 389vim . bo [ b ]. bufhidden ='wipe' 390vim . bo [ b ]. swapfile =false 391 392local meta =draw ( buf , b ) 393 394vim . api . nvim_win_set_cursor ( w , { vim . api . nvim_buf_line_count ( b ), 0 } ) 395 396local group =vim . api . nvim_create_augroup ( 'nvim.undotree' , {} ) 397 398vim . api . nvim_win_call ( w , function () 399vim . cmd . syntax ( 'region Comment start="(" end=")"' ) 400end ) 401 402vim . api . nvim_create_autocmd ( 'CursorMoved' , { 403group =group , 404buffer =b , 405callback =function () 406local row =vim . fn . line ( '.' ) 407vim . api . nvim_buf_call ( buf , function () 408vim . cmd . undo { meta [ row ], mods ={ silent =true } } 409end ) 410end , 411} ) 412 413vim . api . nvim_create_autocmd ( { 'TextChanged' , 'InsertLeave' } , { 414group =group , 415buffer =buf , 416callback =function () 417if not vim . api . nvim_buf_is_valid ( b ) then 418return true 419end 420 421meta =draw ( buf , b ) 422 423if vim . api . nvim_win_is_valid ( w ) then 424vim . wo [ w ][ 0 ]. foldlevel =99 425end 426end , 427} ) 428 429vim . bo [ b ]. filetype ='nvim-undotree' 430end 431 432return M