<commit_before>// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const _ = require('lodash');

const findDiagram = require('./find-diagram');

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
    class MermaidDocumentContentProvider {
        constructor () {
            this._onDidChange =  new vscode.EventEmitter();
            this.graph = '';
            this.update = _.throttle(this.unthrottledUpdate, 250);
        }

        provideTextDocumentContent (uri, token) {
            const config = JSON.stringify(vscode.workspace.getConfiguration('mermaid'));
            
            return `<!DOCTYPE html>
            <html>
            <head>
            <script src="${context.extensionPath}/node_modules/mermaid/dist/mermaid.min.js"></script>
            </head>
            <body>
                <div class="mermaid">
                ${this.graph}
                </div>
                <script type="text/javascript">
                    var css = document.createElement('link');
                    
                    css.setAttribute('rel', 'stylesheet');
                    css.setAttribute('type', 'text/css');
                    
                    if (document.body.classList.contains('vscode-dark')) {
                        css.setAttribute('href', '${context.extensionPath}/node_modules/mermaid/dist/mermaid.dark.css');
                    } else {
                        css.setAttribute('href', '${context.extensionPath}/node_modules/mermaid/dist/mermaid.forest.css');
                    }
                    document.head.appendChild(css);

                    mermaidAPI.initialize(JSON.parse('${config}'));
                </script>
            </body>`;
        }
        
        get onDidChange () {
            return this._onDidChange.event;
        }
        
        unthrottledUpdate (uri) {
            const editor = vscode.window.activeTextEditor;
            const text = editor.document.getText();
            const selStart = editor.document.offsetAt(editor.selection.anchor);
            
            const graph = findDiagram(text, selStart);
            
            if (graph) {
                this.graph = graph;
            } else {
                console.log("Cannot determine the rule's properties.");
                this.graph = 'select a diagram...'
            }
            
            this._onDidChange.fire(uri);
        }
    }
    
    const registerCommand = vscode.commands.registerCommand;
    let previewUri = vscode.Uri.parse('mermaid-preview://authority/mermaid-preview');
    
    let provider = new MermaidDocumentContentProvider();
    let registration = vscode.workspace.registerTextDocumentContentProvider('mermaid-preview', provider);

    // Use the console to output diagnostic information (console.log) and errors (console.error)
    // This line of code will only be executed once when your extension is activated
    console.log('Congratulations, your extension "mermaid-preview" is now active!');

    // The command has been defined in the package.json file
    // Now provide the implementation of the command with  registerCommand
    // The commandId parameter must match the command field in package.json
    let disposable = registerCommand('extension.previewMermaidDiagram', () => {
        return vscode.commands.executeCommand('vscode.previewHtml', previewUri, vscode.ViewColumn.Two, 'Mermaid Preview').then((success) => {
        }, (reason) => {
            vscode.window.showErrorMessage(reason);
        });
    });
    
    vscode.workspace.onDidChangeTextDocument((e) => {
        if (e.document === vscode.window.activeTextEditor.document) {
            provider.update(previewUri);
        }
    });

    vscode.window.onDidChangeTextEditorSelection((e) => {
        if (e.textEditor === vscode.window.activeTextEditor) {
            provider.update(previewUri);
        }
    });
    
    context.subscriptions.push(disposable, registration);
}
exports.activate = activate;

// this method is called when your extension is deactivated
function deactivate() {
}
exports.deactivate = deactivate;<commit_msg>refactored content provider class and html<commit_after>// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const _ = require('lodash');

const findDiagram = require('./find-diagram');

class MermaidDocumentContentProvider {
    constructor (context) {
        this._onDidChange =  new vscode.EventEmitter();
        this.graph = '';
        this.update = _.throttle(this.unthrottledUpdate, 250);

        this.context = context;
    }

    provideTextDocumentContent (uri, token) {
        const config = JSON.stringify(vscode.workspace.getConfiguration('mermaid'));

        return `<!DOCTYPE html>
        <html>
        <head>
            <script src="${this.context.extensionPath}/node_modules/mermaid/dist/mermaid.min.js"></script>
        </head>
        <body>
            <div class="mermaid">
            ${this.graph}
            </div>

            <script type="text/javascript">
                css = document.createElement('link');
                style = document.body.classList.contains('vscode-dark') ? 'dark' : 'forest';
                
                css.setAttribute('rel', 'stylesheet');
                css.setAttribute('type', 'text/css');
                css.setAttribute('href', '${this.context.extensionPath}/node_modules/mermaid/dist/mermaid.' + style + '.css');

                document.head.appendChild(css);
                mermaidAPI.initialize(JSON.parse('${config}'));
            </script>
        </body>`;
    }
    
    get onDidChange () {
        return this._onDidChange.event;
    }
    
    unthrottledUpdate (uri) {
        const editor = vscode.window.activeTextEditor;
        const text = editor.document.getText();
        const selStart = editor.document.offsetAt(editor.selection.anchor);
        
        const graph = findDiagram(text, selStart);
        
        if (graph) {
            this.graph = graph;
        } else {
            console.log("Cannot determine the rule's properties.");
            this.graph = 'select a diagram...'
        }
        
        this._onDidChange.fire(uri);
    }
}

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
    const registerCommand = vscode.commands.registerCommand;
    let previewUri = vscode.Uri.parse('mermaid-preview://authority/mermaid-preview');
    
    let provider = new MermaidDocumentContentProvider(context);
    let registration = vscode.workspace.registerTextDocumentContentProvider('mermaid-preview', provider);

    // Use the console to output diagnostic information (console.log) and errors (console.error)
    // This line of code will only be executed once when your extension is activated
    console.log('Congratulations, your extension "mermaid-preview" is now active!');

    // The command has been defined in the package.json file
    // Now provide the implementation of the command with  registerCommand
    // The commandId parameter must match the command field in package.json
    let disposable = registerCommand('extension.previewMermaidDiagram', () => {
        return vscode.commands.executeCommand('vscode.previewHtml', previewUri, vscode.ViewColumn.Two, 'Mermaid Preview').then((success) => {
        }, (reason) => {
            vscode.window.showErrorMessage(reason);
        });
    });
    
    vscode.workspace.onDidChangeTextDocument((e) => {
        if (e.document === vscode.window.activeTextEditor.document) {
            provider.update(previewUri);
        }
    });

    vscode.window.onDidChangeTextEditorSelection((e) => {
        if (e.textEditor === vscode.window.activeTextEditor) {
            provider.update(previewUri);
        }
    });
    
    context.subscriptions.push(disposable, registration);
}
exports.activate = activate;

// this method is called when your extension is deactivated
function deactivate() {
}
exports.deactivate = deactivate;
{'fraction_non_alphanumeric': 0.11189144149506011, 'fraction_numerical': 0.0007142006903940007, 'mean_word_length': 2.3104806934594166, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 37, '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': '7407615', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2177, 'n_words': 507}