<commit_before>import {
    Disposable,
    Position,
    Range,
    TextDocumentContentChangeEvent,
    TextEditor,
    TextLine,
    window,
    workspace,
} from "vscode";
import { Config, ConfigType } from "../Config";
import CodeParser from "./CodeParser";
import CParser from "./CParser/CParser";
import CppParser from "./CParser/CppParser";

/**
 *
 * Checks if the event matches the specified guidelines and if a parser exists for this language
 *
 * @export
 * @class CodeParserController
 */
export default class CodeParserController {
    private disposable: Disposable;
    private triggerSequence: string;

    /**
     * Creates an instance of CodeParserController
     *
     * @memberOf CodeParserController
     */
    public constructor() {
        const subscriptions: Disposable[] = [];

        // Hand off the event to the parser if a valid parser is found
        workspace.onDidChangeTextDocument((event) => {
            const activeEditor: TextEditor = window.activeTextEditor;
            if (activeEditor && event.document === activeEditor.document) {
                this.readConfig();

                this.onEvent(activeEditor, event.contentChanges[0]);
            }
        }, this, subscriptions);

        this.disposable = Disposable.from(...subscriptions);
    }

    /**
     *
     * Disposes of the subscriptions
     *
     * @memberOf CodeParserController
     */
    public dispose() {
        this.disposable.dispose();
    }

    /***************************************************************************
                                    Implementation
     ***************************************************************************/

    private readConfig() {
        this.triggerSequence = workspace
            .getConfiguration(ConfigType.generic)
            .get<string>(Config.triggerSequence, "/**");
    }

    private check(activeEditor: TextEditor, event: TextDocumentContentChangeEvent): boolean {
        if (activeEditor == null || event.text == null) {
            return false;
        }
        const activeSelection: Position = activeEditor.selection.active;
        const activeLine: TextLine = activeEditor.document.lineAt(activeSelection.line);
        const activeChar: string = activeLine.text.charAt(activeSelection.character);
        const startsWith: boolean = event.text.startsWith("\n") || event.text.startsWith("\r\n");

        // Check if enter was pressed. Note the !
        if (!((activeChar === "") && startsWith)) {
            return false;
        }

        const cont: string = activeLine.text.trim();

        return this.triggerSequence === cont;
    }

    private onEvent(activeEditor: TextEditor, event: TextDocumentContentChangeEvent) {
        if (!this.check(activeEditor, event)) {
            return null;
        }

        const lang: string = activeEditor.document.languageId;
        let parser: CodeParser;

        switch (lang) {
            case "c":
                parser = new CParser();
                break;
            case "cpp":
                parser = new CppParser();
                break;
            default:
                // tslint:disable-next-line:no-console
                console.log("No comments can be generated for language: " + lang);
                return null;
        }

        const currentPos: Position = window.activeTextEditor.selection.active;
        const startReplace: Position = new Position(
            currentPos.line,
            currentPos.character - this.triggerSequence.length,
        );

        const nextLineText: string = window.activeTextEditor.document.lineAt(endReplace.line + 1).text;
        const endReplace = new Position(currentPos.line + 1, nextLineText.length);

        parser.Parse(activeEditor, event).GenerateDoc(new Range(startReplace, endReplace));
    }
}<commit_msg>-- Fixed error where endReplace was used before declaration.<commit_after>import {
    Disposable,
    Position,
    Range,
    TextDocumentContentChangeEvent,
    TextEditor,
    TextLine,
    window,
    workspace,
} from "vscode";
import { Config, ConfigType } from "../Config";
import CodeParser from "./CodeParser";
import CParser from "./CParser/CParser";
import CppParser from "./CParser/CppParser";

/**
 *
 * Checks if the event matches the specified guidelines and if a parser exists for this language
 *
 * @export
 * @class CodeParserController
 */
export default class CodeParserController {
    private disposable: Disposable;
    private triggerSequence: string;

    /**
     * Creates an instance of CodeParserController
     *
     * @memberOf CodeParserController
     */
    public constructor() {
        const subscriptions: Disposable[] = [];

        // Hand off the event to the parser if a valid parser is found
        workspace.onDidChangeTextDocument((event) => {
            const activeEditor: TextEditor = window.activeTextEditor;
            if (activeEditor && event.document === activeEditor.document) {
                this.readConfig();

                this.onEvent(activeEditor, event.contentChanges[0]);
            }
        }, this, subscriptions);

        this.disposable = Disposable.from(...subscriptions);
    }

    /**
     *
     * Disposes of the subscriptions
     *
     * @memberOf CodeParserController
     */
    public dispose() {
        this.disposable.dispose();
    }

    /***************************************************************************
                                    Implementation
     ***************************************************************************/

    private readConfig() {
        this.triggerSequence = workspace
            .getConfiguration(ConfigType.generic)
            .get<string>(Config.triggerSequence, "/**");
    }

    private check(activeEditor: TextEditor, event: TextDocumentContentChangeEvent): boolean {
        if (activeEditor == null || event.text == null) {
            return false;
        }
        const activeSelection: Position = activeEditor.selection.active;
        const activeLine: TextLine = activeEditor.document.lineAt(activeSelection.line);
        const activeChar: string = activeLine.text.charAt(activeSelection.character);
        const startsWith: boolean = event.text.startsWith("\n") || event.text.startsWith("\r\n");

        // Check if enter was pressed. Note the !
        if (!((activeChar === "") && startsWith)) {
            return false;
        }

        const cont: string = activeLine.text.trim();

        return this.triggerSequence === cont;
    }

    private onEvent(activeEditor: TextEditor, event: TextDocumentContentChangeEvent) {
        if (!this.check(activeEditor, event)) {
            return null;
        }

        const lang: string = activeEditor.document.languageId;
        let parser: CodeParser;

        switch (lang) {
            case "c":
                parser = new CParser();
                break;
            case "cpp":
                parser = new CppParser();
                break;
            default:
                // tslint:disable-next-line:no-console
                console.log("No comments can be generated for language: " + lang);
                return null;
        }

        const currentPos: Position = window.activeTextEditor.selection.active;
        const startReplace: Position = new Position(
            currentPos.line,
            currentPos.character - this.triggerSequence.length,
        );

        const nextLineText: string = window.activeTextEditor.document.lineAt(startReplace.line + 1).text;
        const endReplace = new Position(currentPos.line + 1, nextLineText.length);

        parser.Parse(activeEditor, event).GenerateDoc(new Range(startReplace, endReplace));
    }
}
{'fraction_non_alphanumeric': 0.13643991222408675, 'fraction_numerical': 0.0007744933522653931, 'mean_word_length': 2.35121107266436, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7376600', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1850, 'n_words': 474}