Skip to content

feat: format single-line go expressions to a single line#1267

Merged
a-h merged 3 commits intoa-h:mainfrom
MilkeeyCat:pr_fix_go_code_formatting
Oct 8, 2025
Merged

feat: format single-line go expressions to a single line#1267
a-h merged 3 commits intoa-h:mainfrom
MilkeeyCat:pr_fix_go_code_formatting

Conversation

@MilkeeyCat
Copy link
Copy Markdown
Contributor

This pr changes the way raw Go code statements are formatted. Previously the code was formatted by first line indentation as described in https://pkg.go.dev/go/format#Source, now it uses current element level indentation.

Also now empty lines are removed from top and bottom of Go code, previously only empty lines after code and before }} were removed.

There's one more change, now one line code is formatted in one line even if it wasn't before, e.g.

{{
    hello := "world"
}}

will be formatted as

{{ hello := "world" }}

I think it'a a feature but if you think it isn't I'll bring back old behaviour.

fixes #1116

This pr changes the way raw Go code statements are formatted.
Previously the code was formatted by first line indentation as described
in https://pkg.go.dev/go/format#Source, now it uses
current element level indentation.

Also now empty lines are removed from top and bottom of Go code, previously
only empty lines after code and before `}}` were removed.

There's one more change, now one line code is formatted in one line even
if it wasn't before, e.g.
```
{{
    hello := "world"
}}
```
will be formatted as
```go
{{ hello := "world" }}
```
I think it'a a feature but if you think it isn't I'll bring back old
behaviour.
@MilkeeyCat MilkeeyCat force-pushed the pr_fix_go_code_formatting branch from 3b22772 to 2ee10eb Compare September 14, 2025 19:31
@a-h
Copy link
Copy Markdown
Owner

a-h commented Sep 22, 2025

Thanks for this. PR looks good, I just want to consider whether an extra test or two is needed or not.

@a-h a-h changed the title fix: go code formatting feat: format single-line go expressions to a single line Oct 8, 2025
@a-h a-h merged commit 028f398 into a-h:main Oct 8, 2025
4 checks passed
@MilkeeyCat
Copy link
Copy Markdown
Contributor Author

@a-h it doesn't only formats single line code, previously code was indented by the same amount as the first line, for example code below would stay as is even after templ fmt

templ Test() {
	<div>
		{{
	a := 5
	fmt.Println(a)
		}}
	</div>
}

but now the code is formatted this way

templ Test() {
	<div>
		{{
			a := 5
			fmt.Println(a)
		}}
	</div>
}

@a-h
Copy link
Copy Markdown
Owner

a-h commented Oct 8, 2025

Oh cool. If you have a few mins to create the extra test, I'd take that in to make sure we don't have a future regression.

@a-h
Copy link
Copy Markdown
Owner

a-h commented Oct 8, 2025

Thanks for this by the way, cool improvement.

@MilkeeyCat
Copy link
Copy Markdown
Contributor Author

Oh cool. If you have a few mins to create the extra test

<div id="multiline-expressions-stay-multiline">
{{
hello = "world"
name := "foo"
}}
</div>

Doesn't this already checks the formatting?

@MilkeeyCat
Copy link
Copy Markdown
Contributor Author

MilkeeyCat commented Oct 8, 2025

Maybe it would be a good idea to add a test which checks that spaces before the code is also removed

templ Test() {
	<div>
		{{


			a := 5
			fmt.Println(a)


		}}
	</div>
}

will be formatted as

templ Test() {
	<div>
		{{
			a := 5
			fmt.Println(a)
		}}
	</div>
}

Previously it was formatted as

templ Test() {
	<div>
		{{


			a := 5
			fmt.Println(a)
		}}
	</div>
}

@a-h
Copy link
Copy Markdown
Owner

a-h commented Oct 8, 2025

Maybe it would be a good idea to add a test which checks that spaces before the code is also removed

templ Test() {
	<div>
		{{


			a := 5
			fmt.Println(a)


		}}
	</div>
}

will be formatted as

templ Test() {
	<div>
		{{
			a := 5
			fmt.Println(a)
		}}
	</div>
}

Previously it was formatted as

templ Test() {
	<div>
		{{


			a := 5
			fmt.Println(a)
		}}
	</div>
}

Oh yeah, it does cover that. Thanks.

@MilkeeyCat MilkeeyCat deleted the pr_fix_go_code_formatting branch October 8, 2025 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

inconsistent formatting for multi-line go code in templ block

2 participants