From d03807afbac119125b81f92349e4d7c78e8e79fa Mon Sep 17 00:00:00 2001 From: Jonathan Apodaca Date: Sun, 13 Apr 2025 22:38:30 -0600 Subject: [PATCH] (examples/filetree.lua) integrate file-watcher --- examples/filetree.lua | 50 +++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/examples/filetree.lua b/examples/filetree.lua index 5249c41..1079e6b 100644 --- a/examples/filetree.lua +++ b/examples/filetree.lua @@ -44,11 +44,7 @@ end --- Normalizes the given path to an absolute path. --- @param path string -function H.normalize(path) - path = vim.fs.normalize(path) - if path:sub(1, 1) ~= '/' then path = vim.fs.joinpath(vim.uv.cwd(), path) end - return vim.fs.normalize(path) -end +function H.normalize(path) return vim.fs.abspath(vim.fs.normalize(path)) end --- Computes the relative path from `base` to `path`. --- @param path string @@ -155,19 +151,35 @@ local function _render_in_buffer(opts) end) end) - -- -- - -- -- TODO: :help watch-file - -- -- - -- local watcher = vim.uv.new_fs_event() - -- if watcher ~= nil then - -- watcher:start(root_path, { recursive = true }, function(err, fname, status) - -- -- TODO: more efficient update: - -- s_tree_inf:set(H.get_tree(root_path)) -- - -- -- TODO: proper disposal - -- watcher:stop() - -- end) - -- end + -- :help watch-file + -- + local watcher = vim.uv.new_fs_event() + if watcher ~= nil then + --- @diagnostic disable-next-line: unused-local + watcher:start(opts.root_path, { recursive = true }, function(_err, fname, _status) + fname = H.normalize(fname) + + local dir_path = vim.fs.dirname(fname) + local dir = s_tree_inf:get().path_to_node[dir_path] + if not dir then return end + + s_tree_inf:schedule_update(function(tree_inf) + H.populate_dir_children(dir, tree_inf.path_to_node) + return tree_inf + end) + end) + end + vim.api.nvim_create_autocmd('WinClosed', { + once = true, + pattern = tostring(winnr), + callback = function() + if watcher == nil then return end + + watcher:stop() + watcher = nil + end, + }) local controller = {} @@ -411,8 +423,8 @@ function M.show(opts) callback = M.hide, }) - vim.wo.number = false - vim.wo.relativenumber = false + vim.wo[0][0].number = false + vim.wo[0][0].relativenumber = false local bufnr = vim.api.nvim_get_current_buf()