fix: Prevent overlay duplication by making overlay list permanent-local - #5
Conversation
39e8825 to
0d38ede
Compare
|
I thought you originally used remove-overlays? Why the current changes? 😮 |
|
Hi @jcs090218, Keeping track of the overlays might be slightly more performant, because we know exactly what to remove, but breaks if you modify the variable manually in a different way. The original fix works in any case because there is no variable, thus it is more reliable. I pushed the change because I thought you would prefer tracking it. But I can revert it quickly and push it for you. Just let me know which one do you prefer. |
|
Hi @jcs090218. A quick reminder on this. Let me know how to proceed here. :) |
|
|
||
| (defvar-local cognitive-complexity--ovs nil | ||
| "List of overlays.") | ||
| (put 'cognitive-complexity--ovs 'permanent-local t) |
There was a problem hiding this comment.
I'm no very familiar to the property permanent-local. How does it work? 🤔 I thought defvar-local would do everything for us.
There was a problem hiding this comment.
Basically, defvar-local makes a variable unique to a buffer, but it doesn't make it "permanent". Every time you switch modes or revert a buffer (M-x revert-buffer), Emacs does a "clean sweep" and deletes those local variables so old settings do not leak into new modes. The issue here is that while the variable disappears, the overlays stay stuck to the text. The variable just has references to the overlays, and only this reference is deleted, not the overlays themselves. When the minor mode starts back up, it sees a blank variable, thinks "there are no overlays yet", and creates a whole new set right on top of the old ones. If you refresh the buffer multiple times, you end up with multiple overlays for the same thing.
That is where the permanent-local comes in: it tells Emacs, "do not delete this specific variable during the cleanup". That way, the variable survives the refresh, the minor mode sees the overlays are already there, and it won't keep stacking new ones every time.
|
Thank you for fixing this! :D |
The fix removes the internal overlay tracking variable and instead uses
remove-overlayswith the'cognitive-complexityproperty to reliably clean up all overlays, ensuring no duplicates remain after buffer state changes.Fixes #4.