Skip to content

Commit

Permalink
feat(tangle): add report_on_empty option in core.tangle (nvim-neo…
Browse files Browse the repository at this point in the history
…rg#1250)

Co-authored-by: vhyrro <[email protected]>
  • Loading branch information
2 people authored and benlubas committed Jan 11, 2024
1 parent 534a8d5 commit 4fc85bd
Showing 1 changed file with 26 additions and 32 deletions.
58 changes: 26 additions & 32 deletions lua/neorg/modules/core/tangle/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -416,53 +416,47 @@ 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

local file_count = vim.tbl_count(tangles)
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 <pysan3>
"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 <pysan3>
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(
Expand All @@ -475,8 +469,8 @@ module.on_event = function(event)
)
)
end
end
)
end)
end)
end
end
end
Expand Down

0 comments on commit 4fc85bd

Please sign in to comment.