How to get cursor position in eclipse TextEditor

I am trying to get the row number and column number of the cursor position in a jface text editor. I tried the getCursorPosition () function. But when printed, it only shows "?". Note that I need the row number and column number in the editor, not in relation to the screen. I saw that there is a JTextArea.getCaretPosition function. But I do not know how to convert a text editor to JTextArea. Also, is it possible to read the word in which the cursor is located?

thanks

+7
source share
1 answer

From TextEditor you can get a document, document provider and choice. This will give you access to the current cursor offset.

ITextEditor editor = (ITextEditor) editorPart .getAdapter(ITextEditor.class); IDocumentProvider provider = editor.getDocumentProvider(); IDocument document = provider.getDocument(editorPart .getEditorInput()); ITextSelection textSelection = (ITextSelection) editorPart .getSite().getSelectionProvider().getSelection(); int offset = textSelection.getOffset(); int lineNumber = document.getLineOfOffset(offset); 

IDocument provides other methods for running rows (you can calculate a column from this).

For more information see http://wiki.eclipse.org/The_Official_Eclipse_FAQs#Text_Editors

+8
source

All Articles