From 30bae3f71989f5df03965d7df610d12ba52de0f5 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Tue, 16 Jul 2024 09:21:08 +0200 Subject: [PATCH] refactor: optimizations --- lua/neotest-golang/lib/cmd.lua | 16 +++------ lua/neotest-golang/lib/convert.lua | 12 ------- lua/neotest-golang/logging.lua | 58 +++++++++++++++++++++++++----- 3 files changed, 53 insertions(+), 33 deletions(-) diff --git a/lua/neotest-golang/lib/cmd.lua b/lua/neotest-golang/lib/cmd.lua index ded29e9..db8396d 100644 --- a/lua/neotest-golang/lib/cmd.lua +++ b/lua/neotest-golang/lib/cmd.lua @@ -3,7 +3,6 @@ local async = require("neotest.async") local logger = require("neotest-golang.logging") -local convert = require("neotest-golang.lib.convert") local options = require("neotest-golang.options") local json = require("neotest-golang.lib.json") @@ -17,18 +16,11 @@ function M.golist_data(cwd) "-json", "./...", } - logger.debug( - "Running Go list: (" .. table.concat(go_list_command, " ") .. ") in " .. cwd - ) + local go_list_command_concat = table.concat(go_list_command, " ") + logger.debug("Running Go list: " .. go_list_command_concat .. " in " .. cwd) local output = - vim.fn.system("cd " .. cwd .. " && " .. table.concat(go_list_command, " ")) - - if logger.get_level() < 2 then - -- NOTE: this is behind an if-condition for optimization reasons. - -- See the log level integer values by inspecting vim.log.levels.DEBUG. - logger.info("Go list output: " .. convert.table_to_string(output)) - end - + vim.fn.system("cd " .. cwd .. " && " .. go_list_command_concat, " ") + logger.info({ "Go list output: ", output }) return json.decode_from_string(output) end diff --git a/lua/neotest-golang/lib/convert.lua b/lua/neotest-golang/lib/convert.lua index 88df740..3184e42 100644 --- a/lua/neotest-golang/lib/convert.lua +++ b/lua/neotest-golang/lib/convert.lua @@ -88,16 +88,4 @@ function M.to_lua_pattern(str) return str end -function M.table_to_string(tbl) - local inspected = vim.inspect(tbl) - - -- First, replace escaped newlines and tabs - inspected = inspected:gsub("\\n", ""):gsub("\\t", "") - - -- Then, remove any remaining actual newlines and tabs - inspected = inspected:gsub("[\n\t]", "") - - return inspected -end - return M diff --git a/lua/neotest-golang/logging.lua b/lua/neotest-golang/logging.lua index 43f2580..88d2687 100644 --- a/lua/neotest-golang/logging.lua +++ b/lua/neotest-golang/logging.lua @@ -5,34 +5,74 @@ local logger = require("neotest.logging") local prefix = "[neotest-golang] " --- NOTE: this level is not needed. --- ---@param msg string --- function M.trace(msg) --- return logger.trace(prefix .. msg) --- end +local function table_to_string(tbl) + local inspected = vim.inspect(tbl) + + -- First, replace escaped newlines and tabs + inspected = inspected:gsub("\\n", ""):gsub("\\t", "") + + -- Then, remove any remaining actual newlines and tabs + inspected = inspected:gsub("[\n\t]", "") + + return inspected +end + +---@param tbl table +---@return string +local function handle_table(tbl) + local msg = "" + for _, t in ipairs(tbl) do + if type(t) == "table" then + msg = msg .. table_to_string(t) + elseif type(t) == "string" then + msg = msg .. t + else + msg = msg .. tostring(t) + end + end + return msg +end ---Log the debug information. ----@param msg string +---@param msg string|table function M.debug(msg) + if M.get_level() > vim.log.levels.DEBUG then + return + end + if type(msg) == "table" then + msg = handle_table(msg) + end logger.debug(prefix .. msg) end ---Log the information. ----@param msg string +---@param msg string|table function M.info(msg) + if M.get_level() > vim.log.levels.INFO then + return + end + if type(msg) == "table" then + msg = handle_table(msg) + end logger.info(prefix .. msg) end ---Notify and log the warning. ----@param msg string +---@param msg string|table function M.warn(msg) + if type(msg) == "table" then + msg = handle_table(msg) + end vim.notify(msg, vim.log.levels.WARN) logger.warn(prefix .. msg) end ---Notify, log and throw error. ----@param msg string +---@param msg string|table function M.error(msg) + if type(msg) == "table" then + msg = handle_table(msg) + end vim.notify(msg, vim.log.levels.ERROR) logger.error(prefix .. msg) error(msg)