diff --git a/lua/remote-sshfs/connections.lua b/lua/remote-sshfs/connections.lua index 9d344f7..a1fdb7d 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 @@ -296,11 +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 - pcall(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 end end diff --git a/lua/remote-sshfs/statusline.lua b/lua/remote-sshfs/statusline.lua index 483ccc5..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 + 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 f066602..009fadc 100644 --- a/lua/remote-sshfs/utils.lua +++ b/lua/remote-sshfs/utils.lua @@ -151,10 +151,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.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 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