DataGrip 2023.1 Help

Code refactoring

Refactoring is a process of improving your source code without creating a new functionality. Refactoring helps you keep your code solid and easy to maintain.

Rename refactoring

Rename database objects

  1. Place the caret at the object that you want to rename.

  2. Navigate to Refactor | Rename Shift+F6. If you edit an argument in the function, you can start typing a new name inline, or press Shift+F6 twice to invoke the Rename dialog.

    If you rename the element inline, you can click icons near the element, and select whether you want to rename it in comments and strings or all the text occurrences.

     rename the element inline
  3. In the Rename dialog, type a new name for the object.

  4. Review changes and click Refactor. To search for usages of the object name, click Preview.

    Rename database objects

Rename code in place

  1. In the editor start renaming a column, a variable, or any other code element. DataGrip will display the Rename the usages of icon in the gutter.

  2. Click the gutter icon or press Alt+Enter and apply a suggestion.

    Rename inline

    DataGrip renames the code element and updates its usages accordingly.

    Rename refactoring result

Extract refactorings

Extract a variable

  1. Place the caret at a variable in the function.

  2. Navigate to Refactor | Introduce Variable (Control+Alt+V).

  3. Select a part of the expression that you want to extract and press Enter.

    Extract a variable

Extract as a Common Table Expression (CTE)

CTE is a temporary result set that you can use within another SELECT, INSERT, UPDATE, or DELETE statements. In DataGrip you can extract a subquery and convert it to CTE.

  1. Place the caret inside the subquery that you want to convert.

  2. From the main menu, select Refactor | Extract/Introduce | Subquery as CTE.

    Extract as a Common Table Expression (CTE)

Extract queries as a table function

  1. Select a query that you want to extract as a table function.

  2. Right-click a selection and select Refactor | Extract Routine. Alternatively, press Control+Alt+M.

  3. In the Name field, type the routine's name.

  4. In the Parameters field, select or clear checkboxes for parameters that you want to use.

    Extract queries as a table function

Preview refactoring changes

For some refactorings, DataGrip lets you preview changes before applying them.

  1. To see potential changes (the list of usages where the refactoring will be performed), click Preview in the Refactoring Preview dialog.

  2. Check the changes that are going to be made in the Find tool window. You can exclude Delete or remove Control+X changes that you consider unnecessary.

  3. Click Do Refactor to proceed with the changes.

The Preview changes window
Last modified: 27 February 2023