Skip to main content
The GLua Language Server includes a built-in code formatter that reprints your Lua files from the parsed syntax tree. It is designed to be safe: it refuses to format files that do not parse, and it never drops a comment even when the layout is complex.

How it works

The formatter reads your file into a syntax tree and then prints it back with consistent indentation and spacing. It follows two safety rules:
  1. Refuses unparseable files — If your file contains a syntax error, the formatter exits without making changes.
  2. Never drops a comment — If the formatter cannot determine where to place a comment in the output, it falls back to the original text for that statement rather than discarding it.

Idiomatic one-liners

The formatter recognises common GLua guard clauses and keeps them on a single line instead of expanding them.
These patterns stay compact because the formatter knows they are idiomatic in Garry’s Mod code.

Setting up

To make the GLua formatter the default for Lua files in your workspace, add it to your VS Code settings:
You can also format manually with the Format Document command (Shift+Alt+F or Shift+Option+F).

Config file

Create a .gluafmtrc.json file in your workspace root to share formatting rules with your team.

Existing config files

The formatter also reads settings from .editorconfig and .prettierrc if they exist in your workspace. Values in .gluafmtrc.json take precedence over .editorconfig and .prettierrc.
Run the GLua: Create Formatter Config File command from the Command Palette to generate a .gluafmtrc.json with sensible defaults in your workspace root.