Skip to content

Commit

Permalink
refactor: optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 16, 2024
1 parent 8149905 commit 30bae3f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
16 changes: 4 additions & 12 deletions lua/neotest-golang/lib/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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

Expand Down
12 changes: 0 additions & 12 deletions lua/neotest-golang/lib/convert.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 49 additions & 9 deletions lua/neotest-golang/logging.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 30bae3f

Please sign in to comment.