且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Eclipse文本编辑器中获取光标位置

更新时间:2023-01-28 15:14:50

从TextEditor中,您可以获取文档,文档提供者和选择。这将让您访问当前的游标偏移。

From a TextEditor, you can get the document, document provider, and selection. That 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 提供其他方法来获取开始的行(您可以从中计算列)。

IDocument provides other methods to get the starts of lines (you can calculate the column from that).

有关更多信息,请参阅 http://wiki.eclipse.org/The_Official_Eclipse_FAQs#Text_Editors

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