8000 mini.nvim/lua/mini/bracketed.lua at main · lucastrvsn/mini.nvim · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
2016 lines (1704 loc) · 79.6 KB

bracketed.lua

File metadata and controls

2016 lines (1704 loc) · 79.6 KB
--- gets confusing really guickly in extensively edited buffer.
---
--- This `undo()` target provides a way to cycle through linear undo history
--- **in order states actually appeared**. It does so by registering any new undo
--- states plus every time |MiniBracketed.register_undo_state()| is called. To have
--- more "out of the box" experience, |u| and |<C-R>| are remapped to call it after
--- they perform their undo/redo.
---
--- Example ~
---
--- To show more clearly the difference between advancing this target and using
--- built-in functionality, here is an example:
---
--- - Create undo history in a new buffer (|:new|):
--- - Enter `one two three` text.
--- - Delete first word with `daw` and undo the change with `u`.
--- - Delete second word with `daw` and undo the change with `u`.
--- - Delete third word with `daw` and undo the change with `u`.
---
--- - Now try one of the following (each one after performing previous steps in
--- separate new buffer):
--- - Press `u`. It goes back to empty buffer. Press `<C-R>` twice and it
--- goes to the latest change (`one two`). No way to get to other states
--- (like `two three` or `one three`) with these two keys.
---
--- - Press `g-`. It goes to an empty buffer. Press `g+` 4 times. It cycles
--- through all available undo states **in order they were created**.
---
--- - Finally, press `[u`. It goes back to `one two` - state which was
--- **previously visited** by the user. Another `[u` restores `one two three`.
--- Use `]U` to go to latest visited undo state.
---
---@param direction __bracketed_direction
---@param opts __bracketed_opts
MiniBracketed.undo = function(direction, opts)
if H.is_disabled() then return end
H.validate_direction(direction, { 'first', 'backward', 'forward', 'last' }, 'undo')
opts = vim.tbl_deep_extend('force', { n_times = vim.v.count1, wrap = true }, H.get_config().undo.options, opts or {})
-- Define iterator that traverses undo states in order they appeared
local buf_id = vim.api.nvim_get_current_buf()
H.undo_sync(buf_id, vim.fn.undotree())
local iterator = {}
local buf_history = H.cache.undo[buf_id]
local n = #buf_history
iterator.next = function(id)
if id == nil or n <= id then return end
return id + 1
end
iterator.prev = function(id)
if id == nil or id <= 1 then return end
return id - 1
end
iterator.state = buf_history.current_id
iterator.start_edge = 0
iterator.end_edge = n + 1
0