From cc6d8b150de7bf806f3a191867a7f143970b5112 Mon Sep 17 00:00:00 2001 From: takuto Date: Thu, 28 Dec 2023 21:58:59 +0900 Subject: [PATCH] feat(tangle): add `report_on_empty` option in `core.tangle` (#1250) Co-authored-by: vhyrro <76052559+vhyrro@users.noreply.github.com> --- lua/neorg/modules/core/tangle/module.lua | 58 +++++++++++------------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/lua/neorg/modules/core/tangle/module.lua b/lua/neorg/modules/core/tangle/module.lua index 27fb9dc37..20e190692 100644 --- a/lua/neorg/modules/core/tangle/module.lua +++ b/lua/neorg/modules/core/tangle/module.lua @@ -416,12 +416,19 @@ module.public = { end, } +module.config.public = { + -- Notify when there is nothing to tangle (INFO) or when the content is empty (WARN). + report_on_empty = true, +} + module.on_event = function(event) if event.type == "core.neorgcmd.events.core.tangle.current-file" then local tangles = module.public.tangle(event.buffer) if not tangles or vim.tbl_isempty(tangles) then - utils.notify("Nothing to tangle!", vim.log.levels.WARN) + if module.config.public.report_on_empty then + utils.notify("Nothing to tangle!", vim.log.levels.INFO) + end return end @@ -429,40 +436,27 @@ module.on_event = function(event) local tangled_count = 0 for file, content in pairs(tangles) do - local upward_count = 0 - - for _ in string.gmatch(file, "%.%.[\\/]") do - upward_count = upward_count + 1 - end - + -- resolve upward relative path like `../../` + local relative_file, upward_count = string.gsub(file, "%.%.[\\/]", "") if upward_count > 0 then - -- adding one because the filename also has to be removed - local base = vim.fn.fnamemodify(vim.fn.expand("%"), ":p" .. string.rep(":h", upward_count + 1)) - local path = string.gsub(file, "%.%.[\\/]", "") - file = vim.fs.joinpath(base, path) + local base_dir = vim.fn.expand("%:p" .. string.rep(":h", upward_count + 1)) --[[@as string]] + file = vim.fs.joinpath(base_dir, relative_file) end - vim.loop.fs_open( - vim.fn.expand(file), ---@diagnostic disable-line -- TODO: type error workaround - "w", - 438, - function(err, fd) - file_count = file_count - 1 - assert(not err, lib.lazy_string_concat("Failed to open file '", file, "' for tangling: ", err)) - - vim.loop.fs_write( - fd, ---@diagnostic disable-line -- TODO: type error workaround - table.concat(content, "\n"), - 0, - function(werr) - assert( - not werr, - lib.lazy_string_concat("Failed to write to file '", file, "' for tangling: ", werr) - ) - end - ) + vim.loop.fs_open(vim.fn.expand(file) --[[@as string]], "w", 438, function(err, fd) + assert(not err and fd, lib.lazy_string_concat("Failed to open file '", file, "' for tangling: ", err)) + + local write_content = table.concat(content, "\n") + if module.config.public.report_on_empty and write_content:len() == 0 then + vim.schedule(function() + utils.notify(string.format("Tangled content for %s is empty.", file), vim.log.levels.WARN) + end) + end + vim.loop.fs_write(fd, write_content, 0, function(werr) + assert(not werr, lib.lazy_string_concat("Failed to write to '", file, "' for tangling: ", werr)) tangled_count = tangled_count + 1 + file_count = file_count - 1 if file_count == 0 then vim.schedule( lib.wrap( @@ -475,8 +469,8 @@ module.on_event = function(event) ) ) end - end - ) + end) + end) end end end