From 1b3a56acdd2e60518d036d44dcf53b1f00a69343 Mon Sep 17 00:00:00 2001 From: ray-x Date: Tue, 7 Nov 2023 19:44:17 +1100 Subject: [PATCH] Use async fs_write for logging --- lua/go/inlay.lua | 3 +-- lua/go/utils.lua | 59 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/lua/go/inlay.lua b/lua/go/inlay.lua index e8601dd..13c5920 100644 --- a/lua/go/inlay.lua +++ b/lua/go/inlay.lua @@ -6,8 +6,7 @@ local api = vim.api local fn = vim.fn local utils = require('go.utils') local log = utils.log --- local trace = utils.trace -trace = log +local trace = utils.trace local config local inlay_display = vim.fn.has('nvim-0.10') == 1 and _GO_NVIM_CFG.lsp_inlay_hints.style == 'inlay' -- local inlay_display = true diff --git a/lua/go/utils.lua b/lua/go/utils.lua index dbbf89f..b21b4d8 100644 --- a/lua/go/utils.lua +++ b/lua/go/utils.lua @@ -253,6 +253,21 @@ util.handle_job_data = function(data) return data end +local function fs_write(path, data) + uv.fs_open(path, 'a', tonumber('644', 8), function(err, fd) + if err then + print('Error opening file: ' .. err) + return err + end + uv.fs_write(fd, data, 0, function(e2, _) + assert(not e2, e2) + uv.fs_close(fd, function(e3) + assert(not e3, e3) + end) + end) + end) +end + local cache_dir = fn.stdpath('cache') util.log = function(...) if not _GO_NVIM_CFG or not _GO_NVIM_CFG.verbose then @@ -273,22 +288,12 @@ util.log = function(...) if type(v) == 'table' then str = str .. ' |' .. tostring(i) .. ': ' .. vim.inspect(v or 'nil') .. '\n' else - str = str .. ' |' .. tostring(i) .. ': ' .. tostring(v or 'nil') + str = str .. ' |' .. tostring(i) .. ': ' .. tostring(v or 'nil') .. '\n' end end if #str > 2 then if log_path ~= nil and #log_path > 3 then - local f, err = io.open(log_path, 'a+') - if err then - vim.notify('failed to open log' .. log_path .. err, vim.log.levels.ERROR) - return - end - if not f then - error('open file ' .. log_path, f) - end - io.output(f) - io.write(str .. '\n') - io.close(f) + fs_write(log_path, str) else vim.notify(str .. '\n', vim.log.levels.DEBUG) end @@ -802,7 +807,6 @@ function util.quickfix(cmd) end end - util.throttle = function(func, duration) local timer = uv.new_timer() -- util.log(func, duration) @@ -834,6 +838,35 @@ util.throttle = function(func, duration) return inner, timer end +-- function M.debounce_trailing(ms, fn) +-- local timer = uv.new_timer() +-- return function(...) +-- local argv = { ... } +-- if timer:is_active() then +-- timer:stop() +-- return +-- end +-- timer:start(ms, 0, function() +-- timer:stop() +-- fn(unpack(argv)) +-- end) +-- end +-- end +-- +util.debounce = function(func, ms) + local timer = uv.new_timer() + local function inner(...) + local argv = { ... } + if not timer:is_active() then + timer:start(ms, 0, function() + timer:stop() + pcall(vim.schedule_wrap(func), unpack(argv)) + end) + end + end + return inner, timer +end + local namepath = {} util.extract_filepath = function(msg, pkg_path)