{"size": 1234, "ext": "js", "max_stars_repo_path": "commands/transformPug.js", "max_stars_repo_name": "waynehong/vscode-html-pug-convertor", "max_stars_repo_stars_event_min_datetime": "2021-01-13T07:01:14.000Z", "max_stars_repo_stars_event_max_datetime": "2021-01-13T07:01:14.000Z", "max_issues_repo_path": "commands/transformPug.js", "max_issues_repo_name": "waynehong/vscode-html-pug-convertor", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "commands/transformPug.js", "max_forks_repo_name": "waynehong/vscode-html-pug-convertor", "max_forks_repo_forks_event_min_datetime": "2020-11-13T09:50:23.000Z", "max_forks_repo_forks_event_max_datetime": "2020-11-13T09:50:23.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 27.4222222222, "max_line_length": 66, "alphanum_fraction": 0.5940032415}
const vscode = require('vscode');
const pug = require('pug');
function transform (htmlCode, config) {
    return new Promise((resolve, reject) => {
        if (!htmlCode || !htmlCode.length) return resolve(null);
        try { 
            resolve(pug.render(htmlCode,{pretty:true}));
         }
        catch (error) { reject(error); }
    })
}

function transformSelections(editor) {
    let selections = editor.selections;
    const config = {
        tabs: !editor.options.insertSpaces,
        fragment: true
    };
    return selections.map((selection) => {
        const htmlCode = editor.document.getText(selection);
        return transform(htmlCode, config);
    });
}

function replaceSelections(editor, fragments) {
    editor.edit(builder => {
        fragments.forEach((fragment, i) => {
            if (!fragment || !fragment.length) return;
            builder.replace(editor.selections[i], fragment);
        });
    });
}

module.exports = () => {
    let editor = vscode.window.activeTextEditor;
    if (!editor) return;

    Promise
        .all(transformSelections(editor))
        .then((fragments) => replaceSelections(editor, fragments))
        .catch((err) => {
            console.error(err);
        });
}
