Skip to content

Commit 09078bc

Browse files
committed
update
1 parent c626195 commit 09078bc

File tree

1 file changed

+39
-41
lines changed

1 file changed

+39
-41
lines changed

lua/dired/init.lua

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local api, uv, ffi = vim.api, vim.uv, require('ffi')
22
local FileOps = require('dired.fileops')
3-
local ns_id = vim.api.nvim_create_namespace('dired_highlights')
4-
local sep = vim.uv.os_uname().sysname:find('win') and '\\' or '/'
3+
local ns_id = api.nvim_create_namespace('dired_highlights')
54

65
-- FFI definitions
76
ffi.cdef([[
@@ -191,42 +190,42 @@ local UI = {}
191190

192191
UI.Highlights = {
193192
set_header_highlights = function(bufnr)
194-
vim.api.nvim_buf_set_extmark(bufnr, ns_id, 0, 0, {
193+
api.nvim_buf_set_extmark(bufnr, ns_id, 0, 0, {
195194
line_hl_group = 'DiredHeader',
196195
end_row = 0,
197196
})
198-
vim.api.nvim_buf_set_extmark(bufnr, ns_id, 1, 0, {
197+
api.nvim_buf_set_extmark(bufnr, ns_id, 1, 0, {
199198
line_hl_group = 'DiredHeaderLine',
200199
end_row = 1,
201200
})
202201
end,
203202

204203
set_entry_highlights = function(bufnr, line_num, entry)
205-
local line = vim.api.nvim_buf_get_lines(bufnr, line_num, line_num + 1, false)[1]
204+
local line = api.nvim_buf_get_lines(bufnr, line_num, line_num + 1, false)[1]
206205
if not line then
207206
return
208207
end
209208

210209
-- permissions
211-
vim.api.nvim_buf_set_extmark(bufnr, ns_id, line_num, 0, {
210+
api.nvim_buf_set_extmark(bufnr, ns_id, line_num, 0, {
212211
hl_group = 'DiredPermissions',
213212
end_col = 10,
214213
})
215214

216215
-- user
217-
vim.api.nvim_buf_set_extmark(bufnr, ns_id, line_num, 11, {
216+
api.nvim_buf_set_extmark(bufnr, ns_id, line_num, 11, {
218217
hl_group = 'DiredUser',
219218
end_col = 20,
220219
})
221220

222221
-- size
223-
vim.api.nvim_buf_set_extmark(bufnr, ns_id, line_num, 21, {
222+
api.nvim_buf_set_extmark(bufnr, ns_id, line_num, 21, {
224223
hl_group = 'DiredSize',
225224
end_col = 30,
226225
})
227226

228227
-- date
229-
vim.api.nvim_buf_set_extmark(bufnr, ns_id, line_num, 31, {
228+
api.nvim_buf_set_extmark(bufnr, ns_id, line_num, 31, {
230229
hl_group = 'DiredDate',
231230
end_col = 50,
232231
})
@@ -245,7 +244,7 @@ UI.Highlights = {
245244
hl_group = 'DiredExecutable'
246245
end
247246

248-
vim.api.nvim_buf_set_extmark(bufnr, ns_id, line_num, name_start, {
247+
api.nvim_buf_set_extmark(bufnr, ns_id, line_num, name_start, {
249248
hl_group = hl_group,
250249
end_col = name_start + #entry.name,
251250
})
@@ -274,30 +273,29 @@ UI.Entry = {
274273
}
275274

276275
UI.Window = {
277-
create = function(config)
276+
create = function(config, enter)
278277
return F.IO.fromEffect(function()
279278
local buf = api.nvim_create_buf(false, false)
280-
local win = api.nvim_open_win(buf, true, {
279+
local win = api.nvim_open_win(buf, enter or true, {
281280
relative = 'editor',
282281
width = config.width,
283282
height = config.height,
284283
row = config.row,
285284
col = config.col,
286285
border = 'rounded',
287286
})
287+
vim.bo[buf].buftype = 'nofile'
288+
vim.bo[buf].bufhidden = 'wipe'
289+
vim.wo[win].wrap = false
290+
vim.wo[win].number = false
291+
vim.wo[win].stc = ''
288292
return { buf = buf, win = win }
289293
end)
290294
end,
291295

292296
setup = function(state)
293297
return F.IO.fromEffect(function()
294298
vim.bo[state.buf].modifiable = true
295-
vim.bo[state.buf].buftype = 'nofile'
296-
vim.bo[state.buf].bufhidden = 'wipe'
297-
vim.wo[state.win].wrap = false
298-
vim.wo[state.win].number = false
299-
vim.wo[state.win].stc = ''
300-
301299
local header = string.format(
302300
'%-11s %-10s %-10s %-20s %s',
303301
'Permissions',
@@ -402,7 +400,7 @@ Browser.Operations = {
402400
delete = function(state, name)
403401
local path = vim.fs.joinpath(state.current_path, name)
404402
return F.IO.fromEffect(function()
405-
vim.uv.fs_stat(path, function(err, stat)
403+
uv.fs_stat(path, function(err, stat)
406404
if err or not stat then
407405
Notify.err('Failed to stat path: ' .. err)
408406
return
@@ -451,25 +449,28 @@ Browser.Operations = {
451449
end)
452450
end,
453451

454-
-- Preview file content
452+
-- Preview file content if we need
455453
preview = function(state, name)
456454
local path = vim.fs.joinpath(state.current_path, name)
457455
return F.IO.fromEffect(function()
458456
FileOps.readFile(path, 1024).fork(function(err)
459457
Notify.err(err)
460458
end, function(content)
461459
local lines = vim.split(content, '\n')
462-
local bufnr = vim.api.nvim_create_buf(false, true)
463-
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
464-
vim.api.nvim_open_win(bufnr, true, {
465-
relative = 'editor',
466-
width = 60,
467-
height = math.min(#lines, 10),
468-
row = 3,
469-
col = 10,
470-
style = 'minimal',
471-
border = 'rounded',
472-
})
460+
vim.schedule(function()
461+
local bufnr = api.nvim_create_buf(false, false)
462+
api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
463+
local cfg = api.nvim_win_get_config(0)
464+
api.nvim_open_win(bufnr, false, {
465+
relative = 'editor',
466+
width = cfg.width,
467+
height = math.min(#lines, cfg.row),
468+
row = 1,
469+
col = cfg.col,
470+
style = 'minimal',
471+
border = 'rounded',
472+
})
473+
end)
473474
end)
474475
return state
475476
end)
@@ -480,7 +481,6 @@ Browser.setup = function(state)
480481
return F.IO.fromEffect(function()
481482
local keymaps = {
482483
{
483-
mode = 'n',
484484
key = '<CR>',
485485
action = function()
486486
local line = api.nvim_get_current_line()
@@ -498,7 +498,6 @@ Browser.setup = function(state)
498498
end,
499499
},
500500
{
501-
mode = 'n',
502501
key = 'u',
503502
action = function()
504503
local current = state.current_path
@@ -508,14 +507,12 @@ Browser.setup = function(state)
508507
end,
509508
},
510509
{
511-
mode = 'n',
512510
key = 'q',
513511
action = function()
514512
api.nvim_win_close(state.win, true)
515513
end,
516514
},
517515
{
518-
mode = 'n',
519516
key = 'cf',
520517
action = function()
521518
vim.ui.input({ prompt = 'Create file: ' }, function(name)
@@ -526,7 +523,6 @@ Browser.setup = function(state)
526523
end,
527524
},
528525
{
529-
mode = 'n',
530526
key = 'cd',
531527
action = function()
532528
vim.ui.input({ prompt = 'Create directory: ' }, function(name)
@@ -540,7 +536,7 @@ Browser.setup = function(state)
540536
mode = 'n',
541537
key = 'D',
542538
action = function()
543-
local line = vim.api.nvim_get_current_line()
539+
local line = api.nvim_get_current_line()
544540
local name = line:match('%s(%S+)$')
545541
if name then
546542
vim.ui.input({
@@ -554,7 +550,6 @@ Browser.setup = function(state)
554550
end,
555551
},
556552
{
557-
mode = 'n',
558553
key = 'R',
559554
action = function()
560555
local line = api.nvim_get_current_line()
@@ -571,7 +566,6 @@ Browser.setup = function(state)
571566
end,
572567
},
573568
{
574-
mode = 'n',
575569
key = 'yy',
576570
action = function()
577571
local line = api.nvim_get_current_line()
@@ -583,7 +577,6 @@ Browser.setup = function(state)
583577
end,
584578
},
585579
{
586-
mode = 'n',
587580
key = 'p',
588581
action = function()
589582
if state.clipboard then
@@ -601,8 +594,12 @@ Browser.setup = function(state)
601594
},
602595
}
603596

597+
local nmap = function(map)
598+
vim.keymap.set('n', map.key, map.action, { buffer = state.buf })
599+
end
600+
604601
vim.iter(keymaps):map(function(map)
605-
vim.keymap.set(map.mode, map.key, map.action, { buffer = state.buf })
602+
nmap(map)
606603
end)
607604

608605
return state
@@ -660,6 +657,7 @@ Browser.refresh = function(state, path)
660657
-- update window width for better look
661658
cfg.width = math.min(cfg.width, maxwidth + 8)
662659
cfg.col = math.floor((vim.o.columns - cfg.width) / 2)
660+
cfg.height = math.min(cfg.height, #collected_entries + 5)
663661
local curpath = vim.fs.basename(vim.fs.normalize(state.current_path))
664662
if not cfg.title then
665663
cfg.title = curpath

0 commit comments

Comments
 (0)