Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatter Adding Additional Whitespace Characters #344

Closed
JonnyLoughlin opened this issue Dec 19, 2023 · 1 comment
Closed

Formatter Adding Additional Whitespace Characters #344

JonnyLoughlin opened this issue Dec 19, 2023 · 1 comment

Comments

@JonnyLoughlin
Copy link
Contributor

When formatting with templ fmt, vim.lsp.buf.format(), or with vscode-templ, extra whitespace characters are introduced on some lines. Attached are what the expected formatting should be and what is actually output. Plugging the output into a whitespace checker, shows that it is an extra space on the start of these lines.

I am going to try looking into this myself, but I'm not sure if I'll be able to figure it out, so I wanted to file an issue in case anyone else is encountering this issue/ has any insight into why this would be happening.

If there is anything else I can do to help with this issue, please just let me know.

bad_formatting
expected_formatting
whitespace_viewer

@JonnyLoughlin
Copy link
Contributor Author

So it seems the problem is caused by

if e.IndentAttrs {
	if _, err := w.Write([]byte("\n")); err != nil {
		return err
	}
	attrIndent = indent + 1
} 
if _, err := w.Write([]byte(" ")); err != nil {
	return err
}

in func (e Element) Write(w io.Writer, indent int) error {} of types.go.

This seems to add the space regardless of if the attributes should be indented on a new line. Replacing this with

if e.IndentAttrs {
	if _, err := w.Write([]byte("\n")); err != nil {
		return err
	}
	attrIndent = indent + 1
} else {
	if _, err := w.Write([]byte(" ")); err != nil {
		return err
	}
}

seems to fix the issue.

Running types_test.go fails a few tests, but looking at the tests, they have incorrect expected output. ie.
incorrect_test
The expected output has the additional whitespaces in it. Removing them leads to the tests passing with this change.

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

No branches or pull requests

1 participant