簡體   English   中英

如何使用特定的光標偏移位置打開新的Eclipse編輯器

[英]How to open a new eclipse editor with a specific cursor offset position

我想以編程方式執行上述操作。

我研究了如何在 Eclipse TextEditor中獲取光標位置以及Eclipse插件如何獲取當前文本編輯器的光標位置,因此我很了解如何從當前打開的編輯器獲取光標偏移。 但是,我試圖在新的編輯器中設置光標偏移,該編輯器由我以編程方式打開。

我當前打開新編輯器的方式如下:

IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage page = win.getActivePage();
    if (page != null) {
        IEditorPart editor = page.getActiveEditor();
        if (editor != null) {
            IEditorInput input = editor.getEditorInput();
            if (input instanceof IFileEditorInput) {
                String fileLocation = ((IFileEditorInput) input).getFile().getLocation().toOSString();
                String newFileLocartion = generateNewFileLocation(fileLocation);
                File file = new File(newFileLocartion);
                IFileStore fileStore = EFS.getLocalFileSystem().getStore(file.toURI());
                try {
                    IDE.openEditorOnFileStore(page, fileStore);
                } catch (PartInitException e) {
                    // TODO error handling
                }
            }
        }
    }

有沒有一種方法可以將新編輯器設置為以特定的偏移量打開(假設我已經提前知道了偏移量)?

謝謝!

它使用此代碼段導航到文件中的指定行。

public static void navigateToLine(IFile file, Integer line)
{
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put(IMarker.LINE_NUMBER, line);
    IMarker marker = null;
    try {
        marker = file.createMarker(IMarker.TEXT);
        marker.setAttributes(map);
        try {
            IDE.openEditor(getActivePage(), marker);
        } catch ( PartInitException e ) {
            //complain
        }
    } catch ( CoreException e1 ) {
        //complain
    } finally {
        try {
            if (marker != null)
                marker.delete();
        } catch ( CoreException e ) {
            //whatever
        }
    }
}

可能不完全是您所需要的,但可能很有用。 (// complain替換了使用此產品的錯誤處理代碼)

我使用了以下內容,它比以前的答案更簡單。 假設您有int offsetIWorkbenchPage pageIFile file (似乎它們都存在於OP的問題中):

ITextEditor editor = (ITextEditor) IDE.openEditor(page, file);
editor.selectAndReveal(offset, 0);

我從此答案中找出了解決方法。 (但是通過將selectAndReveal的第二個參數設置為零,不會突出顯示任何文本)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM