Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I would like to do the above programmatically.

I looked on How to get cursor position in an eclipse TextEditor and Eclipse-plugin how to get current text editor corsor position so i kind of know how get the cursor offset from the current open editor. However, I'm trying to set the cursor offset in a new editor which is opened programmatically by me.

The way I'm currently open my new editor is as follows:

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
                }
            }
        }
    }

Is there a way to open set the new editor to open in a specific offset (assuming i already knows the offset in advance)?

Thanks!

like image 375
akaspi Avatar asked Sep 04 '12 05:09

akaspi


2 Answers

I used the following, which is more simple than the previous answer. Assuming you have int offset, IWorkbenchPage page and IFile file (and it seems that they all exist in the OP's question):

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

I found out how to do this from this answer. (but by setting the second parameter of selectAndReveal to zero, no text is highlighted)

like image 187
Eyal Avatar answered Nov 15 '22 15:11

Eyal


It use this snippet to navigate to specified line in a file.

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
        }
    }
}

Probably not exactly what you need, but could be useful. (//complain replaces error handling code that was specific to product where this is used )

like image 23
dbrank0 Avatar answered Nov 15 '22 16:11

dbrank0