Maintaining range options outside of Kakoune

This doesn’t seem to be as straight forward as I had hoped. I’m wondering if anybody else has tried to tackle this problem, or have insights on how to do it.

Firstly the background, I have notes that are separate markdown files that I would like to ‘link’ to specific ranges in code files I’m working with. I have most of the framework for this done in a plugin. The notes have "tags’ in them that tell the plugin which file and range the note links to. The plugin then adds a highlighter to that range in the code file buffer so I can see I have a note there. I can then have the plugin display the note in an info box or open it in a new buffer.

Nextly the problem, the ranges in Kakoune are auto-updated as editing happens. I need to sync these updates back to the notes when the buffer is written. I’ve tried maintaining a str-list that’s sort of a parallel array to the active range so that I can see what the original range spec was before the edits. But the range is sorted and so the arrays de-sync when the specs move around each other or a new spec is added to the middle of the range. I know ranges keep a timestamp to play subsequent edits, but can you go back in time somehow to get the spec at a previous timestamp.

Could you perhaps store the ranges in two options? Pre-sync option and current ranges option? Then you update-option the current ranges option and once you’re ready to sync do the lookup with ranges in pre-sync option, then write the value at same offset from the current ranges option.

Just have to maintain that both options have same amount of ranges and that you don’t change the order of the ranges for the mapping to be valid. So any time you want to add a new note or remove one, you should probably first sync the ranges to notes, then do both options.

That’s sort of what I was trying to do. The issue was that the active range was re-arranging it’s elements as the buffer was edited. I think I came up with a solution. Since ranges have timestamps, I could create a new range option with the original timestamp and Kakoune will auto-update it to the current timestamp, then I’ll just need to read it back out to update the tags. Getting information out of Kakoune after already entering a shell seems to be a hurdle though.

Wait, just realised that you can store mapping info in the range “value” field to the right of | symbol. Then you can have two options - one for highlighters and other for finding the matching node and keep updating them both.

Could perhaps store the old range there, so you can find it in notes.

I like this idea, as it skips the need to create and read out a new option every time. I’ll have to try this when I get back to the office.

That worked like a charm. Keeping the original spec in the value field of the range option easily allows a mapping to update the ‘tagged’ notes.

1 Like