forked from nvim-neo-tree/neo-tree.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview.lua
452 lines (406 loc) · 13.2 KB
/
preview.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
local vim = vim
local utils = require("neo-tree.utils")
local highlights = require("neo-tree.ui.highlights")
local events = require("neo-tree.events")
local manager = require("neo-tree.sources.manager")
local log = require("neo-tree.log")
local renderer = require("neo-tree.ui.renderer")
local neo_tree_preview_namespace = vim.api.nvim_create_namespace("neo_tree_preview")
local function create_floating_preview_window(state)
local default_position = utils.resolve_config_option(state, "window.position", "left")
state.current_position = state.current_position or default_position
local title = state.config.title or "Neo-tree Preview"
local winwidth = vim.api.nvim_win_get_width(state.winid)
local winheight = vim.api.nvim_win_get_height(state.winid)
local height = vim.o.lines - 4
local width = 120
local row, col = 0, 0
if state.current_position == "left" then
col = winwidth + 1
width = math.min(vim.o.columns - col, 120)
elseif state.current_position == "top" or state.current_position == "bottom" then
height = height - winheight
width = winwidth - 2
if state.current_position == "top" then
row = vim.api.nvim_win_get_height(state.winid) + 1
end
elseif state.current_position == "right" then
width = math.min(vim.o.columns - winwidth - 4, 120)
col = vim.o.columns - winwidth - width - 3
elseif state.current_position == "float" then
local pos = vim.api.nvim_win_get_position(state.winid)
-- preview will be same height and top as tree
row = pos[1] - 1
height = winheight
-- tree and preview window will be side by side and centered in the editor
width = math.min(vim.o.columns - winwidth - 4, 120)
local total_width = winwidth + width + 4
local margin = math.floor((vim.o.columns - total_width) / 2)
col = margin + winwidth + 2
-- move the tree window to make the combined layout centered
local popup = renderer.get_nui_popup(state.winid)
popup:update_layout({
relative = "editor",
position = {
row = row,
col = margin,
},
})
else
local cur_pos = state.current_position or "unknown"
log.error('Preview cannot be used when position = "' .. cur_pos .. '"')
return
end
local popups = require("neo-tree.ui.popups")
local options = popups.popup_options(title, width, {
ns_id = highlights.ns_id,
size = { height = height, width = width },
relative = "editor",
position = {
row = row,
col = col,
},
win_options = {
number = true,
winhighlight = "Normal:"
.. highlights.FLOAT_NORMAL
.. ",FloatBorder:"
.. highlights.FLOAT_BORDER,
},
})
options.zindex = 40
options.buf_options.filetype = "neo-tree-preview"
local NuiPopup = require("nui.popup")
local win = NuiPopup(options)
win:mount()
return win
end
local Preview = {}
local instance = nil
---Creates a new preview.
---@param state table The state of the source.
---@return table preview A new preview. A preview is a table consisting of the following keys:
-- active = boolean Whether the preview is active.
-- winid = number The id of the window being used to preview.
-- is_neo_tree_window boolean Whether the preview window belongs to neo-tree.
-- bufnr = number The buffer that is currently in the preview window.
-- start_pos = array or nil An array-like table specifying the (0-indexed) starting position of the previewed text.
-- end_pos = array or nil An array-like table specifying the (0-indexed) ending position of the preview text.
-- truth = table A table containing information to be restored when the preview ends.
-- events = array A list of events the preview is subscribed to.
--These keys should not be altered directly. Note that the keys `start_pos`, `end_pos` and `truth`
--may be inaccurate if `active` is false.
function Preview:new(state)
local preview = {}
preview.active = false
preview.config = vim.deepcopy(state.config)
setmetatable(preview, { __index = self })
preview:findWindow(state)
return preview
end
---Preview a buffer in the preview window and optionally reveal and highlight the previewed text.
---@param bufnr number? The number of the buffer to be previewed.
---@param start_pos table? The (0-indexed) starting position of the previewed text. May be absent.
---@param end_pos table? The (0-indexed) ending position of the previewed text. May be absent
function Preview:preview(bufnr, start_pos, end_pos)
if self.is_neo_tree_window then
log.warn("Could not find appropriate window for preview")
return
end
bufnr = bufnr or self.bufnr
if not self.active then
self:activate()
end
if not self.active then
return
end
if bufnr ~= self.bufnr then
self:setBuffer(bufnr)
end
self:clearHighlight()
self.bufnr = bufnr
self.start_pos = start_pos
self.end_pos = end_pos
self:reveal()
self:highlight()
end
---Reverts the preview and inactivates it, restoring the preview window to its previous state.
function Preview:revert()
self.active = false
self:unsubscribe()
self:clearHighlight()
if not renderer.is_window_valid(self.winid) then
self.winid = nil
return
end
if self.config.use_float then
vim.api.nvim_win_close(self.winid, true)
self.winid = nil
return
else
local foldenable = utils.get_value(self.truth, "options.foldenable", nil, false)
if foldenable ~= nil then
vim.api.nvim_win_set_option(self.winid, "foldenable", self.truth.options.foldenable)
end
vim.api.nvim_win_set_var(self.winid, "neo_tree_preview", 0)
end
local bufnr = self.truth.bufnr
if type(bufnr) ~= "number" then
return
end
if not vim.api.nvim_buf_is_valid(bufnr) then
return
end
self:setBuffer(bufnr)
self.bufnr = bufnr
if vim.api.nvim_win_is_valid(self.winid) then
vim.api.nvim_win_call(self.winid, function()
vim.fn.winrestview(self.truth.view)
end)
end
vim.api.nvim_buf_set_option(self.bufnr, "bufhidden", self.truth.options.bufhidden)
end
---Subscribe to event and add it to the preview event list.
--@param source string? Name of the source to add the event to. Will use `events.subscribe` if nil.
--@param event table Event to subscribe to.
function Preview:subscribe(source, event)
if source == nil then
events.subscribe(event)
else
manager.subscribe(source, event)
end
self.events = self.events or {}
table.insert(self.events, { source = source, event = event })
end
---Unsubscribe to all events in the preview event list.
function Preview:unsubscribe()
if self.events == nil then
return
end
for _, event in ipairs(self.events) do
if event.source == nil then
events.unsubscribe(event.event)
else
manager.unsubscribe(event.source, event.event)
end
end
self.events = {}
end
---Finds the appropriate window and updates the preview accordingly.
---@param state table The state of the source.
function Preview:findWindow(state)
local winid, is_neo_tree_window
if self.config.use_float then
if
type(self.winid) == "number"
and vim.api.nvim_win_is_valid(self.winid)
and utils.is_floating(self.winid)
then
return
end
local win = create_floating_preview_window(state)
if not win then
self.active = false
return
end
winid = win.winid
is_neo_tree_window = false
else
winid, is_neo_tree_window = utils.get_appropriate_window(state)
self.bufnr = vim.api.nvim_win_get_buf(winid)
end
if winid == self.winid then
return
end
self.winid, self.is_neo_tree_window = winid, is_neo_tree_window
if self.active then
self:revert()
self:preview()
end
end
---Activates the preview, but does not populate the preview window,
function Preview:activate()
if self.active then
return
end
if not renderer.is_window_valid(self.winid) then
return
end
if self.config.use_float then
self.truth = {}
else
self.truth = {
bufnr = self.bufnr,
view = vim.api.nvim_win_call(self.winid, vim.fn.winsaveview),
options = {
bufhidden = vim.api.nvim_buf_get_option(self.bufnr, "bufhidden"),
foldenable = vim.api.nvim_win_get_option(self.winid, "foldenable"),
},
}
vim.api.nvim_buf_set_option(self.bufnr, "bufhidden", "hide")
vim.api.nvim_win_set_option(self.winid, "foldenable", false)
end
self.active = true
vim.api.nvim_win_set_var(self.winid, "neo_tree_preview", 1)
end
---@param winid number
---@param bufnr number
local function try_load_image_nvim_buf(winid, bufnr)
if vim.bo[bufnr].filetype ~= "image_nvim" then
return false
end
local success, mod = pcall(require, "image")
if not success or not mod.hijack_buffer then
local image_nvim_url = "https://github.com/3rd/image.nvim"
log.debug("You'll need to install image.nvim to use this command: " .. image_nvim_url)
return false
end
return mod.hijack_buffer(vim.api.nvim_buf_get_name(bufnr), winid, bufnr)
end
---Set the buffer in the preview window without executing BufEnter or BufWinEnter autocommands.
--@param bufnr number The buffer number of the buffer to set.
function Preview:setBuffer(bufnr)
local eventignore = vim.opt.eventignore
vim.opt.eventignore:append("BufEnter,BufWinEnter")
vim.api.nvim_win_set_buf(self.winid, bufnr)
if self.config.use_image_nvim then
try_load_image_nvim_buf(self.winid, bufnr)
end
if self.config.use_float then
-- I'm not sufe why float windows won;t show numbers without this
vim.api.nvim_win_set_option(self.winid, "number", true)
end
vim.opt.eventignore = eventignore
end
---Move the cursor to the previewed position and center the screen.
function Preview:reveal()
local pos = self.start_pos or self.end_pos
if not self.active or not self.winid or not pos then
return
end
vim.api.nvim_win_set_cursor(self.winid, { (pos[1] or 0) + 1, pos[2] or 0 })
vim.api.nvim_win_call(self.winid, function()
vim.cmd("normal! zz")
end)
end
---Highlight the previewed range
function Preview:highlight()
if not self.active or not self.bufnr then
return
end
local start_pos, end_pos = self.start_pos, self.end_pos
if not start_pos and not end_pos then
return
elseif not start_pos then
start_pos = end_pos
elseif not end_pos then
end_pos = start_pos
end
local highlight = function(line, col_start, col_end)
vim.api.nvim_buf_add_highlight(
self.bufnr,
neo_tree_preview_namespace,
highlights.PREVIEW,
line,
col_start,
col_end
)
end
local start_line, end_line = start_pos[1], end_pos[1]
local start_col, end_col = start_pos[2], end_pos[2]
if start_line == end_line then
highlight(start_line, start_col, end_col)
else
highlight(start_line, start_col, -1)
for line = start_line + 1, end_line - 1 do
highlight(line, 0, -1)
end
highlight(end_line, 0, end_col)
end
end
---Clear the preview highlight in the buffer currently in the preview window.
function Preview:clearHighlight()
if type(self.bufnr) == "number" and vim.api.nvim_buf_is_valid(self.bufnr) then
vim.api.nvim_buf_clear_namespace(self.bufnr, neo_tree_preview_namespace, 0, -1)
end
end
local toggle_state = false
Preview.hide = function()
toggle_state = false
if instance then
instance:revert()
end
instance = nil
end
Preview.is_active = function()
return instance and instance.active
end
Preview.show = function(state)
local node = state.tree:get_node()
if node.type == "directory" then
return
end
if instance then
instance:findWindow(state)
else
instance = Preview:new(state)
end
local extra = node.extra or {}
local position = extra.position
local end_position = extra.end_position
local path = node.path or node:get_id()
local bufnr = extra.bufnr or vim.fn.bufadd(path)
if bufnr and bufnr > 0 and instance then
instance:preview(bufnr, position, end_position)
end
end
Preview.toggle = function(state)
if toggle_state then
Preview.hide()
else
Preview.show(state)
if instance and instance.active then
toggle_state = true
else
Preview.hide()
return
end
local winid = state.winid
local source_name = state.name
local preview_event = {
event = events.VIM_CURSOR_MOVED,
handler = function()
local did_enter_preview = vim.api.nvim_get_current_win() == instance.winid
if not toggle_state or (did_enter_preview and instance.config.use_float) then
return
end
if vim.api.nvim_get_current_win() == winid then
log.debug("Cursor moved in tree window, updating preview")
Preview.show(state)
else
log.debug("Neo-tree window lost focus, disposing preview")
Preview.hide()
end
end,
id = "preview-event",
}
instance:subscribe(source_name, preview_event)
end
end
Preview.focus = function()
if Preview.is_active() then
vim.fn.win_gotoid(instance.winid)
end
end
Preview.scroll = function(state)
local direction = state.config.direction
-- NOTE: Chars below are raw escape codes for <Ctrl-E>/<Ctrl-Y>
local input = direction < 0 and [[]] or [[]]
local count = math.abs(direction)
if Preview:is_active() then
vim.api.nvim_win_call(instance.winid, function()
vim.cmd([[normal! ]] .. count .. input)
end)
end
end
return Preview