How can I detect when the user hovers over or places the cursor on a text range?

I'm trying to write a plugin which mimics the UI seen on this page. When the user hovers over an expression, that expression is highlighted and the value is shown somewhere on the screen.

Ideally I'd like to be able to attach some sort of 'onHoverListener' to a TextRange. I'm guessing that's not possible.

Alternatively, I could listen to all mouse movements and implement the above manually, if I can translate TextTange-style coordinates into (x,y) screen coordinates that the mouse listener understands.

A last resort would be to override the usual Ctrl+Hover or Shift+Hover behaviour, either by replacing the tooltip with my own text or just by being able to listen to the event.

I'd also like to know when the user places the text cursor inside a text range, e.g. by clicking on it. This is for a different purpose, but I could also use it if the hovering stuff above fails.

To make matters more complicated, we're talking about text ranges that can be arbitrarily nested in a complicated structure, so I need to be able to trigger the action only for the smallest, innermost text range where the pointer/cursor is. And the relevant text ranges might cover all the text visible on the screen, so I'd like to avoid a solution that underlines text.

Is any of this possible?

0
3 comments

That's possible - e.g. Java debugger shows the values of expressions on mouse hover in editor.

There's no API to attach a 'hover listener' to a TextRange. You can add mouse movement listener to an editor (Editor.addEditorMouseMotionListener) or to all editors (EditorFactory.getEventMulticaster().addEditorMouseMotionListener). Mouse pointer coordinates can be converted to offset using methods in Editor (xyToLogicalPosition, logicalPositionToOffset).

In a similar way, you can listen to caret position changes in editor (Editor.getCaretModel().addCaretListener(), or via multi-caster).

2

The response above says you can do the following.

Editor.addEditorMouseMotionListener(...)

There is no static in Editor that provides the above. Is the following more descriptive?

Editor editor = ....
editor.addEditorMouseMotionListener(..)

 

0
Avatar
Permanently deleted user

Rinaldo DiGiorgio I think that was just Dmitry's way of conveniently referring to the method. I do the same thing, it's normal.

By the way I did follow the suggestion above and it was helpful, you can find the code here: https://github.com/alexmojaki/birdseye-pycharm/blob/master/src/com/github/alexmojaki/birdseye/pycharm/HoverListener.java#L17-L59

So thanks Dmitry!

0

Please sign in to leave a comment.