Skip to content

Commit 673e0fc

Browse files
authored
fix(filesystem): fix numerous problems with searching, fixes #806 (#815)
1 parent fee76c0 commit 673e0fc

File tree

3 files changed

+180
-158
lines changed

3 files changed

+180
-158
lines changed

lua/neo-tree/collections.lua

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
local log = require("neo-tree.log")
2+
3+
Node = {}
4+
function Node:new(value)
5+
local props = { prev = nil, next = nil, value = value }
6+
setmetatable(props, self)
7+
self.__index = self
8+
return props
9+
end
10+
11+
LinkedList = {}
12+
function LinkedList:new()
13+
local props = { head = nil, tail = nil, size = 0 }
14+
setmetatable(props, self)
15+
self.__index = self
16+
return props
17+
end
18+
19+
function LinkedList:add_node(node)
20+
if self.head == nil then
21+
self.head = node
22+
self.tail = node
23+
else
24+
self.tail.next = node
25+
node.prev = self.tail
26+
self.tail = node
27+
end
28+
self.size = self.size + 1
29+
return node
30+
end
31+
32+
function LinkedList:remove_node(node)
33+
if node.prev ~= nil then
34+
node.prev.next = node.next
35+
end
36+
if node.next ~= nil then
37+
node.next.prev = node.prev
38+
end
39+
if self.head == node then
40+
self.head = node.next
41+
end
42+
if self.tail == node then
43+
self.tail = node.prev
44+
end
45+
self.size = self.size - 1
46+
node.prev = nil
47+
node.next = nil
48+
node.value = nil
49+
end
50+
51+
-- First in Last Out
52+
Queue = {}
53+
function Queue:new()
54+
local props = { _list = LinkedList:new() }
55+
setmetatable(props, self)
56+
self.__index = self
57+
return props
58+
end
59+
60+
---Add an element to the end of the queue.
61+
---@param value any The value to add.
62+
function Queue:add(value)
63+
self._list:add_node(Node:new(value))
64+
end
65+
66+
---Iterates over the entire list, running func(value) on each element.
67+
---If func returns true, the element is removed from the list.
68+
---@param func function The function to run on each element.
69+
function Queue:for_each(func)
70+
local node = self._list.head
71+
while node ~= nil do
72+
local result = func(node.value)
73+
local node_is_next = false
74+
if result then
75+
if type(result) == "boolean" then
76+
local node_to_remove = node
77+
node = node.next
78+
node_is_next = true
79+
self._list:remove_node(node_to_remove)
80+
elseif type(result) == "table" then
81+
if type(result.handled) == "boolean" and result.handled == true then
82+
log.trace(
83+
"Handler ",
84+
node.value.id,
85+
" for "
86+
.. node.value.event
87+
.. " returned handled = true, skipping the rest of the queue."
88+
)
89+
return result
90+
end
91+
end
92+
end
93+
if not node_is_next then
94+
node = node.next
95+
end
96+
end
97+
end
98+
99+
function Queue:is_empty()
100+
return self._list.size == 0
101+
end
102+
103+
function Queue:remove_by_id(id)
104+
local current = self._list.head
105+
while current ~= nil do
106+
local is_match = false
107+
local item = current.value
108+
if item ~= nil then
109+
local item_id = item.id or item
110+
if item_id == id then
111+
is_match = true
112+
end
113+
end
114+
if is_match then
115+
local next = current.next
116+
self._list:remove_node(current)
117+
current = next
118+
else
119+
current = current.next
120+
end
121+
end
122+
end
123+
124+
return {
125+
Queue = Queue,
126+
LinkedList = LinkedList,
127+
}

lua/neo-tree/events/queue.lua

+1-121
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,6 @@
11
local utils = require("neo-tree.utils")
22
local log = require("neo-tree.log")
3-
4-
Node = {}
5-
function Node:new(value)
6-
local props = { prev = nil, next = nil, value = value }
7-
setmetatable(props, self)
8-
self.__index = self
9-
return props
10-
end
11-
12-
LinkedList = {}
13-
function LinkedList:new()
14-
local props = { head = nil, tail = nil, size = 0 }
15-
setmetatable(props, self)
16-
self.__index = self
17-
return props
18-
end
19-
20-
function LinkedList:add_node(node)
21-
if self.head == nil then
22-
self.head = node
23-
self.tail = node
24-
else
25-
self.tail.next = node
26-
node.prev = self.tail
27-
self.tail = node
28-
end
29-
self.size = self.size + 1
30-
return node
31-
end
32-
33-
function LinkedList:remove_node(node)
34-
if node.prev ~= nil then
35-
node.prev.next = node.next
36-
end
37-
if node.next ~= nil then
38-
node.next.prev = node.prev
39-
end
40-
if self.head == node then
41-
self.head = node.next
42-
end
43-
if self.tail == node then
44-
self.tail = node.prev
45-
end
46-
self.size = self.size - 1
47-
node.prev = nil
48-
node.next = nil
49-
node.value = nil
50-
end
51-
52-
-- First in Last Out
53-
Queue = {}
54-
function Queue:new()
55-
local props = { _list = LinkedList:new() }
56-
setmetatable(props, self)
57-
self.__index = self
58-
return props
59-
end
60-
61-
---Add an element to the end of the queue.
62-
---@param value any The value to add.
63-
function Queue:add(value)
64-
self._list:add_node(Node:new(value))
65-
end
66-
67-
---Iterates over the entire list, running func(value) on each element.
68-
---If func returns true, the element is removed from the list.
69-
---@param func function The function to run on each element.
70-
function Queue:for_each(func)
71-
local node = self._list.head
72-
while node ~= nil do
73-
local result = func(node.value)
74-
local node_is_next = false
75-
if result then
76-
if type(result) == "boolean" then
77-
local node_to_remove = node
78-
node = node.next
79-
node_is_next = true
80-
self._list:remove_node(node_to_remove)
81-
elseif type(result) == "table" then
82-
if type(result.handled) == "boolean" and result.handled == true then
83-
log.trace(
84-
"Handler ",
85-
node.value.id,
86-
" for "
87-
.. node.value.event
88-
.. " returned handled = true, skipping the rest of the queue."
89-
)
90-
return result
91-
end
92-
end
93-
end
94-
if not node_is_next then
95-
node = node.next
96-
end
97-
end
98-
end
99-
100-
function Queue:is_empty()
101-
return self._list.size == 0
102-
end
103-
104-
function Queue:remove_by_id(id)
105-
local current = self._list.head
106-
while current ~= nil do
107-
local is_match = false
108-
local item = current.value
109-
if item ~= nil then
110-
local item_id = item.id or item
111-
if item_id == id then
112-
is_match = true
113-
end
114-
end
115-
if is_match then
116-
local next = current.next
117-
self._list:remove_node(current)
118-
current = next
119-
else
120-
current = current.next
121-
end
122-
end
123-
end
3+
local Queue = require("neo-tree.collections").Queue
1244

1255
local event_queues = {}
1266
local event_definitions = {}

0 commit comments

Comments
 (0)