[英]eclipse plugin: How to programmatically select text in editor?
我想以編程方式跳轉到文本編輯器中的某個位置並突出顯示代碼。
我無法得到Andrew在Eclipse 3.7中的工作答案 。 編譯器給出了這個錯誤:
The method getSourceViewer() from the type AbstractTextEditor is not visible.
但是,我能夠使用selectAndReveal()
方法:
IFile myfile = ...
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
ITextEditor editor = (ITextEditor) IDE.openEditor(page, myfile);
editor.selectAndReveal(offset, length);
如果您已經擁有當前編輯器的句柄,那么您可以執行以下操作:
editor.getSourceViewer().setSelectedRange(offset, length);
如果你沒有當前編輯器的句柄,那么你需要做一些工作來實現(假設一個文本編輯器):
TextEditor editor = (TextEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().getActiveEditor();
雖然這會奏效,但我簡化了一些事情。
TextEditor
,因此您將要執行instanceof測試
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.