From c2e1ffc1e99fc0650a1d3dd28d8298efc6799e2c Mon Sep 17 00:00:00 2001 From: davidoroian <108657697+davidoroian@users.noreply.github.com> Date: Sun, 28 Sep 2025 16:28:40 +0300 Subject: [PATCH 1/7] Dirrefresh (#1) Added `vim.cmd('edit .')` to refresh the nvim UI current directory after the `pwd` is changed to the remote filessytem. Not sure if this is the best way of doing it. I had the issue of the UI not being updated on two separate machines running Fedora42 and Ubuntu22.04 --- lua/remote-sshfs/utils.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/remote-sshfs/utils.lua b/lua/remote-sshfs/utils.lua index da291fe..7639bfe 100644 --- a/lua/remote-sshfs/utils.lua +++ b/lua/remote-sshfs/utils.lua @@ -147,6 +147,7 @@ end M.change_directory = function(path) -- Change the working directory of the Vim instance vim.fn.execute("cd " .. path) + vim.cmd('edit .') vim.notify("Directory changed to " .. path) end From a7693ce8bd401048039349366ee13e29881452d0 Mon Sep 17 00:00:00 2001 From: davidoroian Date: Mon, 29 Sep 2025 08:47:54 +0300 Subject: [PATCH 2/7] replaced or nil expression in the telescope extension since nil is always false with a check if the name parameter exists and commented another place where or nil exists for double-checking --- lua/remote-sshfs/statusline.lua | 2 +- lua/telescope/_extensions/remote-sshfs.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/remote-sshfs/statusline.lua b/lua/remote-sshfs/statusline.lua index 483ccc5..b6d18ec 100644 --- a/lua/remote-sshfs/statusline.lua +++ b/lua/remote-sshfs/statusline.lua @@ -22,7 +22,7 @@ function M.status() return "" end - local host_tbl = conn.get_current_host and conn.get_current_host() or nil + local host_tbl = conn.get_current_host and conn.get_current_host() or nil -- I think the or nil here does not influence the conditional either local name = "remote" if host_tbl and type(host_tbl) == "table" then -- Prefer the explicit entries we create while parsing the ssh-config. diff --git a/lua/telescope/_extensions/remote-sshfs.lua b/lua/telescope/_extensions/remote-sshfs.lua index 06571dd..3861b58 100644 --- a/lua/telescope/_extensions/remote-sshfs.lua +++ b/lua/telescope/_extensions/remote-sshfs.lua @@ -6,7 +6,7 @@ local ns_previewer = vim.api.nvim_create_namespace "telescope.previewers" -- Build virtualized host file from parsed hosts from plugin local function build_host_preview(hosts, name) - if name == "" or nil then + if not name and name == "" then return {} end From ed109f7633d124fa2cb89232e3d93b745cf9f7d7 Mon Sep 17 00:00:00 2001 From: davidoroian Date: Mon, 29 Sep 2025 09:47:23 +0300 Subject: [PATCH 3/7] added correct call to extension clear_cache() function --- lua/remote-sshfs/connections.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/remote-sshfs/connections.lua b/lua/remote-sshfs/connections.lua index 735acfb..c3cb87b 100644 --- a/lua/remote-sshfs/connections.lua +++ b/lua/remote-sshfs/connections.lua @@ -300,7 +300,8 @@ M.unmount_host = function() mount_point = nil current_host = nil -- Clear Telescope extension cache for remote-find commands - pcall(require, "telescope._extensions.remote-sshfs").clear_cache() + require("telescope").extensions["remote-sshfs"].clear_cache() + utils.change_directory(vim.fn.expand("$HOME")) end end From 36e86d817ce573cf0f56f5e35adf710a53f579b2 Mon Sep 17 00:00:00 2001 From: davidoroian Date: Mon, 29 Sep 2025 09:48:45 +0300 Subject: [PATCH 4/7] replaced cd command with the nvim.api.nvim_set_current_dir function and added param comment --- lua/remote-sshfs/utils.lua | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lua/remote-sshfs/utils.lua b/lua/remote-sshfs/utils.lua index 7639bfe..d525ac7 100644 --- a/lua/remote-sshfs/utils.lua +++ b/lua/remote-sshfs/utils.lua @@ -144,11 +144,16 @@ M.parse_host_from_command = function(command) return host end +--- Change the working directory of the Vim instance +--- @param path string M.change_directory = function(path) - -- Change the working directory of the Vim instance - vim.fn.execute("cd " .. path) - vim.cmd('edit .') - vim.notify("Directory changed to " .. path) + local ok, err = pcall(vim.api.nvim_set_current_dir, path) + if not ok then + vim.notify("Failed to change directory: " .. tostring(err), vim.log.levels.ERROR) + else + vim.cmd("edit .") + vim.notify("Directory changed to " .. path) + end end -- CallbackList class From 5f81f02d152788ef009286dfff51048eec09a0eb Mon Sep 17 00:00:00 2001 From: David Oroian Date: Tue, 30 Sep 2025 09:44:48 +0300 Subject: [PATCH 5/7] removed comment and trailing spaces and replaced direct call of extension with pcall() --- lua/remote-sshfs/connections.lua | 5 ++++- lua/remote-sshfs/statusline.lua | 2 +- lua/remote-sshfs/utils.lua | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lua/remote-sshfs/connections.lua b/lua/remote-sshfs/connections.lua index c3cb87b..8b0b14c 100644 --- a/lua/remote-sshfs/connections.lua +++ b/lua/remote-sshfs/connections.lua @@ -300,7 +300,10 @@ M.unmount_host = function() mount_point = nil current_host = nil -- Clear Telescope extension cache for remote-find commands - require("telescope").extensions["remote-sshfs"].clear_cache() + local ok, ext = pcall(require, "telescope._extensions.remote-sshfs") + if ok and ext.clear_cache then + ext.clear_cache() + end utils.change_directory(vim.fn.expand("$HOME")) end end diff --git a/lua/remote-sshfs/statusline.lua b/lua/remote-sshfs/statusline.lua index b6d18ec..c3303ce 100644 --- a/lua/remote-sshfs/statusline.lua +++ b/lua/remote-sshfs/statusline.lua @@ -22,7 +22,7 @@ function M.status() return "" end - local host_tbl = conn.get_current_host and conn.get_current_host() or nil -- I think the or nil here does not influence the conditional either + local host_tbl = conn.get_current_host and conn.get_current_host() local name = "remote" if host_tbl and type(host_tbl) == "table" then -- Prefer the explicit entries we create while parsing the ssh-config. diff --git a/lua/remote-sshfs/utils.lua b/lua/remote-sshfs/utils.lua index d525ac7..3ea60f3 100644 --- a/lua/remote-sshfs/utils.lua +++ b/lua/remote-sshfs/utils.lua @@ -145,7 +145,7 @@ M.parse_host_from_command = function(command) end --- Change the working directory of the Vim instance ---- @param path string +--- @param path string M.change_directory = function(path) local ok, err = pcall(vim.api.nvim_set_current_dir, path) if not ok then From d8e5ba0b9d74470d7a2a6491719027a93999f5c6 Mon Sep 17 00:00:00 2001 From: davidoroian Date: Sun, 5 Oct 2025 13:56:15 +0300 Subject: [PATCH 6/7] added original directory storing, restoring and cleaning --- lua/remote-sshfs/connections.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lua/remote-sshfs/connections.lua b/lua/remote-sshfs/connections.lua index 8b0b14c..4eb61b6 100644 --- a/lua/remote-sshfs/connections.lua +++ b/lua/remote-sshfs/connections.lua @@ -12,6 +12,7 @@ local sshfs_args = {} local sshfs_job_id = nil local mount_point = nil local current_host = nil +local original_dir = nil local M = {} @@ -53,6 +54,8 @@ M.reload = function() end M.connect = function(host) + -- Store original directory + original_dir = vim.fn.getcwd() -- Initialize host variables local remote_host = host["Name"] if config.ui.confirm.connect then @@ -304,7 +307,10 @@ M.unmount_host = function() if ok and ext.clear_cache then ext.clear_cache() end - utils.change_directory(vim.fn.expand("$HOME")) + if original_dir and vim.fn.isdirectory(original_dir) then + utils.change_directory(original_dir) + original_dir = nil + end end end From cf9b2139807c0724e99f9648b66a2623ad9f1d58 Mon Sep 17 00:00:00 2001 From: davidoroian Date: Sun, 5 Oct 2025 14:37:52 +0300 Subject: [PATCH 7/7] moved original dir restore before clearing telescope cache so that memory release is done in the same stage --- lua/remote-sshfs/connections.lua | 9 +++++---- lua/remote-sshfs/utils.lua | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lua/remote-sshfs/connections.lua b/lua/remote-sshfs/connections.lua index 4eb61b6..3831bfa 100644 --- a/lua/remote-sshfs/connections.lua +++ b/lua/remote-sshfs/connections.lua @@ -299,18 +299,19 @@ M.unmount_host = function() -- Fallback to generic umount vim.fn.system { "umount", target } end + -- restore original directory + if original_dir and vim.fn.isdirectory(original_dir) then + utils.change_directory(original_dir) + end sshfs_job_id = nil mount_point = nil current_host = nil + original_dir = nil -- Clear Telescope extension cache for remote-find commands local ok, ext = pcall(require, "telescope._extensions.remote-sshfs") if ok and ext.clear_cache then ext.clear_cache() end - if original_dir and vim.fn.isdirectory(original_dir) then - utils.change_directory(original_dir) - original_dir = nil - end end end diff --git a/lua/remote-sshfs/utils.lua b/lua/remote-sshfs/utils.lua index 3ea60f3..670a8e7 100644 --- a/lua/remote-sshfs/utils.lua +++ b/lua/remote-sshfs/utils.lua @@ -151,7 +151,7 @@ M.change_directory = function(path) if not ok then vim.notify("Failed to change directory: " .. tostring(err), vim.log.levels.ERROR) else - vim.cmd("edit .") + vim.cmd "edit ." vim.notify("Directory changed to " .. path) end end