14

GoLand has syntax support on Go html files with the file extension of .gohtml

So what about Go text files? Does GoLand support that as well? .tmpl is supported by vim-go but not GoLand.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Jim yu
  • 141
  • 1
  • 3
  • 3
    https://www.jetbrains.com/help/go/creating-and-registering-file-types.html Using the steps outlined in the link you should be able to configure Goland to support `.tmpl`. – mkopriva Mar 14 '19 at 19:25
  • Thanks, mkopriva, I figured it out by registering `.tmpl` file type to `go template file type` Feel free to submit the answer and I will mark it – Jim yu Mar 14 '19 at 20:49

2 Answers2

23

As mkopriva has already hinted and you have described as well in the comments, you can go to Preferences/Editor/File Types and add your custom ending as Registered pattern to the Go Template. But imo the real game changer is that you can also set Go as Template data language there. This way even the Go syntax gets validated, not just the template syntax. It's still wonky, but definitly better than nothing.

enter image description here

Still, autoformatting does not work properly and can screw up your code. Therefore, you might also want to make sure the Reformat code in the commit dialog is unchecked. Same with Optimize Imports ... better keep it unchecked.

Community
  • 1
  • 1
NotX
  • 1,516
  • 1
  • 14
  • 28
  • Actually, you can have everything: code highlighting, autocompletion, and formatting for BOTH html and template parts - see https://stackoverflow.com/questions/66878314/jetbrains-goland-html-templates-formatting-and-code-completion – IvanD Mar 31 '21 at 00:53
  • @IvanD I think that sadly doesn't hold true for all cases. For example, I had a go template (not html), and reformatting lines like `//Checks all {{$types}} out there` broke them ... and those broken lines were committed right after, to make things worse. I'ld be careful with that option in general and particulary with templates. – NotX Mar 31 '21 at 15:07
  • Right, I agree that it does not apply to purely text templates, my solution works for HTML templates only, correctly indenting both HTML and range loops – IvanD Mar 31 '21 at 23:31
1

there are two parts to this answer to get both html highlighting and go template highlighting

  1. go template highlighting:

settings > editor > file types > select Go template files from the list > update pattern to the file ending you use for your go templates. I used *.go.html

  1. html highlighting

settings > template data languages > project language (dropdown at the top) > set to html

now you have both html highlighting and go template completion

Jonny Rimek
  • 1,061
  • 11
  • 20