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
else
return {
id = result.data.id,
name = result.data.name,
type = result.data._type,
parsed_name = result.data.name,
@ -429,6 +430,7 @@ end
M.select = function(opts, callback)
local cache = require("oil.cache")
local config = require("oil.config")
local pathutil = require("oil.pathutil")
opts = vim.tbl_extend("keep", opts or {}, {})
local function finish(err)
if err then
@ -496,12 +498,15 @@ M.select = function(opts, callback)
local bufname = vim.api.nvim_buf_get_name(0)
local any_moved = false
for _, entry in ipairs(entries) do
local is_new_entry = entry.id == nil
local is_moved_from_dir = entry.id and cache.get_parent_url(entry.id) ~= bufname
local is_renamed = entry.parsed_name ~= entry.name
if is_new_entry or is_moved_from_dir or is_renamed then
any_moved = true
break
-- Ignore entries with ID 0 (typically the "../" entry)
if entry.id ~= 0 then
local is_new_entry = entry.id == nil
local is_moved_from_dir = entry.id and cache.get_parent_url(entry.id) ~= bufname
local is_renamed = entry.parsed_name ~= entry.name
if is_new_entry or is_moved_from_dir or is_renamed then
any_moved = true
break
end
end
end
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
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
local i = 1
local function open_next_entry(cb)
@ -529,9 +536,7 @@ M.select = function(opts, callback)
if not entry then
return cb()
end
local scheme, dir = util.parse_url(bufname)
local child = dir .. entry.name
local url = scheme .. child
local url = scheme .. dir .. entry.name
local is_directory = entry.type == "directory"
or (
entry.type == "link"
@ -554,7 +559,11 @@ M.select = function(opts, callback)
end
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)
adapter.get_entry_path(url, entry, edit_cb)
end

View file

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

View file

@ -15,11 +15,10 @@ local FIELD_META = constants.FIELD_META
-- map of path->last entry under cursor
local last_cursor_entry = {}
---@param entry oil.InternalEntry
---@param name string
---@param bufnr integer
---@return boolean
M.should_display = function(entry, bufnr)
local name = entry[FIELD_NAME]
M.should_display = function(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)
end
@ -450,8 +449,14 @@ local function render_buffer(bufnr, opts)
for i in ipairs(column_defs) do
col_width[i + 1] = 1
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
if not M.should_display(entry, bufnr) then
if not M.should_display(entry[FIELD_NAME], bufnr) then
goto continue
end
local cols = M.format_entry_cols(entry, column_defs, col_width, adapter)