Code with Gemini Code Assist

This document describes how you can use Gemini Code Assist, an AI-powered collaborator in Google Cloud, to help you do the following in Cloud Code:

  • Provide guidance to help you solve problems with your code.

  • Generate code for your project.

  • Receive inline suggestions while you're coding.

Gemini doesn't use your prompts or its responses as data to train its models. For more information, see How Gemini for Google Cloud uses your data.

To help you comply with any license requirements for your code, Gemini Code Assist provides source citations when its suggestions directly quote at length from a specific source. To learn more about how and when Gemini cites sources, see How Gemini helps you generate code and cites sources.

This document is intended for developers of all skill levels. It assumes you have working knowledge of IntelliJ and are familiar with Google Cloud. If you prefer, you can also explore Gemini Code Assist in Cloud Workstations, Cloud Code for VS Code, and Cloud Shell Editor.

Before you begin

  1. Install the Cloud Code plugin if you haven't already. Gemini Code Assist is integrated with Cloud Code to bring the AI-powered collaborator into your local IDE.
  2. Optional: If you choose to clone a sample for the tasks in this document, install Git. Git is required for copying samples to your machine.

Connect to Google Cloud and select a project

In this section, you'll connect to Google Cloud and select a Google Cloud project with the Cloud AI Companion API enabled in your IDE.

If you select a Google Cloud project without the Cloud AI Companion API enabled, you receive an error notification and are instructed to contact your administrator. For more information, see Set up Gemini Code Assist for a project.

To connect to Google Cloud in your IDE, follow these steps:

  1. Launch your IDE.

  2. Click Tools > Google Cloud Code > Sign in to Google Cloud Platform.

  3. Follow the prompts to sign into your Google Account.

  4. When asked if you downloaded Cloud Code from Google, click Sign In.

    You're now connected to Google Cloud.

Next, to select a Google Cloud project that has the Cloud AI Companion API enabled, follow these steps:

  1. In the Gemini Code Assist status bar, click Gemini Code Assist.

    Gemini status bar button.

  2. In the Gemini Code Assist menu, select Settings.

  3. In the Cloud Project field, click more_horiz Select Google Cloud Project and select a project that has the Cloud AI Companion API enabled.

    Gemini is ready to use.

To test the capabilities of Gemini Code Assist, open an application, or create a sample application in the next section.

Optional: Create a sample application

If you prefer to use an existing application to test the capabilities of Gemini Code Assist, you can skip this section. Otherwise, to create a sample application, follow these steps:

  1. In your IDE, click File > New > Project.

  2. In the left pane of the New Project window, select Cloud Code: Kubernetes.

  3. Select one of the Guestbook samples that correspond to your preferred language, and then click Next.

  4. Enter a project name and choose a project location.

  5. Click Create.

    Cloud Code clones the Python (Flask) template and opens your newly created project for use.

Chat with Gemini Code Assist

In this section, you learn how to open the Gemini Code Assist pane and chat with Gemini Code Assist to get an explanation of your existing code.

To begin chatting with Gemini, follow these steps:

  1. Create a code file, or use an existing code file. If you're using the Python (Flask) sample, you can perform this task in your front.py file: in the Project tool window, navigate to src > frontend and open the front.py file.

  2. In the status bar, click Gemini Code Assist, and then select Open chat.

    The Gemini button in the Cloud Code status bar shows the Open chat option.

  3. In the Gemini Code Assist chat window, enter the prompt Explain this code to me and click Send.

    Gemini uses the code in your code file as a reference to your prompt and responds with an explanation of your code.

Reset chat history

Gemini Code Assist uses the chat history for additional context when responding to your prompts.

If your chat history is no longer relevant to what you're trying to achieve, you can reset the chat history: in the Gemini Code Assist pane, click delete Reset Chat.

Generate code with prompts

The following sections show you how to use Gemini Code Assist to generate code with the example prompt # Function to create a Cloud Storage bucket inside a Python file. You can also receive and accept or reject code suggestions while you code.

Prompt Gemini Code Assist in a code file

  1. Create a new or use an existing code file. If you're using the Python (Flask) sample, you can do this in your front.py file: in the Project pane, navigate to python-guestbook > src > frontend and open the front.py file.

  2. In your code file, on a new line, enter # Function to create a Cloud Storage bucket, and then press Enter (for Windows and Linux) or Return (for macOS).

  3. To generate code, press Alt+Shift+\ (for Windows and Linux) or Option+Shift+\ (macOS). Alternatively, in the gutter of your code file, click spark Gemini Code Assist.

    The gutter in Cloud Code shows the Generate Code with Gemini icon.

    Next to your prompt text in the Python file, Gemini Code Assist generates the code in the form of ghost text.

  4. Optional: To accept the generated code, press Tab. If you prefer to accept a single line of the generated code, press Shift+Enter.

Optional: Change keyboard shortcut for generating code

If the default keyboard shortcut for generating code isn't working as outlined in the previous section, you can change the keyboard shortcut by following these steps:

  1. In your IDE, click File (for Windows and Linux) or IntelliJ IDEA (for macOS), and then navigate to Settings > Keymap > Plugins > Google Cloud Code.

  2. In the list of keyboard shortcuts, scroll until you find Generate code.

  3. Right-click Generate code, and then select Add Keyboard Shortcut.

  4. In the Keyboard Shortcut dialog, double-click the current keyboard shortcut, and then enter your own shortcut.

  5. Click OK.

  6. Click Apply, and then click OK.

    You can now use your newly assigned keyboard shortcut to generate code with Gemini Code Assist.

To learn more about changing shortcuts in your IDE, see Configure keyboard shortcuts.

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:

  1. Create a new or use an existing code file. If you're using the Python (Flask) sample, you can do this in your front.py file: in the Project pane, navigate to python-guestbook > src > frontend and open the front.py file.

  2. 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.

  3. To accept the code suggestion from Gemini Code Assist, press Tab. Otherwise, to ignore the suggestion, press Esc or continue writing your code.

Disable code suggestions that match cited sources

Gemini provides citation information when it directly quotes at length from another source, such as existing open source code. For more information, see How and when Gemini cites sources.

To prevent code that matches cited sources from being suggested, you can modify the cloudcode.duetAI.recitation.maxCitedLength setting to 0 in a settings.json file:

"cloudcode.duetAI.recitation.maxCitedLength": 0

Test other example prompts

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

In the Gemini Code Assist chat pane, enter any of the following prompts:

  • Explain this code to me.
  • Write unit tests for my code.
  • Help me debug my code.
  • Make my code more readable.

Known issues

The following sections outline the known issues of Gemini Code Assist.

Chat responses may be truncated when they include an updated version of a large open file

To work around this issue, select a smaller section of code and include an additional directive in the chat prompt, such as only output the selected code.

IdeaVim: Plugin doesn't show Gemini Code Assist suggestion when pressing Esc to dismiss IntelliJ's suggestion

When using the IdeaVim plugin in IntelliJ, if you press Esc to dismiss IntelliJ's suggestion, it goes to VIM command mode and doesn't show the Gemini Code Assist suggestion.

Gemini Code Assist works as intended in insert mode.

IdeaVim: Cannot accept code generation suggestions unless in insert mode

When using the IdeaVim plugin in normal mode, you can't accept code suggestions.

To work around this issue, press i to enter insert mode, and then press Tab to accept the suggestion.

To dismiss the suggestion, press Esc.

Leave feedback

To leave feedback of your experience, follow these steps:

  1. In the status bar, click Gemini Code Assist, and then in the menu, select Submit feedback.

  2. In the form, enter your feedback in the top text field.

  3. If you want to share your Gemini Code Assist logs, make sure that you select the log options.

  4. Click Submit Feedback.

What's next