GoLand 2023.2 Help

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

  1. Click Edit | Find | Search Structurally.

  2. From the File type list, select a language that you want to use in your search.

  3. In the Search template field, type a search template or select the existing template.

    Do not select templates under the SQL | Common | Contexts node, these templates are used internally by the search engine to eliminate language ambiguities. Saved SQL templates are under the User Defined node.

  4. If the modifier panel is hidden, click the Toggle Modifier Panel button (the Toggle Modifier Panel button).

  5. 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.

  6. (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.

  7. (Optional) From the Scope list, select where you want to search: in a project, module, directory, or within a custom scope.

  8. (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).

  9. Click Find.

    Running structural search

Replace structurally

  1. From the main menu, select Edit | Find | Replace Structurally.

  2. In the Search template field, type a search template or select the existing template.

    Do not select templates under the SQL | Common | Contexts node, these templates are used internally by the search engine to eliminate language ambiguities. Saved SQL templates are under the User Defined node.

  3. In the Replace template field, type a search template or select the existing template.

  4. If you need to add a modifier for the variable in the replace template, place a caret at the variable and use the modifier area to manage modifiers.

  5. Click Find. GoLand displays the results in the Find tool window.

  6. 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.

    Replace a target structurally

Operations with search templates

  1. Click Edit | Find | Search Structurally.

  2. 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.

  3. Click the Save Template… button the Save Template button and select Save Template in IDE or Project….

    Save a template for the structural search

Share search templates

  1. In the Structural Search dialog (Edit | Find | Search Structurally), create a new search template or use the existing one.

  2. To export a template, click the Export Template to Clipboard button (the Export Template to Clipboard icon). 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 (the Import Template from Clipboard icon). GoLand takes the XML code representation and converts it into a template, including variables and a scope if it is present.

    Share search templates
Last modified: 23 August 2023