Skip to content

Commit

Permalink
fix: correctly resolve new files when selected (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Sep 19, 2023
1 parent da05530 commit 83e4d04
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lua/oil/adapters/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
15 changes: 14 additions & 1 deletion lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 83e4d04

Please sign in to comment.