<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 {
	createConnection, IConnection,
	TextDocuments, TextDocument, InitializeParams, InitializeResult, NotificationType, RequestType,
	DocumentRangeFormattingRequest, Disposable, ServerCapabilities, Diagnostic
} from 'vscode-languageserver';

import { xhr, XHRResponse, configure as configureHttpRequests, getErrorStatusDescription } from 'request-light';
import * as fs from 'fs';
import { URI } from 'vscode-uri';
import * as URL from 'url';
import { posix } from 'path';
import { setTimeout, clearTimeout } from 'timers';
import { formatError, runSafe, runSafeAsync } from './utils/runner';
import { JSONDocument, JSONSchema, getLanguageService, DocumentLanguageSettings, SchemaConfiguration, ClientCapabilities } from 'vscode-json-languageservice';
import { getLanguageModelCache } from './languageModelCache';

interface ISchemaAssociations {
	[pattern: string]: string[];
}

namespace SchemaAssociationNotification {
	export const type: NotificationType<ISchemaAssociations, any> = new NotificationType('json/schemaAssociations');
}

namespace VSCodeContentRequest {
	export const type: RequestType<string, string, any, any> = new RequestType('vscode/content');
}

namespace SchemaContentChangeNotification {
	export const type: NotificationType<string, any> = new NotificationType('json/schemaContent');
}

namespace ForceValidateRequest {
	export const type: RequestType<string, Diagnostic[], any, any> = new RequestType('json/validate');
}

// Create a connection for the server
const connection: IConnection = createConnection();

process.on('unhandledRejection', (e: any) => {
	console.error(formatError(`Unhandled exception`, e));
});
process.on('uncaughtException', (e: any) => {
	console.error(formatError(`Unhandled exception`, e));
});


console.log = connection.console.log.bind(connection.console);
console.error = connection.console.error.bind(connection.console);

const workspaceContext = {
	resolveRelativePath: (relativePath: string, resource: string) => {
		return URL.resolve(resource, relativePath);
	}
};
function getSchemaRequestService(handledSchemas: { [schema: string]: boolean } = {}) {

	return (uri: string): Thenable<string> => {
		const protocol = uri.substr(0, uri.indexOf(':'));

		if (!handledSchemas || handledSchemas[protocol]) {
			if (protocol === 'file') {
				const fsPath = URI.parse(uri).fsPath;
				return new Promise<string>((c, e) => {
					fs.readFile(fsPath, 'UTF-8', (err, result) => {
						err ? e(err.message || err.toString()) : c(result.toString());
					});
				});
			} else if (protocol === 'http' || protocol === 'https') {
				const headers = { 'Accept-Encoding': 'gzip, deflate' };
				return xhr({ url: uri, followRedirects: 5, headers }).then(response => {
					return response.responseText;
				}, (error: XHRResponse) => {
					return Promise.reject(error.responseText || getErrorStatusDescription(error.status) || error.toString());
				});
			}
		}
		return connection.sendRequest(VSCodeContentRequest.type, uri).then(responseText => {
			return responseText;
		}, error => {
			return Promise.reject(error.message);
		});
	};
}

// create the JSON language service
let languageService = getLanguageService({
	workspaceContext,
	contributions: [],
	clientCapabilities: ClientCapabilities.LATEST
});

// Create a text document manager.
const documents: TextDocuments = new TextDocuments();
<commit_msg>[json] file schema loaded on client instead of server. Fixes #83515<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 {
	createConnection, IConnection,
	TextDocuments, TextDocument, InitializeParams, InitializeResult, NotificationType, RequestType,
	DocumentRangeFormattingRequest, Disposable, ServerCapabilities, Diagnostic
} from 'vscode-languageserver';

import { xhr, XHRResponse, configure as configureHttpRequests, getErrorStatusDescription } from 'request-light';
import * as fs from 'fs';
import { URI } from 'vscode-uri';
import * as URL from 'url';
import { posix } from 'path';
import { setTimeout, clearTimeout } from 'timers';
import { formatError, runSafe, runSafeAsync } from './utils/runner';
import { JSONDocument, JSONSchema, getLanguageService, DocumentLanguageSettings, SchemaConfiguration, ClientCapabilities, SchemaRequestService } from 'vscode-json-languageservice';
import { getLanguageModelCache } from './languageModelCache';

interface ISchemaAssociations {
	[pattern: string]: string[];
}

namespace SchemaAssociationNotification {
	export const type: NotificationType<ISchemaAssociations, any> = new NotificationType('json/schemaAssociations');
}

namespace VSCodeContentRequest {
	export const type: RequestType<string, string, any, any> = new RequestType('vscode/content');
}

namespace SchemaContentChangeNotification {
	export const type: NotificationType<string, any> = new NotificationType('json/schemaContent');
}

namespace ForceValidateRequest {
	export const type: RequestType<string, Diagnostic[], any, any> = new RequestType('json/validate');
}

// Create a connection for the server
const connection: IConnection = createConnection();

process.on('unhandledRejection', (e: any) => {
	console.error(formatError(`Unhandled exception`, e));
});
process.on('uncaughtException', (e: any) => {
	console.error(formatError(`Unhandled exception`, e));
});


console.log = connection.console.log.bind(connection.console);
console.error = connection.console.error.bind(connection.console);

const workspaceContext = {
	resolveRelativePath: (relativePath: string, resource: string) => {
		return URL.resolve(resource, relativePath);
	}
};

const fileRequestService: SchemaRequestService = (uri: string) => {
	const fsPath = URI.parse(uri).fsPath;
	return new Promise<string>((c, e) => {
		fs.readFile(fsPath, 'UTF-8', (err, result) => {
			err ? e(err.message || err.toString()) : c(result.toString());
		});
	});
};

const httpRequestService: SchemaRequestService = (uri: string) => {
	const headers = { 'Accept-Encoding': 'gzip, deflate' };
	return xhr({ url: uri, followRedirects: 5, headers }).then(response => {
		return response.responseText;
	}, (error: XHRResponse) => {
		return Promise.reject(error.responseText || getErrorStatusDescription(error.status) || error.toString());
	});
};

function getSchemaRequestService(handledSchemas: string[] = ['https', 'http', 'file']) {
	const builtInHandlers: { [protocol: string]: SchemaRequestService } = {};
	for (let protocol of handledSchemas) {
		if (protocol === 'file') {
			builtInHandlers[protocol] = fileRequestService;
		} else if (protocol === 'http' || protocol === 'https') {
			builtInHandlers[protocol] = httpRequestService;
		}
	}
	return (uri: string): Thenable<string> => {
		const protocol = uri.substr(0, uri.indexOf(':'));

		const builtInHandler = builtInHandlers[protocol];
		if (builtInHandler) {
			return builtInHandler(uri);
		}
		return connection.sendRequest(VSCodeContentRequest.type, uri).then(responseText => {
			return responseText;
		}, error => {
			return Promise.reject(error.message);
		});
	};
}

// create the JSON language service
let languageService = getLanguageService({
	workspaceContext,
	contributions: [],
	clientCapabilities: ClientCapabilities.LATEST
});

// Create a text document manager.
const documents: TextDocuments = new TextDocuments();

{'fraction_non_alphanumeric': 0.17273070628226112, 'fraction_numerical': 0.001384867178647866, 'mean_word_length': 4.281914893617022, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 37, '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': '4043042', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2127, 'n_words': 560}