Increase productivity with Custom Postfix Completion templates
A while ago, we wrote an article about 14 different ways the IDE can assist you with code completion.
Out of those, Postfix Completion stands apart as it lets you continue to type after the identifier name and get suggestions for it, similar to the example below:
As you can see, using the .sort completion option is much faster than you typing out the whole code snippet for sorting the slice yourself.
Before version 2019.2, however, you were forced to use the Postfix Completion items defined by the IDE. But all of that has now changed, and you can now create your own templates.
Why might this be useful, you ask?
First and foremost, it’s because now you don’t have to wait for a release cycle (at the very minimum) of the IDE to get your favorite completion snippet.
And second, it can be set up for you and save you time and effort specifically with your code.
Take the example in the above picture. It’s not hard to type the whole sort.Slice line, especially given the completion power of the IDE, but it’s a lot quicker to type commits.sort, which also feels natural as you type it.
Let’s look at an example of this in action using a task that might not be that frequent but still requires some typing: removing an element from a slice.
Open up Settings/Preferences | Editor | General | Postfix Completion. Here you’ll be able to view the existing templates and define new ones.
To write our own postfix completion option, let’s use + | Go and then give the completion option a name, or Key as it’s defined in the dialog. This allows us to identify the way the snippet will appear in the completion options.
Under the Applicable expression types section click on the + sign and select slice from the list. You can also see other types in the list. The Postfix Completion items power is that they can be used on specific types from the code, and you can choose from built-in or custom ones, defined by you.
If you don’t want to select the scope for the template each time, then you can use the Apply to the topmost expression checkbox, and it will be applied to the entire expression ignoring the scope.
Then, let’s add the code that uses this option, which will look like this:
“`$EXPR$ = append($EXPR$[:$END$], $EXPR$[+1:]…)“`
As you might expect, $EXPR$ and $END$ are special variables in the template.
$EXPR$ refers to the expression that this option applies to, in this case, our slice.
$END$ refers to the position where the cursor should be placed after the snippet is used. Currently, it’s only possible to specify these two variables.
Now, let’s take a look at the feature in action.
Use the following code and invoke the postfix completion to show up after the d.mySlice identifier by pressing “.” and then typing rm. The result should look similar to this.
What’s the main advantage of this over using Live Templates, you might ask?
Postfix Completion works in tandem with Live Templates. While Live Templates work with the code before you type it and allows you to surround your code with the template; Postfix Completion will enable you to transform existing code without having to select it.
It can also be applied depending on the type of identifier being invoked on, which means it can present more specific functionality.
Both solutions are designed to complement each other and allow you, the user, to make the most out of your code editing session.
That is it for today. We learned what Postfix Completions are, where they are useful and can be used, what the difference is between them and Live Templates, and how to define them.
As usual, please let us know your thoughts on this in the comments section below, on our issue tracker, or on Twitter.
Subscribe to Blog updates
How Do You Become More Productive with GoLand? Complete Interactive Courses!
GoLand has a lot of features, and it can be overwhelming at first. The trick is to remember the most important IDE actions and shortcuts, and the rest will follow. To help you, we have created the Learn GoLand section on the Welcome screen. It contains several interactive courses, mea…
Compile and run Go code using WSL 2 and GoLand
Today, I'm happy to introduce our latest feature to you to use Windows Subsystem for Linux version 2 (WSL 2, or simply WSL) to run or test Go applications seamlessly. This feature is called Run Targets and supports WSL 2, Docker, and SSH remotes. If you'd like to learn more about these, the overvi…
How to use Docker to compile and run Go code from GoLand
Up until now, when you wanted to test or run your shiny new code, you had only the local machine to do so. Many of our users have told us that they'd prefer to use Docker to run their code since that's where they will ultimately test, build, and deploy the code with their CI/CD pipeline. Today…
What Are Run Targets & How To Run Code Anywhere
Previously, when you were using GoLand, you could test and run your code only on the same machine you had the IDE installed on. You could use the FTP/SFTP Connectivity (ex. Remote Hosts Access) plugin and the Settings/Preferences | Tools | External Tools feature or the built-in Docker plugin to achi…