Skip to content

Commit 90f8f52

Browse files
committed
adjust ranged-tag support
1 parent 4565c90 commit 90f8f52

File tree

1 file changed

+74
-34
lines changed

1 file changed

+74
-34
lines changed

lua/neorg/modules/core/summary/module.lua

+74-34
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,15 @@ module.load = function()
119119
local prefix = string.rep(" ", heading_level)
120120

121121
for category, data in vim.spairs(categories) do
122-
if #result > 0 then
123-
table.insert(result, "")
124-
end
125-
table.insert(result, prefix .. "#cat " .. category)
122+
table.insert(result, prefix .. "#category " .. category)
126123
for _, datapoint in ipairs(data) do
127124
table.insert(
128125
result,
126+
-- TODO each item _SHOULD BE_ a list item,
127+
-- but for now, the parser barfs on list items (or headings) inside a ranged tag
129128
table.concat({
130129
prefix,
131-
" {:$",
130+
"_ {:$",
132131
datapoint.norgname,
133132
":}[",
134133
neorg.lib.title(datapoint.title),
@@ -166,21 +165,70 @@ module.events.subscribed = {
166165
},
167166
}
168167

168+
169+
module.private = {
170+
find_existing_summary = function(buffer, root)
171+
local query_str = neorg.lib.match("all")({
172+
_ = [[
173+
(ranged_tag
174+
name: (tag_name) @_tag_name
175+
(#eq? @_tag_name "group")
176+
)
177+
]],
178+
})
179+
180+
local query = neorg.utils.ts_parse_query("norg", query_str)
181+
for _, node in query:iter_captures(root, buffer, 0, -1) do
182+
-- node is the tag_name. we need its parent node
183+
local ranged_tag = node:parent()
184+
for child in ranged_tag:iter_children() do
185+
if child:type() == "tag_parameters" then
186+
local _, param = child:iter_children()
187+
local text = module.required["core.integrations.treesitter"].get_node_text(param)
188+
if text == "summary" then
189+
return ranged_tag
190+
end
191+
end
192+
end
193+
end
194+
return nil
195+
end
196+
}
197+
169198
module.on_event = function(event)
170199
if event.type == "core.neorgcmd.events.summary.summarize" then
171200
local ts = module.required["core.integrations.treesitter"]
172201
local buffer = event.buffer
173202

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

176-
if not node_at_cursor or not node_at_cursor:type():match("^heading%d$") then
177-
neorg.utils.notify(
178-
"No heading under cursor! Please move your cursor under the heading you'd like to generate the summary under."
179-
)
180-
return
204+
local start_line = event.cursor_position[1]
205+
local end_line = start_line
206+
local existing = module.private.find_existing_summary(buffer, ts.get_document_root(buffer))
207+
local heading_level = 0
208+
if existing ~= nil then
209+
-- neorg.utils.notify("matched: " .. existing.type())
210+
start_line, _ = existing:start()
211+
end_line, _ = existing:end_()
212+
end_line = end_line + 1
213+
local level = tonumber(string.sub(existing:type(), -1))
214+
if level ~= nil then
215+
heading_level = level
216+
end
217+
else
218+
local node_at_cursor = ts.get_first_node_on_line(buffer, event.cursor_position[1] - 1)
219+
if not node_at_cursor or not node_at_cursor:type():match("^heading%d$") then
220+
neorg.utils.notify(
221+
"No heading under cursor! Please move your cursor under the heading you'd like to generate the summary under."
222+
)
223+
return
224+
end
225+
-- heading level of 'node_at_cursor' (summary headings should be one level deeper)
226+
local level = tonumber(string.sub(node_at_cursor:type(), -1))
227+
if level ~= nil then
228+
heading_level = level
229+
end
181230
end
182-
-- heading level of 'node_at_cursor' (summary headings should be one level deeper)
183-
local level = tonumber(string.sub(node_at_cursor:type(), -1))
231+
184232

185233
local dirman = neorg.modules.get_module("core.dirman")
186234

@@ -189,11 +237,13 @@ module.on_event = function(event)
189237
return
190238
end
191239

192-
local ws_root = dirman.get_current_workspace()[2]
240+
local current_workspace = dirman.get_current_workspace()
241+
local ws_name = current_workspace[1]
242+
local ws_root = current_workspace[2]
193243
local generated = module.config.public.strategy(
194-
dirman.get_norg_files(dirman.get_current_workspace()[1]) or {},
244+
dirman.get_norg_files(ws_name) or {},
195245
ws_root,
196-
level + 1
246+
heading_level + 2
197247
)
198248

199249
if not generated or vim.tbl_isempty(generated) then
@@ -202,26 +252,16 @@ module.on_event = function(event)
202252
)
203253
return
204254
end
205-
206-
-- surround with a ranged tag
207-
table.insert(generated, 1, "|group summary")
208-
table.insert(generated, "|end")
209-
210-
local start_line = event.cursor_position[1]
211-
local end_line = start_line
212-
-- find & replace an existing ranged tag below this heading
213-
local node_line_below = ts.get_first_node_on_line(buffer, start_line)
214-
if node_line_below and node_line_below:type() == "_paragraph_break" then
215-
-- allow for a line break between heading and tag. Go down one more line.
216-
node_line_below = ts.get_first_node_on_line(buffer, start_line+1)
217-
end
218-
if node_line_below and node_line_below:type() == "ranged_tag" then
219-
start_line, _ = node_line_below:start()
220-
end_line, _ = node_line_below:end_()
221-
end_line = end_line + 1
255+
-- use a tag to contain the result
256+
local content = {
257+
string.rep(" ", heading_level) .. "|group summary " .. ws_name,
258+
}
259+
for _, gen in ipairs(generated) do
260+
table.insert(content, gen)
222261
end
262+
table.insert(content, string.rep(" ", heading_level) .. "|end")
223263

224-
vim.api.nvim_buf_set_lines(buffer, start_line, end_line, true, generated)
264+
vim.api.nvim_buf_set_lines(buffer, start_line, end_line, true, content)
225265
end
226266
end
227267

0 commit comments

Comments
 (0)