GDShrapt.Formatter provides configurable code formatting for GDScript 2.0 (Godot 4.x) with auto type inference. Format Rules: - Indentation (GDF001): Tabs or spaces with configurable size - Blank Lines (GDF002): Between functions, after class declaration, between member types - Spacing (GDF003): Around operators, after commas/colons, inside brackets - Trailing Whitespace (GDF004): Remove trailing spaces, ensure EOF newline - Line Endings (GDF005): Normalize to LF, CRLF, or Platform - Line Wrapping (GDF006): Automatic wrapping for long lines - Auto Type Hints (GDF007): Automatically add inferred type hints (opt-in) - Code Reorder (GDF008): Reorder class members by type (opt-in) Key Features: - Style extraction from sample code ("format by example") - LSP compatible options (tabSize, insertSpaces, trimTrailingWhitespace, etc.) - Uses GDTypeInferenceEngine for automatic type hints - Presets: Default, GDScriptStyleGuide, Minimal Usage: new GDFormatter(options).FormatCode(code) Requires: GDShrapt.Reader, GDShrapt.Validator
$ dotnet add package GDShrapt.FormatterConfigurable code formatting for GDScript 2.0 (Godot 4.x) with auto type inference.
textDocument/formattingusing GDShrapt.Reader;
var code = @"func test():
var x=10+5
print(x)
";
var formatter = new GDFormatter();
var formatted = formatter.FormatCode(code);
// Result: properly spaced "var x = 10 + 5"
| Rule | Description |
|---|---|
| GDF001 | Indentation (tabs/spaces) |
| GDF002 | Blank lines between members |
| GDF003 | Spacing around operators |
| GDF004 | Trailing whitespace removal |
| GDF005 | Line ending normalization |
| GDF006 | Line wrapping for long lines |
| GDF007 | Auto type hints (opt-in) |
| GDF008 | Code member reordering (opt-in) |
var options = new GDFormatterOptions
{
IndentStyle = IndentStyle.Tabs,
IndentSize = 4,
SpaceAroundOperators = true,
SpaceAfterComma = true,
MaxLineLength = 100,
WrapLongLines = true,
RemoveTrailingWhitespace = true,
EnsureTrailingNewline = true
};
var formatter = new GDFormatter(options);
Automatically add type hints using type inference:
var options = new GDFormatterOptions
{
AutoAddTypeHints = true,
AutoAddTypeHintsToClassVariables = true,
AutoAddTypeHintsToLocals = true,
UnknownTypeFallback = "Variant"
};
var formatter = new GDFormatter(options);
var code = "var x = 10\nvar name = \"hello\"";
var result = formatter.FormatCode(code);
// Result: var x: int = 10
// var name: String = "hello"
Extract style from sample code and apply to other code:
var formatter = new GDFormatter();
// Sample with 2-space indentation
var sampleCode = @"func sample():
var x = 10
";
var codeToFormat = @"func test():
var y = 20
";
// Will format using 2-space indentation
var result = formatter.FormatCodeWithStyle(codeToFormat, sampleCode);
var formatter = new GDFormatter(GDFormatterOptions.Default);
var formatter = new GDFormatter(GDFormatterOptions.GDScriptStyleGuide);
var formatter = new GDFormatter(GDFormatterOptions.Minimal);
| Package | Description |
|---|---|
| GDShrapt.Reader | Core parser and AST (required) |
| GDShrapt.Validator | Type inference (required for GDF007) |
| GDShrapt.Builder | Fluent API for code generation |
| GDShrapt.Linter | Style checking and naming conventions |
Full documentation and examples: GitHub Repository
MIT License - see LICENSE for details.