Getting the the element that the cursor is currently in


#1

Hi all,
I’ve looked through atom’s API and for some reason I can’t figure out how to get the element that the cursor is currently inside of.

What I want to do is get that element so that I can create a tooltip on it.

Any help? (much appreciated)

I’d like to have a little pop-up occur when the user places the cursor over text such as “function.foo” or “function.bar”. The text in the pop-up would describe the function.


#2

It sounds like you want to get the word the cursor is inside.


#3

@DamnedScholar

Awh yes, that’s a useful function!

The word is inside of an element though, isn’t it? Like a span or a div? I’d like to get that element… or am I mistaken about how the editor works…?


#4

Important words that have been highlighted by the grammar are inside span tags, yes. I’m not sure of a way to directly get that element, though it must be possible. There’s an editor:log-cursor-scope command, which will display all scopes at the cursor down to the specific span it’s inside.

Edit: That command uses cursor.getScopeDescriptor. Following the breadcrumbs, I’ve found that it pulls the scope descriptor not from the element itself, but from the tokenized buffer, and I wouldn’t have the first clue if that can be reverse-engineered to grab the element.

I think what you ultimately want is an overlay decoration. You don’t need to get the element the text is in if you can find the position of the beginning and end of that word as well as its contents.


#5

@DamnedScholar you’re helping me out a lot, thank you. I didn’t know that it’s the grammar that creates the span tags. This teaches me that the words that I’d like to give a pop-up description won’t be in their own span then.

Overlay decoration sounds close to what I’m aiming for, but not quite. I like that the decoration persists until the cursor leaves the given marker, and I want my pop-up div to persist that long as well, but I don’t see how the decoration can act as a pop-up div.

I looked at tooltip manager but the function atom.tooltips.add takes a DOM node for some reason. I’d like to have something like this only it takes a marker.


#6

An overlay decoration will appear at the beginning or end of the range you give it, shifted up and to the left or down and to the right. Popups (like the autocomplete-plus popup) are what it does.


#7

@DamnedScholar
Awesome! Thank you so much for walking me through this.

Now that I have a programming strategy, I can play with the API and take it from there. Would you like me to send you a message when the package is complete (sometime within the next two weeks)?


#8

You can update this thread.


#9

Hi @DamnedScholar,
Here is the package that I worked onhttps://atom.io/packages/autocomplete-oracle. (I updated it from version .5 to version 1.4).

Thanks for your help as it allowed me to implement a few of its most important features :slight_smile:


#10

Glad that I could help you find what you needed. :slight_smile: