EditorConfig - Convert tabs to spaces in SQL files transparently

Viewed 252

I have a set of .sql scripts in a project I am working on which currently contain a mix of tabs and spaces for indentation. I would like to find a method which will automatically and transparently convert these tabs to spaces on file save. It would be great if the auto formatting worked in both VS Code AND Visual Studio.

I have tried an EditorConfig (.editorconfig) file like so:

root = true

# Enforce 4 space indenting and NO tabs in SQL script files
[*.sql]
end_of_line = crlf
indent_size = 4
indent_style = space

but it doesnt seem to work. Even when I try the manual "Format Document" option in either VS Code or Visual Studio, nothing happens. I also tried to set up the Prettier VS Code extension but I couldnt get that to work either.

First of all, is this possible using EditorConfig and if not, how can I go about achieving this?

1 Answers

You have a couple of options, and the "best" one depends on the dialect of SQL you want to use.

  1. RedGate SQL Toolbelt and SQL Toolbelt Essentials contains a command line verison of RedGate SQL Prompt that will format your code for you. See: https://documentation.red-gate.com/sp/features-available-only-in-sql-toolbelt-essentials-and-sql-toolbelt/bulk-operations/bulk-operations-via-the-command-line for instructions on using SqlPrompt.Format.CommandLine.exe
  2. vscode-sql-formatter plug-in, which handles the following dialect options:
    1. sql: Standard SQL. Defaults to sql.
    2. n1ql: Couchbase N1QL
    3. db2: IBM DB2
    4. pl/sql: Oracle PL/SQL).

SQL Server Management Studio doesn't support .editorconfig, and the only feature request for Microsoft Azure Data Studio to support .editorconfig was closed by Microsoft saying it was too low a priority for them to even bother reviewing. I think that transparency is great, since they're being brutally honest with customers that providing free SQL formatting tools isn't a priority for them.

Related