Skip to content

Commit

Permalink
Escape heavy processing to another thread to avoid blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
uga-rosa committed Jan 16, 2024
1 parent 680b328 commit 372b1bb
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions lua/cmp_dictionary/dict/trie.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local Trie = require("cmp_dictionary.lib.trie")
local async = require("plenary.async")

---@class CmpDictionaryDictTrie: CmpDictionaryDict
---@field trie_map table<string, Trie>
Expand All @@ -21,41 +20,35 @@ local function same(x, y)
return vim.json.encode(x) == vim.json.encode(y)
end

---@param path string
---@return string
local function read_file(path)
---@diagnostic disable: redefined-local
-- luacheck: no redefined
local err, fd = async.uv.fs_open(path, "r", tonumber("0666", 8))
assert(not err, err)
local err, stat = async.uv.fs_fstat(fd)
assert(not err, err)
local err, data = async.uv.fs_read(fd, stat.size, 0)
assert(not err, err)
local err = async.uv.fs_close(fd)
assert(not err, err)
---@diagnostic enable
return data
end

---@param paths string[]
---@param force? boolean
function M:update(paths, force)
if not force and same(self.paths, paths) then
return
end
self.paths = paths
async.void(function()
for _, path in ipairs(paths) do
-- if force or not self.trie_map[path] then
local trie = Trie.new()
for word in vim.gsplit(read_file(path), "%s+", { trimempty = true }) do
trie:insert(word)
end
self.trie_map[path] = trie
-- end
for _, path in ipairs(paths) do
if force or not self.trie_map[path] then
vim.uv
.new_work(function(path)
local fd = assert(vim.uv.fs_open(path, "r", 438))
local stat = assert(vim.uv.fs_fstat(fd))
local data = assert(vim.uv.fs_read(fd, stat.size, 0))
assert(vim.uv.fs_close(fd))

local Trie = require("cmp_dictionary.lib.trie")
local trie = Trie.new()
for word in vim.gsplit(data, "%s+", { trimempty = true }) do
trie:insert(word)
end
return vim.json.encode(trie)
end, function(json_str)
local trie = setmetatable(vim.json.decode(json_str), { __index = Trie })
self.trie_map[path] = trie
end)
:queue(path)
end
end)()
end
end

---@param prefix string
Expand Down

0 comments on commit 372b1bb

Please sign in to comment.