I've ended up pulling this code (and an enhancement for collapsing empty and nearly-empty nodes) into the fix for bug 1005040 as it's necessary to collapse the query parse tree appropriately for that change.
I've ended up pulling this code (and an enhancement for collapsing empty and nearly-empty nodes) into the fix for bug 1005040 as it's necessary to collapse the query parse tree appropriately for that change.