<commit_before>'use strict';

import { commands, ExtensionContext, Range, Position, TextEdit, window, workspace } from 'vscode';
const prettier = require('prettier')

export function activate(context: ExtensionContext) {
    const eventDisposable = (workspace as any).onWillSaveTextDocument(e => {
        const document = e.document;

        if (!document.isDirty) {
            return;
        }

        const config = workspace.getConfiguration('prettier');
        const formatOnSave = (config as any).formatOnSave;
        if (!formatOnSave) {
            return;
        }

        e.waitUntil(new Promise(resolve => {
            const prettified = format(document, null);
            const rangeObj = new Range(0, 0, document.lineCount, 0);
            const edit = TextEdit.replace(rangeObj, prettified);

            resolve([edit]);
        }))
    });

    const disposable = commands.registerCommand('prettier.format', () => {
        let editor = window.activeTextEditor;
        if (!editor) {
            return;
        }

        const selection = editor.selection;
        const prettified = format(editor.document, selection);

        editor.edit((editBuilder) => {
            const rangeObj = new Range(
                selection.start.line,
                selection.start.character,
                selection.end.line,
                selection.end.character
            );
            editBuilder.replace(rangeObj, prettified);
        })
    });
    context.subscriptions.push(eventDisposable);
    context.subscriptions.push(disposable);
}

// this method is called when your extension is deactivated
export function deactivate() {
}
const config = workspace.getConfiguration('prettier');

const printWidth = (config as any).printWidth;
const tabWidth = (config as any).tabWidth;
const useFlowParser = (config as any).useFlowParser;
const singleQuote = (config as any).singleQuote;
const trailingComma = (config as any).trailingComma;
const bracketSpacing = (config as any).bracketSpacing;

const format = (document, selection = null) => {
    const text = document.getText(selection)

    try {
        var transformed = prettier.format(text, {
            printWidth: printWidth,
            tabWidth: tabWidth,
            useFlowParser: useFlowParser,
            singleQuote: singleQuote,
            trailingComma: trailingComma,
            bracketSpacing: bracketSpacing
        });
    } catch (e) {
        console.log("Error transforming using prettier:", e);
        transformed = text;
    }

    return transformed
}<commit_msg>Read config fresh each run to pick up configuration changes.<commit_after>'use strict';

import { commands, ExtensionContext, Range, Position, TextEdit, window, workspace } from 'vscode';
const prettier = require('prettier')

export function activate(context: ExtensionContext) {
    const eventDisposable = (workspace as any).onWillSaveTextDocument(e => {
        const document = e.document;

        if (!document.isDirty) {
            return;
        }

        const config = workspace.getConfiguration('prettier') as any;
        const formatOnSave = config.formatOnSave;
        if (!formatOnSave) {
            return;
        }

        e.waitUntil(new Promise(resolve => {
            const prettified = format(document, null);
            const rangeObj = new Range(0, 0, document.lineCount, 0);
            const edit = TextEdit.replace(rangeObj, prettified);

            resolve([edit]);
        }))
    });

    const disposable = commands.registerCommand('prettier.format', () => {
        let editor = window.activeTextEditor;
        if (!editor) {
            return;
        }

        const selection = editor.selection;
        const prettified = format(editor.document, selection);

        editor.edit((editBuilder) => {
            const rangeObj = new Range(
                selection.start.line,
                selection.start.character,
                selection.end.line,
                selection.end.character
            );
            editBuilder.replace(rangeObj, prettified);
        })
    });
    context.subscriptions.push(eventDisposable);
    context.subscriptions.push(disposable);
}

// this method is called when your extension is deactivated
export function deactivate() {
}

const format = (document, selection = null) => {
    const text = document.getText(selection)
    const config = workspace.getConfiguration('prettier') as any;

    try {
        var transformed = prettier.format(text, {
            printWidth: config.printWidth,
            tabWidth: config.tabWidth,
            useFlowParser: config.useFlowParser,
            singleQuote: config.singleQuote,
            trailingComma: config.trailingComma,
            bracketSpacing: config.bracketSpacing
        });
    } catch (e) {
        console.log("Error transforming using prettier:", e);
        transformed = text;
    }

    return transformed
}
{'fraction_non_alphanumeric': 0.10099170208459826, 'fraction_numerical': 0.0012143290831815423, 'mean_word_length': 2.3803009575923393, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2175708', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1249, 'n_words': 317}