Aptana Outline Auto-Expansion Monkeyed!

You may have seen my last post Monkeying with my outline! where I apparently managed to get Aptana automatically expanding my outline whenever I viewed a new file in the editor. Well… it didn’t quite go to plan. Long story short, the script only executed properly when it was loaded after editing in the script editor, not when Aptana loaded up at the start. Details here in the Aptana scripting forum. I was rescued by Kevin Lindsey. Above and beyond the run of the mill support, he even emailed me to let me know he’d snuck the fix into 1.2. It’s working brilliantly! So now here’s my code with the updated bits from Kevin, I’ve commented out all the println’s cause I like a clean console.

/*
 *
 * Menu: EHU > HandlerService (Auto expand outline)
 * Kudos: Ingo Muschenetz & Kevin Lindsey & Steve Daniels
 * License: EPL 1.0
 * Listener: getPartService().addPartListener(this);
 * DOM: http://localhost/com.aptana.ide.scripting
 * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
 *
 *
*/
 
function getPartService()
{
    var workbench = Packages.org.eclipse.ui.PlatformUI.getWorkbench();
    var result = null;
 
    if (workbench)
    {
        var window = null;
 
        runOnUIThread(function() {
            window = workbench.getActiveWorkbenchWindow();
 
            if (window != null)
            {
                result = window.getPartService();
                //out.println("result set to " + result);
            }
            else
            {
                //out.println("window is not defined");
            }
        });
    }
    else
    {
        //out.println("workbench is not defined");
    }
 
    //out.println("getPartService() is returning " + result);
 
    return result;
}
 
function partBroughtToTop(part)
{
  //out.println("part*()\n");
}  
 
function partClosed(part)
{
  //out.println("part*()\n");
}
 
function partDeactivated(part)
{
  //out.println("part*()\n");
}  
 
function partOpened(part)
{
  //out.println("part*()\n");
}
 
function partActivated(part)
{
  if (part.getEditorInput)
  {
    expandOutline();
  }
}
 
function partVisible(part)
{
  //out.println("part*()\n");
}
 
function partHidden(part)
{
  //out.println("part*()\n");
}
 
function expandOutline()
{
  //out.println("expandOutline()\n");
  try
  {
    //out.println("try\n");
    var editor = editors.activeEditor.textEditor;
 
    editor.getOutlinePage().getTreeViewer().expandAll();
  }
  catch (e)
  {
    //out.println("catch\n");
  }
}

Enjoy 🙂

Steve Daniels

Monkeying with my outline!

After having been here about 6 months I have finally succumbed to the pressure from my boss to make a blog post. Normally my ideas for such posts would be large and grand, take too long to type out, and slip into the infinite chasm that is otherwise known as a draft…

I like Aptana, I like the Outline view. What I don’t like is that you always have to expand it. So thus, I never used it, I’m simple and lazy, and not afraid to admit it. Andy Davies reignited my interest in the Outline view earlier today in a round about way. Andy pointed out the lesser noticed Scripts menu in Aptana. Specifically Experimental -> Colorize as HTML. A nice quick and neat tool. Not that I’d use it much mind. But then below its entry I saw “Expand outline”. Bingo!

What followed is my discovery of Monkey Script. I figured getting expand outline on a keyboard shorcut would be good, and perhaps renew my interest in it. But then I noticed the mention of listeners… Queue  a bit of creative Googlin and this post by Ingo on the PartService event handler.

Hacking the existing expand outline script into this proved painless so here we have it:

/*
 *
 * Menu: EHU > HandlerService (Auto expand outline)
 * Kudos: Ingo Muschenetz & Kevin Lindsey & Steve Daniels
 * License: EPL 1.0
 * Listener: getPartService().addPartListener(this);
 * DOM: http://localhost/com.aptana.ide.scripting
 * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
 *
*/
 
function getPartService(){
  window = Packages.org.eclipse.ui.PlatformUI.getWorkbench().getActiveWorkbenchWindow();
 
  if (window != null) {
    return window.getPartService();
  }
}
 
function partBroughtToTop(part)
{
 
}  
 
function partClosed(part)
{
 
}
 
function partDeactivated(part)
{
 
}  
 
function partOpened(part)
{
 
}
 
function partActivated(part)
{
  if (part.getEditorInput)
  {
    expandOutline();
  }
}
 
function partVisible(part)
{
 
}
 
function partHidden(part)
{
 
}
 
function expandOutline()
{
  try
  {
    var editor = editors.activeEditor.textEditor;
 
    editor.getOutlinePage().getTreeViewer().expandAll();
  }
  catch (e)
  {
    out.println("Failed to expand outline.\n");
  }
}

You’ll notice I hooked the Activated event rather than the Opened one this was because on opening expandOutline executed before the active editor had switched to the new file so the outline was expended on an already opened file. This way has a slight caveat that if you collapse the outline and then go back to the file it’ll expand it again, but that doesn’t bother me at all.

To use the above script simply drop it in a file with a .js suffix and place it in a folder called “scripts” in a project that is currently open on your workspace. I created a new project just for this purpose.

I hope someone found this useful 🙂

Steve Daniels

>