문제

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

도움이 되었습니까?

해결책

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top