<commit_before>import {
    createConnection,
    Diagnostic,
    DiagnosticSeverity,
    ErrorMessageTracker,
    Files,
    IConnection,
    InitializeError,
    InitializeParams,
    InitializeResult,
    IPCMessageReader,
    IPCMessageWriter,
    ResponseError,
    TextDocument,
    TextDocuments,
} from 'vscode-languageserver';
import { ExecuteCommandParams } from 'vscode-languageserver/lib/protocol';
import Uri from 'vscode-uri';
import * as fs from "fs";
import * as path from "path";
import * as _ from "lodash";
import { Settings } from "./settings";
import { Linter } from "./linters/linter";
import { Flexelint } from './linters/flexelint';
import { CppCheck } from './linters/cppcheck';

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

// Create a simple text document manager. The text document manager supports full document sync only.
const documents: TextDocuments = new TextDocuments();

// Make the text document manager listen on the connection for open, change, and close text document events.
documents.listen(connection);

let settings: Settings;
let linters: Linter[] = [];

// After the server has started the client sends an initialize request.
// The server receives in the passed params the rootPath of the workspace plus the client capabilities.
let workspaceRoot: string;
connection.onInitialize((params): InitializeResult => {
    let workspaceUri = Uri.parse(params.rootUri!);

    if (workspaceUri.scheme !== 'file') {
        console.log('Workspace root URI is not of the file scheme.');
    } else {
        workspaceRoot = workspaceUri.fsPath;
    }

    let result: InitializeResult = {
        capabilities: {
            textDocumentSync: documents.syncKind
        }
    };

    return result;
});

// The content of a text document has changed.
// This event is emitted when the text document is first opened or when its content has changed.
documents.onDidChangeContent((event) => {
    if (settings['c-cpp-flylint'].run === "onType") {
        validateTextDocument(event.document);
    }
});

documents.onDidSave((event) => {
    if (settings['c-cpp-flylint'].run === "onSave" && settings['c-cpp-flylint'].run === "onType") {
        validateTextDocument(event.document);
    }
});

// A text document was closed. Clear the diagnostics.
documents.onDidClose((event) => {
    connection.sendDiagnostics({uri: event.document.uri, diagnostics: []});
});

function validateTextDocument(textDocument: TextDocument): void {
    let fileUri = Uri.parse(textDocument.uri);
    let filePath = fileUri.fsPath;

    if (fileUri.scheme !== 'file') {
        // lint can only lint files on disk.
        return;
    }

    if (linters === undefined || linters === null) {
        // cannot perform lint without active configuration!
        return;
    }

    const documentLines: string[] = textDocument.getText().replace(/\r/g, '').split('\n');

    const diagnostics: Diagnostic[] = [];

    const relativePath = path.relative(workspaceRoot, filePath);

    // deep-copy current items, so mid-stream configuration change doesn't spoil the party
    const lintersCopy : Linter[] = _.cloneDeep(linters);<commit_msg>Added some type information to server component<commit_after>import {
    createConnection,
    Diagnostic,
    DiagnosticSeverity,
    ErrorMessageTracker,
    Files,
    IConnection,
    InitializeError,
    InitializeParams,
    InitializeResult,
    IPCMessageReader,
    IPCMessageWriter,
    ResponseError,
    TextDocument,
    TextDocuments,
} from 'vscode-languageserver';
import { ExecuteCommandParams } from 'vscode-languageserver/lib/protocol';
import Uri from 'vscode-uri';
import * as fs from "fs";
import * as path from "path";
import * as _ from "lodash";
import { Settings } from "./settings";
import { Linter } from "./linters/linter";
import { Flexelint } from './linters/flexelint';
import { CppCheck } from './linters/cppcheck';

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

// Create a simple text document manager. The text document manager supports full document sync only.
const documents: TextDocuments = new TextDocuments();

// Make the text document manager listen on the connection for open, change, and close text document events.
documents.listen(connection);

let settings: Settings;
let linters: Linter[] = [];

// After the server has started the client sends an initialize request.
// The server receives in the passed params the rootPath of the workspace plus the client capabilities.
let workspaceRoot: string;
connection.onInitialize((params): InitializeResult => {
    let workspaceUri = Uri.parse(params.rootUri!);

    if (workspaceUri.scheme !== 'file') {
        console.log('Workspace root URI is not of the file scheme.');
    } else {
        workspaceRoot = workspaceUri.fsPath;
    }

    let result: InitializeResult = {
        capabilities: {
            textDocumentSync: documents.syncKind
        }
    };

    return result;
});

// The content of a text document has changed.
// This event is emitted when the text document is first opened or when its content has changed.
documents.onDidChangeContent((event) => {
    if (settings['c-cpp-flylint'].run === "onType") {
        validateTextDocument(event.document);
    }
});

documents.onDidSave((event) => {
    if (settings['c-cpp-flylint'].run === "onSave" && settings['c-cpp-flylint'].run === "onType") {
        validateTextDocument(event.document);
    }
});

// A text document was closed. Clear the diagnostics.
documents.onDidClose((event) => {
    connection.sendDiagnostics({uri: event.document.uri, diagnostics: []});
});

function validateTextDocument(textDocument: TextDocument): void {
    let fileUri: Uri = Uri.parse(textDocument.uri);
    let filePath: string = fileUri.fsPath;

    if (fileUri.scheme !== 'file') {
        // lint can only lint files on disk.
        return;
    }

    if (linters === undefined || linters === null) {
        // cannot perform lint without active configuration!
        return;
    }

    const documentLines: string[] = textDocument.getText().replace(/\r/g, '').split('\n');

    const diagnostics: Diagnostic[] = [];

    const relativePath = path.relative(workspaceRoot, filePath);

    // deep-copy current items, so mid-stream configuration change doesn't spoil the party
    const lintersCopy : Linter[] = _.cloneDeep(linters);
{'fraction_non_alphanumeric': 0.11460369980448187, 'fraction_numerical': 0.0, 'mean_word_length': 3.614850798056905, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4103565', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1792, 'n_words': 583}