Question

I'm trying to add a selection changed event to a text editor based on org.eclipse.ui.editors.text.TextEditor.

I would like to get the row(s) and the column(s) of the selected text (when selected/highlighted by the user).

WHats the simplest (if any) way to achieve this?

Thank you

Was it helpful?

Solution

The ITextViewer object (or the ISourceViewer extension) has a getSelectedRange() method which returns you a Point containing the offset in the document and the length of the selected range.

The IDocument object has a getLineOfOffset() method which returns the line containing an offset. It also has getLineInformationOfOffset() returning information about the line.

So:

Point selected = textViewer.getSelectedRange();

int line = document.getLineOfOffset(selected.x);

IRegion lineRegion = document.getLineInformationOfOffset(selected.x);

int col = selected.x - lineRegion.getOffset();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top