monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

Formatter for sql?

Open RickCole21 opened this issue 8 years ago • 4 comments

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.

RickCole21 avatar Aug 30 '18 03:08 RickCole21

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

sanderhahn avatar Sep 29 '18 00:09 sanderhahn

I found this one helpful: https://stackoverflow.com/a/66344338/368254

loopmode avatar Jul 28 '21 18:07 loopmode

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.

hediet avatar Jul 30 '21 17:07 hediet

Well, there is one: sql-formatter 😃 See stack overflow link above

loopmode avatar Jul 30 '21 17:07 loopmode