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 45a93d9979.

* 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 ada77cb7e9.

---------

Co-authored-by: Alexander Courtis <alex@courtis.org>
This commit is contained in:
Jie Liu 2024-10-25 08:11:21 +08:00 committed by GitHub
parent 9b82ff9bba
commit 63c7ad9037
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 13 deletions

View File

@ -331,9 +331,9 @@ local function open_in_new_window(filename, mode)
local fname local fname
if M.relative_path then if M.relative_path then
fname = vim.fn.fnameescape(utils.path_relative(filename, vim.fn.getcwd())) fname = utils.escape_special_chars(vim.fn.fnameescape(utils.path_relative(filename, vim.fn.getcwd())))
else else
fname = vim.fn.fnameescape(filename) fname = utils.escape_special_chars(vim.fn.fnameescape(filename))
end end
local command local command
@ -370,28 +370,27 @@ end
---@param mode string ---@param mode string
---@param filename string ---@param filename string
function M.fn(mode, filename) function M.fn(mode, filename)
local fname = utils.escape_special_chars(filename)
if type(mode) ~= "string" then if type(mode) ~= "string" then
mode = "" mode = ""
end end
if mode == "tabnew" then if mode == "tabnew" then
return open_file_in_tab(fname) return open_file_in_tab(filename)
end end
if mode == "drop" then if mode == "drop" then
return drop(fname) return drop(filename)
end end
if mode == "tab_drop" then if mode == "tab_drop" then
return tab_drop(fname) return tab_drop(filename)
end end
if mode == "edit_in_place" then if mode == "edit_in_place" then
return edit_in_current_buf(fname) return edit_in_current_buf(filename)
end end
local buf_loaded = is_already_loaded(fname) local buf_loaded = is_already_loaded(filename)
local found_win = utils.get_win_buf_from_path(filename) local found_win = utils.get_win_buf_from_path(filename)
if found_win and (mode == "preview" or mode == "preview_no_picker") then if found_win and (mode == "preview" or mode == "preview_no_picker") then
@ -399,7 +398,7 @@ function M.fn(mode, filename)
end end
if not found_win then if not found_win then
open_in_new_window(fname, mode) open_in_new_window(filename, mode)
else else
vim.api.nvim_set_current_win(found_win) vim.api.nvim_set_current_win(found_win)
vim.bo.bufhidden = "" vim.bo.bufhidden = ""

View File

@ -59,6 +59,17 @@ function M.path_basename(path)
return path:sub(i + 1, #path) return path:sub(i + 1, #path)
end end
--- Check if there are parentheses before brackets, it causes problems for windows.
--- Refer to issue #2862 and #2961 for more details.
local function has_parentheses_and_brackets(path)
local _, i_parentheses = path:find("(", 1, true)
local _, i_brackets = path:find("[", 1, true)
if i_parentheses and i_brackets then
return true
end
return false
end
--- Get a path relative to another path. --- Get a path relative to another path.
---@param path string ---@param path string
---@param relative_to string|nil ---@param relative_to string|nil
@ -68,13 +79,18 @@ function M.path_relative(path, relative_to)
return path return path
end end
local _, r = path:find(M.path_add_trailing(relative_to), 1, true) local norm_path = path
local p = path if M.is_windows and has_parentheses_and_brackets(path) then
norm_path = path:gsub("/", "\\")
end
local _, r = norm_path:find(M.path_add_trailing(relative_to), 1, true)
local p = norm_path
if r then if r then
-- take the relative path starting after '/' -- take the relative path starting after '/'
-- if somehow given a completely matching path, -- if somehow given a completely matching path,
-- returns "" -- returns ""
p = path:sub(r + 1) p = norm_path:sub(r + 1)
end end
return p return p
end end
@ -272,6 +288,14 @@ function M.canonical_path(path)
return path return path
end end
--- Escapes special characters in string for windows, refer to issue #2862 and #2961 for more details.
local function escape_special_char_for_windows(path)
if has_parentheses_and_brackets(path) then
return path:gsub("\\", "/"):gsub("/ ", "\\ ")
end
return path:gsub("%(", "\\("):gsub("%)", "\\)")
end
--- Escapes special characters in string if windows else returns unmodified string. --- Escapes special characters in string if windows else returns unmodified string.
---@param path string ---@param path string
---@return string|nil ---@return string|nil
@ -279,7 +303,7 @@ function M.escape_special_chars(path)
if path == nil then if path == nil then
return path return path
end end
return M.is_windows and path:gsub("\\", "/") or path return M.is_windows and escape_special_char_for_windows(path) or path
end end
--- Create empty sub-tables if not present --- Create empty sub-tables if not present