-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfloat-preview.lua
More file actions
387 lines (329 loc) · 9.2 KB
/
Copy pathfloat-preview.lua
File metadata and controls
387 lines (329 loc) · 9.2 KB
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
local CFG = require "float-preview.config"
local api = require "nvim-tree.api"
local Event = api.events.Event
local get_node = api.tree.get_node_under_cursor
local FloatPreview = {}
FloatPreview.__index = FloatPreview
local preview_au = "float_preview_au"
vim.api.nvim_create_augroup(preview_au, { clear = true })
local st = {}
local all_floats = {}
local disabled = false
-- orignal fzf lua
local read_file_async = function(filepath, callback)
vim.loop.fs_open(filepath, "r", 438, function(err_open, fd)
if err_open then
-- we must schedule this or we get
-- E5560: nvim_exec must not be called in a lua loop callback
vim.schedule(function()
vim.notify(("Unable to open file '%s', error: %s"):format(filepath, err_open), vim.log.levels.WARN)
end)
return
end
vim.loop.fs_fstat(fd, function(err_fstat, stat)
assert(not err_fstat, err_fstat)
if stat.type ~= "file" then
return callback ""
end
vim.loop.fs_read(fd, stat.size, 0, function(err_read, data)
assert(not err_read, err_read)
vim.loop.fs_close(fd, function(err_close)
assert(not err_close, err_close)
return callback(data)
end)
end)
end)
end)
end
function FloatPreview.is_float(bufnr, path)
if path then
return st[path] ~= nil
end
if not bufnr then
bufnr = vim.api.nvim_get_current_buf()
end
return st[bufnr] ~= nil
end
local function all_close()
for _, fl in pairs(all_floats) do
fl:_close()
end
end
FloatPreview.close = all_close
local function all_open()
for _, fl in pairs(all_floats) do
fl:preview_under_cursor()
end
end
local function toggle_or_preview()
local node = api.tree.get_node_under_cursor()
if not node then
return
end
if node.type ~= "file" then
api.node.open.edit() -- Toggle folder
end
end
function FloatPreview.setup(cfg)
CFG.update(cfg)
cfg = CFG.config()
disabled = not cfg.toggled_on
if cfg.wrap_nvimtree_commands then
api.node.open.preview = toggle_or_preview -- disable the
api.node.open.tab = FloatPreview.close_wrap(api.node.open.tab)
api.node.open.vertical = FloatPreview.close_wrap(api.node.open.vertical)
api.node.open.horizontal = FloatPreview.close_wrap(api.node.open.horizontal)
api.node.open.edit = FloatPreview.close_wrap(api.node.open.edit)
api.node.open.no_window_picker = FloatPreview.close_wrap(api.node.open.no_window_picker)
api.fs.create = FloatPreview.close_wrap(api.fs.create)
api.fs.remove = FloatPreview.close_wrap(api.fs.remove)
api.fs.rename = FloatPreview.close_wrap(api.fs.rename)
end
end
function FloatPreview.attach_nvimtree(bufnr)
local prev = FloatPreview:new()
prev:attach(bufnr)
return prev
end
function FloatPreview.close_wrap(f)
return function(...)
all_close()
local args = ...
return vim.schedule(function()
f(args)
end)
end
end
function FloatPreview.toggle()
disabled = not disabled
if disabled then
all_close()
else
all_open()
end
end
function FloatPreview:new(cfg)
local prev = {}
setmetatable(prev, FloatPreview)
cfg = cfg or CFG.config()
prev.buf = nil
prev.win = nil
prev.path = nil
prev.current_line = 1
prev.max_line = 999999
prev.disabled = false
prev.cfg = cfg
local function action_wrap(f)
return function(...)
prev:_close()
return f(...)
end
end
return prev, action_wrap
end
function FloatPreview:_close(reason)
if self.path ~= nil and self.buf ~= nil then
pcall(vim.api.nvim_win_close, self.win, { force = true })
pcall(vim.api.nvim_buf_delete, self.buf, { force = true })
self.win = nil
st[self.buf] = nil
st[self.path] = nil
self.buf = nil
self.path = nil
self.current_line = 1
self.max_line = 999999
end
end
--TODO: add bat preview
function FloatPreview:preview(path)
if disabled then
return
end
if not self.cfg.hooks.pre_open(path) then
return
end
self.path = path
self.buf = vim.api.nvim_create_buf(false, false)
st[self.path] = 1
st[self.buf] = 1
vim.api.nvim_set_option_value("bufhidden", "wipe", { buf = self.buf })
vim.api.nvim_set_option_value("buftype", "nofile", { buf = self.buf })
vim.api.nvim_set_option_value("buflisted", false, { buf = self.buf })
local width = vim.api.nvim_get_option_value("columns", {})
local height = vim.api.nvim_get_option_value("lines", {})
local prev_height = math.ceil(height / 2)
local opts = {
width = math.ceil(width / 2),
height = prev_height,
row = vim.fn.line ".",
col = vim.fn.winwidth(0) + 1,
focusable = false,
noautocmd = true,
style = self.cfg.window.style,
relative = self.cfg.window.relative,
border = self.cfg.window.border,
}
local open_win_config = self.cfg.window.open_win_config
if type(open_win_config) == "function" then
open_win_config = open_win_config()
end
if open_win_config then
for k, v in pairs(open_win_config) do
opts[k] = v
end
end
self.win = vim.api.nvim_open_win(self.buf, true, opts)
vim.api.nvim_set_option_value("wrap", self.cfg.window.wrap, { win = self.win })
read_file_async(
path,
vim.schedule_wrap(function(data)
if not self.buf then
return
end
local lines = vim.split(data, "[\r]?\n")
-- if file ends in new line, don't write an empty string as the last
-- line.
if data:sub(#data, #data) == "\n" or data:sub(#data - 1, #data) == "\r\n" then
table.remove(lines)
end
self.max_line = #lines
if self.cfg.window.trim_height then
if self.max_line < prev_height then
opts.height = self.max_line + 1
opts.noautocmd = nil
vim.api.nvim_win_set_config(self.win, opts)
end
end
vim.api.nvim_buf_set_lines(self.buf, 0, -1, false, lines)
local ft = vim.filetype.match { buf = self.buf, filename = path }
vim.bo[self.buf].ft = ft
local has_lang, lang = pcall(vim.treesitter.language.get_lang, ft)
local has_ts, _ = pcall(vim.treesitter.start, self.buf, has_lang and lang or ft)
local syntax_ = vim.g.syntax_on or vim.g.syntax_manual
if not has_ts and syntax_ then
vim.bo[self.buf].syntax = ft
end
end)
)
if not self.cfg.hooks.post_open(self.buf) then
self:_close "post open"
end
end
function FloatPreview:preview_under_cursor()
local _, node = pcall(get_node)
if not node then
return
end
if node.type ~= "file" then
return
end
local win = vim.api.nvim_get_current_win()
self:preview(node.absolute_path)
local ok, _ = pcall(vim.api.nvim_set_current_win, win)
if not ok then
self:_close "cant set win"
end
end
function FloatPreview:scroll(line)
if self.win then
local ok, _ = pcall(vim.api.nvim_win_set_cursor, self.win, { line, 0 })
if ok then
self.current_line = line
end
end
end
function FloatPreview:scroll_down()
if self.buf then
local next_line = math.min(self.current_line + self.cfg.scroll_lines, self.max_line)
self:scroll(next_line)
end
end
function FloatPreview:scroll_up()
if self.buf then
local next_line = math.max(self.current_line - self.cfg.scroll_lines, 1)
self:scroll(next_line)
end
end
function FloatPreview:close_preview()
local _, node = pcall(get_node)
if not node then
return
end
self:_close "cursor moved"
end
function FloatPreview:attach(bufnr)
for _, key in ipairs(self.cfg.mapping.up) do
vim.keymap.set("n", key, function()
self:scroll_up()
end, { buffer = bufnr })
end
for _, key in ipairs(self.cfg.mapping.down) do
vim.keymap.set("n", key, function()
self:scroll_down()
end, { buffer = bufnr })
end
for _, key in ipairs(self.cfg.mapping.toggle) do
vim.keymap.set("n", key, function()
FloatPreview.toggle()
end, { buffer = bufnr })
end
local toggle_preview = function()
local _, node = pcall(get_node)
if self.path ~= nil and self.path == node.absolute_path then
self.close_preview(self)
return
end
self:preview_under_cursor()
end
vim.api.nvim_create_user_command("TogglePreviewFile", toggle_preview, {})
local au = {}
table.insert(
au,
vim.api.nvim_create_autocmd("BufDelete", {
group = preview_au,
callback = function()
if vim.fn.exists ":TogglePreviewFile" > 0 then
vim.api.nvim_del_user_command "TogglePreviewFile"
end
end,
})
)
table.insert(
au,
vim.api.nvim_create_autocmd({ "CursorHold" }, {
group = preview_au,
callback = function()
if self.cfg.auto_preview then
if bufnr == vim.api.nvim_get_current_buf() then
self:preview_under_cursor()
return
end
self:_close "changed buffer"
return
end
end,
})
)
table.insert(
au,
vim.api.nvim_create_autocmd({ "CursorMoved" }, {
group = preview_au,
callback = function()
self:close_preview()
end,
})
)
api.events.subscribe(Event.TreeClose, function(opts)
if not self then
return
end
all_floats[bufnr] = nil
for _, au_id in pairs(au) do
vim.api.nvim_del_autocmd(au_id)
end
self:_close "nvim close"
self = nil
end)
all_floats[bufnr] = self
end
return FloatPreview