Skip to content

Commit

Permalink
feat: display ../ entry in oil buffers (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Nov 5, 2023
1 parent 0715f1b commit d8f0d91
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
29 changes: 19 additions & 10 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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"
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lua/oil/mutator/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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
Expand Down
13 changes: 9 additions & 4 deletions lua/oil/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d8f0d91

Please sign in to comment.