<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 * as vscode from 'vscode';
import * as nls from 'vscode-nls';
import TypeScriptServiceClientHost from '../typeScriptServiceClientHost';
import { nulToken } from '../utils/cancellation';
import { Command } from '../utils/commandManager';
import { Lazy } from '../utils/lazy';
import { isImplicitProjectConfigFile, openOrCreateConfigFile } from '../utils/tsconfig';
import { ServerResponse } from '../typescriptService';

const localize = nls.loadMessageBundle();

export class TypeScriptGoToProjectConfigCommand implements Command {
	public readonly id = 'typescript.goToProjectConfig';

	public constructor(
		private readonly lazyClientHost: Lazy<TypeScriptServiceClientHost>,
	) { }

	public execute() {
		const editor = vscode.window.activeTextEditor;
		if (editor) {
			goToProjectConfig(this.lazyClientHost.value, true, editor.document.uri);
		}
	}
}

export class JavaScriptGoToProjectConfigCommand implements Command {
	public readonly id = 'javascript.goToProjectConfig';

	public constructor(
		private readonly lazyClientHost: Lazy<TypeScriptServiceClientHost>,
	) { }

	public execute() {
		const editor = vscode.window.activeTextEditor;
		if (editor) {
			goToProjectConfig(this.lazyClientHost.value, false, editor.document.uri);
		}
	}
}

async function goToProjectConfig(
	clientHost: TypeScriptServiceClientHost,
	isTypeScriptProject: boolean,
	resource: vscode.Uri
): Promise<void> {
	const client = clientHost.serviceClient;
	const rootPath = client.getWorkspaceRootForResource(resource);
	if (!rootPath) {
		vscode.window.showInformationMessage(
			localize(
				'typescript.projectConfigNoWorkspace',
				'Please open a folder in VS Code to use a TypeScript or JavaScript project'));
		return;
	}

	const file = client.toPath(resource);
	// TSServer errors when 'projectInfo' is invoked on a non js/ts file
	if (!file || !await clientHost.handles(resource)) {
		vscode.window.showWarningMessage(
			localize(
				'typescript.projectConfigUnsupportedFile',
				'Could not determine TypeScript or JavaScript project. Unsupported file type'));
		return;
	}

	let res: ServerResponse.Response<protocol.ProjectInfoResponse> | undefined;
	try {
		res = await client.execute('projectInfo', { file, needFileNameList: false }, nulToken);
	} catch {
		// noop
	}
	if (!res || res.type !== 'response' || !res.body) {
		vscode.window.showWarningMessage(localize('typescript.projectConfigCouldNotGetInfo', 'Could not determine TypeScript or JavaScript project'));
		return;
	}

	const { configFileName } = res.body;
	if (configFileName && !isImplicitProjectConfigFile(configFileName)) {
		const doc = await vscode.workspace.openTextDocument(configFileName);<commit_msg>Remove extra check<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 * as vscode from 'vscode';
import * as nls from 'vscode-nls';
import TypeScriptServiceClientHost from '../typeScriptServiceClientHost';
import { nulToken } from '../utils/cancellation';
import { Command } from '../utils/commandManager';
import { Lazy } from '../utils/lazy';
import { isImplicitProjectConfigFile, openOrCreateConfigFile } from '../utils/tsconfig';
import { ServerResponse } from '../typescriptService';

const localize = nls.loadMessageBundle();

export class TypeScriptGoToProjectConfigCommand implements Command {
	public readonly id = 'typescript.goToProjectConfig';

	public constructor(
		private readonly lazyClientHost: Lazy<TypeScriptServiceClientHost>,
	) { }

	public execute() {
		const editor = vscode.window.activeTextEditor;
		if (editor) {
			goToProjectConfig(this.lazyClientHost.value, true, editor.document.uri);
		}
	}
}

export class JavaScriptGoToProjectConfigCommand implements Command {
	public readonly id = 'javascript.goToProjectConfig';

	public constructor(
		private readonly lazyClientHost: Lazy<TypeScriptServiceClientHost>,
	) { }

	public execute() {
		const editor = vscode.window.activeTextEditor;
		if (editor) {
			goToProjectConfig(this.lazyClientHost.value, false, editor.document.uri);
		}
	}
}

async function goToProjectConfig(
	clientHost: TypeScriptServiceClientHost,
	isTypeScriptProject: boolean,
	resource: vscode.Uri
): Promise<void> {
	const client = clientHost.serviceClient;
	const rootPath = client.getWorkspaceRootForResource(resource);
	if (!rootPath) {
		vscode.window.showInformationMessage(
			localize(
				'typescript.projectConfigNoWorkspace',
				'Please open a folder in VS Code to use a TypeScript or JavaScript project'));
		return;
	}

	const file = client.toPath(resource);
	// TSServer errors when 'projectInfo' is invoked on a non js/ts file
	if (!file || !await clientHost.handles(resource)) {
		vscode.window.showWarningMessage(
			localize(
				'typescript.projectConfigUnsupportedFile',
				'Could not determine TypeScript or JavaScript project. Unsupported file type'));
		return;
	}

	let res: ServerResponse.Response<protocol.ProjectInfoResponse> | undefined;
	try {
		res = await client.execute('projectInfo', { file, needFileNameList: false }, nulToken);
	} catch {
		// noop
	}

	if (res?.type !== 'response' || !res.body) {
		vscode.window.showWarningMessage(localize('typescript.projectConfigCouldNotGetInfo', 'Could not determine TypeScript or JavaScript project'));
		return;
	}

	const { configFileName } = res.body;
	if (!isImplicitProjectConfigFile(configFileName)) {
		const doc = await vscode.workspace.openTextDocument(configFileName);
{'fraction_non_alphanumeric': 0.16049991777668146, 'fraction_numerical': 0.0, 'mean_word_length': 3.6784615384615384, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '2773017', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1654, 'n_words': 431}