@@ -141,41 +141,38 @@ local function path_resolve_dot(path)
141141 return (is_path_absolute and " /" or " " ) .. table.concat (new_path_components , " /" )
142142end
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 ))
163151end
164-
165- --- Backporting vim.fs.normalize from neovim 0.11
152+ --- A modified vim.fs.normalize from neovim 0.11, with proper home expansion
166153function 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
0 commit comments