Skip to content

Commit

Permalink
fix: remove duplications from DB and workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
delphinus committed Aug 6, 2023
1 parent bd1c1c9 commit b8aaaef
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 29 deletions.
24 changes: 15 additions & 9 deletions lua/frecency/async_finder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ AsyncFinder.new = function(fs, path, entry_maker, initial_results)
return self:_find(...)
end,
})
local seen = {}
for i, file in ipairs(initial_results) do
local entry = entry_maker(file)
seen[entry.filename] = true
entry.index = i
table.insert(self.entries, entry)
end
Expand All @@ -34,15 +36,19 @@ AsyncFinder.new = function(fs, path, entry_maker, initial_results)
if self.closed then
break
end
index = index + 1
count = count + 1
local entry = entry_maker { id = 0, count = 0, path = vim.fs.joinpath(path, name), score = 0 }
if entry then
entry.index = index
table.insert(self.entries, entry)
tx.send(entry)
if count % 1000 == 0 then
a.util.sleep(0)
local fullpath = vim.fs.joinpath(path, name)
if not seen[fullpath] then
seen[fullpath] = true
index = index + 1
count = count + 1
local entry = entry_maker { id = 0, count = 0, path = vim.fs.joinpath(path, name), score = 0 }
if entry then
entry.index = index
table.insert(self.entries, entry)
tx.send(entry)
if count % 1000 == 0 then
a.util.sleep(0)
end
end
end
end
Expand Down
61 changes: 41 additions & 20 deletions lua/frecency/tests/async_finder_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ local function with_files(files, initial_results, callback)
end
local entry_maker = EntryMaker.new(fs, web_devicons, { show_filter_column = false, show_scores = false })
:create(filepath_formatter, dir:absolute())
local async_finder = AsyncFinder.new(fs, dir:absolute(), entry_maker, initial_results)
local initials = vim.tbl_map(function(v)
return { path = (dir / v):absolute() }
end, initial_results)
local async_finder = AsyncFinder.new(fs, dir:absolute(), entry_maker, initials)
callback(async_finder, dir)
close()
end
Expand All @@ -33,23 +36,22 @@ describe("async_finder", function()
return
end

describe("with no initial_results", function()
local files = { "hoge1.txt", "hoge2.txt" }
with_files(files, {}, function(async_finder, dir)
local count = { process_result = 0, process_complete = 0 }
local results
local function run()
results = {}
async_finder("", function(result)
count.process_result = count.process_result + 1
table.insert(results, result.filename)
end, function()
count.process_complete = count.process_complete + 1
end)
end
local function run(async_finder)
local count = { process_result = 0, process_complete = 0 }
local results = {}
async_finder("", function(result)
count.process_result = count.process_result + 1
table.insert(results, result.filename)
end, function()
count.process_complete = count.process_complete + 1
end)
return count, results
end

describe("with no initial_results", function()
with_files({ "hoge1.txt", "hoge2.txt" }, {}, function(async_finder, dir)
describe("when run at the first time", function()
run()
local count, results = run(async_finder)
it("called process_result() at 2 times", function()
assert.are.same(2, count.process_result)
end)
Expand All @@ -65,12 +67,12 @@ describe("async_finder", function()
end)

describe("when run again", function()
run()
it("called process_result() at 4 times", function()
assert.are.same(4, count.process_result)
local count, results = run(async_finder)
it("called process_result() at 2 times", function()
assert.are.same(2, count.process_result)
end)
it("called process_complete() at 1 time", function()
assert.are.same(2, count.process_complete)
assert.are.same(1, count.process_complete)
end)
it("returns the same results", function()
assert.are.same({
Expand All @@ -81,4 +83,23 @@ describe("async_finder", function()
end)
end)
end)

describe("with initial_results", function()
with_files({ "fuga1.txt", "hoge1.txt", "hoge2.txt" }, { "fuga1.txt" }, function(async_finder, dir)
local count, results = run(async_finder)
it("called process_result() at 3 times", function()
assert.are.same(3, count.process_result)
end)
it("called process_complete() at 1 time", function()
assert.are.same(1, count.process_complete)
end)
it("returns the same results without duplications", function()
assert.are.same({
dir:joinpath("fuga1.txt").filename,
dir:joinpath("hoge1.txt").filename,
dir:joinpath("hoge2.txt").filename,
}, results)
end)
end)
end)
end)

0 comments on commit b8aaaef

Please sign in to comment.