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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
local Ui = require "mason-core.ui"
local a = require "mason-core.async"
local display = require "mason-core.ui.display"
local match = require "luassert.match"
local spy = require "luassert.spy"
describe("ui", function()
it("produces a correct tree", function()
local function renderer(state)
return Ui.CascadingStyleNode({ "INDENT" }, {
Ui.When(not state.is_active, function()
return Ui.Text {
"I'm not active",
"Another line",
}
end),
Ui.When(state.is_active, function()
return Ui.Text {
"I'm active",
"Yet another line",
}
end),
})
end
assert.same({
children = {
{
type = "HL_TEXT",
lines = {
{ { "I'm not active", "" } },
{ { "Another line", "" } },
},
},
{
type = "NODE",
children = {},
},
},
styles = { "INDENT" },
type = "CASCADING_STYLE",
}, renderer { is_active = false })
assert.same({
children = {
{
type = "NODE",
children = {},
},
{
type = "HL_TEXT",
lines = {
{ { "I'm active", "" } },
{ { "Yet another line", "" } },
},
},
},
styles = { "INDENT" },
type = "CASCADING_STYLE",
}, renderer { is_active = true })
end)
it("renders a tree correctly", function()
local render_output = display._render_node(
{
win_width = 120,
},
Ui.CascadingStyleNode({ "INDENT" }, {
Ui.Keybind("i", "INSTALL_PACKAGE", { "sumneko_lua" }, true),
Ui.HlTextNode {
{
{ "Hello World!", "MyHighlightGroup" },
},
{
{ "Another Line", "Comment" },
},
},
Ui.HlTextNode {
{
{ "Install something idk", "Stuff" },
},
},
Ui.StickyCursor { id = "sticky" },
Ui.Keybind("<CR>", "INSTALL_PACKAGE", { "tsserver" }, false),
Ui.DiagnosticsNode {
message = "yeah this one's outdated",
severity = vim.diagnostic.severity.WARN,
source = "trust me bro",
},
Ui.Text { "I'm a text node" },
})
)
assert.same({
highlights = {
{
col_start = 2,
col_end = 14,
line = 0,
hl_group = "MyHighlightGroup",
},
{
col_start = 2,
col_end = 14,
line = 1,
hl_group = "Comment",
},
{
col_start = 2,
col_end = 23,
line = 2,
hl_group = "Stuff",
},
},
lines = { " Hello World!", " Another Line", " Install something idk", " I'm a text node" },
virt_texts = {},
sticky_cursors = { line_map = { [3] = "sticky" }, id_map = { ["sticky"] = 3 } },
keybinds = {
{
effect = "INSTALL_PACKAGE",
key = "i",
line = -1,
payload = { "sumneko_lua" },
},
{
effect = "INSTALL_PACKAGE",
key = "<CR>",
line = 3,
payload = { "tsserver" },
},
},
diagnostics = {
{
line = 3,
message = "yeah this one's outdated",
source = "trust me bro",
severity = vim.diagnostic.severity.WARN,
},
},
}, render_output)
end)
end)
describe("integration test", function()
it(
"calls vim APIs as expected during rendering",
async_test(function()
local window = display.new_view_only_win("test", "my-filetype")
window.view(function(state)
return Ui.Node {
Ui.Keybind("U", "EFFECT", nil, true),
Ui.Text {
"Line number 1!",
state.text,
},
Ui.Keybind("R", "R_EFFECT", { state.text }),
Ui.HlTextNode {
{
{ "My highlighted text", "MyHighlightGroup" },
},
},
}
end)
local mutate_state = window.state { text = "Initial state" }
local clear_namespace = spy.on(vim.api, "nvim_buf_clear_namespace")
local buf_set_option = spy.on(vim.api, "nvim_buf_set_option")
local win_set_option = spy.on(vim.api, "nvim_win_set_option")
local set_lines = spy.on(vim.api, "nvim_buf_set_lines")
local set_extmark = spy.on(vim.api, "nvim_buf_set_extmark")
local add_highlight = spy.on(vim.api, "nvim_buf_add_highlight")
local set_keymap = spy.on(vim.keymap, "set")
window.init {
effects = {
["EFFECT"] = function() end,
["R_EFFECT"] = function() end,
},
winhighlight = {
"NormalFloat:MasonNormal",
"CursorLine:MasonCursorLine",
},
}
window.open()
-- Initial window and buffer creation + initial render
a.wait(vim.schedule)
assert.spy(win_set_option).was_called(9)
assert.spy(win_set_option).was_called_with(match.is_number(), "number", false)
assert.spy(win_set_option).was_called_with(match.is_number(), "relativenumber", false)
assert.spy(win_set_option).was_called_with(match.is_number(), "wrap", false)
assert.spy(win_set_option).was_called_with(match.is_number(), "spell", false)
assert.spy(win_set_option).was_called_with(match.is_number(), "foldenable", false)
assert.spy(win_set_option).was_called_with(match.is_number(), "signcolumn", "no")
assert.spy(win_set_option).was_called_with(match.is_number(), "colorcolumn", "")
assert.spy(win_set_option).was_called_with(match.is_number(), "cursorline", true)
assert
.spy(win_set_option)
.was_called_with(match.is_number(), "winhighlight", "NormalFloat:MasonNormal,CursorLine:MasonCursorLine")
assert.spy(buf_set_option).was_called(10)
assert.spy(buf_set_option).was_called_with(match.is_number(), "modifiable", false)
assert.spy(buf_set_option).was_called_with(match.is_number(), "swapfile", false)
assert.spy(buf_set_option).was_called_with(match.is_number(), "textwidth", 0)
assert.spy(buf_set_option).was_called_with(match.is_number(), "buftype", "nofile")
assert.spy(buf_set_option).was_called_with(match.is_number(), "bufhidden", "wipe")
assert.spy(buf_set_option).was_called_with(match.is_number(), "buflisted", false)
assert.spy(buf_set_option).was_called_with(match.is_number(), "filetype", "my-filetype")
assert.spy(buf_set_option).was_called_with(match.is_number(), "undolevels", -1)
assert.spy(set_lines).was_called(1)
assert
.spy(set_lines)
.was_called_with(match.is_number(), 0, -1, false, { "Line number 1!", "Initial state", "My highlighted text" })
assert.spy(set_extmark).was_called(0)
assert.spy(add_highlight).was_called(1)
assert
.spy(add_highlight)
.was_called_with(match.is_number(), match.is_number(), "MyHighlightGroup", 2, 0, 19)
assert.spy(set_keymap).was_called(2)
assert.spy(set_keymap).was_called_with(
"n",
"U",
match.is_function(),
match.tbl_containing { nowait = true, silent = true, buffer = match.is_number() }
)
assert.spy(set_keymap).was_called_with(
"n",
"R",
match.is_function(),
match.tbl_containing { nowait = true, silent = true, buffer = match.is_number() }
)
assert.spy(clear_namespace).was_called(1)
assert.spy(clear_namespace).was_called_with(match.is_number(), match.is_number(), 0, -1)
mutate_state(function(state)
state.text = "New state"
end)
assert.spy(set_lines).was_called(1)
a.wait(vim.schedule)
assert.spy(set_lines).was_called(2)
assert
.spy(set_lines)
.was_called_with(match.is_number(), 0, -1, false, { "Line number 1!", "New state", "My highlighted text" })
end)
)
it(
"anchors to sticky cursor",
async_test(function()
local window = display.new_view_only_win("test", "my-filetype")
window.view(function(state)
local extra_lines = state.show_extra_lines
and Ui.Text {
"More",
"Lines",
"Here",
}
or Ui.Node {}
return Ui.Node {
extra_lines,
Ui.Text {
"Line 1",
"Line 2",
"Line 3",
"Line 4",
"Special line",
},
Ui.StickyCursor { id = "special" },
Ui.Text {
"Line 6",
"Line 7",
"Line 8",
"Line 9",
"Line 10",
},
}
end)
local mutate_state = window.state { show_extra_lines = false }
window.init {}
window.open()
a.wait(vim.schedule)
window.set_cursor { 5, 3 } -- move cursor to sticky line
mutate_state(function(state)
state.show_extra_lines = true
end)
a.wait(vim.schedule)
local cursor = window.get_cursor()
assert.same({ 8, 3 }, cursor)
end)
)
it(
"should respect border ui setting",
async_test(function()
local nvim_open_win = spy.on(vim.api, "nvim_open_win")
local window = display.new_view_only_win("test", "my-filetype")
window.view(function()
return Ui.Node {}
end)
window.state {}
window.init { border = "rounded" }
window.open()
a.wait(vim.schedule)
assert.spy(nvim_open_win).was_called(1)
assert.spy(nvim_open_win).was_called_with(
match.is_number(),
true,
match.tbl_containing {
border = "rounded",
}
)
end)
)
end)
|