Fastest way to detect when cursor is within a marker ranges?


What’s the fastest way to detect when a cursor has entered a marker range?

Right now, for each editorView, I’m listening for the ‘cursor:moved’ event, and when I receive it I iterate through all available markers to see if its one we’re interested in. If so, perform some logic; if not, continue on.

It works but I’m not sure about how performant it’s going to be in the long run. Is there a more direct way of asking Atom “What markers is the cursor(s) on?”


Can you add a link to the file or code snippet?


If nothing else, create an optimized data structure to make this fast. If you keep them sorted you could do a very fast binary search.


Discourse won’t let me link to Github. So… radicaled/dart-tools/lib/views/ / line 15.


I was kinda hoping to avoid tracking a bunch of instances, then having to invalidate them / etc whenever they change or are removed.


If you just put the link from the address bar onto its own line, you should end up with something like this:

@benogle, as the Decorations expert … do you have a better method for doing what @radicaled wants?


This probably won’t help, but I was trying to do something similar recently. I was trying to match up selections and cursors. I found out that selections have an instance variable of the cursor (so I could use cursor = selection.cursor).

Now, you use isDartMarker to let you know that the marker was from your package. Could you save a reference to the cursor in the same manner?


I tried that, and it worked in the preview, but Discourse told me that new users could only post 2 links, even though I only had one. Spam countermeasures gone awry, I guess.

I could probably save a reference to the cursor, but I’d still have to traverse through all the markers every ‘cursor:moved’ event to locate markers within that cursor’s range. Unless I’m misunderstanding you?


Oh right. Maybe the other way around, then. Just spit-balling, don’t have a good answer.


Upgrade the marker class to maintain it’s own list and also provide a match test. This would also be useful for other packages. Your package code would be trivial.

The match method could be brute-force simple at first and improved later with no breakage. I assume this marker-class-based implementation would be easier than the one you were considering implementing in your package. You could even talk someone else into contributing it for you. :smile:

It’s a win-win.


Why not simply using the buffer.markers property?
You’ll get a MarkerManager that contains all the markers created on a buffer so that you don’t have to manager them yourself. And since a cursor is bound to an editor you probably don’t wat to scan every markers, even those that aren’t related to the current editor.


So you can find based on a range / position. I do the same thing in find-and-replace.


Event handler:

In FindModel:

The crux is that you can search for markers based on a set of properties. So you might give your markers a class of dart-marker, then you can use Editor::findMarkers and search for {class: 'dart-marker', containsBufferPosition: myPosition}.

Check the docs:

For performance, this should be the fastest way. It uses an IntervalSkipList under the hood to find markers. And also, that will probably get faster in the future.

Another thing, if you dont need this searching for markers to be instant, you can debounce after some interval so you arent running a bunch of code in some synchronous render path, and slowing down each cursor movement.

And as far as the event, nearly all view events will be going away very soon. You should probably use the cursor-moved event on editor. Apologies for the lack of event docs in the api docs. Those will be getting waaaaay better within a week or so.

Hope this helps.


Thank you! That is literally exactly what I was hoping to find in Atom.