|
1 |
| -local core = {} -- circular dependency |
2 |
| -local lib = {} -- circular dependency |
| 1 | +local Iterator = require "nvim-tree.iterators.node-iterator" |
| 2 | +local core = require "nvim-tree.core" |
| 3 | +local lib = require "nvim-tree.lib" |
3 | 4 | local notify = require "nvim-tree.notify"
|
4 |
| -local remove_file = {} -- circular dependency |
5 |
| -local rename_file = {} -- circular dependency |
6 |
| -local trash = {} -- circular dependency |
7 |
| -local renderer = {} -- circular dependency |
| 5 | +local open_file = require "nvim-tree.actions.node.open-file" |
| 6 | +local remove_file = require "nvim-tree.actions.fs.remove-file" |
| 7 | +local rename_file = require "nvim-tree.actions.fs.rename-file" |
| 8 | +local renderer = require "nvim-tree.renderer" |
| 9 | +local trash = require "nvim-tree.actions.fs.trash" |
8 | 10 | local utils = require "nvim-tree.utils"
|
9 | 11 |
|
10 | 12 | ---@class Marks
|
|
84 | 86 |
|
85 | 87 | ---Delete marked; each removal will be optionally notified
|
86 | 88 | ---@public
|
87 |
| -function Marks:delete() |
| 89 | +function Marks:bulk_delete() |
88 | 90 | if not next(self.marks) then
|
89 | 91 | notify.warn "No bookmarks to delete."
|
90 | 92 | return
|
|
113 | 115 |
|
114 | 116 | ---Trash marked; each removal will be optionally notified
|
115 | 117 | ---@public
|
116 |
| -function Marks:trash() |
| 118 | +function Marks:bulk_trash() |
117 | 119 | if not next(self.marks) then
|
118 | 120 | notify.warn "No bookmarks to trash."
|
119 | 121 | return
|
|
142 | 144 |
|
143 | 145 | ---Move marked
|
144 | 146 | ---@public
|
145 |
| -function Marks:move() |
| 147 | +function Marks:bulk_move() |
146 | 148 | if not next(self.marks) then
|
147 | 149 | notify.warn "No bookmarks to move."
|
148 | 150 | return
|
@@ -183,13 +185,89 @@ function Marks:move()
|
183 | 185 | end)
|
184 | 186 | end
|
185 | 187 |
|
186 |
| -function Marks.setup() |
187 |
| - core = require "nvim-tree.core" |
188 |
| - lib = require "nvim-tree.lib" |
189 |
| - remove_file = require "nvim-tree.actions.fs.remove-file" |
190 |
| - rename_file = require "nvim-tree.actions.fs.rename-file" |
191 |
| - renderer = require "nvim-tree.renderer" |
192 |
| - trash = require "nvim-tree.actions.fs.trash" |
| 188 | +---Focus nearest marked node in direction. |
| 189 | +---@private |
| 190 | +---@param up boolean |
| 191 | +function Marks:navigate(up) |
| 192 | + local node = lib.get_node_at_cursor() |
| 193 | + if not node then |
| 194 | + return |
| 195 | + end |
| 196 | + |
| 197 | + local first, prev, next, last = nil, nil, nil, nil |
| 198 | + local found = false |
| 199 | + |
| 200 | + Iterator.builder(self.explorer.nodes) |
| 201 | + :recursor(function(n) |
| 202 | + return n.open and n.nodes |
| 203 | + end) |
| 204 | + :applier(function(n) |
| 205 | + if n.absolute_path == node.absolute_path then |
| 206 | + found = true |
| 207 | + return |
| 208 | + end |
| 209 | + |
| 210 | + if not self:get(n) then |
| 211 | + return |
| 212 | + end |
| 213 | + |
| 214 | + last = n |
| 215 | + first = first or n |
| 216 | + |
| 217 | + if found and not next then |
| 218 | + next = n |
| 219 | + end |
| 220 | + |
| 221 | + if not found then |
| 222 | + prev = n |
| 223 | + end |
| 224 | + end) |
| 225 | + :iterate() |
| 226 | + |
| 227 | + if not found then |
| 228 | + return |
| 229 | + end |
| 230 | + |
| 231 | + if up then |
| 232 | + utils.focus_node_or_parent(prev or last) |
| 233 | + else |
| 234 | + utils.focus_node_or_parent(next or first) |
| 235 | + end |
| 236 | +end |
| 237 | + |
| 238 | +---@public |
| 239 | +function Marks:navigate_prev() |
| 240 | + self:navigate(true) |
| 241 | +end |
| 242 | + |
| 243 | +---@public |
| 244 | +function Marks:navigate_next() |
| 245 | + self:navigate(false) |
| 246 | +end |
| 247 | + |
| 248 | +---Prompts for selection of a marked node, sorted by absolute paths. |
| 249 | +---A folder will be focused, a file will be opened. |
| 250 | +---@public |
| 251 | +function Marks:navigate_select() |
| 252 | + local list = vim.tbl_map(function(n) |
| 253 | + return n.absolute_path |
| 254 | + end, self:list()) |
| 255 | + |
| 256 | + table.sort(list) |
| 257 | + |
| 258 | + vim.ui.select(list, { |
| 259 | + prompt = "Select a file to open or a folder to focus", |
| 260 | + }, function(choice) |
| 261 | + if not choice or choice == "" then |
| 262 | + return |
| 263 | + end |
| 264 | + local node = self:get { absolute_path = choice } |
| 265 | + if node and not node.nodes and not utils.get_win_buf_from_path(node.absolute_path) then |
| 266 | + open_file.fn("edit", node.absolute_path) |
| 267 | + elseif node then |
| 268 | + utils.focus_file(node.absolute_path) |
| 269 | + end |
| 270 | + end) |
193 | 271 | end
|
194 | 272 |
|
195 | 273 | return Marks
|
0 commit comments