Effective Coding with Custom Live Templates in GoLand

Live Templates a.k.a. code snippets can be a very powerful tool in your day-to-day work. Who wouldn’t like to write whole blocks of code by pressing just a few keystrokes? Eliminate some routine, and you’ll have more time to focus on what’s really important.

Imagine you are working on web applications. How many times do you write such code patterns as a setting response header to UTF-8, TLS configurations, connecting to databases, and so on? This is exactly the area where code snippets save our time and effort.

In GoLand, the predefined Live Templates are available under Preferences | Editor | Live Templates and divided by groups. There you can find numerous templates not only for Go and Go struct tags, but even for JavaScript, Angular, Angular JS, React, CSS, HTML, and SQL.

But when that’s not enough, you’ll want to create your own templates. Let’s create several custom ones together. Before we start, let’s take a quick look at how it works inside.

A Live Template can be one of three types:

Simple templates contain only fixed plain text and just replace the abbreviation.

simple-templates

Parameterized templates contain plain text with variables that enable user input.

parameterized-templates

Surround templates wrap a block of selected code with the text you specify.

surround-templates

As you can see, each live template has its own template abbreviation. However, the abbreviation must be unique only within a group. The same abbreviation may be used in several different groups: as a simple template, a parameterized template, and a surround template.

Regarding terminology, Live Templates support two variables whose behavior can’t be modified:

  • $END$ indicates where the cursor will be placed after applying the template.
  • $SELECTION$ will surround the selected text with custom live templates, just like it would with HTML tags.

Variables within templates are declared in the following format: $$. Each variable can be defined by custom or predefined functions in the Expression field, and it can accept some default value for cases when the expression fails to evaluate.

Note that a default value of a variable is an expression that can refer to other live template variables. It might be a macro_call (e.g. capitalize), a reference to another variable (e.g. VAR1), or a string literal (e.g. “Hello”).

aappend-template

After a template is expanded, variables appear in the editor as input fields. Use the Move Up / Move Down buttons in the Edit Templates Variable dialog to change the order of variables in the list. The order in this table shows the order in which GoLand will switch between input fields when the template is expanded in the editor.

Congratulations, now you know everything you need to create your own custom template with ease. The theory is over, so let’s get to practice!

For example, we want to have a custom template for connecting to our database with a specified password, hostname, port, and the name of the database to connect to.

Steps:

  • Go to Preferences | Editor | Live Templates and click + to add a new live template.
  • Enter an abbreviation – you’ll use every time you want to invoke the template. For this one, we’ll go with sqlconnect.
  • Add a description that will help you remember what the template does. Invoke the Quick Documentation popup (F1 on macOS or Ctrl+Q on Windows/Linux) to look for the template code snippet.
  • Provide the code that should be generated in the Template text field. To specify where the cursor should appear, use the $END$ variable. In our case, we’ll want to have the cursor at the end of the expression.
  • connect-to-sql

  • Click the Edit Variables button to add expressions to variables and specify the default values if it’s needed. In our case, we’ll only need to set the default values.

connecting-to-db-edit-template-variables

  • Don’t forget to define contexts in which you are going to employ a template (Such as Go, Go Structs, CSS, JS, etc.).
  • Finally, choose how you want to insert the code: with Enter, Tab, or Space.
  • You’re done. Good job!

sql-conntcting-live-template-in-action

That’s all. Go ahead and create as many custom live templates as you need, to get the routine out of the way and reach for the stars!

Share your feedback with us – or your own custom live templates – by dropping a request in our bug tracker or tweeting us at @GoLandIDE. Thank you!

This entry was posted in Features, Tutorial and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *