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

import {
    IPCMessageReader, IPCMessageWriter,
    createConnection, IConnection, TextDocumentSyncKind,
    TextDocuments, TextDocument, Diagnostic, DiagnosticSeverity,
    InitializeParams, InitializeResult, TextDocumentPositionParams,
    CompletionItem, CompletionItemKind, Hover, SignatureHelp
} from 'vscode-languageserver'
import Ycm, {Settings} from './ycm'
import * as _ from 'lodash'
import {logger, loggerInit} from './utils'

// Create a connection for the server. The connection uses Node's IPC as a transport
let connection: IConnection = createConnection(new IPCMessageReader(process), new IPCMessageWriter(process))

// Create a simple text document manager. The text document manager
// supports full document sync only
let documents: TextDocuments = new TextDocuments()
// Make the text document manager listen on the connection
// for open, change and close text document events
documents.listen(connection)

// After the server has started the client sends an initilize request. The server receives
// in the passed params the rootPath of the workspace plus the client capabilites. 
let workspaceRoot: string
let workspaceConfiguration: Settings

connection.onInitialize((params): InitializeResult => {
    workspaceRoot = params.rootPath
    return {
        capabilities: {
            // Tell the client that the server works in FULL text document sync mode
            textDocumentSync: documents.syncKind,
            // Tell the client that the server support code complete
            completionProvider: {
                resolveProvider: true,
                triggerCharacters: ['.', ':', '<', '"', '=', '/', '>', '*', '&']
            },
            hoverProvider: true,
            definitionProvider: true,
            // signatureHelpProvider: {
            //     triggerCharacters: ['(']
            // }
        }
    }
})

connection.onHover(async (event): Promise<Hover> => {
    const ycm = await getYcm()
    try {
        return await ycm.getType(event.textDocument.uri, event.position, documents)
    } catch (err) {
        logger(`onHover error`, err)
    }
})

connection.onDefinition(async (event) => {
    const ycm = await getYcm()
    try {
        return await ycm.goToDefinition(event.textDocument.uri, event.position, documents)
    } catch (err) {
        logger(`onDefinition error`, err)
    }
})

// The content of a text document has changed. This event is emitted
// when the text document first opened or when its content has changed.
documents.onDidChangeContent(async (change) => {
    logger(`onDidChangeContent ${JSON.stringify(change.document.uri)}`)
    const ycm = await getYcm()
    // connection.sendDiagnostics({
    //     uri: change.document.uri,
    //     diagnostics: await ycm.readyToParse(change.document, documents)
    // })
    ycm.currentIdentifierFinished(change.document.uri, documents)
    // await getIssues(change.document)
    // validateTextDocument(change.document)
})

// The settings interface describe the server relevant settings part

async function getYcm(): Promise<Ycm> {
    if (!workspaceRoot || !workspaceConfiguration)
        return await new Promise<Ycm>((resolve, reject) => setTimeout(() => getYcm(), 100))
    try {
        return await Ycm.getInstance(workspaceRoot, workspaceConfiguration, connection.window)
    } catch (err) {
        logger('getYcm error', err)
        connection.window.showErrorMessage(`Ycm startup failed. Please check your ycmd or python path. Detail: ${err.message || err}`)
    }
}

async function getIssues(document: TextDocument) {
    const ycm = await getYcm()
    connection.sendDiagnostics({
        uri: document.uri,
        diagnostics: await ycm.readyToParse(document.uri, documents)
    })
}

// connection.onSignatureHelp((event) => {
//     logger(`onSignatureHelp: ${JSON.stringify(event)}`)
//     return {
//         signatures: [{
//             label: 'test1',
//             documentation: ' test1 test1 test1 test1 test1 test1 test1 test1',
//             parameters: [{
//                 label: 'string',
//                 documentation: 'string string string'
//             }, {
//                 label: 'int',
//                 documentation: 'int int int'
//             }]
//         }, {
//             label: 'test2',
//             documentation: ' test2 test2 test2 test2 test2 test2 test2 test2',
//             parameters: [{
//                 label: 'int',
//                 documentation: 'string string string'
//             }, {
//                 label: 'string',
//                 documentation: 'int int int'
//             }]
//         }]
//     } as SignatureHelp
//     // try {
//     //     // const ycm = await getYcm()
//     //     // await ycm.getDocQuick(event.textDocument.uri, event.position, documents)
        
//     // } catch (err) {
//     //     logger('onSignatureHelp error', err)
//     // }
// })


// The settings have changed. Is send on server activation
// as well.
connection.onDidChangeConfiguration(async (change) => {
    let settings = <Settings>change.settings
    loggerInit(settings.ycmd.debug)
    logger(`onDidChangeConfiguration settings`, JSON.stringify(settings))
    try {
        ensureValidConfiguration(settings)
        workspaceConfiguration = settings
    } catch (err) {
        connection.window.showErrorMessage(`[Ycm] ${err.message || err}`)
    }
    await getYcm()<commit_msg>fix lint.<commit_after>/* --------------------------------------------------------------------------------------------
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 * ------------------------------------------------------------------------------------------ */
'use strict'

import {
    IPCMessageReader, IPCMessageWriter,
    createConnection, IConnection, TextDocumentSyncKind,
    TextDocuments, TextDocument, Diagnostic, DiagnosticSeverity,
    InitializeParams, InitializeResult, TextDocumentPositionParams,
    CompletionItem, CompletionItemKind, Hover, SignatureHelp
} from 'vscode-languageserver'
import Ycm, {Settings} from './ycm'
import * as _ from 'lodash'
import {logger, loggerInit} from './utils'

// Create a connection for the server. The connection uses Node's IPC as a transport
let connection: IConnection = createConnection(new IPCMessageReader(process), new IPCMessageWriter(process))

// Create a simple text document manager. The text document manager
// supports full document sync only
let documents: TextDocuments = new TextDocuments()
// Make the text document manager listen on the connection
// for open, change and close text document events
documents.listen(connection)

// After the server has started the client sends an initilize request. The server receives
// in the passed params the rootPath of the workspace plus the client capabilites. 
let workspaceRoot: string
let workspaceConfiguration: Settings

connection.onInitialize((params): InitializeResult => {
    workspaceRoot = params.rootPath
    return {
        capabilities: {
            // Tell the client that the server works in FULL text document sync mode
            textDocumentSync: documents.syncKind,
            // Tell the client that the server support code complete
            completionProvider: {
                resolveProvider: true,
                triggerCharacters: ['.', ':', '<', '"', '=', '/', '>', '*', '&']
            },
            hoverProvider: true,
            definitionProvider: true,
            // signatureHelpProvider: {
            //     triggerCharacters: ['(']
            // }
        }
    }
})

connection.onHover(async (event): Promise<Hover> => {
    const ycm = await getYcm()
    try {
        return await ycm.getType(event.textDocument.uri, event.position, documents)
    } catch (err) {
        logger(`onHover error`, err)
    }
})

connection.onDefinition(async (event) => {
    const ycm = await getYcm()
    try {
        return await ycm.goToDefinition(event.textDocument.uri, event.position, documents)
    } catch (err) {
        logger(`onDefinition error`, err)
    }
})

// The content of a text document has changed. This event is emitted
// when the text document first opened or when its content has changed.
documents.onDidChangeContent(async (change) => {
    logger(`onDidChangeContent ${JSON.stringify(change.document.uri)}`)
    const ycm = await getYcm()
    // connection.sendDiagnostics({
    //     uri: change.document.uri,
    //     diagnostics: await ycm.readyToParse(change.document, documents)
    // })
    ycm.currentIdentifierFinished(change.document.uri, documents)
    // await getIssues(change.document)
    // validateTextDocument(change.document)
})

// The settings interface describe the server relevant settings part

async function getYcm(): Promise<Ycm> {
    if (!workspaceRoot || !workspaceConfiguration)
        return await new Promise<Ycm>((resolve, reject) => setTimeout(() => getYcm(), 100))
    try {
        return await Ycm.getInstance(workspaceRoot, workspaceConfiguration, connection.window)
    } catch (err) {
        logger('getYcm error', err)
        connection.window.showErrorMessage(`Ycm startup failed. Please check your ycmd or python path. Detail: ${err.message || err}`)
    }
}

async function getIssues(document: TextDocument) {
    const ycm = await getYcm()
    connection.sendDiagnostics({
        uri: document.uri,
        diagnostics: await ycm.readyToParse(document.uri, documents)
    })
}

// connection.onSignatureHelp((event) => {
//     logger(`onSignatureHelp: ${JSON.stringify(event)}`)
//     return {
//         signatures: [{
//             label: 'test1',
//             documentation: ' test1 test1 test1 test1 test1 test1 test1 test1',
//             parameters: [{
//                 label: 'string',
//                 documentation: 'string string string'
//             }, {
//                 label: 'int',
//                 documentation: 'int int int'
//             }]
//         }, {
//             label: 'test2',
//             documentation: ' test2 test2 test2 test2 test2 test2 test2 test2',
//             parameters: [{
//                 label: 'int',
//                 documentation: 'string string string'
//             }, {
//                 label: 'string',
//                 documentation: 'int int int'
//             }]
//         }]
//     } as SignatureHelp
//     // try {
//     //     // const ycm = await getYcm()
//     //     // await ycm.getDocQuick(event.textDocument.uri, event.position, documents)

//     // } catch (err) {
//     //     logger('onSignatureHelp error', err)
//     // }
// })


// The settings have changed. Is send on server activation
// as well.
connection.onDidChangeConfiguration(async (change) => {
    let settings = <Settings>change.settings
    loggerInit(settings.ycmd.debug)
    logger(`onDidChangeConfiguration settings`, JSON.stringify(settings))
    try {
        ensureValidConfiguration(settings)
        workspaceConfiguration = settings
    } catch (err) {
        connection.window.showErrorMessage(`[Ycm] ${err.message || err}`)
    }
    await getYcm()
{'fraction_non_alphanumeric': 0.1435361216730038, 'fraction_numerical': 0.003629450397511234, 'mean_word_length': 2.2831205673758865, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, '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': '7017744', 'n_tokens_mistral': 2960, 'n_tokens_neox': 2931, 'n_words': 862}