From 5ef11419116578a6cbfb0a18674e590237ded080 Mon Sep 17 00:00:00 2001 From: Kristijan Husak Date: Mon, 9 Oct 2023 17:03:05 +0200 Subject: [PATCH] refactor: Use promises for readfile --- lua/orgmode/colors/todo_highlighter.lua | 44 ++++++++++--------------- lua/orgmode/parser/file.lua | 12 ++----- lua/orgmode/utils/init.lua | 40 ++++++++++------------ 3 files changed, 39 insertions(+), 57 deletions(-) diff --git a/lua/orgmode/colors/todo_highlighter.lua b/lua/orgmode/colors/todo_highlighter.lua index 3dadb4c8c..c5d5a912e 100644 --- a/lua/orgmode/colors/todo_highlighter.lua +++ b/lua/orgmode/colors/todo_highlighter.lua @@ -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') @@ -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 { diff --git a/lua/orgmode/parser/file.lua b/lua/orgmode/parser/file.lua index 0420ca247..58d91aef2 100644 --- a/lua/orgmode/parser/file.lua +++ b/lua/orgmode/parser/file.lua @@ -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 diff --git a/lua/orgmode/utils/init.lua b/lua/orgmode/utils/init.lua index 7b5a720e7..767eee525 100644 --- a/lua/orgmode/utils/init.lua +++ b/lua/orgmode/utils/init.lua @@ -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)