From 05b318a0013c244b587c9465376c3bd056920255 Mon Sep 17 00:00:00 2001 From: Davis Sanders Date: Mon, 14 Aug 2023 11:56:52 -0400 Subject: [PATCH 1/5] Fix escape special characters on windows fixes #2362 --- lua/nvim-tree/actions/node/open-file.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 35ec6b66873..2592f27f604 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -270,6 +270,9 @@ local function open_in_new_window(filename, mode) end local fname = vim.fn.fnameescape(filename) + if vim.fn.has "win32" == 1 or vim.fn.has "win32unix" == 1 then + fname = fname:gsub("%(", "\\("):gsub("%)", "\\)") + end local cmd if create_new_window then From d9b8ab15f5d0740e9d5c79d9477a6d59fd3edce3 Mon Sep 17 00:00:00 2001 From: Davis Sanders Date: Mon, 14 Aug 2023 15:57:25 -0400 Subject: [PATCH 2/5] use utils for windows check --- lua/nvim-tree/actions/node/open-file.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 2592f27f604..9eba8cfbe04 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -270,7 +270,7 @@ local function open_in_new_window(filename, mode) end local fname = vim.fn.fnameescape(filename) - if vim.fn.has "win32" == 1 or vim.fn.has "win32unix" == 1 then + if utils.is_windows then fname = fname:gsub("%(", "\\("):gsub("%)", "\\)") end From 05222b4f5eb2371543f802941a97a6e0c453d2dc Mon Sep 17 00:00:00 2001 From: Davis Sanders Date: Tue, 15 Aug 2023 09:03:36 -0400 Subject: [PATCH 3/5] Add function to escape special chars on windows --- lua/nvim-tree/actions/node/open-file.lua | 4 +--- lua/nvim-tree/utils.lua | 10 ++++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 9eba8cfbe04..c6ec6c55d0c 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -270,9 +270,7 @@ local function open_in_new_window(filename, mode) end local fname = vim.fn.fnameescape(filename) - if utils.is_windows then - fname = fname:gsub("%(", "\\("):gsub("%)", "\\)") - end + fname = utils.escape_windows_special_chars(fname) local cmd if create_new_window then diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index 78d7853bfb7..d8d2550a5ad 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -210,6 +210,16 @@ function M.canonical_path(path) return path end +-- Escapes special characters in string if windows else returns unmodified string. +-- @param string string +-- @return string +function M.escape_windows_special_chars(string) + if M.is_windows then + return string:gsub("%(", "\\("):gsub("%)", "\\)") + end + return string +end + -- Create empty sub-tables if not present -- @param tbl to create empty inside of -- @param path dot separated string of sub-tables From 37613cefaee00fca14e909c5875336123a59a709 Mon Sep 17 00:00:00 2001 From: Davis Sanders Date: Tue, 15 Aug 2023 10:06:03 -0400 Subject: [PATCH 4/5] Change escape string function to use and/or --- lua/nvim-tree/utils.lua | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index d8d2550a5ad..e24b94c9dc6 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -214,10 +214,7 @@ end -- @param string string -- @return string function M.escape_windows_special_chars(string) - if M.is_windows then - return string:gsub("%(", "\\("):gsub("%)", "\\)") - end - return string + return M.is_windows and string:gsub("%(", "\\("):gsub("%)", "\\)") or string end -- Create empty sub-tables if not present From ef5fc4b8bc4920036639e2884fa5f2d41d566436 Mon Sep 17 00:00:00 2001 From: Davis Sanders Date: Wed, 16 Aug 2023 08:38:19 -0400 Subject: [PATCH 5/5] Add nil check in escape special chars function --- lua/nvim-tree/actions/node/open-file.lua | 2 +- lua/nvim-tree/utils.lua | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index c6ec6c55d0c..0add9560879 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -270,7 +270,7 @@ local function open_in_new_window(filename, mode) end local fname = vim.fn.fnameescape(filename) - fname = utils.escape_windows_special_chars(fname) + fname = utils.escape_special_chars(fname) local cmd if create_new_window then diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index e24b94c9dc6..386472eaa85 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -211,10 +211,13 @@ function M.canonical_path(path) end -- Escapes special characters in string if windows else returns unmodified string. --- @param string string --- @return string -function M.escape_windows_special_chars(string) - return M.is_windows and string:gsub("%(", "\\("):gsub("%)", "\\)") or string +-- @param path string +-- @return path +function M.escape_special_chars(path) + if path == nil then + return path + end + return M.is_windows and path:gsub("%(", "\\("):gsub("%)", "\\)") or path end -- Create empty sub-tables if not present