diff --git a/lua/remote-nvim/providers/ssh/ssh_provider.lua b/lua/remote-nvim/providers/ssh/ssh_provider.lua index ce49d7c6..d13c7183 100644 --- a/lua/remote-nvim/providers/ssh/ssh_provider.lua +++ b/lua/remote-nvim/providers/ssh/ssh_provider.lua @@ -31,14 +31,18 @@ end ---@param conn_opts string ---@return string cleaned_conn_opts Cleaned up SSH options function SSHProvider:_cleanup_conn_options(conn_opts) - local host_expression = self.host:gsub("([^%w])", "%%%1") - return vim.trim( - conn_opts - :gsub("^%s*ssh%s*", "") -- Remove "ssh" prefix if it exists - :gsub("%s+", " ") -- Replace multiple whitespaces by a single one - :gsub(host_expression .. " ", " ") -- Remove hostname from connection string - :gsub("%-N", "") -- "-N" restrics command execution so we do not do it - ) + local filtered_conn_opts = vim.tbl_filter(function(elem) + -- We filter following keywords and patterns + -- Any empty string, "-N" and hostname as a keyword in the connection options + return elem ~= self.host and elem ~= "-N" and elem ~= "" + end, vim.split(conn_opts, "%s", { trimempty = true })) + + -- If the connection options begin with "ssh", remove "ssh" + if #filtered_conn_opts > 0 and filtered_conn_opts[1] == require("remote-nvim").config.ssh_config.ssh_binary then + table.remove(filtered_conn_opts, 1) + end + + return table.concat(filtered_conn_opts, " ") end return SSHProvider diff --git a/tests/remote-nvim/providers/ssh/ssh_provider_spec.lua b/tests/remote-nvim/providers/ssh/ssh_provider_spec.lua index 0f8c3768..c244868e 100644 --- a/tests/remote-nvim/providers/ssh/ssh_provider_spec.lua +++ b/tests/remote-nvim/providers/ssh/ssh_provider_spec.lua @@ -15,8 +15,14 @@ describe("SSH Provider", function() local ssh_provider = SSHProvider("localhost", "localhost -p 3011") assert.equals(ssh_provider.conn_opts, "-p 3011") - ssh_provider = SSHProvider("localhost", " localhost ") + ssh_provider = SSHProvider("localhost", " localhost") assert.equals(ssh_provider.conn_opts, "") + + ssh_provider = SSHProvider("localhost", "localhost ") + assert.equals(ssh_provider.conn_opts, "") + + ssh_provider = SSHProvider("user@localhost", "user@localhost") + assert.equals("", ssh_provider.conn_opts) end) it("should remove '-N' ssh option from connection options (if present)", function()