Skip to content

Commit

Permalink
refactor: Use promises for readfile
Browse files Browse the repository at this point in the history
  • Loading branch information
kristijanhusak committed Oct 9, 2023
1 parent 1bc9256 commit 5ef1141
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 57 deletions.
44 changes: 18 additions & 26 deletions lua/orgmode/colors/todo_highlighter.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local config = require('orgmode.config')
local Promise = require('orgmode.utils.promise')
local highlights = require('orgmode.colors.highlights')
local tree_utils = require('orgmode.utils.treesitter')
local utils = require('orgmode.utils')
Expand All @@ -13,44 +13,36 @@ local function add_todo_keyword_highlights()
return
end

local all_lines = {}
local actions = {}
for i, _ in pairs(query_files) do
if i ~= #query_files then
utils.readfile(
query_files[i],
vim.schedule_wrap(function(err, lines)
if err then
return
end
for _, v in ipairs(lines) do
table.insert(all_lines, v)
end
end)
)
table.insert(actions, utils.readfile(query_files[i]))
else
utils.readfile(
query_files[i],
vim.schedule_wrap(function(err, lines)
if err then
return
end
table.insert(
actions,
utils.readfile(query_files[i]):next(function(lines)
for face_name, face_hl in pairs(faces) do
table.insert(
lines,
string.format([[(item . (expr) @%s @nospell (#eq? @%s %s))]], face_hl, face_hl, face_name)
)
end
for _, v in ipairs(lines) do
table.insert(all_lines, v)
end
vim.treesitter.query.set('org', 'highlights', table.concat(all_lines, '\n'))
if vim.bo.filetype == 'org' then
tree_utils.restart_highlights()
end
return lines
end)
)
end
end

return Promise.all(actions):next(function(line_parts)
local all_lines = {}
for _, line_part in ipairs(line_parts) do
utils.concat(all_lines, line_part)
end
vim.treesitter.query.set('org', 'highlights', table.concat(all_lines, '\n'))
if vim.bo.filetype == 'org' then
tree_utils.restart_highlights()
end
end)
end

return {
Expand Down
12 changes: 3 additions & 9 deletions lua/orgmode/parser/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,9 @@ function File.load(path, callback)
return callback(nil)
end
local category = vim.fn.fnamemodify(path, ':t:r')
utils.readfile(
path,
vim.schedule_wrap(function(err, content)
if err then
return callback(nil)
end
return callback(File.from_content(content, category, path, ext == 'org_archive'))
end)
)
utils.readfile(path):next(vim.schedule_wrap(function(content)
return callback(File.from_content(content, category, path, ext == 'org_archive'))
end))
end

---@param content table
Expand Down
40 changes: 18 additions & 22 deletions lua/orgmode/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,28 @@ local debounce_timers = {}
local query_cache = {}
local tmp_window_augroup = vim.api.nvim_create_augroup('OrgTmpWindow', { clear = true })

---@param file string
---@param callback function
---@param as_string? boolean
function utils.readfile(file, callback, as_string)
uv.fs_open(file, 'r', 438, function(err1, fd)
if err1 then
return callback(err1)
end
uv.fs_fstat(fd, function(err2, stat)
if err2 then
return callback(err2)
function utils.readfile(file)
return Promise.new(function(resolve, reject)
uv.fs_open(file, 'r', 438, function(err1, fd)
if err1 then
return reject(err1)
end
uv.fs_read(fd, stat.size, 0, function(err3, data)
if err3 then
return callback(err3)
uv.fs_fstat(fd, function(err2, stat)
if err2 then
return reject(err2)
end
uv.fs_close(fd, function(err4)
if err4 then
return callback(err4)
uv.fs_read(fd, stat.size, 0, function(err3, data)
if err3 then
return reject(err3)
end
local lines = data
if not as_string then
lines = vim.split(data, '\n')
uv.fs_close(fd, function(err4)
if err4 then
return reject(err4)
end
local lines = vim.split(data, '\n')
table.remove(lines, #lines)
end
return callback(nil, lines)
return resolve(lines)
end)
end)
end)
end)
Expand Down

0 comments on commit 5ef1141

Please sign in to comment.