From 46741ede577392f36cad1cb8c8e6029fabb729f6 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 15 Nov 2023 15:33:26 +0100 Subject: [PATCH] feat: allow arguments for `:Neorg generate-workspace-summary` (#1156) Closes https://github.com/nvim-neorg/neorg/issues/1086 --- lua/neorg/modules/core/summary/module.lua | 66 +++++++++++++++-------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/lua/neorg/modules/core/summary/module.lua b/lua/neorg/modules/core/summary/module.lua index 3ebf2a7c4..10f06cf7d 100644 --- a/lua/neorg/modules/core/summary/module.lua +++ b/lua/neorg/modules/core/summary/module.lua @@ -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 @@ -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", }, @@ -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) @@ -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 = {} @@ -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( @@ -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 @@ -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