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, GoLand 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.
GoLand finds and replaces fragments of source code, based on the search templates that you create and conditions you apply.
For tutorials and detailed examples, see the following tutorials:
The following procedures list general instructions for performing structural search and replace.
Search and replace structurally
Search structurally
Click
.From the File type list, select a language that you want to use in your search.
In the Search template field, type a search template or select the existing template.
Do not select templates under the
node, these templates are used internally by the search engine to eliminate language ambiguities. Saved SQL templates are under the node.If the modifier panel is hidden, click the Toggle Modifier Panel button ().
Click each template variable and set a search modifier. To set the modifier, click the Add modifier link.
GoLand instantly highlights the found code occurrences in the editor.
(SQL only) Add the SQL context.
In the Search template field, place the caret at the beginning of the template and click the Add modifier link. From the list, select Context.
(Optional) From the Scope list, select where you want to search: in a project, module, directory, or within a custom scope.
(Optional) From the Search target list, select what part of the template you want to search for: separate variables or the whole template (Complete match).
Click Find.
Replace structurally
In the main menu, go to
.In the Search template field, type a search template or select the existing template.
Do not select templates under the
node, these templates are used internally by the search engine to eliminate language ambiguities. Saved SQL templates are under the node.In the Replace template field, type a search template or select the existing template.
If you need to add a modifier for the variable in the replace template, place the caret at the variable and use the modifier area to manage modifiers.
Click Find. GoLand 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, all of them at once, or previewing your potential changes.
Operations with search templates
Save a template for the structural search
Click
.In the Structural Search dialog, add a search pattern by using variables and modifiers.
Variable is a set of characters that are enclosed in dollar signs (for example,
$tag$
). A template variable matches a source code expression if the expression fits within the user-defined constraints (modifiers) that specify what possible values the template variable might have. Any expression that gets bound to the template variable during the search can be used again in the search or replace templates.Click the Save Template… button and select Save Template in IDE or Project….
Share search templates
In the Structural Search dialog ( ), create a new search template or use the existing one.
To export a template, click the Export Template to Clipboard button (). GoLand adds the XML representation of the template to a clipboard (press Control+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 (Control+C) the shared XML code from anywhere (email, chat, or a forum) and in the Structural Search dialog, click the Import Template from Clipboard button (). GoLand takes the XML code representation and converts it into a template, including variables and a scope if it is present.