How to preserve formatting on web-mode for `go` engine?

What happened?

web-mode doesn’t preserve formatting for HTML files with go engine. This makes all the Go tags appear at a single line.

Given a html like this:

<!-- -*- ENGINE:go -*- -->
<!DOCTYPE html>
<html lang="en-US">
  {{- partial "partials/base/head.html" . -}}
  <body>
    {{- partial "partials/base/header.html" . -}}
    {{- define "main" -}}
    {{- end -}}
    {{- partial "partials/base/footer.html" . -}}
  </body>
</html>

On saving, it becomes:

<!-- -*- ENGINE:go -*- -->
<!DOCTYPE html>
<html lang="en-US">
  {{- partial "partials/base/head.html" . -}}
  <body>
    {{- partial "partials/base/header.html" . -}} {{- define "main" -}} {{- end
    -}} {{- partial "partials/base/footer.html" . -}}
  </body>
</html>

What did you expect to happen?

I expect the formatting of Go tags to be preserved.

<!-- -*- ENGINE:go -*- -->
<!DOCTYPE html>
<html lang="en-US">
  {{- partial "partials/base/head.html" . -}}
  <body>
    {{- partial "partials/base/header.html" . -}}
    {{- define "main" -}}
    {{- end -}}
    {{- partial "partials/base/footer.html" . -}}
  </body>
</html>

This used to work fine before (I think 3 months ago) but now nope.

Steps to reproduce

  1. Create a HTML file.
  2. Set web engine to go.
  3. Write some Go tags.
  4. Save it.

System information


Loading data dump...

Hi !

Same trouble here, but I have some infos.

These changes occur since the move from format-all to apheleia in the format module.

Apheleia uses prettier to format the web-mode buffer. Prettier is unable to handle natively go templates and does not handle global customisation, only per project ones. To solve this, you can create a .prettierrc file at the root of your project and add the content given here Don’t forget to install the node plugin itself. This works but I hate having to set this per project.

Another solution would be to use the lsp formater. html-ls does the trick and works natively perfectly. BUT (!)… It seems that the format module keeps on using the apheleia’s formater, which is NOT the way it should perform as written in the doomemacs’ doc.

Can anyone have a look on this strange behavior which might be a bug please ?

1 Like

Thanks! I don’t wish to install an additional node plugin for this but if it is the way, then I think have to go for it :/

Yep… same thing here. That’s why I Hope to find a solution and use the lsp formater. It works well when running +format/buffer (which should be hooked to after-save-hook but I can’t find the modules line where it is done. I’ll have a look at the discord channel.

1 Like

The Discord server offers a solution which works great and doesn’t need an additional node plugin to be installed ! See here While waiting for a built-in solution, you can add this to your config.el

(setq-hook! 'web-mode-hook
  apheleia-inhibit t
  +format-with nil)
(add-hook 'web-mode-hook
          (lambda()
            (add-hook 'before-save-hook #'+format/buffer nil t)))

Related in the doom’s github here

Thank you for the help! What I found is, if I save the file, it formats it to single line and then if I do C-z (undo), it resets the formatting but doesn’t “modify” the file.

This topic was automatically closed after 90 days. New replies are no longer allowed.