Skip to content

Commit 5ef1141

Browse files
refactor: Use promises for readfile
1 parent 1bc9256 commit 5ef1141

File tree

3 files changed

+39
-57
lines changed

3 files changed

+39
-57
lines changed

lua/orgmode/colors/todo_highlighter.lua

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local config = require('orgmode.config')
1+
local Promise = require('orgmode.utils.promise')
22
local highlights = require('orgmode.colors.highlights')
33
local tree_utils = require('orgmode.utils.treesitter')
44
local utils = require('orgmode.utils')
@@ -13,44 +13,36 @@ local function add_todo_keyword_highlights()
1313
return
1414
end
1515

16-
local all_lines = {}
16+
local actions = {}
1717
for i, _ in pairs(query_files) do
1818
if i ~= #query_files then
19-
utils.readfile(
20-
query_files[i],
21-
vim.schedule_wrap(function(err, lines)
22-
if err then
23-
return
24-
end
25-
for _, v in ipairs(lines) do
26-
table.insert(all_lines, v)
27-
end
28-
end)
29-
)
19+
table.insert(actions, utils.readfile(query_files[i]))
3020
else
31-
utils.readfile(
32-
query_files[i],
33-
vim.schedule_wrap(function(err, lines)
34-
if err then
35-
return
36-
end
21+
table.insert(
22+
actions,
23+
utils.readfile(query_files[i]):next(function(lines)
3724
for face_name, face_hl in pairs(faces) do
3825
table.insert(
3926
lines,
4027
string.format([[(item . (expr) @%s @nospell (#eq? @%s %s))]], face_hl, face_hl, face_name)
4128
)
4229
end
43-
for _, v in ipairs(lines) do
44-
table.insert(all_lines, v)
45-
end
46-
vim.treesitter.query.set('org', 'highlights', table.concat(all_lines, '\n'))
47-
if vim.bo.filetype == 'org' then
48-
tree_utils.restart_highlights()
49-
end
30+
return lines
5031
end)
5132
)
5233
end
5334
end
35+
36+
return Promise.all(actions):next(function(line_parts)
37+
local all_lines = {}
38+
for _, line_part in ipairs(line_parts) do
39+
utils.concat(all_lines, line_part)
40+
end
41+
vim.treesitter.query.set('org', 'highlights', table.concat(all_lines, '\n'))
42+
if vim.bo.filetype == 'org' then
43+
tree_utils.restart_highlights()
44+
end
45+
end)
5446
end
5547

5648
return {

lua/orgmode/parser/file.lua

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,9 @@ function File.load(path, callback)
151151
return callback(nil)
152152
end
153153
local category = vim.fn.fnamemodify(path, ':t:r')
154-
utils.readfile(
155-
path,
156-
vim.schedule_wrap(function(err, content)
157-
if err then
158-
return callback(nil)
159-
end
160-
return callback(File.from_content(content, category, path, ext == 'org_archive'))
161-
end)
162-
)
154+
utils.readfile(path):next(vim.schedule_wrap(function(content)
155+
return callback(File.from_content(content, category, path, ext == 'org_archive'))
156+
end))
163157
end
164158

165159
---@param content table

lua/orgmode/utils/init.lua

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,28 @@ local debounce_timers = {}
66
local query_cache = {}
77
local tmp_window_augroup = vim.api.nvim_create_augroup('OrgTmpWindow', { clear = true })
88

9-
---@param file string
10-
---@param callback function
11-
---@param as_string? boolean
12-
function utils.readfile(file, callback, as_string)
13-
uv.fs_open(file, 'r', 438, function(err1, fd)
14-
if err1 then
15-
return callback(err1)
16-
end
17-
uv.fs_fstat(fd, function(err2, stat)
18-
if err2 then
19-
return callback(err2)
9+
function utils.readfile(file)
10+
return Promise.new(function(resolve, reject)
11+
uv.fs_open(file, 'r', 438, function(err1, fd)
12+
if err1 then
13+
return reject(err1)
2014
end
21-
uv.fs_read(fd, stat.size, 0, function(err3, data)
22-
if err3 then
23-
return callback(err3)
15+
uv.fs_fstat(fd, function(err2, stat)
16+
if err2 then
17+
return reject(err2)
2418
end
25-
uv.fs_close(fd, function(err4)
26-
if err4 then
27-
return callback(err4)
19+
uv.fs_read(fd, stat.size, 0, function(err3, data)
20+
if err3 then
21+
return reject(err3)
2822
end
29-
local lines = data
30-
if not as_string then
31-
lines = vim.split(data, '\n')
23+
uv.fs_close(fd, function(err4)
24+
if err4 then
25+
return reject(err4)
26+
end
27+
local lines = vim.split(data, '\n')
3228
table.remove(lines, #lines)
33-
end
34-
return callback(nil, lines)
29+
return resolve(lines)
30+
end)
3531
end)
3632
end)
3733
end)

0 commit comments

Comments
 (0)