Skip to content

Commit

Permalink
Refactoring add_action to remove duplication
Browse files Browse the repository at this point in the history
Thanks #400
  • Loading branch information
timtatt committed Jul 7, 2024
1 parent 391b42c commit 4e72632
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 43 deletions.
2 changes: 2 additions & 0 deletions lua/oil/adapters/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ local function read_link_data(path, cb)
)
end

M.supports_subdir_rename = true

---@param path string
---@param entry_type nil|oil.EntryType
---@return string
Expand Down
2 changes: 2 additions & 0 deletions lua/oil/adapters/ssh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ local function scp(args, ...)
shell.run(cmd, ...)
end

M.supports_subdir_rename = false

---@param oil_url string
---@return oil.sshUrl
M.parse_url = function(oil_url)
Expand Down
8 changes: 8 additions & 0 deletions lua/oil/adapters/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ local cache = require("oil.cache")
local util = require("oil.util")
local M = {}

M.supports_subdir_rename = true

---@param url string
---@param callback fun(url: string)
M.normalize_url = function(url, callback)
Expand Down Expand Up @@ -88,6 +90,12 @@ M.read_file = function(bufnr)
-- pass
end

---@param path string
---@return boolean
M.file_exists = function(path)
return false
end

---@param bufnr integer
M.write_file = function(bufnr)
-- pass
Expand Down
2 changes: 2 additions & 0 deletions lua/oil/adapters/trash/freedesktop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ local FIELD_META = constants.FIELD_META

local M = {}

M.supports_subdir_rename = false

local function ensure_trash_dir(path)
local mode = 448 -- 0700
fs.mkdirp(fs.join(path, "info"), mode)
Expand Down
2 changes: 2 additions & 0 deletions lua/oil/adapters/trash/mac.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ local uv = vim.uv or vim.loop

local M = {}

M.supports_subdir_rename = false

local function touch_dir(path)
uv.fs_mkdir(path, 448) -- 0700
end
Expand Down
2 changes: 2 additions & 0 deletions lua/oil/adapters/trash/windows.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ local FIELD_TYPE = constants.FIELD_TYPE

local M = {}

M.supports_subdir_rename = false

---@return string
local function get_trash_dir()
local cwd = assert(vim.fn.getcwd())
Expand Down
1 change: 1 addition & 0 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local M = {}
---@class (exact) oil.Adapter
---@field name string The unique name of the adapter (this will be set automatically)
---@field list fun(path: string, column_defs: string[], cb: fun(err?: string, entries?: oil.InternalEntry[], fetch_more?: fun())) Async function to list a directory.
---@field supports_subdir_rename boolean Whether the adapter allows renaming of files into a subdirectory
---@field file_exists fun(path: string) Returns true if the file at path exists
---@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.
Expand Down
71 changes: 29 additions & 42 deletions lua/oil/mutator/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,60 +92,47 @@ M.create_actions_from_diffs = function(all_diffs)
local parent_url = vim.api.nvim_buf_get_name(bufnr)
for _, diff in ipairs(diffs) do
if diff.type == "new" then
if diff.id then
-- Parse nested files like foo/bar/baz
-- Create the parent directories if they don't exist
local path_sep = fs.is_windows and "[/\\]" or "/"
local pieces = vim.split(diff.name, path_sep)
local url = parent_url:gsub("/$", "")
for i, v in ipairs(pieces) do
if i == #pieces then
break
end

url = url .. "/" .. v
add_action({
type = "create",
url = url,
entry_type = "directory",
link = diff.link,
})
end
local file_name = diff.name
local url = parent_url:gsub("/$", "")
--- Parse nested files like foo/bar/baz
local dirs = vim.split(diff.name, fs.sep)
file_name = table.remove(dirs)
for _, v in ipairs(dirs) do
url = url .. "/" .. v
add_action({
type = "create",
url = url,
entry_type = "directory",
link = diff.link,
})
end

if diff.id then
local by_id = diff_by_id[diff.id]
---HACK: set the destination on this diff for use later
---@diagnostic disable-next-line: inject-field
diff.dest = parent_url .. diff.name
table.insert(by_id, diff)
else
-- Parse nested files like foo/bar/baz
local path_sep = fs.is_windows and "[/\\]" or "/"
local pieces = vim.split(diff.name, path_sep)
local url = parent_url:gsub("/$", "")
for i, v in ipairs(pieces) do
local is_last = i == #pieces
local entry_type = is_last and diff.entry_type or "directory"
local alternation = v:match("{([^}]+)}")
if is_last and alternation then
-- Parse alternations like foo.{js,test.js}
for _, alt in ipairs(vim.split(alternation, ",")) do
local alt_url = url .. "/" .. v:gsub("{[^}]+}", alt)
add_action({
type = "create",
url = alt_url,
entry_type = entry_type,
link = diff.link,
})
end
else
url = url .. "/" .. v
local alternation = file_name:match("{([^}]+)}")
if alternation then
-- Parse alternations like foo.{js,test.js}
for _, alt in ipairs(vim.split(alternation, ",")) do
local alt_url = url .. "/" .. file_name:gsub("{[^}]+}", alt)
add_action({
type = "create",
url = url,
entry_type = entry_type,
url = alt_url,
entry_type = diff.entry_type,
link = diff.link,
})
end
else
add_action({
type = "create",
url = url .. "/" .. file_name,
entry_type = diff.entry_type,
link = diff.link,
})
end
end
elseif diff.type == "change" then
Expand Down
2 changes: 1 addition & 1 deletion lua/oil/mutator/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ M.parse = function(bufnr)
elseif not entry then
err_message = "Could not find existing entry (was the ID changed?)"
elseif
adapter.name ~= "files"
not adapter.supports_subdir_rename
and (parsed_entry.name:match("/") or parsed_entry.name:match(fs.sep))
then
err_message = "Filename cannot contain path separator"
Expand Down

0 comments on commit 4e72632

Please sign in to comment.