Skip to content

Commit 146408d

Browse files
authored
fix: proper tilde expansion (#1916)
1 parent fa61e06 commit 146408d

File tree

4 files changed

+25
-30
lines changed

4 files changed

+25
-30
lines changed

lua/neo-tree/command/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ handle_reveal = function(args, state)
231231
return
232232
end
233233

234+
log.debug("Prompting for change cwd", args)
234235
-- force was not specified and the file does not belong to cwd, so we need to ask the user
235236
inputs.confirm("File not in cwd. Change cwd to " .. reveal_file_parent .. "?", function(response)
236237
if response == true then

lua/neo-tree/sources/manager.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ M.get_path_to_reveal = function(include_terminals)
262262
return abspath
263263
end
264264

265-
return utils.path_join(vim.fn.getcwd(), utils.normalize_path(buf_relpath))
265+
return utils.path_join(vim.fn.getcwd(), buf_relpath)
266266
end
267267

268268
---@param source_name string

lua/neo-tree/utils/_compat.lua

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -141,41 +141,38 @@ local function path_resolve_dot(path)
141141
return (is_path_absolute and "/" or "") .. table.concat(new_path_components, "/")
142142
end
143143

144-
--- Expand tilde (~) character at the beginning of the path to the user's home directory.
145-
---
146-
--- @param path string Path to expand.
147-
--- @param sep string|nil Path separator to use. Uses os_sep by default.
148-
--- @return string Expanded path.
149-
local function expand_home(path, sep)
150-
sep = sep or require("neo-tree.utils").path_separator
151-
152-
if vim.startswith(path, "~") then
153-
local home = uv.os_homedir() or "~" --- @type string
154-
155-
if home:sub(-1) == sep then
156-
home = home:sub(1, -2)
157-
end
158-
159-
path = home .. path:sub(2) --- @type string
160-
end
161-
162-
return path
144+
local user = uv.os_get_passwd().username
145+
local path_segment_ends = { "/", "\\", "" }
146+
---@param path string
147+
---@param i integer
148+
---@return boolean
149+
local function path_segment_ends_at(path, i)
150+
return vim.tbl_contains(path_segment_ends, path:sub(i, i))
163151
end
164-
165-
--- Backporting vim.fs.normalize from neovim 0.11
152+
--- A modified vim.fs.normalize from neovim 0.11, with proper home expansion
166153
function compat.fs_normalize(path, opts)
167154
opts = opts or {}
168155

169156
local win = opts.win == nil and require("neo-tree.utils").is_windows or not not opts.win
170-
local os_sep_local = win and "\\" or "/"
157+
local os_sep = win and "\\" or "/"
171158

172159
-- Empty path is already normalized
173160
if path == "" then
174161
return ""
175162
end
176163

177-
-- Expand ~ to user's home directory
178-
path = expand_home(path, os_sep_local)
164+
if path:sub(1, 1) == "~" then
165+
local home = uv.os_homedir() or "~" --- @type string
166+
if home:sub(-1) == os_sep then
167+
home = home:sub(1, -2)
168+
end
169+
170+
if path_segment_ends_at(path, 2) then
171+
path = home .. path:sub(2)
172+
elseif vim.startswith(path, "~" .. user) and path_segment_ends_at(path, 2 + #user) then
173+
path = home .. path:sub(#user + 2) --- @type string
174+
end
175+
end
179176

180177
-- Expand environment variables if `opts.expand_env` isn't `false`
181178
if opts.expand_env == nil or opts.expand_env then
@@ -184,7 +181,7 @@ function compat.fs_normalize(path, opts)
184181

185182
if win then
186183
-- Convert path separator to `/`
187-
path = path:gsub(os_sep_local, "/")
184+
path = path:gsub(os_sep, "/")
188185
end
189186

190187
-- Check for double slashes at the start of the path because they have special meaning

lua/neo-tree/utils/init.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -887,10 +887,7 @@ M.resolve_config_option = function(state, config_option, default_value)
887887
end
888888
end
889889

890-
local fs_normalize = vim.fs.normalize
891-
if vim.fn.has("nvim-0.11") == 0 then
892-
fs_normalize = compat.fs_normalize
893-
end
890+
local fs_normalize = compat.fs_normalize
894891

895892
---Normalize a path, to avoid errors when comparing paths.
896893
---@param path string The path to be normalize.

0 commit comments

Comments
 (0)