fix: track note positions with extmarks to survive file edits#18
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
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).session.lua, addsync_positions_from_extmarks()to flush current extmark positions back into the session data before saving. Also callextmarks.untrack()when a note is removed so stale extmarks are cleaned up immediately.ui/display.lua, apply tracked positions before re-rendering virtual text onrefresh_buffer, and register new extmarks for every displayed note. ABufDeleteautocmd clears extmarks when a buffer is unloaded.plugin/tabi.lua, add aBufWritePreautocmd (TabiPositionSync) that callssync_positions_from_extmarks()for the active (or default) session so positions are persisted to disk before each save.init.luaanddisplay.lua, reload the session from disk onBufEnter/BufWinEnterso that positions updated in another buffer are picked up correctly on file reopen.Test plan
vusted test/passes (unit tests fortabi.extmarksand integration test for position sync on file reopen are included in this PR)