Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow arguments for :Neorg generate-workspace-summary #1156

Merged
merged 18 commits into from
Nov 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 44 additions & 22 deletions lua/neorg/modules/core/summary/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ The `core.summary` module exposes a single command - `:Neorg generate-workspace-
When executed with the cursor hovering over a heading, `core.summary` will generate, you guessed it,
a summary of the entire workspace, with links to each respective entry in that workspace.

If arguments are provided then a partial summary is generated containing only categories that
you have provided.
E.g. `:Neorg generate-workspace-summary work todos` would only generate a
summary of the categories `work` and `todos`.

The way the summary is generated relies on the `strategy` configuration option,
which by default consults the document metadata (see also
[`core.esupports.metagen`](@core.esupports.metagen)) or the first heading title
Expand All @@ -32,7 +37,7 @@ module.load = function()
modules.await("core.neorgcmd", function(neorgcmd)
neorgcmd.add_commands_from_table({
["generate-workspace-summary"] = {
args = 0,
min_args = 0,
condition = "norg",
name = "summary.summarize",
},
Expand Down Expand Up @@ -76,9 +81,13 @@ module.load = function()
end
end

return function(files, ws_root, heading_level)
return function(files, ws_root, heading_level, include_categories)
local categories = vim.defaulttable()

if vim.tbl_isempty(include_categories) then
include_categories = nil
end

utils.read_files(files, function(bufnr, filename)
local metadata = ts.get_document_metadata(bufnr)

Expand Down Expand Up @@ -108,11 +117,13 @@ module.load = function()
if metadata.description == vim.NIL then
metadata.description = nil
end
table.insert(categories[lib.title(category)], {
title = tostring(metadata.title),
norgname = norgname,
description = metadata.description,
})
if not include_categories or vim.tbl_contains(include_categories, category:lower()) then
table.insert(categories[lib.title(category)], {
title = tostring(metadata.title),
norgname = norgname,
description = metadata.description,
})
end
end
end)
local result = {}
Expand Down Expand Up @@ -151,24 +162,22 @@ module.config.public = {
-- Possible options are:
-- - "default" - read the metadata to categorize and annotate files. Files
-- without metadata will use the top level heading as the title. If no headings are present, the filename will be used.
---@type string|fun(files: string[], ws_root: string, heading_level: number?): string[]?
---@type string|fun(files: string[], ws_root: string, heading_level: number?, include_categories: string[]?): string[]?
strategy = "default",
}

module.public = {}

module.events.subscribed = {
["core.neorgcmd"] = {
["summary.summarize"] = true,
},
}

module.on_event = function(event)
if event.type == "core.neorgcmd.events.summary.summarize" then
module.public = {
---@param buf integer? the buffer to insert the summary to
---@param cursor_pos integer[]? a tuple of row, col of the cursor positon (see nvim_win_get_cursor())
---@param include_categories string[]? table of strings (ignores case) for categories that you wish to include in the summary.
-- if excluded then all categories are written into the summary.
generate_workspace_summary = function(buf, cursor_pos, include_categories)
local ts = module.required["core.integrations.treesitter"]
local buffer = event.buffer

local node_at_cursor = ts.get_first_node_on_line(buffer, event.cursor_position[1] - 1)
local buffer = buf or 0
local cursor_position = cursor_pos or vim.api.nvim_win_get_cursor(0)

local node_at_cursor = ts.get_first_node_on_line(buffer, cursor_position[1] - 1)

if not node_at_cursor or not node_at_cursor:type():match("^heading%d$") then
utils.notify(
Expand All @@ -190,7 +199,8 @@ module.on_event = function(event)
local generated = module.config.public.strategy(
dirman.get_norg_files(dirman.get_current_workspace()[1]) or {},
ws_root,
level + 1
level + 1,
vim.tbl_map(string.lower, include_categories or {})
)

if not generated or vim.tbl_isempty(generated) then
Expand All @@ -200,7 +210,19 @@ module.on_event = function(event)
return
end

vim.api.nvim_buf_set_lines(buffer, event.cursor_position[1], event.cursor_position[1], true, generated)
vim.api.nvim_buf_set_lines(buffer, cursor_position[1], cursor_position[1], true, generated)
end,
}

module.events.subscribed = {
["core.neorgcmd"] = {
["summary.summarize"] = true,
},
}

module.on_event = function(event)
if event.type == "core.neorgcmd.events.summary.summarize" then
module.public.generate_workspace_summary(event.buffer, event.cursor_position, event.content)
end
end

Expand Down
Loading