From f1e8ff4cdfb6461e11a5b2caed0328cc0352ad95 Mon Sep 17 00:00:00 2001 From: Luckas Date: Fri, 22 Dec 2023 11:22:39 +0300 Subject: [PATCH] feat: better log system --- lua/nvim-devdocs/build.lua | 28 ++++++++++++++++---- lua/nvim-devdocs/fs.lua | 7 ++--- lua/nvim-devdocs/init.lua | 20 ++++++++++----- lua/nvim-devdocs/list.lua | 4 +-- lua/nvim-devdocs/log.lua | 40 +++++++++++++++++++++++++++++ lua/nvim-devdocs/notify.lua | 9 ------- lua/nvim-devdocs/operations.lua | 45 +++++++++++++++++---------------- lua/nvim-devdocs/pickers.lua | 8 +++--- 8 files changed, 108 insertions(+), 53 deletions(-) create mode 100644 lua/nvim-devdocs/log.lua delete mode 100644 lua/nvim-devdocs/notify.lua diff --git a/lua/nvim-devdocs/build.lua b/lua/nvim-devdocs/build.lua index 84f4a07..0487766 100644 --- a/lua/nvim-devdocs/build.lua +++ b/lua/nvim-devdocs/build.lua @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lua/nvim-devdocs/fs.lua b/lua/nvim-devdocs/fs.lua index 489978e..7448c0f 100644 --- a/lua/nvim-devdocs/fs.lua +++ b/lua/nvim-devdocs/fs.lua @@ -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) diff --git a/lua/nvim-devdocs/init.lua b/lua/nvim-devdocs/init.lua index e92eaab..d36c76e 100644 --- a/lua/nvim-devdocs/init.lua +++ b/lua/nvim-devdocs/init.lua @@ -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") @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lua/nvim-devdocs/list.lua b/lua/nvim-devdocs/list.lua index ddc7c39..205d577 100644 --- a/lua/nvim-devdocs/list.lua +++ b/lua/nvim-devdocs/list.lua @@ -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() @@ -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 diff --git a/lua/nvim-devdocs/log.lua b/lua/nvim-devdocs/log.lua new file mode 100644 index 0000000..06db513 --- /dev/null +++ b/lua/nvim-devdocs/log.lua @@ -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 diff --git a/lua/nvim-devdocs/notify.lua b/lua/nvim-devdocs/notify.lua deleted file mode 100644 index c45621d..0000000 --- a/lua/nvim-devdocs/notify.lua +++ /dev/null @@ -1,9 +0,0 @@ -local M = {} - -M.log = vim.schedule_wrap(function(message) vim.notify(message, vim.log.levels.INFO) end) - -M.log_warn = vim.schedule_wrap(function(message) vim.notify(message, vim.log.levels.WARN) end) - -M.log_err = vim.schedule_wrap(function(message) vim.notify(message, vim.log.levels.ERROR) end) - -return M diff --git a/lua/nvim-devdocs/operations.lua b/lua/nvim-devdocs/operations.lua index bf20671..85c0096 100644 --- a/lua/nvim-devdocs/operations.lua +++ b/lua/nvim-devdocs/operations.lua @@ -4,10 +4,10 @@ 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") @@ -15,19 +15,22 @@ 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 @@ -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 @@ -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 ", }) @@ -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 @@ -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 @@ -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 @@ -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() @@ -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 @@ -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 diff --git a/lua/nvim-devdocs/pickers.lua b/lua/nvim-devdocs/pickers.lua index 060e973..9f76ca5 100644 --- a/lua/nvim-devdocs/pickers.lua +++ b/lua/nvim-devdocs/pickers.lua @@ -9,8 +9,8 @@ local action_state = require("telescope.actions.state") local config = require("telescope.config").values local entry_display = require("telescope.pickers.entry_display") +local log = require("nvim-devdocs.log") local list = require("nvim-devdocs.list") -local notify = require("nvim-devdocs.notify") local operations = require("nvim-devdocs.operations") local transpiler = require("nvim-devdocs.transpiler") local plugin_state = require("nvim-devdocs.state") @@ -72,8 +72,9 @@ local doc_previewer = previewers.new_buffer_previewer({ local function open_doc(selection, float) local bufnr = state.get_global_key("last_preview_bufnr") + local picker_cmd = plugin_config.options.picker_cmd - if plugin_config.options.picker_cmd or not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then + if picker_cmd or not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then bufnr = vim.api.nvim_create_buf(false, true) local lines = plugin_state.get("preview_lines") or operations.read_entry(selection.value) vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) @@ -180,6 +181,7 @@ M.open_picker = function(entries, float) open_doc(selection, float) end end) + return true end, }) @@ -195,7 +197,7 @@ M.open_picker_alias = function(alias, float) if not entries then return end if vim.tbl_isempty(entries) then - notify.log_err(alias .. " documentation is not installed") + log.error(alias .. " documentation is not installed") else plugin_state.set("current_doc", alias) M.open_picker(entries, float)