quickOpen.ts 2.9 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

A
Alex Dima 已提交
8
import {illegalArgument, onUnexpectedError} from 'vs/base/common/errors';
9
import URI from 'vs/base/common/uri';
10 11 12
import {TPromise} from 'vs/base/common/winjs.base';
import {Range} from 'vs/editor/common/core/range';
import {IModel} from 'vs/editor/common/editorCommon';
A
Alex Dima 已提交
13
import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
E
Erich Gamma 已提交
14 15
import {IOutlineEntry, IOutlineSupport} from 'vs/editor/common/modes';
import LanguageFeatureRegistry from 'vs/editor/common/modes/languageFeatureRegistry';
16
import {IModelService} from 'vs/editor/common/services/modelService';
E
Erich Gamma 已提交
17

18
const OutlineRegistry = new LanguageFeatureRegistry<IOutlineSupport>('outlineSupport');
E
Erich Gamma 已提交
19 20

export {
21
	OutlineRegistry,
E
Erich Gamma 已提交
22 23 24 25
	IOutlineEntry,
	IOutlineSupport
}

26 27 28 29 30 31
export interface IOutline {
	entries: IOutlineEntry[];
	outlineGroupLabel: { [n: string]: string; };
}

export function getOutlineEntries(model: IModel): TPromise<IOutline> {
32 33 34 35

	let groupLabels: { [n: string]: string } = Object.create(null);
	let entries: IOutlineEntry[] = [];

36
	let promises = OutlineRegistry.all(model).map(support => {
37 38

		if (support.outlineGroupLabel) {
A
Alex Dima 已提交
39 40 41 42
			let keys = Object.keys(support.outlineGroupLabel);
			for (let i = 0, len = keys.length; i < len; i++) {
				let key = keys[i];
				groupLabels[key] = support.outlineGroupLabel[key];
43 44 45
			}
		}

46
		return support.getOutline(model.getAssociatedResource()).then(result => {
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
			if (Array.isArray(result)) {
				entries.push(...result);
			}
		}, err => {
			onUnexpectedError(err);
		});
	});

	return TPromise.join(promises).then(() => {
		let flatEntries: IOutlineEntry[] = [];
		flatten(flatEntries, entries, '');
		flatEntries.sort(compareEntriesUsingStart);

		return {
			entries: flatEntries,
			outlineGroupLabel: groupLabels
A
tslint  
Alex Dima 已提交
63
		};
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	});
}

function compareEntriesUsingStart(a: IOutlineEntry, b: IOutlineEntry): number{
	return Range.compareRangesUsingStarts(a.range, b.range);
}

function flatten(bucket: IOutlineEntry[], entries: IOutlineEntry[], overrideContainerLabel: string): void {
	for (let entry of entries) {
		bucket.push({
			type: entry.type,
			range: entry.range,
			label: entry.label,
			icon: entry.icon,
			containerLabel: entry.containerLabel || overrideContainerLabel
		});
		if (entry.children) {
			flatten(bucket, entry.children, entry.label);
		}
	}
}
85 86 87 88


CommonEditorRegistry.registerLanguageCommand('_executeDocumentSymbolProvider', function(accessor, args) {
	const {resource} = args;
89
	if (!(resource instanceof URI)) {
90 91 92 93 94 95 96 97
		throw illegalArgument('resource');
	}
	const model = accessor.get(IModelService).getModel(resource);
	if (!model) {
		throw illegalArgument('resource');
	}
	return getOutlineEntries(model);
});