eclipse: calling organizeimports programmatcally

Posted by Stefan Roka on Stack Overflow See other posts from Stack Overflow or by Stefan Roka
Published on 2010-05-04T10:20:27Z Indexed on 2010/05/04 10:58 UTC
Read the original article Hit count: 248

Filed under:
|
|

Hi,

I'm trying to execute 'OrganizeImports' programmatically on files that I'm editing programmatically. My code looks like this:

    final ICommandService cmdService = (ICommandService)PlatformUI.getWorkbench().getService    (ICommandService.class);
    if (cmdService != null) {
        final Command cmd = cmdService.getCommand(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
        final ExecutionEvent execEvt = new ExecutionEvent(cmd, Collections.EMPTY_MAP, compileationUnit, null);

        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        ResourcesPlugin.getWorkspace().

        Display.getDefault().syncExec(new Runnable() {
            @Override
            public void run() {
                try {
                    //cmd.executeWithChecks(execEvt);
                    cmd.execute(execEvt);
                } catch (Exception e) {
                    getLogger().severe("organize imports failed: " + e.getMessage());
                }
            }

My problem is that OrganizeImportsAction executes on the current selection which is not the same as the compilation unit I'm editing. I would like to set the selection programmatically on the compilation unit but I don't know how to do that. Or maybe there is another way to trigger OrganizeImports.

thanks, stefan

© Stack Overflow or respective owner

Related posts about eclipse

Related posts about jdt