Skip to content

Commit 7e3ad90

Browse files
authored
refactor: update fs_scan to avoid memory leaks, part of #393 (#475)
1 parent b11d15c commit 7e3ad90

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

lua/neo-tree/sources/common/components.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,36 +207,38 @@ M.git_status = function(config, node, state)
207207
end
208208

209209
M.filtered_by = function(config, node, state)
210+
local result = {}
210211
if type(node.filtered_by) == "table" then
211212
local fby = node.filtered_by
212213
if fby.name then
213-
return {
214+
result = {
214215
text = "(hide by name) ",
215216
highlight = highlights.HIDDEN_BY_NAME,
216217
}
217218
elseif fby.pattern then
218-
return {
219+
result = {
219220
text = "(hide by pattern) ",
220221
highlight = highlights.HIDDEN_BY_NAME,
221222
}
222223
elseif fby.gitignored then
223-
return {
224+
result = {
224225
text = "(gitignored) ",
225226
highlight = highlights.GIT_IGNORED,
226227
}
227228
elseif fby.dotfiles then
228-
return {
229+
result = {
229230
text = "(dotfile) ",
230231
highlight = highlights.DOTFILE,
231232
}
232233
elseif fby.hidden then
233-
return {
234+
result = {
234235
text = "(hidden) ",
235236
highlight = highlights.WINDOWS_HIDDEN,
236237
}
237238
end
239+
fby = nil
238240
end
239-
return {}
241+
return result
240242
end
241243

242244
M.icon = function(config, node, state)

lua/neo-tree/sources/filesystem/commands.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ local utils = require("neo-tree.utils")
66
local filter = require("neo-tree.sources.filesystem.lib.filter")
77
local renderer = require("neo-tree.ui.renderer")
88
local log = require("neo-tree.log")
9-
local manager = require("neo-tree.sources.manager")
109

1110
local M = {}
1211
local refresh = function(state)

lua/neo-tree/sources/filesystem/lib/fs_scan.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ local function async_scan(context, path)
108108
-- prepend the root path
109109
table.insert(context.paths_to_load, 1, path)
110110

111-
local directories_scanned = 0
112-
local directories_to_scan = #context.paths_to_load
111+
context.directories_scanned = 0
112+
context.directories_to_scan = #context.paths_to_load
113113

114-
local on_exit = vim.schedule_wrap(function()
114+
context.on_exit = vim.schedule_wrap(function()
115115
job_complete(context)
116116
end)
117117

118118
-- from https://github.com/nvim-lua/plenary.nvim/blob/master/lua/plenary/scandir.lua
119-
local function read_dir(current_dir)
119+
local function read_dir(current_dir, ctx)
120120
local on_fs_scandir = function(err, fd)
121121
if err then
122122
log.error(current_dir, ": ", err)
@@ -127,23 +127,23 @@ local function async_scan(context, path)
127127
break
128128
end
129129
local entry = current_dir .. os_sep .. name
130-
local success, item = pcall(file_items.create_item, context, entry, typ)
130+
local success, item = pcall(file_items.create_item, ctx, entry, typ)
131131
if success then
132-
if context.recursive and item.type == "directory" then
133-
directories_to_scan = directories_to_scan + 1
134-
read_dir(item.path)
132+
if ctx.recursive and item.type == "directory" then
133+
ctx.directories_to_scan = ctx.directories_to_scan + 1
134+
table.insert(ctx.paths_to_load, item.path)
135135
end
136136
else
137137
log.error("error creating item for ", path)
138138
end
139139
end
140-
on_directory_loaded(context, current_dir)
141-
directories_scanned = directories_scanned + 1
142-
if directories_scanned == #context.paths_to_load then
143-
on_exit()
140+
on_directory_loaded(ctx, current_dir)
141+
ctx.directories_scanned = ctx.directories_scanned + 1
142+
if ctx.directories_scanned == #ctx.paths_to_load then
143+
ctx.on_exit()
144144
end
145145

146-
--local next_path = dir_complete(context, current_dir)
146+
--local next_path = dir_complete(ctx, current_dir)
147147
--if next_path then
148148
-- local success, error = pcall(read_dir, next_path)
149149
-- if not success then
@@ -163,8 +163,8 @@ local function async_scan(context, path)
163163
--if not success then
164164
-- log.error(first, ": ", err)
165165
--end
166-
for i = 1, directories_to_scan do
167-
read_dir(context.paths_to_load[i])
166+
for i = 1, context.directories_to_scan do
167+
read_dir(context.paths_to_load[i], context)
168168
end
169169
end
170170

0 commit comments

Comments
 (0)