Skip to content

fix: track note positions with extmarks to survive file edits#18

Merged
ushmz merged 6 commits into
mainfrom
fix/note-position-tracking
Apr 11, 2026
Merged

fix: track note positions with extmarks to survive file edits#18
ushmz merged 6 commits into
mainfrom
fix/note-position-tracking

Conversation

@ushmz

@ushmz ushmz commented Mar 29, 2026

Copy link
Copy Markdown
Owner

Summary

Note positions (line numbers) were stored as static integers and became stale whenever the user inserted or deleted lines above a note. This PR replaces the static approach with Neovim extmarks, which move automatically as the buffer is edited.

  • Add lua/tabi/extmarks.lua — a new module that maintains a per-buffer map of note IDs to {start_id, end_id} extmark pairs using a dedicated namespace (tabi_positions).
  • In session.lua, add sync_positions_from_extmarks() to flush current extmark positions back into the session data before saving. Also call extmarks.untrack() when a note is removed so stale extmarks are cleaned up immediately.
  • In ui/display.lua, apply tracked positions before re-rendering virtual text on refresh_buffer, and register new extmarks for every displayed note. A BufDelete autocmd clears extmarks when a buffer is unloaded.
  • In plugin/tabi.lua, add a BufWritePre autocmd (TabiPositionSync) that calls sync_positions_from_extmarks() for the active (or default) session so positions are persisted to disk before each save.
  • In init.lua and display.lua, reload the session from disk on BufEnter / BufWinEnter so that positions updated in another buffer are picked up correctly on file reopen.

Test plan

  • vusted test/ passes (unit tests for tabi.extmarks and integration test for position sync on file reopen are included in this PR)
  • Open a file with notes, insert lines above a note, save — verify the note moves to the correct line and the position is persisted after closing and reopening Neovim
  • Remove a note and confirm no stale extmarks remain for that note
  • Verify default-session notes (no explicit session active) also sync correctly

ushmz added 6 commits March 29, 2026 15:22
Memos were stored with fixed line numbers, causing position drift when
lines were inserted or deleted above them. Fix by:

- Add lua/tabi/extmarks.lua: lightweight position-tracking module using
  a dedicated nvim namespace ("tabi_positions"). Creates two extmarks per
  note (start line and end line) that Neovim updates automatically as the
  buffer is edited.
- display.lua refresh_buffer: clear and re-register extmarks whenever the
  display is refreshed, keeping the extmark map in sync with the note list.
  Also add BufDelete handler to free the per-buffer extmark table.
- session.lua sync_positions_from_extmarks: reads current extmark rows and
  writes them back into note.line / note.end_line, then persists the session
  only when at least one position changed.
- plugin/tabi.lua BufWritePre autocmd (TabiPositionSync group): calls
  sync_positions_from_extmarks just before the file hits disk, so saved
  JSON always reflects the editor's current view of the buffer.
…ufWritePre

- refresh_buffer now reads extmark positions before clearing them so
  line shifts caused by edits are reflected when notes are redrawn
- BufEnter callback loads a fresh session from storage to avoid
  displaying stale positions after file edits
- BufWritePre also syncs the default session when show_default_notes
  is enabled and no named session is active
@ushmz ushmz marked this pull request as ready for review April 11, 2026 08:53
@ushmz ushmz merged commit 1b99f0b into main Apr 11, 2026
4 checks passed
@ushmz ushmz deleted the fix/note-position-tracking branch April 11, 2026 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant