From f3a31eba24587bc038592103d8f7e64648292115 Mon Sep 17 00:00:00 2001 From: Matthew Wilding Date: Wed, 24 Apr 2024 13:06:59 +0800 Subject: [PATCH] fix(windows): navigating into drive letter root directories (#341) * 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 --- lua/oil/adapters/files.lua | 25 +++++++++++++++++++++---- lua/oil/init.lua | 7 ++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lua/oil/adapters/files.lua b/lua/oil/adapters/files.lua index cf6f73ec..aab17501 100644 --- a/lua/oil/adapters/files.lua +++ b/lua/oil/adapters/files.lua @@ -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) }) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 6050245e..f334729b 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -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 @@ -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