robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
2.9 KiB123 linesraw
1-- Filesystem backend.
2
3local api = vim.api
4local fs = vim.fs
5
6local M = {}
7
8local navigating = false
9
10---@param path string
11---@return string
12function M.normalize(path)
13  return fs.normalize(fs.abspath(path), { plain = true })
14end
15
16---@return boolean
17function M.is_navigating()
18  return navigating
19end
20
21---@param path string
22local function edit(path)
23  navigating = true
24  local ok, err = pcall(api.nvim_cmd, {
25    cmd = 'edit',
26    args = { path },
27    mods = { keepalt = vim.b.nvim_dir ~= nil },
28    magic = { file = false, bar = false },
29  })
30  navigating = false
31  if not ok then
32    error(err, 0)
33  end
34end
35
36---@param path string
37---@param select? nvim.dir.Entry
38local function navigate(path, select)
39  edit(path)
40  local buf = api.nvim_get_current_buf()
41  local dir = M.normalize(api.nvim_buf_get_name(buf))
42  if vim.fn.isdirectory(dir) == 0 then
43    return
44  end
45
46  require('nvim.dir').open(buf, dir, M, select)
47end
48
49---@param path string
50function M.open_parent_path(path)
51  if path == '' or path:find('^%a[%w+.-]*://') then
52    navigate('.')
53    return
54  end
55  path = M.normalize(path)
56  navigate(fs.dirname(path), { name = fs.basename(path), dir = vim.fn.isdirectory(path) == 1 })
57end
58
59---@param _ integer
60---@param path string
61---@param cb fun(err?: string, entries?: nvim.dir.Entry[])
62function M.list(_, path, cb)
63  local entries = {} ---@type nvim.dir.Entry[]
64  for name, type, err in fs.dir(path, { err = true, plain = true }) do
65    if err then
66      cb(err)
67      return
68    end
69    if type == 'link' and vim.fn.isdirectory(fs.joinpath(path, name)) == 1 then
70      type = 'directory'
71    end
72    entries[#entries + 1] = {
73      name = name,
74      dir = type == 'directory',
75    }
76  end
77  table.sort(entries, function(a, b)
78    if a.dir ~= b.dir then
79      return a.dir
80    end
81    return a.name < b.name
82  end)
83  cb(nil, entries)
84end
85
86---@param _ integer
87---@param path string
88---@param entry nvim.dir.Entry
89function M.open(_, path, entry)
90  navigate(fs.joinpath(path, entry.name))
91end
92
93---@param _ integer
94---@param path string
95function M.open_parent(_, path)
96  M.open_parent_path(path)
97end
98
99---@param buf integer
100---@param path string
101function M.init(buf, path)
102  if api.nvim_get_option_value('filetype', { buf = buf }) ~= 'directory' then
103    api.nvim_set_option_value('filetype', 'directory', { buf = buf })
104  end
105  local dir = require('nvim.dir')
106  vim.keymap.set('n', '<Plug>(nvim-dir-vsplit)', function()
107    dir._open_entry('vsplit')
108  end, { buffer = buf, silent = true, desc = 'Open directory entry in a vertical split' })
109  vim.keymap.set('n', '<Plug>(nvim-dir-split)', function()
110    dir._open_entry('split')
111  end, { buffer = buf, silent = true, desc = 'Open directory entry in a horizontal split' })
112  dir._map(buf, 'a', '<Plug>(nvim-dir-vsplit)')
113  dir._map(buf, 'o', '<Plug>(nvim-dir-split)')
114  api.nvim_buf_call(buf, function()
115    pcall(api.nvim_cmd, {
116      cmd = 'bcd',
117      args = { path },
118      magic = { file = false, bar = false },
119    })
120  end)
121end
122
123return M