Skip to content

--reorder-code splits [ ... ] as Array[T] line-wrap points awkwardly, sometimes placing as X at statement top or wrapping inside the Array[] type parameter #258

Description

@minami110

Tested on gdscript-formatter 0.21.0 (Pre-release 0.21.0-beta, published 2026-07-12), Linux x86_64. Upstream main checked up to 248fa13b (2026-07-12 09:20 UTC) — no fix commit found.

Impact

When the resulting ] / as Array[T] sits at statement top (i.e. not inside enclosing parens), GDScript rejects as as an unexpected statement. When it happens to land inside enclosing parens / brackets it parses, but the shape is not idiomatic and is fragile against subsequent wrap decisions. The wrapper sometimes chooses to fold the array back to one very long line and wrap inside Array[ … ], which is also non-idiomatic.

Trigger

A typed-collection cast like [ ... ] as Array[T] where the array literal is already multi-line, or where the whole expression exceeds the max-line-length.

Repro

Input (bug.gd):

static func make_defs() -> Variant:
	return [
		SomeType.new(&"aaa"),
		SomeType.new(&"bbb"),
		SomeType.new(&"ccc"),
		SomeType.new(&"ddd"),
	] as Array[SomeType]

Command:

gdscript-formatter --stdout --reorder-code bug.gd

Actual output

static func make_defs() -> Variant:
	return [
		SomeType.new(&"aaa"),
		SomeType.new(&"bbb"),
		SomeType.new(&"ccc"),
		SomeType.new(&"ddd"),
	]
	as Array[SomeType]

The ] and as Array[SomeType] end up on separate lines at the same indent level. Inside a return expression at function body indent this places as ... at statement top and GDScript rejects it (Parse Error: Expected statement, found "as" instead).

A related bad shape observed on longer inputs is as Array[ / SomeType / ] — the formatter folds the array back and wraps inside the Array[T] type parameter, which is also non-idiomatic even though it happens to parse.

Expected

Keep ] as Array[T] on the same line — wrap only the array elements while keeping ] as Array[T] contiguous. Never wrap inside the Array[T] type parameter, and never place as at statement top.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions