I recently found out about a powerful feature in PHPStorm called live templates. They are templates that you can insert into your code by typing a shortcut. But they are so much more than that.
you can write your own templates that use complex methods to get information from your own code as well as other sources to generate exactly the code you want. All within seconds.
I recently found out about live templates when I needed to refactor about 500 classes. All with the same change, with only minor differences. This was a tedious task and as a developer, I always want to automate such tasks. And I found live templates.
Live templates are not just a PHPStorm feature but a feature of all IntelliJ products like WebStorm, PyCharm, or Rider.
You can access all live templates by first opening File -> Settings or simply using the shortcut Ctrl+Alt +S and then opening Editor ->Live Templates:
As you can see there already are hundreds of live neatly grouped templates to choose from. This already speeds up your development process. But the true power of live templates lies in creating your own.
You can write cour custom code snippets and prepopulate with text from your clipboard, with text parsed from a regex expression, or even by writing your own Groovy Script. If you do not know what Groovy Script is you can find a reference to a tutorial in the resources section. But for now, let us just say it a scripting language you will only need for stuff like this.
So let us write a simple template that will take the clipboard contents, parse out the variable name, and include it in the snippet.
First, we will create our own template group so we can separate our own template groups from the rest. We will name our group “Testing group”. An original name, isn’t it?
Now we can create a new live template with the abbreviation “testme”. How do I come up with those names? It truly reflects my ingenuity 😎.
Now we have our own happy little template. We just need to populate it. For this we will use this function:
our live template will:
- take the contents of our clipboard
- parse out the variable name and the value
- insert a function definition that will be named after the variable and output the value.
First I have to explain some basic stuff. Variables in live templates are encased in $ signs. Our live template contains two variables, $VAR1$ and $VAR2$. If you want to use a literal $ sign, you have to escape it. So in our case, $$$VAR1$ would output something like $VariableContents. But let's get back to our implementation.
We have to define what exactly we want the variables to contain. If you do not do this, PHPStorm will give you the option to input the missing pieces yourself after you use the template. Let’s open the variables editor:
Here we can edit the expression and the default value. We will change both expressions to use the regularExpression() and clipboard() functions. We want to copy a variable definition and get out the variable name. So basically we want to take code like this:
and convert it to this:
To achieve this we will add an expression for both variables:
This simply takes the clipboard contents, parses out the relevant text, and replaces the variable with the first matched group.
After saving it, we can try it out:
This was one of the simplest examples I could come up with. Live templates are very handy if you know how to use them. Happy coding.
You really should be using PhpStorm
I know developers who simply hate IDEs like they were the first working day of the week. If you fall in that category…