<commit_before>/// <reference path="../typings/node/node.d.ts"/>
/// <reference path="../typings/vscode-extension-vscode/es6.d.ts"/>


import {
    IConnection,
    createConnection,
    InitializeParams,
    InitializeResult,
    TextDocuments,
    TextDocumentPositionParams,
    Definition,
    ReferenceParams,
    Location,
    Hover,
    WorkspaceSymbolParams,
    DidOpenTextDocumentParams,
    DidCloseTextDocumentParams,
    SymbolInformation,
    RequestType,
    SymbolKind, Range
} from 'vscode-languageserver';

import * as ts from 'typescript';

import * as util from './util';
import TypeScriptService from './typescript-service';

namespace GlobalRefsRequest {
    export const type: RequestType<WorkspaceSymbolParams, SymbolInformation[], any> = {
        get method() {
            return 'textDocument/global-refs';
        }
    };
}

namespace InitializeRequest {
    export const type: RequestType<InitializeParams, InitializeResult, any> = {
        get method() {
            return 'initialize';
        }
    };
}

namespace ShutdownRequest {
    export const type = {
        get method() {
            return 'shutdown';
        }
    };
}

namespace ExitRequest {
    export const type = {
        get method() {
            return 'exit';
        }
    };
}

export default class Connection {

    private connection: IConnection;

    constructor(input: any, output: any, strict: boolean) {

        this.connection = createConnection(input, output);

        input.removeAllListeners('end');
        input.removeAllListeners('close');
        output.removeAllListeners('end');
        output.removeAllListeners('close');

        let workspaceRoot: string;

        let documents: TextDocuments = new TextDocuments();

        let closed = false;

        function close() {
            if (!closed) {
                input.close();
                output.close();
                closed = true;
            }
        }

        let service: TypeScriptService;

        let self = this;

        let initialized: Thenable<void> = null;

        this.connection.onRequest(InitializeRequest.type, (params: InitializeParams): Promise<InitializeResult> => {
            console.error('initialize', params.rootPath);
            return new Promise<InitializeResult>(function (resolve) {
                if (params.rootPath) {
                    workspaceRoot = util.uri2path(params.rootPath);
                    service = new TypeScriptService(workspaceRoot, strict, self.connection);
                    initialized = service.host.initialize(workspaceRoot);
                    resolve({
                        capabilities: {
                            // Tell the client that the server works in FULL text document sync mode
                            textDocumentSync: documents.syncKind,
                            hoverProvider: true,
                            definitionProvider: true,
                            referencesProvider: true
                        }
                    })
                }
            });
        });

        this.connection.onNotification(ExitRequest.type, function () {
            close();
        });

        this.connection.onRequest(ShutdownRequest.type, function () {
            return [];
        });

        this.connection.onDidOpenTextDocument((params: DidOpenTextDocumentParams) => {
            let relpath = util.uri2relpath(params.textDocument.uri, workspaceRoot);
            console.error('add file', workspaceRoot, '/', relpath);
            service.addFile(relpath, params.textDocument.text);
        });

        this.connection.onDidCloseTextDocument((params: DidCloseTextDocumentParams) => {
            let relpath = util.uri2relpath(params.textDocument.uri, workspaceRoot);
            console.error('remove file', workspaceRoot, '/', relpath);
            service.removeFile(relpath);
        });

        this.connection.onWorkspaceSymbol((params: WorkspaceSymbolParams): Promise<SymbolInformation[]> => {
            const enter = new Date().getTime();
            return new Promise<SymbolInformation[]>(function (resolve, reject) {
                initialized.then(function () {
                    let result = [];
                    const init = new Date().getTime();
                    try {
                        if (params.query == "exported") {
                            const exported = service.getExportedEnts();
                            if (exported) {
                                result = exported.map(ent => {
                                    return SymbolInformation.create(ent.name, ent.kind, ent.location.range,
                                        'file:///' + ent.location.file, util.formExternalUri(ent));
                                });
                            }
                        } else if (params.query == "externals") {
                            const externals = service.getExternalRefs();
                            if (externals) {
                                result = externals.map(external => {
                                    return SymbolInformation.create(external.name, util.formEmptyKind(), util.formEmptyRange(), util.formExternalUri(external));
                                });
                            }
                        } else if (params.query == '') {<commit_msg>Extend WorkspaceSymbol request with limit field<commit_after>/// <reference path="../typings/node/node.d.ts"/>
/// <reference path="../typings/vscode-extension-vscode/es6.d.ts"/>


import {
    IConnection,
    createConnection,
    InitializeParams,
    InitializeResult,
    TextDocuments,
    TextDocumentPositionParams,
    Definition,
    ReferenceParams,
    Location,
    Hover,
    WorkspaceSymbolParams,
    DidOpenTextDocumentParams,
    DidCloseTextDocumentParams,
    SymbolInformation,
    RequestType,
    SymbolKind, Range
} from 'vscode-languageserver';

import * as ts from 'typescript';

import * as util from './util';
import TypeScriptService from './typescript-service';

interface WorkspaceSymbolParamsWithLimit {
    query: string;
    limit: number;
}

namespace WorkspaceSymbolsRequest {
    export const type: RequestType<WorkspaceSymbolParamsWithLimit, SymbolInformation[], any> = {
        get method() {
            return 'workspace/symbol';
        }
    };
}



namespace GlobalRefsRequest {
    export const type: RequestType<WorkspaceSymbolParams, SymbolInformation[], any> = {
        get method() {
            return 'textDocument/global-refs';
        }
    };
}



namespace InitializeRequest {
    export const type: RequestType<InitializeParams, InitializeResult, any> = {
        get method() {
            return 'initialize';
        }
    };
}

namespace ShutdownRequest {
    export const type = {
        get method() {
            return 'shutdown';
        }
    };
}

namespace ExitRequest {
    export const type = {
        get method() {
            return 'exit';
        }
    };
}

export default class Connection {

    private connection: IConnection;

    constructor(input: any, output: any, strict: boolean) {

        this.connection = createConnection(input, output);

        input.removeAllListeners('end');
        input.removeAllListeners('close');
        output.removeAllListeners('end');
        output.removeAllListeners('close');

        let workspaceRoot: string;

        let documents: TextDocuments = new TextDocuments();

        let closed = false;

        function close() {
            if (!closed) {
                input.close();
                output.close();
                closed = true;
            }
        }

        let service: TypeScriptService;

        let self = this;

        let initialized: Thenable<void> = null;

        this.connection.onRequest(InitializeRequest.type, (params: InitializeParams): Promise<InitializeResult> => {
            console.error('initialize', params.rootPath);
            return new Promise<InitializeResult>(function (resolve) {
                if (params.rootPath) {
                    workspaceRoot = util.uri2path(params.rootPath);
                    service = new TypeScriptService(workspaceRoot, strict, self.connection);
                    initialized = service.host.initialize(workspaceRoot);
                    resolve({
                        capabilities: {
                            // Tell the client that the server works in FULL text document sync mode
                            textDocumentSync: documents.syncKind,
                            hoverProvider: true,
                            definitionProvider: true,
                            referencesProvider: true
                        }
                    })
                }
            });
        });

        this.connection.onNotification(ExitRequest.type, function () {
            close();
        });

        this.connection.onRequest(ShutdownRequest.type, function () {
            return [];
        });

        this.connection.onDidOpenTextDocument((params: DidOpenTextDocumentParams) => {
            let relpath = util.uri2relpath(params.textDocument.uri, workspaceRoot);
            console.error('add file', workspaceRoot, '/', relpath);
            service.addFile(relpath, params.textDocument.text);
        });

        this.connection.onDidCloseTextDocument((params: DidCloseTextDocumentParams) => {
            let relpath = util.uri2relpath(params.textDocument.uri, workspaceRoot);
            console.error('remove file', workspaceRoot, '/', relpath);
            service.removeFile(relpath);
        });

        this.connection.onRequest(WorkspaceSymbolsRequest.type, (params: WorkspaceSymbolParamsWithLimit): Promise<SymbolInformation[]> => {
              const enter = new Date().getTime();
            return new Promise<SymbolInformation[]>(function (resolve, reject) {
                initialized.then(function () {
                    let result = [];
                    const init = new Date().getTime();
                    try {
                        if (params.query == "exported") {
                            const exported = service.getExportedEnts();
                            if (exported) {
                                result = exported.map(ent => {
                                    return SymbolInformation.create(ent.name, ent.kind, ent.location.range,
                                        'file:///' + ent.location.file, util.formExternalUri(ent));
                                });
                            }
                        } else if (params.query == "externals") {
                            const externals = service.getExternalRefs();
                            if (externals) {
                                result = externals.map(external => {
                                    return SymbolInformation.create(external.name, util.formEmptyKind(), util.formEmptyRange(), util.formExternalUri(external));
                                });
                            }
                        } else if (params.query == '') {
{'fraction_non_alphanumeric': 0.10118940349612543, 'fraction_numerical': 0.0007208506037123806, 'mean_word_length': 1.7004866180048661, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '719898', 'n_tokens_mistral': 2634, 'n_tokens_neox': 2561, 'n_words': 542}