Skip to content

Commit 63c7ad9

Browse files
fix(#2961): windows: escape brackets and parentheses when opening file (#2962)
* Revert "fix(#2862): windows path replaces backslashes with forward slashes (#2903)" This reverts commit 45a93d9. * fix the case when '()' and '[]' are both in file path * remove debug messages * remove unnecessary comments * add is_windows feature flag when normalizing path * add is_windows flag for filename change * Revert "add is_windows flag for filename change" This reverts commit ada77cb. --------- Co-authored-by: Alexander Courtis <[email protected]>
1 parent 9b82ff9 commit 63c7ad9

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

Diff for: lua/nvim-tree/actions/node/open-file.lua

+8-9
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,9 @@ local function open_in_new_window(filename, mode)
331331

332332
local fname
333333
if M.relative_path then
334-
fname = vim.fn.fnameescape(utils.path_relative(filename, vim.fn.getcwd()))
334+
fname = utils.escape_special_chars(vim.fn.fnameescape(utils.path_relative(filename, vim.fn.getcwd())))
335335
else
336-
fname = vim.fn.fnameescape(filename)
336+
fname = utils.escape_special_chars(vim.fn.fnameescape(filename))
337337
end
338338

339339
local command
@@ -370,36 +370,35 @@ end
370370
---@param mode string
371371
---@param filename string
372372
function M.fn(mode, filename)
373-
local fname = utils.escape_special_chars(filename)
374373
if type(mode) ~= "string" then
375374
mode = ""
376375
end
377376

378377
if mode == "tabnew" then
379-
return open_file_in_tab(fname)
378+
return open_file_in_tab(filename)
380379
end
381380

382381
if mode == "drop" then
383-
return drop(fname)
382+
return drop(filename)
384383
end
385384

386385
if mode == "tab_drop" then
387-
return tab_drop(fname)
386+
return tab_drop(filename)
388387
end
389388

390389
if mode == "edit_in_place" then
391-
return edit_in_current_buf(fname)
390+
return edit_in_current_buf(filename)
392391
end
393392

394-
local buf_loaded = is_already_loaded(fname)
393+
local buf_loaded = is_already_loaded(filename)
395394

396395
local found_win = utils.get_win_buf_from_path(filename)
397396
if found_win and (mode == "preview" or mode == "preview_no_picker") then
398397
return
399398
end
400399

401400
if not found_win then
402-
open_in_new_window(fname, mode)
401+
open_in_new_window(filename, mode)
403402
else
404403
vim.api.nvim_set_current_win(found_win)
405404
vim.bo.bufhidden = ""

Diff for: lua/nvim-tree/utils.lua

+28-4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ function M.path_basename(path)
5959
return path:sub(i + 1, #path)
6060
end
6161

62+
--- Check if there are parentheses before brackets, it causes problems for windows.
63+
--- Refer to issue #2862 and #2961 for more details.
64+
local function has_parentheses_and_brackets(path)
65+
local _, i_parentheses = path:find("(", 1, true)
66+
local _, i_brackets = path:find("[", 1, true)
67+
if i_parentheses and i_brackets then
68+
return true
69+
end
70+
return false
71+
end
72+
6273
--- Get a path relative to another path.
6374
---@param path string
6475
---@param relative_to string|nil
@@ -68,13 +79,18 @@ function M.path_relative(path, relative_to)
6879
return path
6980
end
7081

71-
local _, r = path:find(M.path_add_trailing(relative_to), 1, true)
72-
local p = path
82+
local norm_path = path
83+
if M.is_windows and has_parentheses_and_brackets(path) then
84+
norm_path = path:gsub("/", "\\")
85+
end
86+
87+
local _, r = norm_path:find(M.path_add_trailing(relative_to), 1, true)
88+
local p = norm_path
7389
if r then
7490
-- take the relative path starting after '/'
7591
-- if somehow given a completely matching path,
7692
-- returns ""
77-
p = path:sub(r + 1)
93+
p = norm_path:sub(r + 1)
7894
end
7995
return p
8096
end
@@ -272,14 +288,22 @@ function M.canonical_path(path)
272288
return path
273289
end
274290

291+
--- Escapes special characters in string for windows, refer to issue #2862 and #2961 for more details.
292+
local function escape_special_char_for_windows(path)
293+
if has_parentheses_and_brackets(path) then
294+
return path:gsub("\\", "/"):gsub("/ ", "\\ ")
295+
end
296+
return path:gsub("%(", "\\("):gsub("%)", "\\)")
297+
end
298+
275299
--- Escapes special characters in string if windows else returns unmodified string.
276300
---@param path string
277301
---@return string|nil
278302
function M.escape_special_chars(path)
279303
if path == nil then
280304
return path
281305
end
282-
return M.is_windows and path:gsub("\\", "/") or path
306+
return M.is_windows and escape_special_char_for_windows(path) or path
283307
end
284308

285309
--- Create empty sub-tables if not present

0 commit comments

Comments
 (0)