Last active
January 25, 2023 16:47
-
-
Save ternavsky/619d7a8facfbe0100198fced1b16634c to your computer and use it in GitHub Desktop.
VS Code macros: Format json logs - remove everything except json and format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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