forked from cbochs/grapple.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.lua
632 lines (522 loc) · 17.5 KB
/
window.lua
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
local Path = require("grapple.path")
local Util = require("grapple.util")
---@class grapple.window
---@field content grapple.tag_content | grapple.scope_content | grapple.container_content
---@field entries grapple.window.entry[]
---@field ns_id integer
---@field au_id integer
---@field buf_id integer
---@field win_id integer
---@field win_opts grapple.vim.win_opts
local Window = {}
Window.__index = Window
---@alias grapple.hook_fn fun(window: grapple.window): string? error
---@alias grapple.title_fn fun(...: any): string?
-- Create global namespace for Grapple windows
local WINDOW_NS = vim.api.nvim_create_namespace("grapple")
-- Create global autocommand group for Grapple windows. All autocommands are
-- buffer-local and will be cleared whenever the buffer is closed.
local WINDOW_GROUP = vim.api.nvim_create_augroup("GrappleWindow", { clear = true })
---@param win_opts? grapple.vim.win_opts
---@return grapple.window
function Window:new(win_opts)
return setmetatable({
content = nil,
entries = nil,
ns_id = WINDOW_NS,
au_id = WINDOW_GROUP,
buf_id = nil,
win_id = nil,
alt_win = nil,
win_opts = win_opts or {},
}, self)
end
---Create a valid nvim api window configuration
---@return vim.api.keyset.win_config win_opts
function Window:window_options()
---@type vim.api.keyset.win_config
---@diagnostic disable-next-line: assign-type-mismatch
local opts = vim.tbl_deep_extend("keep", self.win_opts, {})
-- Window title
if self:has_content() and self.content:title() then
opts.title = self.content:title()
end
if opts.title and opts.title_padding then
opts.title = string.format("%s%s%s", opts.title_padding, opts.title, opts.title_padding)
end
if not opts.title then
opts.title_pos = nil
end
-- Remove custom fields
if opts.title_padding then
opts.title_padding = nil
end
-- Add "help" footer for nvim-0.10
if vim.fn.has("nvim-0.10") == 1 then
opts.footer = "Press '?' to toggle Help"
opts.footer_pos = "center"
end
-- Window size
if opts.width and opts.width < 1 then
opts.width = math.max(1, math.floor(vim.o.columns * opts.width))
end
if opts.height and opts.height < 1 then
opts.height = math.max(1, math.floor(vim.o.lines * opts.height))
end
-- Window position
if opts.row and opts.row < 1 then
opts.row = math.max(0, math.floor((vim.o.lines - opts.height) * opts.row - 1))
end
if opts.col and opts.col < 1 then
opts.col = math.max(0, math.floor((vim.o.columns - opts.width) * opts.col - 1))
end
return opts
end
---@return boolean
function Window:is_open()
return self.win_id ~= nil
end
---@return boolean
function Window:is_closed()
return not self:is_open()
end
---@return boolean
function Window:has_content()
return self.content ~= nil
end
function Window:has_entries()
return self.entries ~= nil
end
---@return boolean
function Window:is_rendered()
return self:is_open() and self:has_content() and self:has_entries()
end
function Window:open()
if self:is_open() then
return
end
-- Store current window as the "alternate window"
self.alt_win = vim.api.nvim_get_current_win()
-- Create temporary buffer
self.buf_id = self:create_buffer()
-- Create window
local win_opts = self:window_options()
self.win_id = vim.api.nvim_open_win(self.buf_id, true, win_opts)
-- Set window highlights
self:set_highlight("NormalFloat", "GrappleNormal")
self:set_highlight("FloatBorder", "GrappleBorder")
self:set_highlight("FloatTitle", "GrappleTitle")
self:set_highlight("FloatFooter", "GrappleFooter")
-- Setup window to conceal line IDs
vim.api.nvim_set_option_value("concealcursor", "nvic", { win = self.win_id })
vim.api.nvim_set_option_value("conceallevel", 3, { win = self.win_id })
end
---@return string? error
function Window:close()
if self:is_closed() then
return
end
local err = self:sync()
if err then
return err
end
if vim.api.nvim_win_is_valid(self.win_id) then
vim.api.nvim_win_close(self.win_id, true)
end
self.win_id = nil
self.buf_id = nil
self.alt_win = nil
self.entries = nil
end
---@param content grapple.tag_content | grapple.scope_content | grapple.container_content
---@return string? error
function Window:attach(content)
if self:has_content() then
local err = self:detach()
if err then
return err
end
end
self.content = content
self.entries = nil
end
---@return string? error
function Window:detach()
if not self:has_content() then
return
end
local err = self.content:detach(self)
if err then
return err
end
---@diagnostic disable-next-line: redefined-local
local err = self:sync()
if err then
return err
end
self.content = nil
self.entries = nil
end
---@return string? error
function Window:sync()
if not self:is_rendered() then
return
end
local parsed_entries, err = self:parse_lines()
if not parsed_entries then
return err
end
---@diagnostic disable-next-line: redefined-local
local err = self.content:sync(self.entries, parsed_entries)
if err then
return err
end
return nil
end
---@class grapple.window.entity
---@field current boolean
---@field ... any
---@class grapple.window.entry
---@field data table
---@field line string
---@field index integer
---@field min_col integer unused now
---@field highlights grapple.vim.highlight[]
---@field extmarks grapple.vim.extmark[]
---@class grapple.window.parsed_entry
---@field data any
---@field line string
---@field modified boolean unused (for now)
---@field index? integer
---@field min_col? integer unused now
---@field highlights? grapple.vim.highlight[]
---@field extmarks? grapple.vim.extmark[]
---@return grapple.window.parsed_entry[] | nil, string? error
function Window:parse_lines()
if not self:is_rendered() then
return nil, "window is not rendered"
end
if not vim.api.nvim_buf_is_valid(self.buf_id) then
return nil, "buffer is not valid"
end
---@diagnostic disable: redefined-local
local lines = vim.tbl_filter(Util.not_empty, self:lines())
---@type grapple.window.parsed_entry[]
local parsed_entries = {}
for _, line in ipairs(lines) do
local entry = self.content:parse_line(line, self.entries)
table.insert(parsed_entries, entry)
end
return parsed_entries, nil
end
---@param line string
---@return integer min_col
function Window:minimum_column(line)
return self.content:minimum_column(line)
end
---@return string? error
function Window:refresh()
if not self:is_rendered() then
return "window is not rendered"
end
local err = self:sync()
if err then
return err
end
---@diagnostic disable-next-line: redefined-local
local err = self:render()
if err then
return err
end
end
---Convenience function for getting the line for an entry
---@param entry grapple.window.entry
---@return string line
local function to_line(entry)
return entry.line
end
---Convenience function for getting the highlights for an entry
---@param entry grapple.window.entry
---@return grapple.vim.highlight[]
local function to_highlights(entry)
return entry.highlights
end
---Convenience function for getting the extmark for an entry
---@param entry grapple.window.entry
---@return grapple.vim.extmark[]
local function to_extmarks(entry)
return entry.extmarks
end
---@return string? error
function Window:render()
if self:is_closed() then
return "window is not open"
end
if not self:has_content() then
return "no content available"
end
-- Store cursor location to reposition later
local cursor = vim.api.nvim_win_get_cursor(self.win_id)
-- Prevent "BufLeave" from closing the window
vim.api.nvim_clear_autocmds({
event = { "BufLeave" },
group = self.au_id,
buffer = self.buf_id,
})
-- Replace active buffer
self.buf_id = self:create_buffer()
vim.api.nvim_win_set_buf(self.win_id, self.buf_id)
-- Update window options
local win_opts = self:window_options()
vim.api.nvim_win_set_config(self.win_id, win_opts)
-- Attach the content to the window
---@diagnostic disable-next-line: redefined-local
local err = self.content:attach(self)
if err then
return err
end
-- Update window entries
local entities, err = self.content:entities()
if not entities then
return err
end
self.entries = {}
for i, entity in ipairs(entities) do
-- Change the cursor if the cursor is at the default position (1, 0)
if entity.current and cursor[1] == 1 and cursor[2] == 0 then
cursor = { i, 0 }
end
local entry = self.content:create_entry(entity, i)
table.insert(self.entries, entry)
end
-- Render entries to the buffer
vim.api.nvim_set_option_value("modifiable", true, { buf = self.buf_id })
vim.api.nvim_buf_set_lines(self.buf_id, 0, -1, true, vim.tbl_map(to_line, self.entries))
vim.api.nvim_set_option_value("modifiable", self.content:modifiable(), { buf = self.buf_id })
for _, entry_hl in ipairs(vim.tbl_map(to_highlights, self.entries)) do
for _, hl in ipairs(entry_hl) do
vim.api.nvim_buf_add_highlight(self.buf_id, self.ns_id, hl.hl_group, hl.line, hl.col_start, hl.col_end)
end
end
for _, entry_extmarks in ipairs(vim.tbl_map(to_extmarks, self.entries)) do
for _, extmark in ipairs(entry_extmarks) do
vim.api.nvim_buf_set_extmark(self.buf_id, self.ns_id, extmark.line, extmark.col, extmark.opts)
end
end
-- Prevent undo after content has been rendered. Set undolevels to -1 when
-- creating the buffer and then set back to its global default afterwards
-- See :h clear-undo
local undolevels = vim.api.nvim_get_option_value("undolevels", { scope = "global" })
vim.api.nvim_set_option_value("undolevels", undolevels, { buf = self.buf_id })
-- Restore cursor location
local ok = pcall(vim.api.nvim_win_set_cursor, 0, cursor)
if not ok then
vim.api.nvim_win_set_cursor(self.win_id, { 1, 0 })
end
end
---@return integer buf_id
function Window:create_buffer()
local buf_id = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_option_value("bufhidden", "wipe", { buf = buf_id })
vim.api.nvim_set_option_value("buftype", "nofile", { buf = buf_id })
vim.api.nvim_set_option_value("filetype", "grapple", { buf = buf_id })
vim.api.nvim_set_option_value("modifiable", false, { buf = buf_id })
vim.api.nvim_set_option_value("syntax", "grapple", { buf = buf_id })
vim.api.nvim_set_option_value("undolevels", -1, { buf = buf_id })
self:create_buffer_defaults(buf_id)
return buf_id
end
function Window:create_buffer_defaults(buf_id)
local function constrain_cursor()
if not self:is_rendered() then
return
end
local line = self:current_line()
local expected_column = self:minimum_column(line)
local cursor = vim.api.nvim_win_get_cursor(self.win_id)
if cursor[2] < expected_column then
vim.api.nvim_win_set_cursor(self.win_id, { cursor[1], expected_column })
end
end
vim.api.nvim_create_autocmd("CursorMoved", {
group = self.au_id,
buffer = buf_id,
callback = constrain_cursor,
})
vim.api.nvim_create_autocmd("InsertEnter", {
group = self.au_id,
buffer = buf_id,
callback = vim.schedule_wrap(constrain_cursor),
})
vim.api.nvim_create_autocmd({ "BufLeave", "WinLeave" }, {
group = self.au_id,
buffer = buf_id,
callback = function()
local err = self:close()
if err then
vim.notify(err, vim.log.levels.ERROR)
end
-- Only run once
vim.api.nvim_clear_autocmds({
event = { "BufLeave", "WinLeave" },
group = self.au_id,
buffer = self.buf_id,
})
end,
})
vim.api.nvim_create_autocmd({ "VimResized" }, {
group = self.au_id,
buffer = buf_id,
callback = function()
local win_opts = self:window_options()
vim.api.nvim_win_set_config(self.win_id, win_opts)
end,
})
vim.keymap.set("n", "q", vim.cmd.close, { buffer = buf_id, desc = "Close" })
vim.keymap.set("n", "<c-c>", vim.cmd.close, { buffer = buf_id, desc = "Close" })
vim.keymap.set("n", "<esc>", vim.cmd.close, { buffer = buf_id, desc = "Close" })
end
-- See :h vim.keymap.set
---Safety: used only inside a callback hook when a window is open
---@param mode string | table
---@param lhs string
---@param rhs string | function
---@param opts table | nil
function Window:map(mode, lhs, rhs, opts)
vim.keymap.set(
mode,
lhs,
rhs,
vim.tbl_extend("force", opts or {}, {
buffer = self.buf_id,
})
)
end
---See :h vim.api.nvim_create_autocmd
---Safety: used only inside a callback hook when a window is open
---@param event any
---@param opts vim.api.keyset.create_autocmd
function Window:autocmd(event, opts)
vim.api.nvim_create_autocmd(
event,
vim.tbl_extend("force", opts or {}, {
group = self.au_id,
buffer = self.buf_id,
})
)
end
---Perform an action. Action may be sync or async. Prefer perform_close or
---perform_retain to ensure window state is synced with the content state
---before action is attempted.
---Safety: used only inside a callback hook when a window is open
---@param action grapple.action
---@param opts? grapple.action.options
---@return string? error
function Window:perform(action, opts)
opts = vim.tbl_extend("force", opts or {}, {
window = self,
})
---@diagnostic disable-next-line: redefined-local
local err = self.content:perform(action, opts)
if err then
return err
end
end
---Perform an action after closing the Grapple window
---Safety: used only inside a callback hook when a window is open
---@param action grapple.action
---@param opts? grapple.action.options
---@return string? error
function Window:perform_close(action, opts)
local err = self:close()
if err then
return err
end
return self:perform(action, opts)
end
---Perform an action, ensuring the Grapple window is not closed
---Safety: used only inside a callback hook when a window is open
---@param action grapple.action
---@param opts? grapple.action.options
---@return string? error
function Window:perform_retain(action, opts)
local err = self:sync()
if err then
return err
end
-- Prevent "BufLeave" or "WinLeave" from closing the window
vim.api.nvim_clear_autocmds({
event = { "BufLeave", "WinLeave" },
group = self.au_id,
buffer = self.buf_id,
})
return self:perform(action, opts)
end
---Returns a parsed entry for the current line
---Safety: used only inside a callback hook when a window is open
---@return grapple.window.parsed_entry
function Window:current_entry()
local current_line = self:current_line()
local entry = self.content:parse_line(current_line, self.entries)
return entry
end
---Returns a parsed entry for a line at a given index
---Safety: used only inside a callback hook when a window is open
---@param opts { index: integer }
---@return grapple.window.parsed_entry | nil, string? error
function Window:entry(opts)
local lines = self:lines()
local line = lines[opts.index]
if not line then
return nil, string.format("no entry for index: %s", opts.index)
end
local entry = self.content:parse_line(line, self.entries)
return entry, nil
end
---Safety: used only inside a callback hook when a window is open
---@return string
function Window:current_line()
return vim.api.nvim_get_current_line()
end
---Safety: used only inside a callback hook when a window is open
---@return string[]
function Window:lines()
return vim.api.nvim_buf_get_lines(self.buf_id, 0, -1, true)
end
---Safety: used only inside a callback hook when a window is open
---@return integer[]
function Window:cursor()
return vim.api.nvim_win_get_cursor(self.win_id)
end
---Returns the path for the buffer from current window before opening the
---Grapple window. In a sense, it's like vim's alternate file
---Safety: used only inside a callback hook when a window is open
---@return string | nil
function Window:alternate_path()
-- It's possible that the alternate window is not valid after opening the
-- grapple window. For example, opening Grapple from Telescope
if not vim.api.nvim_win_is_valid(self.alt_win) then
return
end
local alt_buf = vim.api.nvim_win_get_buf(self.alt_win)
local alt_name = vim.api.nvim_buf_get_name(alt_buf)
if alt_name == "" then
return
end
return Path.fs_absolute(alt_name)
end
--- Replaces a highlight group in the window
--- @param new_from string
--- @param new_to string
function Window:set_highlight(new_from, new_to)
local new_entry = new_from .. ":" .. new_to
local replace_pattern = string.format("(%s:[^,]*)", vim.pesc(new_from))
local new_winhighlight, n_replace = vim.wo[self.win_id].winhighlight:gsub(replace_pattern, new_entry)
if n_replace == 0 then
new_winhighlight = new_winhighlight .. "," .. new_entry
end
pcall(function()
vim.wo[self.win_id].winhighlight = new_winhighlight
end)
end
return Window