diff --git a/lua/neo-tree/command/init.lua b/lua/neo-tree/command/init.lua index 0dfe2e07..7f8f8021 100644 --- a/lua/neo-tree/command/init.lua +++ b/lua/neo-tree/command/init.lua @@ -231,6 +231,7 @@ handle_reveal = function(args, state) return end + log.debug("Prompting for change cwd", args) -- force was not specified and the file does not belong to cwd, so we need to ask the user inputs.confirm("File not in cwd. Change cwd to " .. reveal_file_parent .. "?", function(response) if response == true then diff --git a/lua/neo-tree/sources/manager.lua b/lua/neo-tree/sources/manager.lua index c8ea0340..edae559b 100644 --- a/lua/neo-tree/sources/manager.lua +++ b/lua/neo-tree/sources/manager.lua @@ -262,7 +262,7 @@ M.get_path_to_reveal = function(include_terminals) return abspath end - return utils.path_join(vim.fn.getcwd(), utils.normalize_path(buf_relpath)) + return utils.path_join(vim.fn.getcwd(), buf_relpath) end ---@param source_name string diff --git a/lua/neo-tree/utils/_compat.lua b/lua/neo-tree/utils/_compat.lua index 334dd4a9..56d550a3 100644 --- a/lua/neo-tree/utils/_compat.lua +++ b/lua/neo-tree/utils/_compat.lua @@ -141,41 +141,38 @@ local function path_resolve_dot(path) return (is_path_absolute and "/" or "") .. table.concat(new_path_components, "/") end ---- Expand tilde (~) character at the beginning of the path to the user's home directory. ---- ---- @param path string Path to expand. ---- @param sep string|nil Path separator to use. Uses os_sep by default. ---- @return string Expanded path. -local function expand_home(path, sep) - sep = sep or require("neo-tree.utils").path_separator - - if vim.startswith(path, "~") then - local home = uv.os_homedir() or "~" --- @type string - - if home:sub(-1) == sep then - home = home:sub(1, -2) - end - - path = home .. path:sub(2) --- @type string - end - - return path +local user = uv.os_get_passwd().username +local path_segment_ends = { "/", "\\", "" } +---@param path string +---@param i integer +---@return boolean +local function path_segment_ends_at(path, i) + return vim.tbl_contains(path_segment_ends, path:sub(i, i)) end - ---- Backporting vim.fs.normalize from neovim 0.11 +--- A modified vim.fs.normalize from neovim 0.11, with proper home expansion function compat.fs_normalize(path, opts) opts = opts or {} local win = opts.win == nil and require("neo-tree.utils").is_windows or not not opts.win - local os_sep_local = win and "\\" or "/" + local os_sep = win and "\\" or "/" -- Empty path is already normalized if path == "" then return "" end - -- Expand ~ to user's home directory - path = expand_home(path, os_sep_local) + if path:sub(1, 1) == "~" then + local home = uv.os_homedir() or "~" --- @type string + if home:sub(-1) == os_sep then + home = home:sub(1, -2) + end + + if path_segment_ends_at(path, 2) then + path = home .. path:sub(2) + elseif vim.startswith(path, "~" .. user) and path_segment_ends_at(path, 2 + #user) then + path = home .. path:sub(#user + 2) --- @type string + end + end -- Expand environment variables if `opts.expand_env` isn't `false` if opts.expand_env == nil or opts.expand_env then @@ -184,7 +181,7 @@ function compat.fs_normalize(path, opts) if win then -- Convert path separator to `/` - path = path:gsub(os_sep_local, "/") + path = path:gsub(os_sep, "/") end -- Check for double slashes at the start of the path because they have special meaning diff --git a/lua/neo-tree/utils/init.lua b/lua/neo-tree/utils/init.lua index f89b43c7..570338e3 100644 --- a/lua/neo-tree/utils/init.lua +++ b/lua/neo-tree/utils/init.lua @@ -887,10 +887,7 @@ M.resolve_config_option = function(state, config_option, default_value) end end -local fs_normalize = vim.fs.normalize -if vim.fn.has("nvim-0.11") == 0 then - fs_normalize = compat.fs_normalize -end +local fs_normalize = compat.fs_normalize ---Normalize a path, to avoid errors when comparing paths. ---@param path string The path to be normalize.