src/vs/workbench/contrib/search/browser/searchActions.ts/collapseDeepestExpandedLevel
export function collapseDeepestExpandedLevel(accessor: ServicesAccessor) {

	const viewsService = accessor.get(IViewsService);
	const searchView = getSearchView(viewsService);
	if (searchView) {
		const viewer = searchView.getControl();

		/**
		 * one level to collapse so collapse everything. If FolderMatch, check if there are visible grandchildren,
		 * i.e. if Matches are returned by the navigator, and if so, collapse to them, otherwise collapse all levels.
		 */
		const navigator = viewer.navigate();
		let node = navigator.first();
		let collapseFileMatchLevel = false;
		if (node instanceof FolderMatch) {
			while (node = navigator.next()) {
				if (node instanceof Match) {
					collapseFileMatchLevel = true;
					break;
				}
			}
		}

		if (collapseFileMatchLevel) {
			node = navigator.first();
			do {
				if (node instanceof FileMatch) {
					viewer.collapse(node);
				}
			} while (node = navigator.next());
		} else {
			viewer.collapseAll();
		}

		viewer.domFocus();
		viewer.focusFirst();
	}
}