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

import { Disposable } from 'vs/base/common/lifecycle';
import { ILanguageDetectionService } from 'vs/workbench/services/languageDetection/common/languageDetection';
import { IUntitledTextEditorService } from 'vs/workbench/services/untitled/common/untitledTextEditorService';
import { FileAccess } from 'vs/base/common/network';
import type { ModelOperations, ModelResult } from '@vscode/vscode-languagedetection';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IModeService } from 'vs/editor/common/services/modeService';
import { URI } from 'vs/base/common/uri';
import { isWeb } from 'vs/base/common/platform';
import { IUntitledTextEditorModel } from 'vs/workbench/services/untitled/common/untitledTextEditorModel';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { Registry } from 'vs/platform/registry/common/platform';
import { Extensions, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions';
import { LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle';

export class LanguageDetectionService extends Disposable implements ILanguageDetectionService {
	private static readonly expectedRelativeConfidence = 0.2;
	static readonly enablementSettingKey = 'workbench.editor.untitled.experimentalLanguageDetection';

	private _loadFailed = false;
	private _modelOperations: ModelOperations | undefined;
	_serviceBrand: undefined;

	constructor(
		@IWorkbenchEnvironmentService private readonly _environmentService: IWorkbenchEnvironmentService,
		@IModeService private readonly _modeService: IModeService,
		@IConfigurationService private readonly _configurationService: IConfigurationService,
		@IUntitledTextEditorService private readonly _untitledTextEditorService: IUntitledTextEditorService) {
		super();

		this._register(_untitledTextEditorService.onDidChangeContent(e => this.handleChangeEvent(e)));
	}

	private async handleChangeEvent(e: IUntitledTextEditorModel) {
		if (!this.isEnabledForMode(e.getMode()) || e.hasModeSetExplicitly) {
			return;
		}

		const value = this._untitledTextEditorService.getValue(e.resource);
		if (!value) { return; }
		const lang = await this.detectLanguage(value);
		if (!lang) { return; }
		e.setMode(lang, false);
	}

	private async getModelOperations(): Promise<ModelOperations> {
		if (this._modelOperations) {
			return this._modelOperations;
		}

		const { ModelOperations } = await import('@vscode/vscode-languagedetection');
		this._modelOperations = new ModelOperations(
			async () => {
				const response = await fetch(this._environmentService.isBuilt && !isWeb
					? FileAccess.asBrowserUri('../../../../../../node_modules.asar.unpacked/@vscode/vscode-languagedetection/model/model.json', require).toString(true)
					: FileAccess.asBrowserUri('../../../../../../node_modules/@vscode/vscode-languagedetection/model/model.json', require).toString(true));<commit_msg>debounce change event in language detection<commit_after>/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { Disposable } from 'vs/base/common/lifecycle';
import { ILanguageDetectionService } from 'vs/workbench/services/languageDetection/common/languageDetection';
import { IUntitledTextEditorService } from 'vs/workbench/services/untitled/common/untitledTextEditorService';
import { FileAccess } from 'vs/base/common/network';
import type { ModelOperations, ModelResult } from '@vscode/vscode-languagedetection';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IModeService } from 'vs/editor/common/services/modeService';
import { URI } from 'vs/base/common/uri';
import { isWeb } from 'vs/base/common/platform';
import { IUntitledTextEditorModel } from 'vs/workbench/services/untitled/common/untitledTextEditorModel';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { Registry } from 'vs/platform/registry/common/platform';
import { Extensions, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions';
import { LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle';
import { debounce } from 'vs/base/common/decorators';

export class LanguageDetectionService extends Disposable implements ILanguageDetectionService {
	private static readonly expectedRelativeConfidence = 0.2;
	static readonly enablementSettingKey = 'workbench.editor.untitled.experimentalLanguageDetection';

	private _loadFailed = false;
	private _modelOperations: ModelOperations | undefined;
	_serviceBrand: undefined;

	constructor(
		@IWorkbenchEnvironmentService private readonly _environmentService: IWorkbenchEnvironmentService,
		@IModeService private readonly _modeService: IModeService,
		@IConfigurationService private readonly _configurationService: IConfigurationService,
		@IUntitledTextEditorService private readonly _untitledTextEditorService: IUntitledTextEditorService) {
		super();

		this._register(_untitledTextEditorService.onDidChangeContent(e => this.handleChangeEvent(e)));
	}

	@debounce(300)
	private async handleChangeEvent(e: IUntitledTextEditorModel) {
		if (!this.isEnabledForMode(e.getMode()) || e.hasModeSetExplicitly) {
			return;
		}

		const value = this._untitledTextEditorService.getValue(e.resource);
		if (!value) { return; }
		const lang = await this.detectLanguage(value);
		if (!lang) { return; }
		e.setMode(lang, false);
	}

	private async getModelOperations(): Promise<ModelOperations> {
		if (this._modelOperations) {
			return this._modelOperations;
		}

		const { ModelOperations } = await import('@vscode/vscode-languagedetection');
		this._modelOperations = new ModelOperations(
			async () => {
				const response = await fetch(this._environmentService.isBuilt && !isWeb
					? FileAccess.asBrowserUri('../../../../../../node_modules.asar.unpacked/@vscode/vscode-languagedetection/model/model.json', require).toString(true)
					: FileAccess.asBrowserUri('../../../../../../node_modules/@vscode/vscode-languagedetection/model/model.json', require).toString(true));
{'fraction_non_alphanumeric': 0.15900603274921, 'fraction_numerical': 0.001005458201666188, 'mean_word_length': 4.255094339622642, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '4558554', 'n_tokens_mistral': 1914, 'n_tokens_neox': 1833, 'n_words': 373}