-
-
Notifications
You must be signed in to change notification settings - Fork 289
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
question: Does/Should javascript blocks be formatted with templ fmt
command?
#481
Comments
templ fmt
command?templ fmt
command?
Hi @jmarais to answer your question of if templ "does" any formatting, it doesn't currently no. Whether it should is a harder question, there are a lot of differing opinions on how formatting should be done in terms of JS, so my suggestion would be to encourage separate tools that allow users to configure preferences, rather than having these baked in to templ. |
Thanks. I had some problem formatting the javascript inside a templ file. I don't know of any formatters that will handle this embedded language situation. Since formatters just overwrite the whole file they essentially compete with each other. |
Interesting idea. I think that templ could detect the presence of a .prettierrc and, if present, and the prettier executable is on the path, could call out and format the JS. If a .prettierrc was found, but there's no prettier executable, then it could be a warning. |
If you are using Neovim, you can run your formatters with https://github.com/stevearc/conform.nvim, which has injected language support through tree sitter. I use it to run biome on the JavaScript portions of my code. |
@JonnyLoughlin, Thanks. I will give conform a shot. edit: @JonnyLoughlin I played around with conform. It doesnt seem to detect the javascript blocks as injected languages in my above example file. I might be messing up the config, do you have a config I can take a look at? |
That is my config. There seems to be a bit of an issue with indenting with this method, but it gets fixed by wrapping the js code. For example:
formats to
for me with my config. I do intend on figuring out why the extra brackets are needed. I just haven't had time to dive in. |
Thanks for the reference. The brackets are a neat trick. Something else I tried was just using the javascript functions in the |
I had a play around with this earlier in the week. I was able to update the It worked, but then I realised that any HTML LSP that you had would also do the same, and that the prettier formatting and LSP formatting might clash. I was also concerned about the performance of starting up lots of processes (one per script or style tag in your code). I noted that if the JS is invalid in some way, prettier bombs out with some log entries on stdout. That gave me the idea of testing the JS early, using the func invalidScriptElementContentsDiagnoser(n Node) ([]Diagnostic, error) {
e, ok := n.(RawElement)
if !ok || e.Name != "script" {
return nil, nil
}
_, err := js.Parse(parse.NewInputString(e.Contents), js.Options{})
if err != nil {
pe, isParseError := err.(*parse.Error)
if !isParseError {
return []Diagnostic{{
Message: fmt.Sprintf("invalid content in script element: %v", err),
Range: e.ContentsRange,
}}, nil
}
// Calculate the offset from the start of the script element to the error.
from := e.ContentsRange.From
from.Line += uint32(pe.Line)
from.Col += uint32(pe.Column)
for li, line := range strings.Split(e.Contents, "\n") {
if li == pe.Line {
from.Index += int64(pe.Column)
break
}
from.Index += int64(len(line)) + 1
}
return []Diagnostic{{
Message: fmt.Sprintf("invalid content in script element: %v", pe.Message),
Range: Range{
From: from,
To: from,
},
}}, nil
}
return nil, nil
} So, basically, I think that we'll get more benefit from implementing #498 than from doing this, so I'm going to pause my experiment on this, and focus on getting multiple LSPs in place. Then, you can add a prettier LSP into your dev workflow which would handle this. At that point, |
I played around with prettier with the LSPs and ended at the same place.
That could help. |
I took a look at the docs for developing Prettier plugin [1] [2] and there is a method for formatting languages within language--in other words, embedded language, for example JavaScript within HTML, CSS-in-JS, etc This made me wonder--is it possible to develop a Prettier plugin for Templ? So that it's possible to do [1] https://prettier.io/docs/en/plugins#optional-embed |
This is just a question as per the title. I am not sure if it should work.
example file with weird indentations:
The html 'body' section at the end does format, however nothing in either a 'script' block or tags gets formatted.
The text was updated successfully, but these errors were encountered: