RubyMine 2019.1 Help

Structural Search and Replace

A conventional search process does not include the syntax and semantics of the source code. Even if you use regular expressions, RubyMine 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.

RubyMine finds and replaces fragments of source code, based on the search templates that you create and conditions you apply.

Search for a target structurally

  1. From the main menu, select Edit | Find | Search Structurally to open the Structural Search dialog.

  2. In the Structural Search dialog, do one of the following:

    • Create your own template from scratch.

      In the editor area, enter the code pattern ($variable$ that represent your code), click the Settings button, and select Save Template to save it for future use.

      Structural Search dialog

      RubyMine adds the created template to the existing template list (User Defined node) in the Existing Templates dialog.

    • Use one of the existing templates to act as a prototype.

      Click the Settings button, select Existing Templates to open the list of predefined templates, and select the appropriate one.

      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 XML/HTML.

      the Existing Templates dialog

      Click OK to return back to the Structural Search dialog to work with the selected template.

  3. To add some condition for the template, for example, add some regular expressions or a script constraint, in the editor area, position the caret at the code item. The popup with the Edit Filters link appears.

    the Edit filters popup
  4. Click the link or the View variable filters button on the toolbar to open the filter area where you can add your conditions.

  5. In the filter area, click Add 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.

  6. In this example, set the following conditions:

    • for $tag$text=div

    • for $attribute$text=id

    • for $value$text=element[1-9]

    Add regular expression to the filter dialog
  7. 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. In our case, it is Ruby.

    • 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 (in our case it is ).

  8. Click Find.

RubyMine displays the results in the Find tool window.

Find tool window results

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 in the popup that appears, click the Edit filter link.

    Replace structurally

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

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

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. RubyMine 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. RubyMine takes the XML code representation and converts it into a template including variables and a scope if it is present.

Last modified: 8 May 2019