What's under the cursor?


#1

Is there an easy way to determine the word or a regular expression defined area that occurs directly under the cursor when a context menu is invoked without having to have the contents actually be selected?


#2

To my knowledge, there isn’t a way to determine what is under the mouse cursor when the context menu is invoked. Ref:


#3

Hmm, alternatively, is there a way to map mouse X/Y coordinates to col/row locations within the editor and are the mouse events registered when the right click for a contextual menu occurs?


#4

There was a way someone figured out at one time but that was a couple UI architecture revisions ago, so I don’t know if it would still work. There isn’t built-in support in the API for this.

This I believe does happen, but that’s not something I’m familiar with how to intercept. Perhaps @mark_hahn or @abe will have more information?

Generally, when an impasse like this is reached it is generally because someone has chosen an implementation that isn’t well supported (or not supported) by the API. What I’ve found to be helpful is if you can give a better idea of what it is you’re trying to accomplish, the end goal in other words, I might be able to help better.


#5

No easy way. Here is a half-baked idea for doing it …

  • Listen for a mouseover event on the line elements selected by atom-text-editor:host .line.

  • Parse the line’s text into all the possible things you are interested in for a right-click.

  • For each potential right-click target put a SPAN around it and then see if that span has the x and y to be under the cursor; where “put a SPAN around it” means replacing the section of text with '<span>' + section + '</span>' in the DOM.

I haven’t actually tried this. A number of stack overflow answers describe the SPAN trick. Let me know if you need more description of my suggestion.

Edit: Baking the idea a little longer, I would just listen for the right click and then scan every line with the SPAN trick and check them all (only visible lines are in the DOM). In other words instead of checking just one line on every mouse movement you check all lines on every right-click.

Edit2: Virtually anything can be done in Atom. You can always work directly on the DOM like I did above. You can even monkey with core code. I’ve hooked into core code much deeper than the API and had good luck.


#6

Maybe it would be easy (easier) to do it in two steps. First, define a grammar for syntax highlighting. Then the editor will put <span>...</span> around the pieces of text that you’re interested in, with a CSS class derived from the syntax highlighting. Then you can listen for mouse clicks and go by the class.