referenceProvider.ts 1.8 KB
Newer Older
I
isidor 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

A
tslint  
Alex Dima 已提交
6
import { ReferenceProvider, Location, TextDocument, Position, Range, CancellationToken } from 'vscode';
E
Erich Gamma 已提交
7 8

import * as Proto from '../protocol';
D
Dirk Baeumer 已提交
9
import { ITypescriptServiceClient } from '../typescriptService';
E
Erich Gamma 已提交
10 11

export default class TypeScriptReferenceSupport implements ReferenceProvider {
M
Matt Bierner 已提交
12 13
	public constructor(
		private client: ITypescriptServiceClient) { }
E
Erich Gamma 已提交
14 15

	public provideReferences(document: TextDocument, position: Position, options: { includeDeclaration: boolean }, token: CancellationToken): Promise<Location[]> {
16
		const filepath = this.client.normalizePath(document.uri);
17 18 19
		if (!filepath) {
			return Promise.resolve<Location[]>([]);
		}
M
Matt Bierner 已提交
20
		const args: Proto.FileLocationRequestArgs = {
21
			file: filepath,
E
Erich Gamma 已提交
22 23 24
			line: position.line + 1,
			offset: position.character + 1
		};
25
		const apiVersion = this.client.apiVersion;
E
Erich Gamma 已提交
26
		return this.client.execute('references', args, token).then((msg) => {
M
Matt Bierner 已提交
27
			const result: Location[] = [];
28 29 30
			if (!msg.body) {
				return result;
			}
M
Matt Bierner 已提交
31
			const refs = msg.body.refs;
E
Erich Gamma 已提交
32
			for (let i = 0; i < refs.length; i++) {
M
Matt Bierner 已提交
33
				const ref = refs[i];
D
Dirk Baeumer 已提交
34
				if (!options.includeDeclaration && apiVersion.has203Features() && ref.isDefinition) {
35 36
					continue;
				}
M
Matt Bierner 已提交
37 38
				const url = this.client.asUrl(ref.file);
				const location = new Location(
E
Erich Gamma 已提交
39
					url,
M
Matt Bierner 已提交
40
					new Range(ref.start.line - 1, ref.start.offset - 1, ref.end.line - 1, ref.end.offset - 1));
E
Erich Gamma 已提交
41 42 43
				result.push(location);
			}
			return result;
44 45
		}, (err) => {
			this.client.error(`'references' request failed with error.`, err);
E
Erich Gamma 已提交
46 47 48 49
			return [];
		});
	}
}