Skip to content

Instantly share code, notes, and snippets.

@ufarooq
Created May 22, 2024 14:57
Show Gist options
  • Save ufarooq/bce5e3f195e2988c997d55c8e1a41ca7 to your computer and use it in GitHub Desktop.
Save ufarooq/bce5e3f195e2988c997d55c8e1a41ca7 to your computer and use it in GitHub Desktop.
Gemini Code Assist

Generate code with prompts

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.

Prompt Gemini Code Assist in a code file

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.

Prompt Gemini Code Assist with selected code using chat

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.

Get inline suggestions while you code

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.

Use smart actions

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.

Test other example prompts

After reading through the Generate code with prompts section of this document, try some of the following example prompts.

Get an explanation of code

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.

Generate documentation for code

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.

Generate comments for 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.

Generate test plans

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.

Get help with debugging 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.

Make your code more compact

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.

Make your code more readable

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment