Use AI prompts to explain and refactor your code
Use pre-written prompts to explain code, refactor, and find problems in your code. CLion 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, or modify the built-in prompts.
Explain code
Select a code fragment and right-click it to open the context menu.
Alternatively, select a code fragment and press Alt+Enter.
Select AI Actions and then Explain Code.
The AI Assistant tool window will open to provide you with an explanation.
Explain CMake errors
AI Assistant can help you investigate CMake execution problems.
Click Explain with AI in the CMake tool window next to the error message:
The AI Assistant will explain the error for you and suggest how to fix it:
Explain regex, cron expressions, and SQL queries
AI Assistant can explain regular expressions, SQL queries, cron expressions, and other injected fragments.
Select a code fragment and right-click it to open the context menu.
Alternatively, select a code fragment and press Alt+Enter.
Select AI actions and then Explain code.
AI Assistant will analyze and explain the fragment.
Suggest refactoring
Select a code fragment and right-click it to open the context menu.
Alternatively, select a code fragment and press Alt+Enter.
Select AI Actions and then Suggest Refactoring.
The AI chat will open to offer you refactoring suggestions.
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.
To apply the suggestion right away, select Apply Immediately:
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
Select a code fragment and right-click it to open the context menu.
Alternatively, select a code fragment and press Alt+Enter.
Select AI Actions and then Find Problems.
The AI chat will open to show the potential issues you may want to look into.
Explain runtime error
For runtime errors shown in the console, CLion shows inlay hints, allowing you to get AI explanations for those errors.
Click Explain with AI in the console.
The AI Assistant tool window will open to give you an explanation of the error and suggest a fix.
If you want to use the suggested fix, click in the field with the refactored code to put the AI-generated code into the editor.
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.
Do one of the following:
Right-click anywhere in the editor to open the context menu, then go to
.Press Alt+Enter, select AI Actions, and click Add your prompts.
Press Ctrl+Alt+S to open settings and then select
.
Click to create a new prompt.
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.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.
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.
Modify built-in prompts
You can adjust how AI Assistant behaves in certain scenarios by modifying the prompts used for the corresponding actions. For example, you can add specific guidelines for generating documentation.
Do one of the following:
Right-click anywhere in the editor to open the context menu, then go to
.Press Alt+Enter, select AI Actions, and click Add your prompts.
Press Ctrl+Alt+S to open settings and then select
.
Select the action for which you want to modify the prompt.
Add new instructions.
Click Apply.
The provided instructions will be appended to the default prompt, extending the functionality of the corresponding action.