From 113176bc2be302aa93dfc2a9c2b8ab9722595eb0 Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Wed, 25 Oct 2023 14:21:10 +0200 Subject: [PATCH 01/16] =?UTF-8?q?=E2=9C=A8=20added=20option=20to=20incluce?= =?UTF-8?q?=20custom=20metadata=20into=20new=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/neorg/modules/core/dirman/module.lua | 30 +++++++++++++++++------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/lua/neorg/modules/core/dirman/module.lua b/lua/neorg/modules/core/dirman/module.lua index cb66f4c18..77ab22fe8 100644 --- a/lua/neorg/modules/core/dirman/module.lua +++ b/lua/neorg/modules/core/dirman/module.lua @@ -271,6 +271,7 @@ module.public = { ---@param opts? table additional options --- - opts.no_open (bool) if true, will not open the file in neovim after creating it --- - opts.force (bool) if true, will overwrite existing file content + --- - opts.metadata (table) Table of metadata data, overrides defaults if present create_file = function(path, workspace, opts) opts = opts or {} @@ -308,20 +309,33 @@ module.public = { fname = fname .. ".norg" end - if opts.no_open then - -- Create the file - local fd = vim.loop.fs_open(fname, opts.force and "w" or "a", 438) + -- Create the file + local fd = vim.loop.fs_open(fname, opts.force and "w" or "a", 438) - if fd then - vim.loop.fs_close(fd) - end + if fd then + vim.loop.fs_close(fd) + end - return + if opts.no_open then + -- new tab for easy closing again + vim.cmd("tabnew") end -- Begin editing that newly created file - vim.cmd("e " .. fname .. " | w") + vim.cmd("e " .. fname) + + local metagen = neorg.modules.get_module("core.esupports.metagen") + if opts.metadata and metagen then + metagen.write_metadata(0, true, opts.metadata) + end + + vim.cmd("w") + + if opts.no_open then + vim.cmd("tabclose") + end end, + --- Takes in a workspace name and a path for a file and opens it ---@param workspace_name string #The name of the workspace to use ---@param path string #A path to open the file (e.g directory/filename.norg) From de387739fb6bb7c2ea259f8a91dbd9467e620f71 Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Wed, 25 Oct 2023 14:22:54 +0200 Subject: [PATCH 02/16] =?UTF-8?q?=E2=9C=A8=20added=20create=5Fmetadata=20t?= =?UTF-8?q?o=20override=20defaults?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/core/esupports/metagen/module.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lua/neorg/modules/core/esupports/metagen/module.lua b/lua/neorg/modules/core/esupports/metagen/module.lua index 9165f67be..b5e2f3bac 100644 --- a/lua/neorg/modules/core/esupports/metagen/module.lua +++ b/lua/neorg/modules/core/esupports/metagen/module.lua @@ -185,10 +185,18 @@ module.public = { } end, - --- Creates the metadata contents from the configuration's template. + --- Constructs the metadata contents from the configuration's template. ---@param buf number #The buffer to query potential data from ---@return table #A table of strings that can be directly piped to `nvim_buf_set_lines` construct_metadata = function(buf) + return module.public.create_metadata(buf, {}) + end, + + --- Creates the metadata contents from the provided metadata table (defaulting to the configuration's template). + ---@param buf number #The buffer to query potential data from + ---@param metadata table #Table of metadata data, overrides defaults if present + ---@return table #A table of strings that can be directly piped to `nvim_buf_set_lines` + create_metadata = function(buf, metadata) local template = module.config.public.template local whitespace = type(module.config.public.tab) == "function" and module.config.public.tab() or module.config.public.tab @@ -200,6 +208,10 @@ module.public = { } for _, data in ipairs(template) do + if metadata and metadata[data[1]] then + -- override with data from metadata table + data = { data[1], metadata[data[1]] } + end table.insert( result, whitespace .. data[1] .. delimiter .. tostring(type(data[2]) == "function" and data[2]() or data[2]) From 13d8426af02fe998039d30e8a4a74915b3d216a8 Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Wed, 25 Oct 2023 14:23:34 +0200 Subject: [PATCH 03/16] =?UTF-8?q?=E2=9C=A8=20added=20write=5Fmetadata?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This method can be used to add custom metadata into file instead of defaults. If table is empty of nil then simply uses default like inject_metadata --- lua/neorg/modules/core/esupports/metagen/module.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lua/neorg/modules/core/esupports/metagen/module.lua b/lua/neorg/modules/core/esupports/metagen/module.lua index b5e2f3bac..1ccf2cc8b 100644 --- a/lua/neorg/modules/core/esupports/metagen/module.lua +++ b/lua/neorg/modules/core/esupports/metagen/module.lua @@ -231,10 +231,18 @@ module.public = { ---@param buf number #The number of the buffer to inject the metadata into ---@param force? boolean #Whether to forcefully override existing metadata inject_metadata = function(buf, force) + module.public.write_metadata(buf, force, {}) + end, + + --- Inject the metadata into a buffer + ---@param buf number #The number of the buffer to inject the metadata into + ---@param force? boolean #Whether to forcefully override existing metadata + ---@param metadata table #Table of metadata data, overrides defaults if present + write_metadata = function(buf, force, metadata) local present, data = module.public.is_metadata_present(buf) if force or not present then - local constructed_metadata = module.public.construct_metadata(buf) + local constructed_metadata = module.public.create_metadata(buf, metadata) vim.api.nvim_buf_set_lines(buf, data.range[1], data.range[2], false, constructed_metadata) end end, From ab1c93b1a8ef0ee38158312f09ace954c571cbce Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Wed, 25 Oct 2023 18:52:12 +0200 Subject: [PATCH 04/16] =?UTF-8?q?=F0=9F=8E=A8=20simplified=20creation=20of?= =?UTF-8?q?=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No longer uses tabnew and relies on buffer 0 to insert metadata into the newly created file. --- lua/neorg/modules/core/dirman/module.lua | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/lua/neorg/modules/core/dirman/module.lua b/lua/neorg/modules/core/dirman/module.lua index 77ab22fe8..d8f292456 100644 --- a/lua/neorg/modules/core/dirman/module.lua +++ b/lua/neorg/modules/core/dirman/module.lua @@ -311,28 +311,19 @@ module.public = { -- Create the file local fd = vim.loop.fs_open(fname, opts.force and "w" or "a", 438) - if fd then vim.loop.fs_close(fd) end - if opts.no_open then - -- new tab for easy closing again - vim.cmd("tabnew") - end - - -- Begin editing that newly created file - vim.cmd("e " .. fname) - local metagen = neorg.modules.get_module("core.esupports.metagen") if opts.metadata and metagen then - metagen.write_metadata(0, true, opts.metadata) + local bufnr = module.public.get_file_bufnr(fname) + metagen.write_metadata(bufnr, true, opts.metadata) end - vim.cmd("w") - - if opts.no_open then - vim.cmd("tabclose") + if not opts.no_open then + -- Begin editing that newly created file + vim.cmd("e " .. fname .. "| w") end end, From 64b7824da898d28bb0771f922fc2ff2e5d52af6c Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Wed, 25 Oct 2023 20:06:19 +0200 Subject: [PATCH 05/16] =?UTF-8?q?=F0=9F=8E=A8=20converted=20tabs=20to=20sp?= =?UTF-8?q?aces=20to=20adhere=20to=20formatting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/neorg/modules/core/dirman/module.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lua/neorg/modules/core/dirman/module.lua b/lua/neorg/modules/core/dirman/module.lua index d8f292456..166695821 100644 --- a/lua/neorg/modules/core/dirman/module.lua +++ b/lua/neorg/modules/core/dirman/module.lua @@ -317,13 +317,13 @@ module.public = { local metagen = neorg.modules.get_module("core.esupports.metagen") if opts.metadata and metagen then - local bufnr = module.public.get_file_bufnr(fname) + local bufnr = module.public.get_file_bufnr(fname) metagen.write_metadata(bufnr, true, opts.metadata) end if not opts.no_open then - -- Begin editing that newly created file - vim.cmd("e " .. fname .. "| w") + -- Begin editing that newly created file + vim.cmd("e " .. fname .. "| w") end end, From 21fc667bc89b253b22b5315aeca7ec7c0490d2e9 Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Wed, 25 Oct 2023 20:07:03 +0200 Subject: [PATCH 06/16] =?UTF-8?q?docs(dirman):=20=F0=9F=93=9D=20extended?= =?UTF-8?q?=20docs=20for=20file=20creation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/neorg/modules/core/dirman/module.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lua/neorg/modules/core/dirman/module.lua b/lua/neorg/modules/core/dirman/module.lua index 166695821..eb6dd7818 100644 --- a/lua/neorg/modules/core/dirman/module.lua +++ b/lua/neorg/modules/core/dirman/module.lua @@ -34,6 +34,19 @@ To query the current workspace, run `:Neorg workspace`. To set the workspace, ru ### Changing the Current Working Directory After a recent update `core.dirman` will no longer change the current working directory after switching workspace. To get the best experience it's recommended to set the `autochdir` Neovim option. + + +### Create a new note +You can use dirman to create new notes in your workspaces. + +```lua +local dirman = require('neorg').modules.get_module("core.dirman") +dirman.create_file("my_file", "my_ws", { + no_open = false, -- open file after creation? + force = false, -- overwrite file if exists + metadata = {} -- key-value table for metadata fields +}) +``` --]] local neorg = require("neorg.core") @@ -272,6 +285,8 @@ module.public = { --- - opts.no_open (bool) if true, will not open the file in neovim after creating it --- - opts.force (bool) if true, will overwrite existing file content --- - opts.metadata (table) Table of metadata data, overrides defaults if present + --- - the table is a key-value table where the key correspondes to a metadata field, + --- and the value is either a string or a function returning a string. create_file = function(path, workspace, opts) opts = opts or {} From 0ac07f2ef5f1d8cd53fecf51a021d5694ca75f14 Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Wed, 25 Oct 2023 20:07:36 +0200 Subject: [PATCH 07/16] =?UTF-8?q?docs(metagen):=20=F0=9F=93=9D=20extended?= =?UTF-8?q?=20docs=20for=20metadata=20creation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/neorg/modules/core/esupports/metagen/module.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lua/neorg/modules/core/esupports/metagen/module.lua b/lua/neorg/modules/core/esupports/metagen/module.lua index 1ccf2cc8b..9dd8010ed 100644 --- a/lua/neorg/modules/core/esupports/metagen/module.lua +++ b/lua/neorg/modules/core/esupports/metagen/module.lua @@ -194,7 +194,9 @@ module.public = { --- Creates the metadata contents from the provided metadata table (defaulting to the configuration's template). ---@param buf number #The buffer to query potential data from - ---@param metadata table #Table of metadata data, overrides defaults if present + ---@param metadata table #Table of metadata, overrides defaults if present + --- - the table is a key-value table where the key correspondes to a metadata field, + --- and the value is either a string or a function returning a string. ---@return table #A table of strings that can be directly piped to `nvim_buf_set_lines` create_metadata = function(buf, metadata) local template = module.config.public.template From 196c2e9646fb0d5925a5ad2a8298f821dcf5a885 Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Thu, 26 Oct 2023 13:46:03 +0200 Subject: [PATCH 08/16] =?UTF-8?q?docs(metadata):=20=F0=9F=93=9D=20class=20?= =?UTF-8?q?annotations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added class annotations to better describe the input options for create_file and create_metadata. --- lua/neorg/modules/core/dirman/module.lua | 13 +++++++------ .../modules/core/esupports/metagen/module.lua | 15 +++++++++++---- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lua/neorg/modules/core/dirman/module.lua b/lua/neorg/modules/core/dirman/module.lua index eb6dd7818..563d76d22 100644 --- a/lua/neorg/modules/core/dirman/module.lua +++ b/lua/neorg/modules/core/dirman/module.lua @@ -278,15 +278,16 @@ module.public = { }) end) end, + + ---@class create_file_opts + ---@field no_open? boolean do not open the file after creation? + ---@field force? boolean overwrite file if it already exists? + ---@field metadata? metadata metadata fields, if provided inserts metadata - an empty table uses default values + --- Takes in a path (can include directories) and creates a .norg file from that path ---@param path string a path to place the .norg file in ---@param workspace? string workspace name - ---@param opts? table additional options - --- - opts.no_open (bool) if true, will not open the file in neovim after creating it - --- - opts.force (bool) if true, will overwrite existing file content - --- - opts.metadata (table) Table of metadata data, overrides defaults if present - --- - the table is a key-value table where the key correspondes to a metadata field, - --- and the value is either a string or a function returning a string. + ---@param opts? create_file_opts additional options create_file = function(path, workspace, opts) opts = opts or {} diff --git a/lua/neorg/modules/core/esupports/metagen/module.lua b/lua/neorg/modules/core/esupports/metagen/module.lua index 9dd8010ed..fa1566d14 100644 --- a/lua/neorg/modules/core/esupports/metagen/module.lua +++ b/lua/neorg/modules/core/esupports/metagen/module.lua @@ -192,11 +192,18 @@ module.public = { return module.public.create_metadata(buf, {}) end, + ---@class metadata + ---@field title? function|string the title of the note + ---@field description? function|string the description of the note + ---@field authors? function|string the authors of the note + ---@field categories? function|string the categories of the note + ---@field created? function|string a timestamp of creation time for the note + ---@field updated? function|string a timestamp of last time the note was updated + ---@field version? function|string the neorg version + --- Creates the metadata contents from the provided metadata table (defaulting to the configuration's template). ---@param buf number #The buffer to query potential data from - ---@param metadata table #Table of metadata, overrides defaults if present - --- - the table is a key-value table where the key correspondes to a metadata field, - --- and the value is either a string or a function returning a string. + ---@param metadata? metadata #Table of metadata, overrides defaults if present ---@return table #A table of strings that can be directly piped to `nvim_buf_set_lines` create_metadata = function(buf, metadata) local template = module.config.public.template @@ -239,7 +246,7 @@ module.public = { --- Inject the metadata into a buffer ---@param buf number #The number of the buffer to inject the metadata into ---@param force? boolean #Whether to forcefully override existing metadata - ---@param metadata table #Table of metadata data, overrides defaults if present + ---@param metadata? table #Table of metadata data, overrides defaults if present write_metadata = function(buf, force, metadata) local present, data = module.public.is_metadata_present(buf) From eac464a39ca62a51b664f2b57333a7c409bf810e Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Thu, 26 Oct 2023 13:50:11 +0200 Subject: [PATCH 09/16] =?UTF-8?q?chore(formatting):=20=F0=9F=8E=A8=20appli?= =?UTF-8?q?ed=20proper=20formatting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit spaces instead of tabs --- lua/neorg/modules/core/dirman/module.lua | 8 ++++---- .../modules/core/esupports/metagen/module.lua | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lua/neorg/modules/core/dirman/module.lua b/lua/neorg/modules/core/dirman/module.lua index 563d76d22..bc1278efd 100644 --- a/lua/neorg/modules/core/dirman/module.lua +++ b/lua/neorg/modules/core/dirman/module.lua @@ -279,10 +279,10 @@ module.public = { end) end, - ---@class create_file_opts - ---@field no_open? boolean do not open the file after creation? - ---@field force? boolean overwrite file if it already exists? - ---@field metadata? metadata metadata fields, if provided inserts metadata - an empty table uses default values + ---@class create_file_opts + ---@field no_open? boolean do not open the file after creation? + ---@field force? boolean overwrite file if it already exists? + ---@field metadata? metadata metadata fields, if provided inserts metadata - an empty table uses default values --- Takes in a path (can include directories) and creates a .norg file from that path ---@param path string a path to place the .norg file in diff --git a/lua/neorg/modules/core/esupports/metagen/module.lua b/lua/neorg/modules/core/esupports/metagen/module.lua index fa1566d14..5bb616ea9 100644 --- a/lua/neorg/modules/core/esupports/metagen/module.lua +++ b/lua/neorg/modules/core/esupports/metagen/module.lua @@ -192,14 +192,14 @@ module.public = { return module.public.create_metadata(buf, {}) end, - ---@class metadata - ---@field title? function|string the title of the note - ---@field description? function|string the description of the note - ---@field authors? function|string the authors of the note - ---@field categories? function|string the categories of the note - ---@field created? function|string a timestamp of creation time for the note - ---@field updated? function|string a timestamp of last time the note was updated - ---@field version? function|string the neorg version + ---@class metadata + ---@field title? function|string the title of the note + ---@field description? function|string the description of the note + ---@field authors? function|string the authors of the note + ---@field categories? function|string the categories of the note + ---@field created? function|string a timestamp of creation time for the note + ---@field updated? function|string a timestamp of last time the note was updated + ---@field version? function|string the neorg version --- Creates the metadata contents from the provided metadata table (defaulting to the configuration's template). ---@param buf number #The buffer to query potential data from From 680b9092eb90b4bb7b9b8bd1c2562e9c03c0bd9d Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Thu, 26 Oct 2023 14:25:55 +0200 Subject: [PATCH 10/16] =?UTF-8?q?chore(names):=20=F0=9F=92=AC=20updated=20?= =?UTF-8?q?names=20for=20@classes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Updated naming of @class annotations to better align with the naming used in the rest of the project. --- lua/neorg/modules/core/dirman/module.lua | 6 +++--- lua/neorg/modules/core/esupports/metagen/module.lua | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lua/neorg/modules/core/dirman/module.lua b/lua/neorg/modules/core/dirman/module.lua index bc1278efd..8def486d3 100644 --- a/lua/neorg/modules/core/dirman/module.lua +++ b/lua/neorg/modules/core/dirman/module.lua @@ -279,15 +279,15 @@ module.public = { end) end, - ---@class create_file_opts + ---@class core.dirman.create_file_opts ---@field no_open? boolean do not open the file after creation? ---@field force? boolean overwrite file if it already exists? - ---@field metadata? metadata metadata fields, if provided inserts metadata - an empty table uses default values + ---@field metadata? core.esupports.metagen.metadata metadata fields, if provided inserts metadata - an empty table uses default values --- Takes in a path (can include directories) and creates a .norg file from that path ---@param path string a path to place the .norg file in ---@param workspace? string workspace name - ---@param opts? create_file_opts additional options + ---@param opts? core.dirman.create_file_opts additional options create_file = function(path, workspace, opts) opts = opts or {} diff --git a/lua/neorg/modules/core/esupports/metagen/module.lua b/lua/neorg/modules/core/esupports/metagen/module.lua index 5bb616ea9..973f39e5c 100644 --- a/lua/neorg/modules/core/esupports/metagen/module.lua +++ b/lua/neorg/modules/core/esupports/metagen/module.lua @@ -192,7 +192,7 @@ module.public = { return module.public.create_metadata(buf, {}) end, - ---@class metadata + ---@class core.esupports.metagen.metadata ---@field title? function|string the title of the note ---@field description? function|string the description of the note ---@field authors? function|string the authors of the note @@ -203,7 +203,7 @@ module.public = { --- Creates the metadata contents from the provided metadata table (defaulting to the configuration's template). ---@param buf number #The buffer to query potential data from - ---@param metadata? metadata #Table of metadata, overrides defaults if present + ---@param metadata? core.esupports.metagen.metadata #Table of metadata, overrides defaults if present ---@return table #A table of strings that can be directly piped to `nvim_buf_set_lines` create_metadata = function(buf, metadata) local template = module.config.public.template @@ -246,7 +246,7 @@ module.public = { --- Inject the metadata into a buffer ---@param buf number #The number of the buffer to inject the metadata into ---@param force? boolean #Whether to forcefully override existing metadata - ---@param metadata? table #Table of metadata data, overrides defaults if present + ---@param metadata? core.esupports.metagen.metadata #Table of metadata data, overrides defaults if present write_metadata = function(buf, force, metadata) local present, data = module.public.is_metadata_present(buf) From 214db65f664381058ef99ea77b8f182d64358fd3 Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Tue, 7 Nov 2023 12:46:08 +0100 Subject: [PATCH 11/16] =?UTF-8?q?refactor(event):=20=E2=99=BB=EF=B8=8F=20e?= =?UTF-8?q?vent=20on=20file=20creation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dirman now broadcasts an event on file creation that metagen listen to. Metagen then injects desired metadata if present. --- lua/neorg/modules/core/dirman/module.lua | 10 +++---- .../modules/core/esupports/metagen/module.lua | 27 +++++++------------ 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/lua/neorg/modules/core/dirman/module.lua b/lua/neorg/modules/core/dirman/module.lua index 8def486d3..0b5508720 100644 --- a/lua/neorg/modules/core/dirman/module.lua +++ b/lua/neorg/modules/core/dirman/module.lua @@ -331,11 +331,10 @@ module.public = { vim.loop.fs_close(fd) end - local metagen = neorg.modules.get_module("core.esupports.metagen") - if opts.metadata and metagen then - local bufnr = module.public.get_file_bufnr(fname) - metagen.write_metadata(bufnr, true, opts.metadata) - end + local bufnr = module.public.get_file_bufnr(fname) + modules.broadcast_event( + modules.create_event(module, "core.dirman.events.file_created", { buffer = bufnr, opts = opts }) + ) if not opts.no_open then -- Begin editing that newly created file @@ -561,6 +560,7 @@ module.events.defined = { workspace_changed = modules.define_event(module, "workspace_changed"), workspace_added = modules.define_event(module, "workspace_added"), workspace_cache_empty = modules.define_event(module, "workspace_cache_empty"), + file_created = modules.define_event(module, "file_created") } module.events.subscribed = { diff --git a/lua/neorg/modules/core/esupports/metagen/module.lua b/lua/neorg/modules/core/esupports/metagen/module.lua index 973f39e5c..10bf1763a 100644 --- a/lua/neorg/modules/core/esupports/metagen/module.lua +++ b/lua/neorg/modules/core/esupports/metagen/module.lua @@ -185,13 +185,6 @@ module.public = { } end, - --- Constructs the metadata contents from the configuration's template. - ---@param buf number #The buffer to query potential data from - ---@return table #A table of strings that can be directly piped to `nvim_buf_set_lines` - construct_metadata = function(buf) - return module.public.create_metadata(buf, {}) - end, - ---@class core.esupports.metagen.metadata ---@field title? function|string the title of the note ---@field description? function|string the description of the note @@ -205,7 +198,7 @@ module.public = { ---@param buf number #The buffer to query potential data from ---@param metadata? core.esupports.metagen.metadata #Table of metadata, overrides defaults if present ---@return table #A table of strings that can be directly piped to `nvim_buf_set_lines` - create_metadata = function(buf, metadata) + construct_metadata = function(buf, metadata) local template = module.config.public.template local whitespace = type(module.config.public.tab) == "function" and module.config.public.tab() or module.config.public.tab @@ -236,22 +229,15 @@ module.public = { return result end, - --- Inject the metadata into a buffer - ---@param buf number #The number of the buffer to inject the metadata into - ---@param force? boolean #Whether to forcefully override existing metadata - inject_metadata = function(buf, force) - module.public.write_metadata(buf, force, {}) - end, - --- Inject the metadata into a buffer ---@param buf number #The number of the buffer to inject the metadata into ---@param force? boolean #Whether to forcefully override existing metadata ---@param metadata? core.esupports.metagen.metadata #Table of metadata data, overrides defaults if present - write_metadata = function(buf, force, metadata) + inject_metadata = function(buf, force, metadata) local present, data = module.public.is_metadata_present(buf) if force or not present then - local constructed_metadata = module.public.create_metadata(buf, metadata) + local constructed_metadata = module.public.construct_metadata(buf, metadata) vim.api.nvim_buf_set_lines(buf, data.range[1], data.range[2], false, constructed_metadata) end end, @@ -388,6 +374,10 @@ module.on_event = function(event) elseif event.type == "core.neorgcmd.events.update-metadata" then module.public.update_metadata(event.buffer) module.private.buffers[event.buffer] = true + elseif event.type == "core.dirman.events.file_created" then + if event.content.opts.metadata then + module.public.inject_metadata(event.content.buffer, true, event.content.opts.metadata) + end end end @@ -402,6 +392,9 @@ module.events.subscribed = { ["inject-metadata"] = true, ["update-metadata"] = true, }, + ["core.dirman"] = { + ["file_created"] = true, + } } return module From cb81fb1451c14d925fc37b90ed7b878b2a8385ae Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Tue, 7 Nov 2023 12:59:56 +0100 Subject: [PATCH 12/16] chore(format): fixed formatting --- lua/neorg/modules/core/dirman/module.lua | 2 +- lua/neorg/modules/core/esupports/metagen/module.lua | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lua/neorg/modules/core/dirman/module.lua b/lua/neorg/modules/core/dirman/module.lua index 0b5508720..5dad018bb 100644 --- a/lua/neorg/modules/core/dirman/module.lua +++ b/lua/neorg/modules/core/dirman/module.lua @@ -560,7 +560,7 @@ module.events.defined = { workspace_changed = modules.define_event(module, "workspace_changed"), workspace_added = modules.define_event(module, "workspace_added"), workspace_cache_empty = modules.define_event(module, "workspace_cache_empty"), - file_created = modules.define_event(module, "file_created") + file_created = modules.define_event(module, "file_created"), } module.events.subscribed = { diff --git a/lua/neorg/modules/core/esupports/metagen/module.lua b/lua/neorg/modules/core/esupports/metagen/module.lua index 10bf1763a..89ca85453 100644 --- a/lua/neorg/modules/core/esupports/metagen/module.lua +++ b/lua/neorg/modules/core/esupports/metagen/module.lua @@ -374,7 +374,7 @@ module.on_event = function(event) elseif event.type == "core.neorgcmd.events.update-metadata" then module.public.update_metadata(event.buffer) module.private.buffers[event.buffer] = true - elseif event.type == "core.dirman.events.file_created" then + elseif event.type == "core.dirman.events.file_created" then if event.content.opts.metadata then module.public.inject_metadata(event.content.buffer, true, event.content.opts.metadata) end @@ -392,9 +392,9 @@ module.events.subscribed = { ["inject-metadata"] = true, ["update-metadata"] = true, }, - ["core.dirman"] = { - ["file_created"] = true, - } + ["core.dirman"] = { + ["file_created"] = true, + }, } return module From db1d7ebfd98aed4bdfa3d23e5783e337304e64d6 Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Thu, 9 Nov 2023 14:11:24 +0100 Subject: [PATCH 13/16] feat(summary): args for specific categories Neorg generate-workspace-summary now takes possible arguments. Each argument is space separated as a distinct category. Also made a method public to call it from lua instead of a vim command. --- lua/neorg/modules/core/summary/module.lua | 57 ++++++++++++++--------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/lua/neorg/modules/core/summary/module.lua b/lua/neorg/modules/core/summary/module.lua index 3ebf2a7c4..1f17cd135 100644 --- a/lua/neorg/modules/core/summary/module.lua +++ b/lua/neorg/modules/core/summary/module.lua @@ -32,7 +32,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 +76,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 +112,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) then + table.insert(categories[lib.title(category)], { + title = tostring(metadata.title), + norgname = norgname, + description = metadata.description, + }) + end end end) local result = {} @@ -151,24 +157,18 @@ 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 = { + 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 +190,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, + include_categories ) if not generated or vim.tbl_isempty(generated) then @@ -200,7 +201,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 From 4bd07d44d081dbfd06dd35c913a344fa6fb7065a Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Thu, 9 Nov 2023 14:18:25 +0100 Subject: [PATCH 14/16] =?UTF-8?q?docs(summary):=20documenting=20new=20publ?= =?UTF-8?q?ic=20method=20=F0=9F=93=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/neorg/modules/core/summary/module.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lua/neorg/modules/core/summary/module.lua b/lua/neorg/modules/core/summary/module.lua index 1f17cd135..4780427ce 100644 --- a/lua/neorg/modules/core/summary/module.lua +++ b/lua/neorg/modules/core/summary/module.lua @@ -157,11 +157,15 @@ 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?, include_categories: string[]): string[]? + ---@type string|fun(files: string[], ws_root: string, heading_level: number?, include_categories: string[]?): string[]? strategy = "default", } 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 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"] From 48b2ea51078bcd6543f835846a1abaca2947f547 Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Thu, 9 Nov 2023 14:33:54 +0100 Subject: [PATCH 15/16] refactor(lowercase): checks lowercase categories --- lua/neorg/modules/core/summary/module.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lua/neorg/modules/core/summary/module.lua b/lua/neorg/modules/core/summary/module.lua index 4780427ce..1f31b4dc1 100644 --- a/lua/neorg/modules/core/summary/module.lua +++ b/lua/neorg/modules/core/summary/module.lua @@ -112,7 +112,7 @@ module.load = function() if metadata.description == vim.NIL then metadata.description = nil end - if not include_categories or vim.tbl_contains(include_categories, category) then + 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, @@ -164,7 +164,7 @@ module.config.public = { 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 for categories that you wish to include in the summary. + ---@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"] @@ -195,7 +195,7 @@ module.public = { dirman.get_norg_files(dirman.get_current_workspace()[1]) or {}, ws_root, level + 1, - include_categories + vim.tbl_map(string.lower, include_categories or {}) ) if not generated or vim.tbl_isempty(generated) then From c1601652dc8fcc029a0e27539fe1e203bc06260f Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Tue, 14 Nov 2023 20:32:03 +0100 Subject: [PATCH 16/16] =?UTF-8?q?docs(summary):=20explain=20arguments=20to?= =?UTF-8?q?=20cmd=20=F0=9F=93=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added documentation explaining how arguments work with the command `Neorg generate-workspace-summary`. --- lua/neorg/modules/core/summary/module.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lua/neorg/modules/core/summary/module.lua b/lua/neorg/modules/core/summary/module.lua index 1f31b4dc1..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