1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local clear = n.clear
local eq = t.eq
local exec = n.exec
local api = n.api
local dedent = t.dedent
local Screen = require('test.functional.ui.screen')
---@param reverse_tree {[integer]:integer}
local function generate_undo_tree_from_rev(reverse_tree)
for k, v in ipairs(reverse_tree) do
exec('undo ' .. v)
api.nvim_buf_set_lines(0, 0, -1, true, { tostring(k) })
end
end
---@param buf integer
---@return string
local function buf_get_lines_and_extmark(buf)
local lines = api.nvim_buf_get_lines(buf, 0, -1, true)
local ns = api.nvim_create_namespace('nvim.undotree')
local extmarks = api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
for i = #extmarks, 1, -1 do
local extmark = extmarks[i]
---@type nil,integer,nil,vim.api.keyset.extmark_details
local _, row, _, opts = unpack(extmark)
local virt_lines = assert(opts.virt_lines)
for _, v in ipairs(virt_lines) do
local virt_line = v[1][1]
table.insert(lines, row + 2, virt_line)
end
end
return table.concat(lines, '\n')
end
local function strip_time(text)
return text:gsub('%s-%(.-%)', '')
end
describe(':Undotree', function()
before_each(function()
clear({ args = { '--clean' } })
exec 'packadd nvim.undotree'
end)
it('works', function()
api.nvim_set_current_line('foo')
exec 'Undotree'
local buf = api.nvim_get_current_buf()
local win = api.nvim_get_current_win()
eq(
dedent [[
* 0
* 1]],
strip_time(buf_get_lines_and_extmark(buf))
)
eq(2, api.nvim_win_get_cursor(win)[1])
exec 'wincmd w'
-- Doing changes moves cursor in undotree
exec 'undo'
eq(1, api.nvim_win_get_cursor(win)[1])
api.nvim_set_current_line('bar')
eq(3, api.nvim_win_get_cursor(win)[1])
eq(
dedent [[
* 0
|\
| * 1
* 2]],
strip_time(buf_get_lines_and_extmark(buf))
)
-- Moving the cursor in undotree changes the buffer
eq('bar', api.nvim_get_current_line())
exec 'wincmd w'
exec '2'
exec 'wincmd w'
eq('foo', api.nvim_get_current_line())
end)
it('sync scroll pos when undo', function()
local screen = Screen.new(30, 10)
for _ = 1, n.api.nvim_get_option_value('lines', {}) do
api.nvim_set_current_line('foo')
end
exec 'Undotree'
exec 'wincmd w'
exec 'silent undo'
screen:expect([[
* 3 │^foo |
* 4 │{1:~ }|
* 5 │{1:~ }|
* 6 │{1:~ }|
* 7 │{1:~ }|
* 8 │{1:~ }|
{21:* 9 }│{1:~ }|
* 10 │{1:~ }|
{2:<or [-] }{3:[No Name] [+] }|
|
]])
end)
describe('branch+remove is correctly graphed', function()
it('when branching left', function()
generate_undo_tree_from_rev({ 0, 1, 2, 3, 1, 3, 4, 3, 2, 0 })
exec 'Undotree'
eq(
dedent([[
* 0
|\
| * 1
| |\
| | * 2
| | |\
| | | * 3
| | | |\
| | | | * 4
| * | | | 5
| / /| |]] --[[This is the line being tested, e.g. remove&branch left]] .. '\n' .. [[
| | | * | 6
| | | /
| | | * 7
| | * 8
| * 9
* 10]]),
strip_time(buf_get_lines_and_extmark(0))
)
end)
it('when branching right', function()
generate_undo_tree_from_rev({ 0, 1, 2, 3, 3, 1, 4, 2, 1, 0 })
exec 'Undotree'
eq(
dedent([[
* 0
|\
| * 1
| |\
| | * 2
| | |\
| | | * 3
| | | |\
| | | | * 4
| | | * | 5
| |\ \ |]] --[[This is the line being tested, e.g. remove&branch right]] .. '\n' .. [[
| | * | | 6
| | / /
| | | * 7
| | * 8
| * 9
* 10]]),
strip_time(buf_get_lines_and_extmark(0))
)
end)
end)
end)
|