Smi*_*tha 7 eclipse jface text-editor
我一直试图在jface TextEditor中获取光标位置的行号和列号.我尝试了函数getCursorPosition().但在打印时,它只显示"?".请注意,我需要编辑器中的行号和列号,而不是屏幕.我看到有一个函数JTextArea.getCaretPosition.但我不知道如何将文本编辑器转换为JTextArea.此外,是否可以读取光标所在的单词?
谢谢
从TextEditor,您可以获取文档,文档提供程序和选择.这将使您可以访问当前的游标偏移量.
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);
Run Code Online (Sandbox Code Playgroud)
IDocument 提供其他方法来获取行的开始(您可以从中计算列).
有关更多信息,请参阅http://wiki.eclipse.org/The_Official_Eclipse_FAQs#Text_Editors
| 归档时间: |
|
| 查看次数: |
5906 次 |
| 最近记录: |