DataGrip 2024.3 Help

Use AI prompts to explain and refactor your code

Install the AI Assistant plugin

This functionality relies on the AI Assistant plugin, which you need to install and enable.

  1. Press Ctrl+Alt+S to open settings and then select Plugins.

  2. Open the Marketplace tab, find the AI Assistant plugin, and click Install (restart the IDE if prompted).

Use pre-written prompts to explain code, refactor, and find problems in your code. DataGrip provides project-specific context, such as the languages and technologies used in your project.

You can also define your own custom prompts and add them to the AI Actions menu.

Explain code

  1. Select a code fragment and right-click it to open the context menu.

    Alternatively, select a code fragment and press Alt+Enter.

  2. Select AI Actions and then Explain Code.

    The AI Assistant tool window will open to provide you with an explanation.

    DataGrip AI Assistant explains code

    Click Attached elements to see the list of files that provided the necessary context for generating the answer.

Suggest refactoring

  1. Select a code fragment and right-click it to open the context menu.

    Alternatively, select a code fragment and press Alt+Enter.

  2. Select AI Actions and then Suggest Refactoring.

    The AI chat will open to offer you refactoring suggestions.

    DataGrip: AI Assistant suggests refactoring
  3. In the field with the refactored code, click Show Diff to open the diff viewer. To skip the diff viewing step, expand the list next to Show Diff and select Apply Immediately.

    In the diff viewer, use Unified or Two-Side view to review the suggested changes.

    Diff tab with refactored code
  4. If you like the result, click Accept in the gutter to move the selected AI-generated code snippets to the source file, or click Accept all to fully replace the originally selected code fragment. Otherwise, close the diff viewer to skip the suggested refactoring.

Find problems

  1. Select a code fragment and right-click it to open the context menu.

    Alternatively, select a code fragment and press Alt+Enter.

  2. Select AI Actions and then Find Problems.

    The AI chat will open to show the potential issues you may want to look into.

    DataGrip: AI Assistant finds potential problems in the selected code

Explain runtime error

DataGrip lets you get AI explanations for errors that occurred during running queries and files.

  1. Select the error to clarify and then select Explain error message with AI from the context menu.

    Explain with AI option in console
    Explain with AI option in console

    The AI Assistant tool window will open to give you an explanation of the error and suggest a fix.

  2. If you want to use the suggested fix, click Insert Snippet at Caret in the field with the refactored code to put the AI-generated code into the editor.

For more information about viewing query log and file run output, refer to View query log and View output.

Explain SQL code

This feature can require attaching the database schema to suggest proper explanations.

For any problems higher than weak warning, the assistant suggests an explanation. To use it, do the following:

  1. In the editor, invoke the intention actions by placing the caret at the highlighted code and pressing Alt+Enter, then select AI Actions.

  2. In the AI Actions dialog, select Explain SQL problem under caret.

AI Assistant will provide its explanation in chat.

AI Assistant explains problems in the selected SQL code

Fix SQL code

This feature can require attaching the database schema to suggest proper fixes.

For any problems higher than weak warning, the assistant suggests a fix. To use it, do the following:

  1. In the editor, invoke the intention actions by placing the caret at the highlighted code and pressing Alt+Enter, then select AI Actions.

  2. In the AI Actions dialog, select Fix SQL problem under caret.

AI Assistant suggests a fix for the problems of selected SQL code

AI Assistant will provide its fix in editor.

AI Assistant suggests a fix for the problems of selected SQL code

AI Assistant can also suggest fixes for syntax errors. To use them, do the following:

  • In the editor, invoke the intention actions by placing the caret at the highlighted code and pressing Alt+Enter, then select Fix with AI Assistant.

AI Assistant suggests a fix for the syntax errors of selected SQL code

AI Assistant will provide its fix in editor.

AI Assistant suggests a fix for the syntax errors of selected SQL code

Add your own prompts to prompts library

You can add your own prompts to the prompts library and use them via the AI Actions menu.

  1. Do one of the following:

    • Right-click anywhere in the editor to open the context menu, then go to AI Actions | Edit User Prompts.

    • Press Alt+Enter, select AI Actions, and click Add your prompts.

    • Press Ctrl+Alt+S to open settings and then select Tools | AI Assistant | Prompt Library.

  2. Click to create a new prompt.

    DataGrip: User prompts library settings
  3. In the text field, write the prompt.

    If needed, click the $SELECTION variable to add a Markdown-formatted code block with current code selection and language name to the new prompt.

  4. Edit the new prompt name.

    Select the first checkbox if you want AI Assistant to wait for you to make additional input in the chat after invoking the prompt.

    Keep the second checkbox if you want your new prompt to be listed in the AI Actions menu.

    DataGrip: Custom prompt parameters
  5. Click Apply.

Once you create a prompt, you can edit or delete it at any time.

You can also select a prompt from the list and move it up or down to change the order in which the prompts are displayed.

Last modified: 11 October 2024