Skip to content

Commit

Permalink
fix(windows): navigating into drive letter root directories (#341)
Browse files Browse the repository at this point in the history
* Fixed drive browsing on windows

* Fixed naming

* fix: Uppercase drive letter only

* updated: Filter out network drives on windows

* Update files.lua

* Update files.lua

* fixed: mapped drives

* addslash to check for double slash

* Fixed indents

* Reverted addslash change

* Fixed windows initial buffer name

* Reverted formatting

* Cleaned up callback

* Fix addslash to handle \ too

* Allow running tests workflow from fork

* Fix workflow

* Test

* Tests

* refactor: readability and comments

* fix: convert buffer name to posix when hijacking directory

---------

Co-authored-by: Steven Arcangeli <[email protected]>
  • Loading branch information
mbwilding and stevearc authored Apr 24, 2024
1 parent 3b3a6b2 commit f3a31eb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
25 changes: 21 additions & 4 deletions lua/oil/adapters/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,36 @@ end
M.normalize_url = function(url, callback)
local scheme, path = util.parse_url(url)
assert(path)
if fs.is_windows and path == "/" then
return callback(url)

if fs.is_windows then
if path == "/" then
return callback(url)
else
local is_root_drive = path:match("^/%u$")
if is_root_drive then
return callback(url .. "/")
end
end
end

local os_path = vim.fn.fnamemodify(fs.posix_to_os_path(path), ":p")
uv.fs_realpath(os_path, function(err, new_os_path)
local realpath = new_os_path or os_path
local realpath
if fs.is_windows then
-- Ignore the fs_realpath on windows because it will resolve mapped network drives to the IP
-- address instead of using the drive letter
realpath = os_path
else
realpath = new_os_path or os_path
end

uv.fs_stat(
realpath,
vim.schedule_wrap(function(stat_err, stat)
local is_directory
if stat then
is_directory = stat.type == "directory"
elseif vim.endswith(realpath, "/") then
elseif vim.endswith(realpath, "/") or (fs.is_windows and vim.endswith(realpath, "\\")) then
is_directory = true
else
local filetype = vim.filetype.match({ filename = vim.fs.basename(realpath) })
Expand Down
7 changes: 4 additions & 3 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ end
---@return boolean
local function maybe_hijack_directory_buffer(bufnr)
local config = require("oil.config")
local fs = require("oil.fs")
local util = require("oil.util")
if not config.default_file_explorer then
return false
Expand All @@ -738,10 +739,10 @@ local function maybe_hijack_directory_buffer(bufnr)
if util.parse_url(bufname) or vim.fn.isdirectory(bufname) == 0 then
return false
end
local replaced = util.rename_buffer(
bufnr,
util.addslash(config.adapter_to_scheme.files .. vim.fn.fnamemodify(bufname, ":p"))
local new_name = util.addslash(
config.adapter_to_scheme.files .. fs.os_to_posix_path(vim.fn.fnamemodify(bufname, ":p"))
)
local replaced = util.rename_buffer(bufnr, new_name)
return not replaced
end

Expand Down

0 comments on commit f3a31eb

Please sign in to comment.