Tutorial: Use Live Templates in TODO Comments
Let's explore an advanced PhpStorm's facility to create a live template for the TODO items' text. Why do we need it at all? For example, you want your team mates to create unified TODO items, with the user name automatically filled in, followed by some arbitrary text.
This is how it's done.
Creating TODO pattern and filter
Open the Settings/Preferences dialog, and under the Editor section, click TODO.
Create pattern review
. To do that, click in the Patterns section:
![Todo create pattern Todo create pattern](https://resources.jetbrains.com/help/img/idea/2022.1/TODO_create_pattern.png)
Define color in the Color Picker - in this case, it's pink.
Next, let's create a filter. To do that, click in the Filters section, and define the filter:
![Todo create filter Todo create filter](https://resources.jetbrains.com/help/img/idea/2022.1/TODO_create_filter.png)
Creating live template and variables
Next, back In the Settings/Preferences dialog (Ctrl+Alt+S), under the Editor section, click Live Templates.
![Todo live template1 Todo live template1](https://resources.jetbrains.com/help/img/idea/2022.1/TODO_live_template1.png)
Note that the new template is added to the automatically created group user
.
Next, pay attention to the red note at the bottom. It says that the new template lacks context, where it should apply. So let's click the link Define, and allow all possible contexts. And finally, let's define the body of the template itself: in the area Template text, type the following:
REVIEW[$WHO$] $TEXT$
We have two undefined variables here: $WHO$
and $TEXT$
. The variable $TEXT$
will be used just as an input field, while the variable $WHO$
should be filled in automatically. To define this variable, click the button Edit variables:
![Todo live template2 Todo live template2](https://resources.jetbrains.com/help/img/idea/2022.1/TODO_live_template2.png)
Next, in the Edit Template Variables dialog box, select an expression for the variable $WHO$
:
![Todo live template3 Todo live template3](https://resources.jetbrains.com/help/img/idea/2022.1/TODO_live_template3.png)
Using the REVIEW items
Now let's make sure it works. Back in the editor, create a line comment Ctrl+/, type rv
, and press Tab:
![Todo live template4 Todo live template4](https://resources.jetbrains.com/help/img/idea/2022.1/TODO_live_template4.png)
Note that a right-gutter stripe next to the TODO comment is also added to the editor. As you see, the live template rv
has automatically populated the user name, leaving us with the task of just entering some meaningful comment:
![Todo live template5 Todo live template5](https://resources.jetbrains.com/help/img/idea/2022.1/TODO_live_template5.png)
Now, when you opt to show REVIEW comments only, use the filter. To do that, click and select the filter review to show those TODO comments only, that have the keyword REVIEW.