Skip to content

Instantly share code, notes, and snippets.

@ternavsky
Last active January 25, 2023 16:47
Show Gist options
  • Save ternavsky/619d7a8facfbe0100198fced1b16634c to your computer and use it in GitHub Desktop.
Save ternavsky/619d7a8facfbe0100198fced1b16634c to your computer and use it in GitHub Desktop.
VS Code macros: Format json logs - remove everything except json and format
const vscode = require('vscode');
/**
* Macro configuration settings
* { [name: string]: { ... Name of the macro
* no: number, ... Order of the macro
* func: ()=> string | undefined ... Name of the body of the macro function
* }
* }
*/
module.exports.macroCommands = {
formatLogs: {
no: 1,
func: formatLogs
}
};
async function formatLogs() {
const editor = vscode.window.activeTextEditor;
if (!editor) {
// Return an error message if necessary.
return 'Editor is not opening.';
}
const document = editor.document;
// const selection = editor.selection;
const text = document.getText();
if (text.length > 0) {
const jsonFragmentsArr = text.match(/\{.*\}/g);
const jsonText = `[` + jsonFragmentsArr.join(',\n').replace(/""/g, `"`) + `]`;
await editor.edit(editBuilder => {
const allDocRange = new vscode.Range(document.lineAt(0).range.start, document.lineAt(document.lineCount - 1).range.end);
editBuilder.replace(allDocRange, jsonText);
});
await vscode.commands.executeCommand('editor.action.formatDocument');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment