goToDeclaration.ts 1.7 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';

J
Johannes Rieken 已提交
8
import {TPromise} from 'vs/base/common/winjs.base';
9
import {onUnexpectedError} from 'vs/base/common/errors';
10
import {IModel, IPosition} from 'vs/editor/common/editorCommon';
E
Erich Gamma 已提交
11 12
import {IDeclarationSupport} from 'vs/editor/common/modes';
import LanguageFeatureRegistry from 'vs/editor/common/modes/languageFeatureRegistry';
J
Johannes Rieken 已提交
13
import {IReference} from 'vs/editor/common/modes';
14
import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
E
Erich Gamma 已提交
15

J
Johannes Rieken 已提交
16
export const DeclarationRegistry = new LanguageFeatureRegistry<IDeclarationSupport>('declarationSupport');
E
Erich Gamma 已提交
17

18
export function getDeclarationsAtPosition(model: IModel, position: IPosition): TPromise<IReference[]> {
J
Johannes Rieken 已提交
19

20 21
	const resource = model.getAssociatedResource();
	const provider = DeclarationRegistry.ordered(model);
J
Johannes Rieken 已提交
22 23

	// get results
J
Johannes Rieken 已提交
24 25 26
	const promises = provider.map((provider, idx) => {
		return provider.findDeclaration(resource, position).then(result => {
			return result;
J
Johannes Rieken 已提交
27 28
		}, err => {
			onUnexpectedError(err);
J
Johannes Rieken 已提交
29
		});
J
Johannes Rieken 已提交
30 31
	});

J
Johannes Rieken 已提交
32
	return TPromise.join(promises).then(allReferences => {
J
Johannes Rieken 已提交
33
		let result: IReference[] = [];
J
Johannes Rieken 已提交
34 35 36 37 38
		for (let references of allReferences) {
			if (Array.isArray(references)) {
				result.push(...references);
			} else if (references) {
				result.push(references);
J
Johannes Rieken 已提交
39 40 41 42
			}
		}
		return result;
	});
43 44
}

A
tslint  
Alex Dima 已提交
45
CommonEditorRegistry.registerDefaultLanguageCommand('_executeDefinitionProvider', getDeclarationsAtPosition);