GoLand 2019.3 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.

Run the structural search by using existing templates

The Structural Search dialog displays the selected template and values of the filters used in the template. For more information about possible filter values, see Search templates, filters, and script constraints.

  1. Click Edit | Find | Search Structurally.

  2. In the Structural Search dialog, click the settings button the Settings button and select Existing templates.

  3. In the list of existing templates, double-click a template.

  4. Click the View variable filters button the View variable filters button.

  5. Click each template variable in the template and set a search filter. To set the filter, click the Add filter link.

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

    GoLand instantly highlights the found code occurrences in the editor.

  6. (Optional) Specify the following options to further narrow the search:

    • File type - use the list to select which file types should be a part of the search (for example, XML).

    • Scope - specify where to search: in a project, module, directory, or within a custom scope.

    • Search target - you can select Complete match to match the exact template or other available code elements.

  7. Click Find.

  1. Click Edit | Find | Search Structurally.

  2. In the Structural Search dialog, add a search pattern by using variables and filters.

    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 (filters) 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 settings button the Settings button and select Save Template.

Save a template for the structural search

Replace a target structurally

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

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

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

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

  4. In the filter area, depending on what your chose as a filter, specify the condition.

    Let's call our variable $Field2$ and add the following filter script which basically is a Groovy script: Field.name.toLowerCase().

  5. 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. GoLand displays the results in the Find tool window.

  6. In the Find tool window, your can work with the results further, replacing found items one by one, 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.

  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 Settings button and select Export Template to Clipboard. GoLand 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 the Settings button and select Import Template from Clipboard. GoLand takes the XML code representation and converts it into a template including variables and a scope if it is present.

Last modified: 23 March 2020