<commit_before>/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import { CodeLensProvider, CodeLens, CancellationToken, TextDocument, Range, Uri, Location, Position, workspace, WorkspaceConfiguration } from 'vscode';
import * as Proto from '../protocol';
import * as PConst from '../protocol.const';

import { ITypescriptServiceClient } from '../typescriptService';

import * as nls from 'vscode-nls';
let localize = nls.loadMessageBundle();


class ReferencesCodeLens extends CodeLens {
	public document: Uri;
	public file: string;

	constructor(document: Uri, file: string, range: Range) {
		super(range);
		this.document = document;
		this.file = file;
	}
}

export default class TypeScriptReferencesCodeLensProvider implements CodeLensProvider {
	private client: ITypescriptServiceClient;
	private enabled = false;

	constructor(client: ITypescriptServiceClient) {
		this.client = client;
	}

	public updateConfiguration(config: WorkspaceConfiguration): void {
		let typeScriptConfig = workspace.getConfiguration('typescript');
		this.enabled = typeScriptConfig.get('referencesCodeLens.enabled', false);
	}

	provideCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
		if (!this.enabled) {
			return Promise.resolve([]);
		}

		const filepath = this.client.asAbsolutePath(document.uri);
		if (!filepath) {
			return Promise.resolve([]);
		}
		return this.client.execute('navtree', { file: filepath }, token).then(response => {
			const tree = response.body;
			const referenceableSpans: Range[] = [];
			if (tree && tree.childItems) {
				tree.childItems.forEach(item => this.extractReferenceableSymbols(document, item, referenceableSpans));
			}
			return Promise.resolve(referenceableSpans.map(span => new ReferencesCodeLens(document.uri, filepath, span)));
		});
	}

	resolveCodeLens(inputCodeLens: CodeLens, token: CancellationToken): Promise<CodeLens> {
		const codeLens = inputCodeLens as ReferencesCodeLens;
		if (!codeLens.document) {
			return Promise.reject<CodeLens>(codeLens);
		}
		const args: Proto.FileLocationRequestArgs = {
			file: codeLens.file,
			line: codeLens.range.start.line + 1,
			offset: codeLens.range.start.character + 1
		};
		return this.client.execute('references', args, token).then(response => {
			if (response && response.body) {
				const referenceCount = Math.max(0, response.body.refs.length - 1);
				const locations = response.body.refs.map(reference =>
					new Location(Uri.file(reference.file),
						new Range(
							new Position(reference.start.line - 1, reference.start.offset - 1),
							new Position(reference.end.line - 1, reference.end.offset - 1))));

				codeLens.command = {
					title: referenceCount + ' ' + (referenceCount === 1 ? localize('oneReferenceLabel', 'reference') : localize('manyReferenceLabel', 'references')),
					command: 'editor.action.showReferences',
					arguments: [codeLens.document, codeLens.range.start, locations]
				};<commit_msg>Exclude Definition From TS code lens (#18338)<commit_after>/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import { CodeLensProvider, CodeLens, CancellationToken, TextDocument, Range, Uri, Location, Position, workspace, WorkspaceConfiguration } from 'vscode';
import * as Proto from '../protocol';
import * as PConst from '../protocol.const';

import { ITypescriptServiceClient } from '../typescriptService';

import * as nls from 'vscode-nls';
let localize = nls.loadMessageBundle();


class ReferencesCodeLens extends CodeLens {
	public document: Uri;
	public file: string;

	constructor(document: Uri, file: string, range: Range) {
		super(range);
		this.document = document;
		this.file = file;
	}
}

export default class TypeScriptReferencesCodeLensProvider implements CodeLensProvider {
	private client: ITypescriptServiceClient;
	private enabled = false;

	constructor(client: ITypescriptServiceClient) {
		this.client = client;
	}

	public updateConfiguration(config: WorkspaceConfiguration): void {
		let typeScriptConfig = workspace.getConfiguration('typescript');
		this.enabled = typeScriptConfig.get('referencesCodeLens.enabled', false);
	}

	provideCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
		if (!this.enabled) {
			return Promise.resolve([]);
		}

		const filepath = this.client.asAbsolutePath(document.uri);
		if (!filepath) {
			return Promise.resolve([]);
		}
		return this.client.execute('navtree', { file: filepath }, token).then(response => {
			const tree = response.body;
			const referenceableSpans: Range[] = [];
			if (tree && tree.childItems) {
				tree.childItems.forEach(item => this.extractReferenceableSymbols(document, item, referenceableSpans));
			}
			return Promise.resolve(referenceableSpans.map(span => new ReferencesCodeLens(document.uri, filepath, span)));
		});
	}

	resolveCodeLens(inputCodeLens: CodeLens, token: CancellationToken): Promise<CodeLens> {
		const codeLens = inputCodeLens as ReferencesCodeLens;
		if (!codeLens.document) {
			return Promise.reject<CodeLens>(codeLens);
		}
		const args: Proto.FileLocationRequestArgs = {
			file: codeLens.file,
			line: codeLens.range.start.line + 1,
			offset: codeLens.range.start.character + 1
		};
		return this.client.execute('references', args, token).then(response => {
			if (response && response.body) {
				// Exclude original definition from references
				const locations = response.body.refs
					.filter(reference =>
						reference.start.line !== codeLens.range.start.line + 1
						&& reference.start.offset !== codeLens.range.start.character + 1)
					.map(reference =>
						new Location(Uri.file(reference.file),
							new Range(
								new Position(reference.start.line - 1, reference.start.offset - 1),
								new Position(reference.end.line - 1, reference.end.offset - 1))));

				codeLens.command = {
					title: locations.length + ' ' + (locations.length === 1 ? localize('oneReferenceLabel', 'reference') : localize('manyReferenceLabel', 'references')),
					command: 'editor.action.showReferences',
					arguments: [codeLens.document, codeLens.range.start, locations]
				};
{'fraction_non_alphanumeric': 0.17298261257244762, 'fraction_numerical': 0.0034180413137167485, 'mean_word_length': 3.456953642384106, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4669503', 'n_tokens_mistral': 2109, 'n_tokens_neox': 1863, 'n_words': 444}