Monaco-editor: Formatter for sql?

Created on 30 Aug 2018  Â·  1Comment  Â·  Source: microsoft/monaco-editor

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.
feature-request

Most helpful comment

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();
    });
  };

>All comments

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();
    });
  };
Was this page helpful?
0 / 5 - 0 ratings