Skip to content

Commit

Permalink
Merge branch 'main' into patch-toc-sync-cursorline
Browse files Browse the repository at this point in the history
  • Loading branch information
champignoom committed Dec 3, 2023
2 parents 6d7fd13 + bf3daa2 commit a29b9c7
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lua/neorg/modules/core/qol/toc/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ local function upper_bound(array, v)
local r = #array

while l <= r do
local m = math.floor((l+r)/2)
local m = math.floor((l + r) / 2)
if v >= array[m] then
l = m + 1
else
Expand Down Expand Up @@ -172,7 +172,7 @@ module.public = {
end,

update_cursor = function(_original_buffer, original_window, ui_buffer, ui_window)
local current_row_1b = vim.fn.line('.', original_window)
local current_row_1b = vim.fn.line(".", original_window)
if last_row_of_norg_win[original_window] == current_row_1b then
return
end
Expand All @@ -181,10 +181,11 @@ module.public = {
local start_lines = start_lines_of_toc_buf[ui_buffer]
assert(start_lines)

local current_toc_item_idx = upper_bound(start_lines, current_row_1b-1) - 1
local current_toc_row = (current_toc_item_idx == 0
and math.max(1, start_lines.offset)
or current_toc_item_idx + start_lines.offset)
local current_toc_item_idx = upper_bound(start_lines, current_row_1b - 1) - 1
local current_toc_row = (
current_toc_item_idx == 0 and math.max(1, start_lines.offset)
or current_toc_item_idx + start_lines.offset
)
vim.api.nvim_win_set_cursor(ui_window, { current_toc_row, 0 })
end,

Expand Down Expand Up @@ -232,6 +233,7 @@ module.public = {
else
prefix = nil
end
title = nil
elseif capture == "title" then
title = node
elseif capture == "cancelled" then
Expand Down Expand Up @@ -387,7 +389,7 @@ module.on_event = function(event)
toc_title = vim.split(module.public.parse_toc_macro(previous_buffer) or "Table of Contents", "\n")
module.public.update_toc(namespace, toc_title, previous_buffer, previous_window, buffer, window)
if module.config.public.sync_cursorline then
last_row_of_norg_win[previous_window] = nil -- invalidate cursor cache
last_row_of_norg_win[previous_window] = nil -- invalidate cursor cache
module.public.update_cursor(previous_buffer, previous_window, buffer, window)
end
end),
Expand All @@ -414,7 +416,7 @@ module.on_event = function(event)

-- Sync cursor: ToC -> content
if module.config.public.sync_cursorline then
vim.api.nvim_create_autocmd({"CursorMoved", "CursorMovedI"}, {
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
buffer = buffer,
callback = function(ev)
if not previous_buffer then
Expand All @@ -436,7 +438,7 @@ module.on_event = function(event)
})

-- Sync cursor: content -> ToC
vim.api.nvim_create_autocmd({"CursorMoved", "CursorMovedI"}, {
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
pattern = "*.norg",
callback = unlisten_if_closed(buffer, function(ev)
if ev.buf ~= previous_buffer then
Expand Down

0 comments on commit a29b9c7

Please sign in to comment.