From 83e4d049228233df1870c92e160effb33e314396 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Mon, 18 Sep 2023 20:46:16 -0700 Subject: [PATCH] fix: correctly resolve new files when selected (#179) --- lua/oil/adapters/files.lua | 19 +++++++++++++++++++ lua/oil/init.lua | 15 ++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lua/oil/adapters/files.lua b/lua/oil/adapters/files.lua index 29641f9b..ee1bb4f6 100644 --- a/lua/oil/adapters/files.lua +++ b/lua/oil/adapters/files.lua @@ -232,6 +232,25 @@ M.normalize_url = function(url, callback) end) end +---@param url string +---@param entry oil.Entry +---@param cb fun(path: nil|string) +M.get_entry_path = function(url, entry, cb) + if entry.id then + local parent_url = cache.get_parent_url(entry.id) + local scheme, path = util.parse_url(parent_url) + M.normalize_url(scheme .. path .. entry.name, cb) + else + if entry.type == "directory" then + cb(url) + else + local _, path = util.parse_url(url) + local os_path = vim.fn.fnamemodify(fs.posix_to_os_path(assert(path)), ":p") + cb(os_path) + end + end +end + ---@param url string ---@param column_defs string[] ---@param cb fun(err?: string, entries?: oil.InternalEntry[], fetch_more?: fun()) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 7e6c2ee6..1dca041c 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -15,6 +15,7 @@ local M = {} ---@field is_modifiable fun(bufnr: integer): boolean Return true if this directory is modifiable (allows for directories with read-only permissions). ---@field get_column fun(name: string): nil|oil.ColumnDefinition If the adapter has any adapter-specific columns, return them when fetched by name. ---@field normalize_url fun(url: string, callback: fun(url: string)) Before oil opens a url it will be normalized. This allows for link following, path normalizing, and converting an oil file url to the actual path of a file. +---@field get_entry_path? fun(url: string, entry: oil.Entry, callback: fun(path: nil|string)) Similar to normalize_url, but used when selecting an entry ---@field render_action? fun(action: oil.Action): string Render a mutation action for display in the preview window. Only needed if adapter is modifiable. ---@field perform_action? fun(action: oil.Action, cb: fun(err: nil|string)) Perform a mutation action. Only needed if adapter is modifiable. ---@field read_file? fun(bufnr: integer) Used for adapters that deal with remote/virtual files. Read the contents of the file into a buffer. @@ -456,6 +457,7 @@ M.select = function(opts, callback) local mode = vim.api.nvim_get_mode().mode local is_visual = mode:match("^[vV]") + ---@type oil.Entry[] local entries = {} if is_visual then -- This is the best way to get the visual selection at the moment @@ -546,9 +548,20 @@ M.select = function(opts, callback) end end + local get_edit_path + if adapter.get_entry_path then + get_edit_path = function(edit_cb) + adapter.get_entry_path(url, entry, edit_cb) + end + else + get_edit_path = function(edit_cb) + adapter.normalize_url(url, edit_cb) + end + end + -- Normalize the url before opening to prevent needing to rename them inside the BufReadCmd -- Renaming buffers during opening can lead to missed autocmds - adapter.normalize_url(url, function(normalized_url) + get_edit_path(function(normalized_url) local mods = { vertical = opts.vertical, horizontal = opts.horizontal,