Formatter for sql?
Is there a formatter plugin for sql?
i used editor.getAction('editor.action.formatDocument').run(), but it didn't work.
console:
There is no formatter for sql-files installed.
You can add your own formatter using:
monaco.languages.registerDocumentFormattingEditProvider("sql", {
async provideDocumentFormattingEdits(model) {
var formatted = await sqlfmt(model.getValue());
return [
{
range: model.getFullModelRange(),
text: formatted,
},
];
}
});
You still need to implement your own sql formatting though. For a test setup i have installed https://github.com/lopezator/sqlfmt (https://sqlfum.pt/) as a command line tool and made it available through the webserver.
const sqlfmt = (sql) => {
const loc = document.location;
const base = loc.protocol + "//" + loc.host;
const url = base + "/sqlfmt?sql=" + encodeURIComponent(sql);
return fetch(url).then((response) => {
return response.text();
});
};
I found this one helpful: https://stackoverflow.com/a/66344338/368254
An SQL formatter is out of scope for the monaco-editor package. However, as @sanderhahn already noted, you can easily add your own formatter.
We would welcome an npm package by the community that contributes an SQL formatter that can be used in monaco!
I'm closing this issue as we are not planning to implement this functionality in the forseeable future.
Well, there is one: sql-formatter 😃 See stack overflow link above