diff options
| author | coast <coasteen@proton.me> | 2026-10-15 04:35:38 +0330 |
|---|---|---|
| committer | coast <coasteen@proton.me> | 2026-10-15 04:35:38 +0330 |
| commit | 57331238c20775916068580aff7d46968c45d575 (patch) | |
| tree | 7450c990e796de8402f648a6cf1f3423e0469fca /nvim/after/plugin/netrw_tree_fix.lua | |
| parent | 8c5c2d270fd0f3bbc276e3b1cc28fc1dfae3fa59 (diff) | |
| download | coast-newdot-57331238c20775916068580aff7d46968c45d575.tar.gz coast-newdot-57331238c20775916068580aff7d46968c45d575.tar.bz2 coast-newdot-57331238c20775916068580aff7d46968c45d575.tar.xz coast-newdot-57331238c20775916068580aff7d46968c45d575.tar.zst | |
Diffstat (limited to 'nvim/after/plugin/netrw_tree_fix.lua')
| -rw-r--r-- | nvim/after/plugin/netrw_tree_fix.lua | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/nvim/after/plugin/netrw_tree_fix.lua b/nvim/after/plugin/netrw_tree_fix.lua new file mode 100644 index 0000000..af2030e --- /dev/null +++ b/nvim/after/plugin/netrw_tree_fix.lua @@ -0,0 +1,58 @@ +local orig = {} + +local function parse(line) + local prefix, name = line:match('^([| ]*)(.-)%s*$') + local _, depth = prefix:gsub('|', '') + return depth, name +end + +local function open_entry() + local buf = vim.api.nvim_get_current_buf() + local lnum = vim.fn.line('.') + local depth, name = parse(vim.fn.getline(lnum)) + + local function fallback() + local rhs = orig[buf] and orig[buf].rhs + if rhs and rhs ~= '' then + vim.api.nvim_feedkeys(vim.keycode(rhs), 'n', false) + end + end + + if name == '' or name:sub(-1) == '/' or depth == 0 then + return fallback() + end + + local parts, cur, top = {}, depth, nil + for l = lnum - 1, 1, -1 do + local d, n = parse(vim.fn.getline(l)) + if n ~= '' and d < cur and n:sub(-1) == '/' then + if d == 0 then + top = n + break + end + table.insert(parts, 1, n) + cur = d + end + end + + if not top or not top:match('^%a+://') then + return fallback() + end + + vim.cmd('edit ' .. vim.fn.fnameescape(top .. table.concat(parts) .. name)) +end + +local function install(buf) + local m = vim.fn.maparg('<CR>', 'n', false, true) + if m.callback then return end + orig[buf] = m + vim.keymap.set('n', '<CR>', open_entry, { buffer = buf, silent = true }) +end + +vim.api.nvim_create_autocmd({ 'BufEnter', 'CursorMoved' }, { + callback = function(a) + if vim.bo[a.buf].filetype == 'netrw' and vim.g.netrw_liststyle == 3 then + install(a.buf) + end + end, +}) |
