Skip to content

fix: handle more edge cases for Windows path escaping #1458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions lua/neo-tree/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1042,24 +1042,20 @@ end
---be lost.
---
---For more details, see issue #889 when this function was introduced, and further
---discussions in #1264 and #1352.
---discussions in #1264, #1352, and #1448.
---@param path string
---@return string
M.escape_path_for_cmd = function(path)
local escaped_path = vim.fn.fnameescape(path)
if M.is_windows then
-- on windows, some punctuation preceeded by a `\` needs to have a second
-- `\` added to preserve the path separator. this is a naive replacement and
-- definitely not bullet proof. if we start finding issues with opening files
-- or changing directories, look here first. #1382 was the first regression
-- from the implementation that used lua's %p to match punctuation, which
-- did not quite work. the following characters were tested on windows to
-- be known to require an extra escape character.
for _, c in ipairs({ "&", "(", ")", ";", "^", "`" }) do
-- lua doesn't seem to have a problem with an unnecessary `%` escape
-- (e.g., `%;`), so we can use it to ensure we match the punctuation
-- for the ones that do (e.g., `%(` and `%^`)
escaped_path = escaped_path:gsub("\\%" .. c, "\\%1")
-- there is too much history to this logic to capture in a reasonable comment.
-- essentially, the following logic adds a number of `\` depending on the leading
-- character in a path segment. see #1264, #1352, and #1448 for more info.
local need_extra_esc = path:find("[%[%]`%$~]")
local esc = need_extra_esc and "\\\\" or "\\"
escaped_path = escaped_path:gsub("\\[%(%)%^&;]", esc .. "%1")
if need_extra_esc then
escaped_path = escaped_path:gsub("\\\\['` ]", "\\%1")
end
end
return escaped_path
Expand Down
Loading