This document shows you how to use Gemini Code Assist in Project IDX to generate code with the example prompt # Function to perform binary search on a list of numbers
inside a Python file. You can also select a part of your code and then prompt Gemini Code Assist for help through the chat feature, and receive and accept or reject code suggestions while you code.
Create a new or use an existing code file. If you're using the Python (Flask) sample, you can do this in your main.py
file: navigate to Explorer > src > and open the main.py
file.
In your code file, on a new line, enter # Function to perform binary search on a list of numbers
, and then press Enter (for Windows and Linux) or Return (for macOS).
Next to your prompt text in the Python file, Gemini Code Assist generates the code in the form of ghost text.
Optional: To accept the generated code, press Tab.
Gemini Code Assist can perform tasks or answer your questions based on the code that you select. To get generated code that's based on a prompt with selected code, follow these steps:
In your IDE, open a file in your project that contains code, or use the same code file that you used in previous steps.
In the activity bar, click chat_spark Gemini Code Assist to open the Gemini Code Assist pane.
In your code file, select a block of code.
In the Gemini Code Assist pane text field, enter a prompt for the selected code.
For example, select a function in your code and enter the prompt Write a unit test for this function:
Gemini writes a unit test for a selected function.
Gemini uses your selected code as reference and responds to your prompt.
While you write code, Gemini Code Assist makes inline code suggestions that you can either accept or ignore. To try this feature, follow these steps:
Create a new or use an existing code file. If you're using the Python (Flask) sample, you can do this in your main.py
file: navigate to Explorer > src > and open the main.py
file.
In your code file, on a new line, start writing a function. For example, if you're in a Python file, write def.
Gemini suggests code in the form of ghost text.
To accept the code suggestion from Gemini Code Assist, press Tab. Otherwise, to ignore the suggestion, press Esc or continue writing your code.
To help you be more productive while minimizing context switching, Gemini Code Assist provides AI-powered smart actions directly in your code editor. When you select your code in your code editor, you can view and select from a list of actions relevant to your context.
To use smart actions in your code, follow these steps:
In your code file, select a block of code.
Next to the selected code block, click lightbulb Show Code Actions.
Smart actions lightbulb icon appears after selecting a block of code in VS Code.
Select an action such as Generate unit tests.
Gemini generates a response that's based on the action you selected.
After reading through the Generate code with prompts section of this document, try some of the following example prompts.
In your code file, select a function that you want explained.
In the Gemini Code Assist pane, enter the prompt Explain this code to me
.
Gemini uses your selected code as a reference and responds with an explanation of the selected function.
In your code file, select the code that you want to add documentation.
In the Gemini Code Assist pane, enter the prompt Write documentation for my code.
In your code file, select the code that you want to add comments.
In the Gemini Code Assist pane, enter the prompt Write comments for my code.
In your code file, select the code that you want to add unit tests for.
In the Gemini Code Assist pane, enter the prompt Write unit tests for my code.
In your code file, select the code that you want to debug. In the Gemini Code Assist pane, enter the prompt Help me debug my code.
In your code file, select the code that you want to make more compact.
In the Gemini Code Assist pane, enter the prompt Make my code shorter
.
In your code file, select the code that you want to make more readable. In the Gemini Code Assist pane, enter the prompt Make my code more readable.
If you prefer to focus on a specific part of your code, then select the preferred part of code before prompting Gemini.