Code Formatter

DWScript includes a built-in code formatter that helps maintain consistent coding styles. The formatter parses the source code into a Code DOM (Document Object Model) and then regenerates the source code with standardized formatting.

Configuration

The formatter (TdwsAutoFormat) can be configured with the following properties:

  • IndentChar: The character used for indentation (default is tab #9).
  • IndentSize: The number of characters per indentation level (default is 1).

Formatting Rules

The formatter applies several structural rules to ensure code readability:

Indentation

Code blocks are automatically indented. This includes:

  • begin ... end blocks
  • case ... end statements
  • try ... finally/except blocks
  • repeat ... until loops

Line Breaks

The formatter is designed to keep code compact while maintaining clarity. It does not force a new line after then, else, or do. This allows placing the begin on the same line, which is a supported and common style.

  • New lines are inserted after semicolons ;
  • Sections like var, const, type are clearly separated.

Spacing

Spaces are normalized around operators and punctuation:

  • Spaces are added around assignment operators :=
  • Spaces are added after commas ,
  • Spaces are handled around parenthesis to separate them from keywords (e.g., if (condition)).

Example

Before

var i:Integer;for i:=1 to 10 do begin PrintLn(i);end;

After

var i : Integer;
for i := 1 to 10 do begin
  PrintLn(i);
end;
Result
1
2
3
4
5
6
7
8
9
10
On this page