From f718b071edd77c0093847561d12f22009afab013 Mon Sep 17 00:00:00 2001 From: Am Laher Date: Thu, 6 Jul 2023 22:17:39 +1200 Subject: [PATCH 1/6] summary as a ranged tag --- lua/neorg/modules/core/summary/module.lua | 33 +++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lua/neorg/modules/core/summary/module.lua b/lua/neorg/modules/core/summary/module.lua index 34fdfc885..6f5680d80 100644 --- a/lua/neorg/modules/core/summary/module.lua +++ b/lua/neorg/modules/core/summary/module.lua @@ -116,16 +116,19 @@ module.load = function() end end) local result = {} - local prefix = string.rep("*", heading_level) + local prefix = string.rep(" ", heading_level) for category, data in vim.spairs(categories) do - table.insert(result, prefix .. " " .. category) - + if #result > 0 then + table.insert(result, "") + end + table.insert(result, prefix .. "#cat " .. category) for _, datapoint in ipairs(data) do table.insert( result, table.concat({ - " - {:$", + prefix, + " {:$", datapoint.norgname, ":}[", neorg.lib.title(datapoint.title), @@ -200,7 +203,27 @@ module.on_event = function(event) return end - vim.api.nvim_buf_set_lines(buffer, event.cursor_position[1], event.cursor_position[1], true, generated) + -- surround with a ranged tag + table.insert(generated, 1, "|group summary") + table.insert(generated, "|end") + + local start_line = event.cursor_position[1] + local end_line = start_line + -- find * replace an existing ranged tag + local node_line_below = ts.get_first_node_on_line(buffer, start_line) + if node_line_below and node_line_below:type() == "_paragraph_break" then + -- allow for a line break between heading and tag + node_line_below = ts.get_first_node_on_line(buffer, start_line+1) + end + if node_line_below and node_line_below:type() == "ranged_tag" then + start_line, _ = node_line_below:start() + end_line, _ = node_line_below:end_() + end_line = end_line + 1 + else + neorg.utils.notify(node_line_below:type()) + end + + vim.api.nvim_buf_set_lines(buffer, start_line, end_line, true, generated) end end From 582f7d9bef08c0a3caa8ca7917d16c7428f21326 Mon Sep 17 00:00:00 2001 From: Am Laher Date: Thu, 6 Jul 2023 23:02:43 +1200 Subject: [PATCH 2/6] tidyup --- lua/neorg/modules/core/summary/module.lua | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lua/neorg/modules/core/summary/module.lua b/lua/neorg/modules/core/summary/module.lua index 6f5680d80..5af008d0e 100644 --- a/lua/neorg/modules/core/summary/module.lua +++ b/lua/neorg/modules/core/summary/module.lua @@ -212,15 +212,13 @@ module.on_event = function(event) -- find * replace an existing ranged tag local node_line_below = ts.get_first_node_on_line(buffer, start_line) if node_line_below and node_line_below:type() == "_paragraph_break" then - -- allow for a line break between heading and tag - node_line_below = ts.get_first_node_on_line(buffer, start_line+1) + -- allow for a line break between heading and tag. Go down one more line. + node_line_below = ts.get_first_node_on_line(buffer, start_line+1) end if node_line_below and node_line_below:type() == "ranged_tag" then start_line, _ = node_line_below:start() end_line, _ = node_line_below:end_() end_line = end_line + 1 - else - neorg.utils.notify(node_line_below:type()) end vim.api.nvim_buf_set_lines(buffer, start_line, end_line, true, generated) From 8f9f6ee33500e410c9672ae241d80f25385c727d Mon Sep 17 00:00:00 2001 From: Am Laher Date: Thu, 6 Jul 2023 23:04:21 +1200 Subject: [PATCH 3/6] typo --- lua/neorg/modules/core/summary/module.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/neorg/modules/core/summary/module.lua b/lua/neorg/modules/core/summary/module.lua index 5af008d0e..38578385a 100644 --- a/lua/neorg/modules/core/summary/module.lua +++ b/lua/neorg/modules/core/summary/module.lua @@ -209,7 +209,7 @@ module.on_event = function(event) local start_line = event.cursor_position[1] local end_line = start_line - -- find * replace an existing ranged tag + -- find & replace an existing ranged tag below this heading local node_line_below = ts.get_first_node_on_line(buffer, start_line) if node_line_below and node_line_below:type() == "_paragraph_break" then -- allow for a line break between heading and tag. Go down one more line. From 6eb16f3df9a7e859c708c2f6348422e67af99a6b Mon Sep 17 00:00:00 2001 From: Am Laher Date: Sat, 8 Jul 2023 20:40:08 +1200 Subject: [PATCH 4/6] adjust ranged-tag support --- lua/neorg/modules/core/summary/module.lua | 108 +++++++++++++++------- 1 file changed, 74 insertions(+), 34 deletions(-) diff --git a/lua/neorg/modules/core/summary/module.lua b/lua/neorg/modules/core/summary/module.lua index 38578385a..05a28e3ee 100644 --- a/lua/neorg/modules/core/summary/module.lua +++ b/lua/neorg/modules/core/summary/module.lua @@ -119,16 +119,15 @@ module.load = function() local prefix = string.rep(" ", heading_level) for category, data in vim.spairs(categories) do - if #result > 0 then - table.insert(result, "") - end - table.insert(result, prefix .. "#cat " .. category) + table.insert(result, prefix .. "#category " .. category) for _, datapoint in ipairs(data) do table.insert( result, + -- TODO each item _SHOULD BE_ a list item, + -- but for now, the parser barfs on list items (or headings) inside a ranged tag table.concat({ prefix, - " {:$", + "_ {:$", datapoint.norgname, ":}[", neorg.lib.title(datapoint.title), @@ -166,21 +165,70 @@ module.events.subscribed = { }, } + +module.private = { + find_existing_summary = function(buffer, root) + local query_str = neorg.lib.match("all")({ + _ = [[ + (ranged_tag + name: (tag_name) @_tag_name + (#eq? @_tag_name "group") + ) + ]], + }) + + local query = neorg.utils.ts_parse_query("norg", query_str) + for _, node in query:iter_captures(root, buffer, 0, -1) do + -- node is the tag_name. we need its parent node + local ranged_tag = node:parent() + for child in ranged_tag:iter_children() do + if child:type() == "tag_parameters" then + local _, param = child:iter_children() + local text = module.required["core.integrations.treesitter"].get_node_text(param) + if text == "summary" then + return ranged_tag + end + end + end + end + return nil + end +} + module.on_event = function(event) if event.type == "core.neorgcmd.events.summary.summarize" then 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) - if not node_at_cursor or not node_at_cursor:type():match("^heading%d$") then - neorg.utils.notify( - "No heading under cursor! Please move your cursor under the heading you'd like to generate the summary under." - ) - return + local start_line = event.cursor_position[1] + local end_line = start_line + local existing = module.private.find_existing_summary(buffer, ts.get_document_root(buffer)) + local heading_level = 0 + if existing ~= nil then + -- neorg.utils.notify("matched: " .. existing.type()) + start_line, _ = existing:start() + end_line, _ = existing:end_() + end_line = end_line + 1 + local level = tonumber(string.sub(existing:type(), -1)) + if level ~= nil then + heading_level = level + end + else + local node_at_cursor = ts.get_first_node_on_line(buffer, event.cursor_position[1] - 1) + if not node_at_cursor or not node_at_cursor:type():match("^heading%d$") then + neorg.utils.notify( + "No heading under cursor! Please move your cursor under the heading you'd like to generate the summary under." + ) + return + end + -- heading level of 'node_at_cursor' (summary headings should be one level deeper) + local level = tonumber(string.sub(node_at_cursor:type(), -1)) + if level ~= nil then + heading_level = level + end end - -- heading level of 'node_at_cursor' (summary headings should be one level deeper) - local level = tonumber(string.sub(node_at_cursor:type(), -1)) + local dirman = neorg.modules.get_module("core.dirman") @@ -189,11 +237,13 @@ module.on_event = function(event) return end - local ws_root = dirman.get_current_workspace()[2] + local current_workspace = dirman.get_current_workspace() + local ws_name = current_workspace[1] + local ws_root = current_workspace[2] local generated = module.config.public.strategy( - dirman.get_norg_files(dirman.get_current_workspace()[1]) or {}, + dirman.get_norg_files(ws_name) or {}, ws_root, - level + 1 + heading_level + 2 ) if not generated or vim.tbl_isempty(generated) then @@ -202,26 +252,16 @@ module.on_event = function(event) ) return end - - -- surround with a ranged tag - table.insert(generated, 1, "|group summary") - table.insert(generated, "|end") - - local start_line = event.cursor_position[1] - local end_line = start_line - -- find & replace an existing ranged tag below this heading - local node_line_below = ts.get_first_node_on_line(buffer, start_line) - if node_line_below and node_line_below:type() == "_paragraph_break" then - -- allow for a line break between heading and tag. Go down one more line. - node_line_below = ts.get_first_node_on_line(buffer, start_line+1) - end - if node_line_below and node_line_below:type() == "ranged_tag" then - start_line, _ = node_line_below:start() - end_line, _ = node_line_below:end_() - end_line = end_line + 1 + -- use a tag to contain the result + local content = { + string.rep(" ", heading_level) .. "|group summary " .. ws_name, + } + for _, gen in ipairs(generated) do + table.insert(content, gen) end + table.insert(content, string.rep(" ", heading_level) .. "|end") - vim.api.nvim_buf_set_lines(buffer, start_line, end_line, true, generated) + vim.api.nvim_buf_set_lines(buffer, start_line, end_line, true, content) end end From 3b680841592f0d56a5f9a996798048c4be33261a Mon Sep 17 00:00:00 2001 From: Am Laher Date: Sat, 8 Jul 2023 20:50:55 +1200 Subject: [PATCH 5/6] summary: stylua --- lua/neorg/modules/core/summary/module.lua | 96 +++++++++++------------ 1 file changed, 45 insertions(+), 51 deletions(-) diff --git a/lua/neorg/modules/core/summary/module.lua b/lua/neorg/modules/core/summary/module.lua index 05a28e3ee..386a18b79 100644 --- a/lua/neorg/modules/core/summary/module.lua +++ b/lua/neorg/modules/core/summary/module.lua @@ -123,8 +123,9 @@ module.load = function() for _, datapoint in ipairs(data) do table.insert( result, - -- TODO each item _SHOULD BE_ a list item, - -- but for now, the parser barfs on list items (or headings) inside a ranged tag + -- TODO each line _SHOULD BE_ a list item, + -- but due to an apparent bug, treesitter currently barfs on list items (or headings) inside a ranged tag. + -- :InspectTree shows an "ERROR" instead of the ranged_tag node. table.concat({ prefix, "_ {:$", @@ -165,34 +166,33 @@ module.events.subscribed = { }, } - module.private = { - find_existing_summary = function(buffer, root) - local query_str = neorg.lib.match("all")({ - _ = [[ + find_existing_summary = function(buffer, root) + local query_str = neorg.lib.match("all")({ + _ = [[ (ranged_tag name: (tag_name) @_tag_name (#eq? @_tag_name "group") ) ]], - }) - - local query = neorg.utils.ts_parse_query("norg", query_str) - for _, node in query:iter_captures(root, buffer, 0, -1) do - -- node is the tag_name. we need its parent node - local ranged_tag = node:parent() - for child in ranged_tag:iter_children() do - if child:type() == "tag_parameters" then - local _, param = child:iter_children() - local text = module.required["core.integrations.treesitter"].get_node_text(param) - if text == "summary" then - return ranged_tag - end + }) + + local query = neorg.utils.ts_parse_query("norg", query_str) + for _, node in query:iter_captures(root, buffer, 0, -1) do + -- node is the tag_name. we need its parent node + local ranged_tag = node:parent() + for child in ranged_tag:iter_children() do + if child:type() == "tag_parameters" then + local _, param = child:iter_children() + local text = module.required["core.integrations.treesitter"].get_node_text(param) + if text == "summary" then + return ranged_tag + end + end end end - end - return nil - end + return nil + end, } module.on_event = function(event) @@ -200,36 +200,33 @@ module.on_event = function(event) local ts = module.required["core.integrations.treesitter"] local buffer = event.buffer - local start_line = event.cursor_position[1] local end_line = start_line local existing = module.private.find_existing_summary(buffer, ts.get_document_root(buffer)) local heading_level = 0 if existing ~= nil then - -- neorg.utils.notify("matched: " .. existing.type()) - start_line, _ = existing:start() - end_line, _ = existing:end_() - end_line = end_line + 1 - local level = tonumber(string.sub(existing:type(), -1)) - if level ~= nil then - heading_level = level - end + start_line, _ = existing:start() + end_line, _ = existing:end_() + end_line = end_line + 1 + local level = tonumber(string.sub(existing:type(), -1)) + if level ~= nil then + heading_level = level + end else - local node_at_cursor = ts.get_first_node_on_line(buffer, event.cursor_position[1] - 1) - if not node_at_cursor or not node_at_cursor:type():match("^heading%d$") then - neorg.utils.notify( - "No heading under cursor! Please move your cursor under the heading you'd like to generate the summary under." - ) - return - end - -- heading level of 'node_at_cursor' (summary headings should be one level deeper) - local level = tonumber(string.sub(node_at_cursor:type(), -1)) - if level ~= nil then - heading_level = level - end + local node_at_cursor = ts.get_first_node_on_line(buffer, event.cursor_position[1] - 1) + if not node_at_cursor or not node_at_cursor:type():match("^heading%d$") then + neorg.utils.notify( + "No heading under cursor! Please move your cursor under the heading you'd like to generate the summary under." + ) + return + end + -- heading level of 'node_at_cursor' (summary headings should be one level deeper) + local level = tonumber(string.sub(node_at_cursor:type(), -1)) + if level ~= nil then + heading_level = level + end end - local dirman = neorg.modules.get_module("core.dirman") if not dirman then @@ -240,11 +237,8 @@ module.on_event = function(event) local current_workspace = dirman.get_current_workspace() local ws_name = current_workspace[1] local ws_root = current_workspace[2] - local generated = module.config.public.strategy( - dirman.get_norg_files(ws_name) or {}, - ws_root, - heading_level + 2 - ) + local generated = + module.config.public.strategy(dirman.get_norg_files(ws_name) or {}, ws_root, heading_level + 2) if not generated or vim.tbl_isempty(generated) then neorg.utils.notify( @@ -254,10 +248,10 @@ module.on_event = function(event) end -- use a tag to contain the result local content = { - string.rep(" ", heading_level) .. "|group summary " .. ws_name, + string.rep(" ", heading_level) .. "|group summary " .. ws_name, } for _, gen in ipairs(generated) do - table.insert(content, gen) + table.insert(content, gen) end table.insert(content, string.rep(" ", heading_level) .. "|end") From 3748ecc04975ebad426c2e800a5c8c7976b0989c Mon Sep 17 00:00:00 2001 From: Am Laher Date: Sat, 8 Jul 2023 21:38:11 +1200 Subject: [PATCH 6/6] fix node:child(0) --- lua/neorg/modules/core/summary/module.lua | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lua/neorg/modules/core/summary/module.lua b/lua/neorg/modules/core/summary/module.lua index 386a18b79..0da819428 100644 --- a/lua/neorg/modules/core/summary/module.lua +++ b/lua/neorg/modules/core/summary/module.lua @@ -183,10 +183,13 @@ module.private = { local ranged_tag = node:parent() for child in ranged_tag:iter_children() do if child:type() == "tag_parameters" then - local _, param = child:iter_children() - local text = module.required["core.integrations.treesitter"].get_node_text(param) - if text == "summary" then - return ranged_tag + local param = child:child(0) + if param ~= nil then + --for param in child:iter_children() do + local text = module.required["core.integrations.treesitter"].get_node_text(param) + if text == "summary" then + return ranged_tag + end end end end