feat: display ../ entry in oil buffers (#166)

This commit is contained in:
Steven Arcangeli 2023-11-05 08:00:38 -08:00
parent 0715f1b0aa
commit d8f0d91b10
3 changed files with 34 additions and 16 deletions

View file

@ -55,6 +55,7 @@ M.get_entry_on_line = function(bufnr, lnum)
return entry return entry
else else
return { return {
id = result.data.id,
name = result.data.name, name = result.data.name,
type = result.data._type, type = result.data._type,
parsed_name = result.data.name, parsed_name = result.data.name,
@ -429,6 +430,7 @@ end
M.select = function(opts, callback) M.select = function(opts, callback)
local cache = require("oil.cache") local cache = require("oil.cache")
local config = require("oil.config") local config = require("oil.config")
local pathutil = require("oil.pathutil")
opts = vim.tbl_extend("keep", opts or {}, {}) opts = vim.tbl_extend("keep", opts or {}, {})
local function finish(err) local function finish(err)
if err then if err then
@ -496,12 +498,15 @@ M.select = function(opts, callback)
local bufname = vim.api.nvim_buf_get_name(0) local bufname = vim.api.nvim_buf_get_name(0)
local any_moved = false local any_moved = false
for _, entry in ipairs(entries) do for _, entry in ipairs(entries) do
local is_new_entry = entry.id == nil -- Ignore entries with ID 0 (typically the "../" entry)
local is_moved_from_dir = entry.id and cache.get_parent_url(entry.id) ~= bufname if entry.id ~= 0 then
local is_renamed = entry.parsed_name ~= entry.name local is_new_entry = entry.id == nil
if is_new_entry or is_moved_from_dir or is_renamed then local is_moved_from_dir = entry.id and cache.get_parent_url(entry.id) ~= bufname
any_moved = true local is_renamed = entry.parsed_name ~= entry.name
break if is_new_entry or is_moved_from_dir or is_renamed then
any_moved = true
break
end
end end
end end
if any_moved and not opts.preview and config.prompt_save_on_select_new_entry then if any_moved and not opts.preview and config.prompt_save_on_select_new_entry then
@ -521,6 +526,8 @@ M.select = function(opts, callback)
end end
local prev_win = vim.api.nvim_get_current_win() local prev_win = vim.api.nvim_get_current_win()
local scheme, dir = util.parse_url(bufname)
assert(scheme and dir)
-- Async iter over entries so we can normalize the url before opening -- Async iter over entries so we can normalize the url before opening
local i = 1 local i = 1
local function open_next_entry(cb) local function open_next_entry(cb)
@ -529,9 +536,7 @@ M.select = function(opts, callback)
if not entry then if not entry then
return cb() return cb()
end end
local scheme, dir = util.parse_url(bufname) local url = scheme .. dir .. entry.name
local child = dir .. entry.name
local url = scheme .. child
local is_directory = entry.type == "directory" local is_directory = entry.type == "directory"
or ( or (
entry.type == "link" entry.type == "link"
@ -554,7 +559,11 @@ M.select = function(opts, callback)
end end
local get_edit_path local get_edit_path
if adapter.get_entry_path then if entry.name == ".." then
get_edit_path = function(edit_cb)
edit_cb(scheme .. pathutil.parent(dir))
end
elseif adapter.get_entry_path then
get_edit_path = function(edit_cb) get_edit_path = function(edit_cb)
adapter.get_entry_path(url, entry, edit_cb) adapter.get_entry_path(url, entry, edit_cb)
end end

View file

@ -165,8 +165,9 @@ M.parse = function(bufnr)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true) local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)
local original_entries = {} local original_entries = {}
for _, child in pairs(children) do for _, child in pairs(children) do
if view.should_display(child, bufnr) then local name = child[FIELD_NAME]
original_entries[child[FIELD_NAME]] = child[FIELD_ID] if view.should_display(name, bufnr) then
original_entries[name] = child[FIELD_ID]
end end
end end
local seen_names = {} local seen_names = {}
@ -191,6 +192,9 @@ M.parse = function(bufnr)
col = 0, col = 0,
}) })
goto continue goto continue
elseif result.data.id == 0 then
-- Ignore entries with ID 0 (typically the "../" entry)
goto continue
end end
local parsed_entry = result.data local parsed_entry = result.data
local entry = result.entry local entry = result.entry

View file

@ -15,11 +15,10 @@ local FIELD_META = constants.FIELD_META
-- map of path->last entry under cursor -- map of path->last entry under cursor
local last_cursor_entry = {} local last_cursor_entry = {}
---@param entry oil.InternalEntry ---@param name string
---@param bufnr integer ---@param bufnr integer
---@return boolean ---@return boolean
M.should_display = function(entry, bufnr) M.should_display = function(name, bufnr)
local name = entry[FIELD_NAME]
return not config.view_options.is_always_hidden(name, bufnr) return not config.view_options.is_always_hidden(name, bufnr)
and (not config.view_options.is_hidden_file(name, bufnr) or config.view_options.show_hidden) and (not config.view_options.is_hidden_file(name, bufnr) or config.view_options.show_hidden)
end end
@ -450,8 +449,14 @@ local function render_buffer(bufnr, opts)
for i in ipairs(column_defs) do for i in ipairs(column_defs) do
col_width[i + 1] = 1 col_width[i + 1] = 1
end end
if M.should_display("..", bufnr) then
local cols = M.format_entry_cols({ 0, "..", "directory" }, column_defs, col_width, adapter)
table.insert(line_table, cols)
end
for _, entry in ipairs(entry_list) do for _, entry in ipairs(entry_list) do
if not M.should_display(entry, bufnr) then if not M.should_display(entry[FIELD_NAME], bufnr) then
goto continue goto continue
end end
local cols = M.format_entry_cols(entry, column_defs, col_width, adapter) local cols = M.format_entry_cols(entry, column_defs, col_width, adapter)