Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
feat: better log system
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Dec 22, 2023
1 parent 738c5d2 commit f1e8ff4
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 53 deletions.
28 changes: 23 additions & 5 deletions lua/nvim-devdocs/build.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local M = {}

local fs = require("nvim-devdocs.fs")
local notify = require("nvim-devdocs.notify")
local log = require("nvim-devdocs.log")
local transpiler = require("nvim-devdocs.transpiler")

---@param entry RegisteryEntry
Expand All @@ -11,7 +11,7 @@ M.build_docs = function(entry, doc_index, docs)
local alias = entry.slug:gsub("~", "-")
local current_doc_dir = DOCS_DIR:joinpath(alias)

notify.log("Building " .. alias .. " documentation...")
log.info("Building " .. alias .. " documentation...")

if not DOCS_DIR:exists() then DOCS_DIR:mkdir() end
if not current_doc_dir:exists() then current_doc_dir:mkdir() end
Expand All @@ -36,11 +36,26 @@ M.build_docs = function(entry, doc_index, docs)
local sort_lookup = {}
local sort_lookup_last_index = 1
local count = 1
local total = vim.tbl_count(docs)

for key, doc in pairs(docs) do
log.debug(string.format("Converting %s (%s/%s)", key, count, total))

local sections = section_map[key]
local markdown, md_sections = transpiler.html_to_md(doc, sections)
local file_path = current_doc_dir:joinpath(tostring(count) .. ".md")
local success, result, md_sections =
xpcall(transpiler.html_to_md, debug.traceback, doc, sections)

if not success then
local message = string.format(
'Failed to convert "%s", please report this issue\n\n%s\n\nOriginal html document:\n\n%s',
key,
result,
doc
)
log.error(message)
return
end

for _, section in ipairs(md_sections) do
path_map[key .. "#" .. section.id] = count .. "," .. section.md_path
Expand All @@ -50,16 +65,19 @@ M.build_docs = function(entry, doc_index, docs)

-- Use number as filename instead of the entry name to avoid invalid filenames
path_map[key] = tostring(count)
file_path:write(markdown, "w")
file_path:write(result, "w")
count = count + 1
log.debug(file_path .. " has been writen")
end

log.debug("Sorting docs entries")
table.sort(doc_index.entries, function(a, b)
local index_a = sort_lookup[a.path] or -1
local index_b = sort_lookup[b.path] or -1
return index_a < index_b
end)

log.debug("Filling docs links and path")
for i, index_entry in ipairs(doc_index.entries) do
local main = vim.split(index_entry.path, "#")[1]
doc_index.entries[i].link = doc_index.entries[i].path
Expand All @@ -72,7 +90,7 @@ M.build_docs = function(entry, doc_index, docs)
fs.write_index(index)
fs.write_lockfile(lockfile)

notify.log("Build complete!")
log.info("Build complete!")
end

return M
7 changes: 2 additions & 5 deletions lua/nvim-devdocs/fs.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
local M = {}

---@param registery RegisteryEntry[]
M.write_registery = function(registery)
local encoded = vim.fn.json_encode(registery)
REGISTERY_PATH:write(encoded, "w")
end
---@param registery string
M.write_registery = function(registery) REGISTERY_PATH:write(registery, "w") end

---@param index IndexTable
M.write_index = function(index)
Expand Down
20 changes: 13 additions & 7 deletions lua/nvim-devdocs/init.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local M = {}

local log = require("nvim-devdocs.log")
local list = require("nvim-devdocs.list")
local state = require("nvim-devdocs.state")
local notify = require("nvim-devdocs.notify")
local pickers = require("nvim-devdocs.pickers")
local operations = require("nvim-devdocs.operations")
local config = require("nvim-devdocs.config")
Expand All @@ -29,11 +29,13 @@ end

M.open_doc = function(args, float)
if vim.tbl_isempty(args.fargs) then
log.debug("Opening all installed entries")
local installed = list.get_installed_alias()
local entries = list.get_doc_entries(installed)
pickers.open_picker(entries or {}, float)
else
local alias = args.fargs[1]
log.debug("Opening " .. alias .. " entries")
pickers.open_picker_alias(alias, float)
end
end
Expand All @@ -53,7 +55,7 @@ M.open_doc_current_file = function(float)
if entries and not vim.tbl_isempty(entries) then
pickers.open_picker(entries, float)
else
notify.log_err("No documentation found for the current filetype")
log.error("No documentation found for the current filetype")
end
end

Expand All @@ -69,7 +71,7 @@ M.update_all = function()
local updatable = list.get_updatable()

if vim.tbl_isempty(updatable) then
notify.log("All documentations are up to date")
log.info("All documentations are up to date")
else
operations.install_args(updatable, true, true)
end
Expand All @@ -96,16 +98,18 @@ M.keywordprg = function(args)
if keyword then
operations.keywordprg(keyword)
else
notify.log("No keyword provided")
log.error("No keyword provided")
end
end

---@param opts nvim_devdocs.Config
M.setup = function(opts)
config.setup(opts)

local ensure_installed = config.options.ensure_installed

vim.defer_fn(function() operations.install_args(ensure_installed) end, 3000)
vim.defer_fn(function()
log.debug("Installing required docs")
operations.install_args(config.options.ensure_installed)
end, 3000)

local cmd = vim.api.nvim_create_user_command

Expand All @@ -120,6 +124,8 @@ M.setup = function(opts)
cmd("DevdocsUpdate", M.update, { nargs = "*", complete = completion.get_updatable })
cmd("DevdocsUpdateAll", M.update_all, {})
cmd("DevdocsToggle", M.toggle, {})

log.debug("Plugin initialized")
end

return M
4 changes: 2 additions & 2 deletions lua/nvim-devdocs/list.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local M = {}

local fs = require("nvim-devdocs.fs")
local notify = require("nvim-devdocs.notify")
local log = require("nvim-devdocs.log")

---@return string[]
M.get_installed_alias = function()
Expand Down Expand Up @@ -67,7 +67,7 @@ local function get_registery_entry(predicate)
local registery = fs.read_registery()

if not registery then
notify.log_err("DevDocs registery not found, please run :DevdocsFetch")
log.error("DevDocs registery not found, please run :DevdocsFetch")
return
end

Expand Down
40 changes: 40 additions & 0 deletions lua/nvim-devdocs/log.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
local M = {}

local log = require("plenary.log").new({
plugin = "nvim-devdocs",
use_console = false, -- use vim.notify instead
outfile = vim.fn.stdpath("data") .. "/devdocs/log.txt",
fmt_msg = function(_, mode_name, src_path, src_line, message)
local mode = mode_name:upper()
local timestamp = os.date("%Y-%m-%d %H:%M:%S")
local source = vim.fn.fnamemodify(src_path, ":t") .. ":" .. src_line

return string.format("[%s][%s] %s: %s\n", mode, timestamp, source, message)
end,
}, false)

local notify = vim.schedule_wrap(
function(message, level) vim.notify("[nvim-devdocs] " .. message, level) end
)

M.debug = function(message)
notify(message, vim.log.levels.DEBUG)
log.debug(message)
end

M.info = function(message)
notify(message, vim.log.levels.INFO)
log.info(message)
end

M.warn = function(message)
notify(message, vim.log.levels.WARN)
log.warn(message)
end

M.error = function(message)
notify(message, vim.log.levels.ERROR)
log.error(message)
end

return M
9 changes: 0 additions & 9 deletions lua/nvim-devdocs/notify.lua

This file was deleted.

45 changes: 23 additions & 22 deletions lua/nvim-devdocs/operations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@ local job = require("plenary.job")
local curl = require("plenary.curl")

local fs = require("nvim-devdocs.fs")
local log = require("nvim-devdocs.log")
local list = require("nvim-devdocs.list")
local state = require("nvim-devdocs.state")
local build = require("nvim-devdocs.build")
local notify = require("nvim-devdocs.notify")
local config = require("nvim-devdocs.config")
local keymaps = require("nvim-devdocs.keymaps")

local devdocs_site_url = "https://devdocs.io"
local devdocs_cdn_url = "https://documents.devdocs.io"

M.fetch = function()
notify.log("Fetching DevDocs registery...")
log.info("Fetching DevDocs registery...")

curl.get(devdocs_site_url .. "/docs.json", {
headers = {
["User-agent"] = "chrome", -- fake user agent, see #25
},
callback = function(response)
if not DATA_DIR:exists() then DATA_DIR:mkdir() end
REGISTERY_PATH:write(response.body, "w")
notify.log("DevDocs registery has been written to the disk")
if not DATA_DIR:exists() then
log.debug("Docs directory not found, creating a new directory")
DATA_DIR:mkdir()
end
fs.write_registery(response.body)
log.info("DevDocs registery has been written to the disk")
end,
on_error = function(error)
notify.log_err("nvim-devdocs: Error when fetching registery, exit code: " .. error.exit)
log.error("Error when fetching registery, exit code: " .. error.exit)
end,
})
end
Expand All @@ -37,7 +40,7 @@ end
---@param is_update? boolean
M.install = function(entry, verbose, is_update)
if not REGISTERY_PATH:exists() then
if verbose then notify.log_err("DevDocs registery not found, please run :DevdocsFetch") end
if verbose then log.error("DevDocs registery not found, please run :DevdocsFetch") end
return
end

Expand All @@ -46,11 +49,13 @@ M.install = function(entry, verbose, is_update)
local is_installed = vim.tbl_contains(installed, alias)

if not is_update and is_installed then
if verbose then notify.log("Documentation for " .. alias .. " is already installed") end
if verbose then log.warn("Documentation for " .. alias .. " is already installed") end
else
local ui = vim.api.nvim_list_uis()

if ui[1] and entry.db_size > 10000000 then
log.debug(string.format("%s docs is too large (%s)", alias, entry.db_size))

local input = vim.fn.input({
prompt = "Building large docs can freeze neovim, continue? y/n ",
})
Expand All @@ -61,32 +66,28 @@ M.install = function(entry, verbose, is_update)
local callback = function(index)
local doc_url = string.format("%s/%s/db.json?%s", devdocs_cdn_url, entry.slug, entry.mtime)

notify.log("Downloading " .. alias .. " documentation...")
log.info("Downloading " .. alias .. " documentation...")
curl.get(doc_url, {
callback = vim.schedule_wrap(function(response)
local docs = vim.fn.json_decode(response.body)
build.build_docs(entry, index, docs)
end),
on_error = function(error)
notify.log_err(
"nvim-devdocs[" .. alias .. "]: Error during download, exit code: " .. error.exit
)
log.error("(" .. alias .. ") Error during download, exit code: " .. error.exit)
end,
})
end

local index_url = string.format("%s/%s/index.json?%s", devdocs_cdn_url, entry.slug, entry.mtime)

notify.log("Fetching " .. alias .. " documentation entries...")
log.info("Fetching " .. alias .. " documentation entries...")
curl.get(index_url, {
callback = vim.schedule_wrap(function(response)
local index = vim.fn.json_decode(response.body)
callback(index)
end),
on_error = function(error)
notify.log_err(
"nvim-devdocs[" .. alias .. "]: Error during download, exit code: " .. error.exit
)
log.error("(" .. alias .. ") Error during download, exit code: " .. error.exit)
end,
})
end
Expand All @@ -100,7 +101,7 @@ M.install_args = function(args, verbose, is_update)
local registery = fs.read_registery()

if not registery then
if verbose then notify.log_err("DevDocs registery not found, please run :DevdocsFetch") end
if verbose then log.error("DevDocs registery not found, please run :DevdocsFetch") end
return
end

Expand All @@ -116,10 +117,10 @@ M.install_args = function(args, verbose, is_update)
end

if vim.tbl_isempty(data) then
notify.log_err("No documentation available for " .. arg)
log.error("No documentation available for " .. arg)
else
if is_update and not vim.tbl_contains(updatable, arg) then
notify.log(arg .. " documentation is already up to date")
log.info(arg .. " documentation is already up to date")
else
M.install(data, verbose, is_update)
end
Expand All @@ -132,7 +133,7 @@ M.uninstall = function(alias)
local installed = list.get_installed_alias()

if not vim.tbl_contains(installed, alias) then
notify.log(alias .. " documentation is already uninstalled")
log.info(alias .. " documentation is already uninstalled")
else
local index = fs.read_index()
local lockfile = fs.read_lockfile()
Expand All @@ -146,7 +147,7 @@ M.uninstall = function(alias)
fs.write_lockfile(lockfile)
fs.remove_docs(alias)

notify.log(alias .. " documentation has been uninstalled")
log.info(alias .. " documentation has been uninstalled")
end
end

Expand Down Expand Up @@ -313,7 +314,7 @@ M.keywordprg = function(keyword)
end
end

if not entry then notify.log("No documentation found for " .. keyword) end
if not entry then log.error("No documentation found for " .. keyword) end
end

return M
Loading

0 comments on commit f1e8ff4

Please sign in to comment.