mirror of
https://github.com/nvim-tree/nvim-tree.lua
synced 2024-09-16 14:34:03 +02:00
refactor(#2830): multi instance marks
This commit is contained in:
parent
ad0b95dee5
commit
741511a4c6
7 changed files with 115 additions and 96 deletions
|
@ -840,7 +840,7 @@ function M.setup(conf)
|
|||
require("nvim-tree.view").setup(opts)
|
||||
require("nvim-tree.lib").setup(opts)
|
||||
require("nvim-tree.renderer").setup(opts)
|
||||
require("nvim-tree.marks").setup(opts)
|
||||
require("nvim-tree.marks").setup()
|
||||
require("nvim-tree.buffers").setup(opts)
|
||||
require("nvim-tree.help").setup(opts)
|
||||
require("nvim-tree.watcher").setup(opts)
|
||||
|
|
|
@ -7,9 +7,6 @@ local appearance_diagnostics = require "nvim-tree.appearance.diagnostics"
|
|||
local events = require "nvim-tree.events"
|
||||
local help = require "nvim-tree.help"
|
||||
local marks_navigation = require "nvim-tree.marks.navigation"
|
||||
local marks_bulk_delete = require "nvim-tree.marks.bulk-delete"
|
||||
local marks_bulk_trash = require "nvim-tree.marks.bulk-trash"
|
||||
local marks_bulk_move = require "nvim-tree.marks.bulk-move"
|
||||
local keymap = require "nvim-tree.keymap"
|
||||
local notify = require "nvim-tree.notify"
|
||||
|
||||
|
@ -76,28 +73,16 @@ local function wrap_node_or_nil(fn)
|
|||
end
|
||||
end
|
||||
|
||||
---Inject the explorer as the first argument if present otherwise do nothing.
|
||||
---@param fn function function to invoke
|
||||
---@return fun(...) : any
|
||||
local function wrap_explorer(fn)
|
||||
return function(...)
|
||||
local explorer = core.get_explorer()
|
||||
if explorer then
|
||||
return fn(explorer, ...)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Invoke a member's method on the singleton explorer.
|
||||
---Print error when setup not called.
|
||||
---@param explorer_member string explorer member name
|
||||
---@param member_method string method name to invoke on member
|
||||
---@param member string explorer member name
|
||||
---@param method string method name to invoke on member
|
||||
---@return fun(...) : any
|
||||
local function wrap_explorer_member(explorer_member, member_method)
|
||||
local function explorer(member, method)
|
||||
return wrap(function(...)
|
||||
local explorer = core.get_explorer()
|
||||
if explorer then
|
||||
return explorer[explorer_member][member_method](explorer[explorer_member], ...)
|
||||
local e = core.get_explorer()
|
||||
if e then
|
||||
return e[member][method](e[member], ...)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
@ -264,16 +249,16 @@ Api.git.reload = wrap(actions.reloaders.reload_git)
|
|||
Api.events.subscribe = events.subscribe
|
||||
Api.events.Event = events.Event
|
||||
|
||||
Api.live_filter.start = wrap_explorer_member("live_filter", "start_filtering")
|
||||
Api.live_filter.clear = wrap_explorer_member("live_filter", "clear_filter")
|
||||
Api.live_filter.start = explorer("live_filter", "start_filtering")
|
||||
Api.live_filter.clear = explorer("live_filter", "clear_filter")
|
||||
|
||||
Api.marks.get = wrap_node(wrap_explorer_member("marks", "get_mark"))
|
||||
Api.marks.list = wrap_explorer_member("marks", "get_marks")
|
||||
Api.marks.toggle = wrap_node(wrap_explorer_member("marks", "toggle_mark"))
|
||||
Api.marks.clear = wrap_explorer_member("marks", "clear_marks")
|
||||
Api.marks.bulk.delete = wrap_explorer(marks_bulk_delete.bulk_delete)
|
||||
Api.marks.bulk.trash = wrap_explorer(marks_bulk_trash.bulk_trash)
|
||||
Api.marks.bulk.move = wrap_explorer(marks_bulk_move.bulk_move)
|
||||
Api.marks.get = wrap_node(explorer("marks", "get_mark"))
|
||||
Api.marks.list = explorer("marks", "get_marks")
|
||||
Api.marks.toggle = wrap_node(explorer("marks", "toggle_mark"))
|
||||
Api.marks.clear = explorer("marks", "clear_marks")
|
||||
Api.marks.bulk.delete = explorer("bulk_delete", "bulk_delete")
|
||||
Api.marks.bulk.trash = explorer("bulk_trash", "bulk_trash")
|
||||
Api.marks.bulk.move = explorer("bulk_move", "bulk_move")
|
||||
Api.marks.navigate.next = wrap(marks_navigation.next)
|
||||
Api.marks.navigate.prev = wrap(marks_navigation.prev)
|
||||
Api.marks.navigate.select = wrap(marks_navigation.select)
|
||||
|
|
|
@ -4,6 +4,9 @@ local watch = require "nvim-tree.explorer.watch"
|
|||
local explorer_node = require "nvim-tree.explorer.node"
|
||||
local Filters = require "nvim-tree.explorer.filters"
|
||||
local Marks = require "nvim-tree.marks"
|
||||
local BulkDelete -- cyclic
|
||||
local BulkMove -- cyclic
|
||||
local BulkTrash -- cyclic
|
||||
local LiveFilter = require "nvim-tree.explorer.live-filter"
|
||||
local Sorters = require "nvim-tree.explorer.sorters"
|
||||
|
||||
|
@ -18,6 +21,9 @@ M.reload = require("nvim-tree.explorer.reload").reload
|
|||
---@field open boolean
|
||||
---@field filters Filters
|
||||
---@field live_filter LiveFilter
|
||||
---@field bulk_delete BulkDelete
|
||||
---@field bulk_move BulkMove
|
||||
---@field bulk_trash BulkTrash
|
||||
---@field sorters Sorter
|
||||
---@field marks Marks
|
||||
|
||||
|
@ -50,6 +56,9 @@ function Explorer.new(path)
|
|||
explorer.watcher = watch.create_watcher(explorer)
|
||||
explorer.filters = Filters:new(M.config, explorer)
|
||||
explorer.live_filter = LiveFilter:new(M.config, explorer)
|
||||
explorer.bulk_delete = BulkDelete:new(M.config, explorer)
|
||||
explorer.bulk_move = BulkMove:new(M.config, explorer)
|
||||
explorer.bulk_trash = BulkTrash:new(M.config, explorer)
|
||||
explorer:_load(explorer)
|
||||
return explorer
|
||||
end
|
||||
|
@ -85,6 +94,11 @@ function M.setup(opts)
|
|||
require("nvim-tree.explorer.explore").setup(opts)
|
||||
require("nvim-tree.explorer.reload").setup(opts)
|
||||
require("nvim-tree.explorer.watch").setup(opts)
|
||||
|
||||
-- cyclic
|
||||
BulkDelete = require "nvim-tree.marks.bulk-delete"
|
||||
BulkMove = require "nvim-tree.marks.bulk-move"
|
||||
BulkTrash = require "nvim-tree.marks.bulk-trash"
|
||||
end
|
||||
|
||||
M.Explorer = Explorer
|
||||
|
|
|
@ -3,33 +3,46 @@ local remove_file = require "nvim-tree.actions.fs.remove-file"
|
|||
local notify = require "nvim-tree.notify"
|
||||
local lib = require "nvim-tree.lib"
|
||||
|
||||
local M = {
|
||||
config = {},
|
||||
}
|
||||
---@class BulkDelete
|
||||
---@field private explorer Explorer
|
||||
---@field private config table hydrated user opts.filters
|
||||
local BulkDelete = {}
|
||||
|
||||
---@param opts table user options
|
||||
---@param explorer Explorer
|
||||
---@return Filters
|
||||
function BulkDelete:new(opts, explorer)
|
||||
local o = {
|
||||
config = {
|
||||
ui = opts.ui,
|
||||
filesystem_watchers = opts.filesystem_watchers,
|
||||
},
|
||||
explorer = explorer,
|
||||
}
|
||||
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
return o
|
||||
end
|
||||
|
||||
--- Delete nodes; each removal will be optionally notified
|
||||
---@param nodes Node[]
|
||||
---@param marks Marks
|
||||
local function do_delete(marks, nodes)
|
||||
function BulkDelete:do_delete(marks, nodes)
|
||||
for _, node in pairs(nodes) do
|
||||
remove_file.remove(node)
|
||||
end
|
||||
|
||||
marks:clear_marks()
|
||||
|
||||
if not M.config.filesystem_watchers.enable then
|
||||
if not self.config.filesystem_watchers.enable then
|
||||
require("nvim-tree.actions.reloaders").reload_explorer()
|
||||
end
|
||||
end
|
||||
|
||||
--- Delete marked nodes, optionally prompting
|
||||
---@param explorer Explorer
|
||||
function M.bulk_delete(explorer)
|
||||
if not explorer then
|
||||
return
|
||||
end
|
||||
|
||||
local marks = explorer.marks
|
||||
function BulkDelete:bulk_delete()
|
||||
local marks = self.explorer.marks
|
||||
|
||||
local nodes = marks:get_marks()
|
||||
if not nodes or #nodes == 0 then
|
||||
|
@ -37,23 +50,18 @@ function M.bulk_delete(explorer)
|
|||
return
|
||||
end
|
||||
|
||||
if M.config.ui.confirm.remove then
|
||||
if self.config.ui.confirm.remove then
|
||||
local prompt_select = "Remove bookmarked ?"
|
||||
local prompt_input = prompt_select .. " y/N: "
|
||||
lib.prompt(prompt_input, prompt_select, { "", "y" }, { "No", "Yes" }, "nvimtree_bulk_delete", function(item_short)
|
||||
utils.clear_prompt()
|
||||
if item_short == "y" then
|
||||
do_delete(marks, nodes)
|
||||
self:do_delete(marks, nodes)
|
||||
end
|
||||
end)
|
||||
else
|
||||
do_delete(marks, nodes)
|
||||
self:do_delete(marks, nodes)
|
||||
end
|
||||
end
|
||||
|
||||
function M.setup(opts)
|
||||
M.config.ui = opts.ui
|
||||
M.config.filesystem_watchers = opts.filesystem_watchers
|
||||
end
|
||||
|
||||
return M
|
||||
return BulkDelete
|
||||
|
|
|
@ -4,16 +4,29 @@ local rename_file = require "nvim-tree.actions.fs.rename-file"
|
|||
local notify = require "nvim-tree.notify"
|
||||
local lib = require "nvim-tree.lib"
|
||||
|
||||
local M = {
|
||||
config = {},
|
||||
}
|
||||
---@class BulkMove
|
||||
---@field private explorer Explorer
|
||||
---@field private config table hydrated user opts.filters
|
||||
local BulkMove = {}
|
||||
|
||||
---@param opts table user options
|
||||
---@param explorer Explorer
|
||||
function M.bulk_move(explorer)
|
||||
if not explorer then
|
||||
return
|
||||
end
|
||||
local marks = explorer.marks
|
||||
---@return Filters
|
||||
function BulkMove:new(opts, explorer)
|
||||
local o = {
|
||||
config = {
|
||||
filesystem_watchers = opts.filesystem_watchers,
|
||||
},
|
||||
explorer = explorer,
|
||||
}
|
||||
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
return o
|
||||
end
|
||||
|
||||
function BulkMove:bulk_move()
|
||||
local marks = self.explorer.marks
|
||||
|
||||
if #marks:get_marks() == 0 then
|
||||
notify.warn "No bookmarks to move."
|
||||
|
@ -54,14 +67,10 @@ function M.bulk_move(explorer)
|
|||
|
||||
marks:clear_marks()
|
||||
|
||||
if not M.config.filesystem_watchers.enable then
|
||||
if not self.config.filesystem_watchers.enable then
|
||||
require("nvim-tree.actions.reloaders").reload_explorer()
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function M.setup(opts)
|
||||
M.config.filesystem_watchers = opts.filesystem_watchers
|
||||
end
|
||||
|
||||
return M
|
||||
return BulkMove
|
||||
|
|
|
@ -3,51 +3,58 @@ local remove_file = require "nvim-tree.actions.fs.trash"
|
|||
local notify = require "nvim-tree.notify"
|
||||
local lib = require "nvim-tree.lib"
|
||||
|
||||
local M = {
|
||||
config = {},
|
||||
}
|
||||
---@class BulkTrash
|
||||
---@field private explorer Explorer
|
||||
---@field private config table hydrated user opts.filters
|
||||
local BulkTrash = {}
|
||||
|
||||
--- Delete nodes; each removal will be optionally notified
|
||||
---@param opts table user options
|
||||
---@param explorer Explorer
|
||||
---@return Filters
|
||||
function BulkTrash:new(opts, explorer)
|
||||
local o = {
|
||||
config = {
|
||||
ui = opts.ui,
|
||||
filesystem_watchers = opts.filesystem_watchers,
|
||||
},
|
||||
explorer = explorer,
|
||||
}
|
||||
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
return o
|
||||
end
|
||||
|
||||
---Delete nodes; each removal will be optionally notified
|
||||
---@private
|
||||
---@param nodes Node[]
|
||||
local function do_trash(nodes)
|
||||
function BulkTrash:do_trash(nodes)
|
||||
for _, node in pairs(nodes) do
|
||||
remove_file.remove(node)
|
||||
end
|
||||
end
|
||||
|
||||
---@param explorer Explorer
|
||||
function M.bulk_trash(explorer)
|
||||
if not explorer then
|
||||
return
|
||||
end
|
||||
|
||||
local marks = explorer.marks
|
||||
|
||||
local nodes = marks:get_marks()
|
||||
function BulkTrash:bulk_trash()
|
||||
local nodes = self.explorer.marks:get_marks()
|
||||
if not nodes or #nodes == 0 then
|
||||
notify.warn "No bookmarks to trash."
|
||||
return
|
||||
end
|
||||
|
||||
if M.config.ui.confirm.trash then
|
||||
if self.config.ui.confirm.trash then
|
||||
local prompt_select = "Trash bookmarked ?"
|
||||
local prompt_input = prompt_select .. " y/N: "
|
||||
lib.prompt(prompt_input, prompt_select, { "", "y" }, { "No", "Yes" }, "nvimtree_bulk_trash", function(item_short)
|
||||
utils.clear_prompt()
|
||||
if item_short == "y" then
|
||||
do_trash(nodes)
|
||||
marks:clear_marks()
|
||||
self:do_trash(nodes)
|
||||
self.explorer.marks:clear_marks()
|
||||
end
|
||||
end)
|
||||
else
|
||||
do_trash(nodes)
|
||||
marks:clear_marks()
|
||||
self:do_trash(nodes)
|
||||
self.explorer.marks:clear_marks()
|
||||
end
|
||||
end
|
||||
|
||||
function M.setup(opts)
|
||||
M.config.ui = opts.ui
|
||||
M.config.filesystem_watchers = opts.filesystem_watchers
|
||||
end
|
||||
|
||||
return M
|
||||
return BulkTrash
|
||||
|
|
|
@ -67,12 +67,8 @@ function Marks:get_marks()
|
|||
return list
|
||||
end
|
||||
|
||||
function Marks.setup(opts)
|
||||
function Marks.setup()
|
||||
renderer = require "nvim-tree.renderer"
|
||||
|
||||
require("nvim-tree.marks.bulk-delete").setup(opts)
|
||||
require("nvim-tree.marks.bulk-trash").setup(opts)
|
||||
require("nvim-tree.marks.bulk-move").setup(opts)
|
||||
end
|
||||
|
||||
return Marks
|
||||
|
|
Loading…
Reference in a new issue