IntelliJ IDEA 2019.2 Help

Structural search and replace examples

One statement

$Statement$;

Increasing the number of occurrences count to a certain number, you can find sequences of statements that contain up to the specified number of elements.

Method call

$Instance$.$MethodCall$($Parameter$)

This template matches method call expressions. If the number of occurrences is zero, it means that a method call can be omitted.

@Deprecated $Instance$.$MethodCall$($Parameter$)

You can use this template to find deprecated methods and use it as prototype for creating other annotated method templates. Specifically for searching method calls to deprecated methods you can select the already existing template method calls to deprecated methods.

Refer to Structural search and replace on how to create a search template.

If statement

if ($Expr$) { $ThenStatements$; } else { $ElseStatements$; }

Search in comments and/or string literals

Consider one wants to find comments or literal containing 'foo'. Search template would be like $SomethingWeWantToFind$ or "$SomethingWeWantToFind$". In case one wants to find comments/string containing some particular words (say, foo as a word), this should be specified as a text constraint.

Add try/catch/finally code

If one wants to replace a statement with a try/catch/finally construct, the following pair of search and replace templates can be suggested. The search template is:

$Statements$;

with a certain maximum number of occurrences specified as a constraint.

The replacement template is:

try { $Statements$; } catch(Exception ex) { }

Finding all descendants of a class or all classes that implement a certain interface

Consider the following search templates:

class $Clazz$ extends $AnotherClass$ {}

or

class $Clazz$ implements $SomeInterface$ {}

As the text constraint for the variables $AnotherClass$ or $SomeInterface$, specify the name of the base class or implemented interface.

Finding all such methods

To look for the different implementations of the same interface method, use the following search template:

class $a$ { public void $show$(); }

Specify text constraint for the $show$ variable, and enable the option This variable is the target of the search.

Using @Modifier for finding package local and instance methods

IntelliJ IDEA suggests pre-defined templates for the package local and instance fields of a class. These templates make use of the @Modifier annotation, which helps describe search target, when there is no way to express it using the natural language means.

However, if you need to search for package local or instance methods, you will have to create the corresponding search templates yourself, applying the @Modifier annotation.

To specify criteria for finding all methods with the visibility modifiers package local and instance, use the following search template:

class $Class$ { @Modifier("packageLocal") @Modifier("Instance" ) $ReturnType$ $MethodName$($ParameterType$ $Parameter$); } }

Using 'Contained in Constraints' field in a search

The existing example uses the following template:

LOG.debug($params$);

Placing if('_a) { '_st*; } where _a and _st are variables and * denotes zero or more occurrences in Contained in Constraints field and selecting Invert condition checkbox of Complete Match variable will result a search of logging statements that are not contained in the if statement.

Searching for XML and HTML tags, attributes, and their values

The simplest template to search for a tag is <$tag$/>.

By placing constraints on the variable $tag$, you can specify tags that you want to find. For example, if you specify li, you will all li tags.

Consider the following template for searching in XML and HTML: <$tag$ $attribute$=$value$ />. For example, if you specify the text filter id for the $attribute$ variable and the \d+ regular expression as the text filter for the $value$ variable, you can find all tags that have numeric values in the id attribute.

Serching for li tags with numeric ids

Using the Script filter for XML and HTML content

Delete all lines that have the id attribute greater than 2

In the Search template field, we create a template that searches for all li tags with numeric values (\d+) in id attributes. We expand our search to the whole string with such values (Search target = Complete match).

We filter those lines with the following Groovy script: d.getText().replaceAll (/"/, '').toInteger() > 2. The script reads the content of the d variable and returns it as a string (for example, "1"). Then the script replaces all quotes and converts the string value to integer and compares it with 2.

In the Replace template field, we put nothing to delete the whole string. After the search, we select Replace All to perform the replacing.

Convert uppercase values of the class attribute in p tags to lowercase

In the Search template field, we create a template that searches for all p tags with uppercase values ([A-Z].* and Match case) in class attributes. We narrow our search only to these to class values (Search target = b).

In the Replace template field, we create a new variable $d$ and assign a Groovy script to it (b.getText().toLowerCase()). After the search, we select Replace All to perform the replacing.

Last modified: 19 August 2019

See Also