![](/img/trans.png)
[英]How to open a new eclipse editor with a specific cursor offset position
[英]get the position of cursor from the open editor
我有一個插件(eclipse),它會將代碼片段添加到打開的java類中的當前光標位置。 我使用以下代碼獲取光標位置:
final ITextSelection selection = (ITextSelection) editorPart.getEditorSite().getSelectionProvider().getSelection();
final ITextEditor editor = (ITextEditor) editorPart.getAdapter(ITextEditor.class);
final IDocumentProvider documentProvider = editor.getDocumentProvider();
final IDocument document = documentProvider.getDocument(editor.getEditorInput());
final String finalSnippet = snippet.trim();
document.replace(selection.getOffset(), 0, finalSnippet);
當我從保存的java類調用我的插件時,我得到光標的正確位置。 但是如果我在java類中進行任何更改,並在不保存java類的情況下調用插件,則光標位置是錯誤的。 看起來像。 上面的代碼考慮了已保存的java類副本並計算了位置,而不是編輯器中打開的當前副本。
我使用以下代碼獲取編輯器對象:
editorPart = this.window.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
有沒有辦法像在編輯器中一樣獲取光標的位置(行和列),即使是否保存了類。
您可以嘗試調用editor.getAdapter(ITextEditor.class),然后將其轉換為StyledText,然后從StyledText#getCaretOffset()獲取光標位置。
另見其他鏈接:
如何在日食TextEditor中獲取光標位置
Eclipse-plugin如何獲取當前文本編輯器的corsor位置
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.