Structural search and replace
A conventional search process does not take into account the syntax and semantics of the source code. Even if you use regular expressions, PyCharm still treats your code as a regular text. The structural search and replace (SSR) actions let you search for a particular code pattern or grammatical construct in your code considering your code structure.
PyCharm finds and replaces fragments of source code, based on the search templates that you create and conditions you apply.
Search for a target structurally
From the main menu, select Structural Search dialog.
to open theIn the Structural Search dialog, do one of the following:
Create your own template from scratch.
Select Draft Template from the list of templates.
In the editor area, enter the code template (
$variable$
that represents your code), in the dialog's toolbar click to save it for future use. You can opt to save the template as inspection as well.PyCharm adds the created template to the existing template list (Recent node).
Use one of the existing templates to act as a prototype.
In the available existing templates list, select the needed template.
Consider the following style definitions added to an HTML file:
<style> #element1 { display: inline-block; width: 25%; margin-top:90px; } #element2 { display: inline-block; width: 40%; margin-top:10px; } </style>Let's find all the references to the style elements included into the <div> tags of your HTML file.
In the list of existing templates, select html attribute value under .
Click Find.
PyCharm instantly highlights the found code occurrences in the editor.
The Structural Search dialog displays the selected template and the values of the filters used in the template. You can edit the existing filters or add new conditions, for example, add some regular expressions, or a script constraint. Place the caret at the code variable and use the filter area to manage filters.
In the filter area, click to add a new condition. If, for example, you need to add a regular expression, select Text. You can also add other conditions depending on your variable.
In this example, set the following conditions:
for $tag$ –
text=div
for $attribute$ –
text=id
for $value$ –
text=element[1-9]
Specify where to search: in a project, module, directory, or within a custom scope.
Click Find.
PyCharm displays the results in the Find tool window.
Replace a target structurally
From the main menu, select
.In the Replace Structurally dialog, add new or existing templates to the search and replace template areas. You can save the replace template the same way as the search one.
If you need to add a filter for the variable in the replace template, place a caret at the variable of interest and use the filter area to manage filters.
In the filter area, depending on what you chose as a filter, specify the condition.
To narrow down your replace results, select the following options:
Shorten fully-qualified names - replaces fully qualified class names with short names and imports.
Reformat - automatically formats the replaced code.
Use static import - uses static import in replacement when possible.
After specifying the necessary options, click Find. PyCharm displays the results in the Find tool window.
In the Find tool window, you can work with the results further, replacing found items one by one, or all of them at once, or previewing your potential changes.
Consider the following example that converts 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.
Share search templates
You can share a search template with your peers by exporting or importing it.
In the Structural Search dialog ( ), create a new search template or use the existing one.
To export a template, click . PyCharm adds the XML representation of the template to a clipboard (press Ctrl+Shift+V to see the clipboard's content). You can share this representation with other developers in chat, email, or a forum.
To import a template, copy (Ctrl+C) the shared XML code from anywhere (email, chat, or a forum) and in the Structural Search dialog, click . PyCharm takes the XML code representation and converts it into a template including variables and a scope if it is present.