Search code examples
eclipse-plugincaret

How do I register a caret listener with an Eclipse editor?


Disclaimer

When I wrote this question I was wrong about the behavior of the SelectionService...the SelectionService does normally notify about cursor movement, but not for every movement either: having your IDE rapidly flash through showing detailed information as you scroll your cursor up 10 lines to get to what you're interested in is both useless, as the information is displayed for much too short a period to be useful, highly distracting, and perhaps even dangerous to those with potential epilepsy.

For this reason, the SelectionService responds only after the cursor has been left in the same place long enough that it makes sense for Eclipse views to update in response to the new context.

I've also heard that there was one Eclipse version some years back in which the SelectionService was bugged, so I'm not sure whether I posted this because I happend to first develop Eclipse plugins in exactly that version, or because I didn't understand why the SelectionService works the way it does, but in the end, using the Eclispe SelectionService is all I've ever needed, or would recommend to others.

The original question

I'm developing an Eclipse plugin that needs to respond to the current cursor position in an Eclipse editor.

From the tests I've done, it appears that using the SelectionService only tells you when a non-empty selection is made in an editor, not when the cursor is moved.

I've found one site describing how to track selections by registering for mouse and keyboard events, but that seems like a bit of a hack.

I've also seen someone asking on StackOverflow about alternatives to the CaretListener interface for tracking cursor movement in an Eclipse editor, and describing a way to register with an editor's text viewer (rather than the global SelectionService) to get caret movement updates, but they suggest that it's a less-effecient method that would be better replaced with the CaretListener interface in more recent versions of Eclipse.

That last might be an option, but it sounds like using the CaretListener would be the preferred approach...but if it is, how can it be done?


Solution

  • If you can live without supporting older Eclipse versions (3.4 and below) then CaretListener is definitely the way to go. Older StyledText implementations don't send any notifications about caret movement.

    Get access to the StyledText control of the editor as described in your first link, but instead of adding key or mouse listeners, add a CaretListener.

    workbenchWindow.getActivePage().addPartListener(new PartListener() {
        public void partOpened(IWorkbenchPartReference partRef) {
            //Check if this is an editor and its input is what I need
            AbstractTextEditor e =
                (AbstractTextEditor)((IEditorReference) partRef).getEditor(false);
            ((StyledText)e.getAdapter(Control.class)).addCaretListener(l);
        }
    });