<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 { CancellationToken, Connection, InitializeParams, InitializeResult, NotebookDocuments, TextDocuments } from 'vscode-languageserver';
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as lsp from 'vscode-languageserver-types';
import * as md from 'vscode-markdown-languageservice';
import { URI } from 'vscode-uri';
import { getLsConfiguration, LsConfiguration } from './config';
import { ConfigurationManager } from './configuration';
import { registerValidateSupport } from './languageFeatures/diagnostics';
import { LogFunctionLogger } from './logging';
import * as protocol from './protocol';
import { IDisposable } from './util/dispose';
import { VsCodeClientWorkspace } from './workspace';

interface MdServerInitializationOptions extends LsConfiguration { }

export async function startServer(connection: Connection) {
	const documents = new TextDocuments(TextDocument);
	const notebooks = new NotebookDocuments(documents);

	const configurationManager = new ConfigurationManager(connection);

	let mdLs: md.IMdLanguageService | undefined;
	let workspace: VsCodeClientWorkspace | undefined;

	connection.onInitialize((params: InitializeParams): InitializeResult => {
		const parser = new class implements md.IMdParser {
			slugifier = md.githubSlugifier;

			async tokenize(document: md.ITextDocument): Promise<md.Token[]> {
				return await connection.sendRequest(protocol.parse, { uri: document.uri.toString() });
			}
		};

		const initOptions = params.initializationOptions as MdServerInitializationOptions | undefined;
		const config = getLsConfiguration(initOptions ?? {});

		const logger = new LogFunctionLogger(connection.console.log.bind(connection.console));
		workspace = new VsCodeClientWorkspace(connection, config, documents, notebooks, logger);
		mdLs = md.createLanguageService({
			workspace,
			parser,
			logger,
			markdownFileExtensions: config.markdownFileExtensions,
			excludePaths: config.excludePaths,
		});

		registerCompletionsSupport(connection, documents, mdLs, configurationManager);
		registerValidateSupport(connection, workspace, mdLs, configurationManager, logger);

		workspace.workspaceFolders = (params.workspaceFolders ?? []).map(x => URI.parse(x.uri));
		return {
			capabilities: {
				diagnosticProvider: {
					documentSelector: null,
					identifier: 'markdown',
					interFileDependencies: true,
					workspaceDiagnostics: false,
				},
				completionProvider: { triggerCharacters: ['.', '/', '#'] },
				definitionProvider: true,
				documentLinkProvider: { resolveProvider: true },
				documentSymbolProvider: true,
				foldingRangeProvider: true,<commit_msg>Fix markdown not registering find all references support (#159242)<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 { CancellationToken, Connection, InitializeParams, InitializeResult, NotebookDocuments, TextDocuments } from 'vscode-languageserver';
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as lsp from 'vscode-languageserver-types';
import * as md from 'vscode-markdown-languageservice';
import { URI } from 'vscode-uri';
import { getLsConfiguration, LsConfiguration } from './config';
import { ConfigurationManager } from './configuration';
import { registerValidateSupport } from './languageFeatures/diagnostics';
import { LogFunctionLogger } from './logging';
import * as protocol from './protocol';
import { IDisposable } from './util/dispose';
import { VsCodeClientWorkspace } from './workspace';

interface MdServerInitializationOptions extends LsConfiguration { }

export async function startServer(connection: Connection) {
	const documents = new TextDocuments(TextDocument);
	const notebooks = new NotebookDocuments(documents);

	const configurationManager = new ConfigurationManager(connection);

	let mdLs: md.IMdLanguageService | undefined;
	let workspace: VsCodeClientWorkspace | undefined;

	connection.onInitialize((params: InitializeParams): InitializeResult => {
		const parser = new class implements md.IMdParser {
			slugifier = md.githubSlugifier;

			async tokenize(document: md.ITextDocument): Promise<md.Token[]> {
				return await connection.sendRequest(protocol.parse, { uri: document.uri.toString() });
			}
		};

		const initOptions = params.initializationOptions as MdServerInitializationOptions | undefined;
		const config = getLsConfiguration(initOptions ?? {});

		const logger = new LogFunctionLogger(connection.console.log.bind(connection.console));
		workspace = new VsCodeClientWorkspace(connection, config, documents, notebooks, logger);
		mdLs = md.createLanguageService({
			workspace,
			parser,
			logger,
			markdownFileExtensions: config.markdownFileExtensions,
			excludePaths: config.excludePaths,
		});

		registerCompletionsSupport(connection, documents, mdLs, configurationManager);
		registerValidateSupport(connection, workspace, mdLs, configurationManager, logger);

		workspace.workspaceFolders = (params.workspaceFolders ?? []).map(x => URI.parse(x.uri));
		return {
			capabilities: {
				diagnosticProvider: {
					documentSelector: null,
					identifier: 'markdown',
					interFileDependencies: true,
					workspaceDiagnostics: false,
				},
				completionProvider: { triggerCharacters: ['.', '/', '#'] },
				definitionProvider: true,
				documentLinkProvider: { resolveProvider: true },
				documentSymbolProvider: true,
				foldingRangeProvider: true,
				referencesProvider: true,
{'fraction_non_alphanumeric': 0.15780866721177433, 'fraction_numerical': 0.0009811937857726902, 'mean_word_length': 3.857823669579031, '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': '1561445', 'n_tokens_mistral': 1682, 'n_tokens_neox': 1499, 'n_words': 393}