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.

Learn how and when 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 .

Code customization lets you get code suggestions based on your organization's private codebase directly from Gemini Code Assist. Learn how to configure code customization .

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. Before testing Gemini Code Assist capabilities in your code file, make sure your file's coding language is supported. For more information on supported coding languages, see Supported coding languages .

  2. Install the Gemini Code Assist/Cloud Code extension if you haven't already. Gemini Code Assist and Cloud Code are bundled together.

  3. If you prefer to use your IDE behind a proxy, see HTTP Proxy .

Install the Gemini Code Assist plugin

To install the Gemini Code Assist plugin in your JetBrains IDE, follow these steps:

  1. Click settings IDE and Project Settings > Plugins.
  2. In the Marketplacetab, search for Gemini Code Assist/Cloud Code .
  3. Click Installto install the plugin.
  4. When the installation is finished, click Restart IDE.
  5. When the IDE restarts, Gemini Code Assist appears in your activity bar.

    The Gemini Code Assist icon appears in the activity bar

Now you're ready to use Gemini Code Assist in your IDE.

In this section, you sign in to your Google Account, and if it's your first time using Gemini Code Assist in your IDE, you select a Google Cloud project.

To sign in to your Google Account, follow these steps:

  1. In the activity bar, click spark Gemini Code Assist.

  2. Click Log in to Google.

  3. On the page that opens in the web browser, select your Google Account.

  4. On the screen that asks you to make sure that you downloaded this app from Google, click Sign in.

    Gemini Code Assist is now authorized to access your account.

Next, if this is your first time using Gemini in your IDE, you must select a Google Cloud project by following these steps:

  1. Return to your IDE. In the Gemini Code Assist tool window, if you agree to allow Google to enable the APIs required to use Gemini Code Assist on your behalf for your selected project, click Select a GCP projectto continue.

  2. In the Select Google Cloud Projectdialog, search for and select your Google Cloud project, and then click OK.

  3. Click FINISH.

Your Google Cloud project is selected with the Gemini Code Assist API enabled. You're ready to use Gemini in your IDE!

Chat with Gemini Code Assist

In this section, you prompt Gemini Code Assist to provide an explanation of your existing code.

To get an explanation of your code, follow these steps:

  1. In your IDE, open your code file.

  2. In the Gemini Code Assist tool window, enter the prompt Explain this code to me and click Submit.

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

    If you only want an explanation of a certain part of your code, you can select certain code and then prompt Gemini Code Assist again. Gemini Code Assist will only use the selected code as a reference to its generated response.

Here are some other example prompts you can try asking Gemini Code Assist:

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

When Gemini Code Assist provides you with code in its response, you can select the following options listed at the end of its response:

  • Insert at Cursor: Inserts the generated code into your current file at your current cursor position.

  • Insert in New File: Opens a new file and inserts the generated code into the new file.

Gemini lists code actions at the end of a generated code response.

These options are available when Gemini Code Assist identifies the language used in your code block, and if this language is supported in your current IDE.

View query history

If you want to re-use your previous prompts, you can find them in your Query Historyin the Gemini Code Assist tool window by clicking schedule Show Query History.

Gemini Query History in the tool window.

Your query and conversation history persist across IDE sessions. To clear query and conversation history, click delete Clear Conversation History.

Button to clear conversation history in Gemini tool window.

Get code completions

While you write code, Gemini Code Assist makes inline code suggestions, called code completions , that you can either accept or ignore. To get code completions, follow these steps:

  1. In your code file, on a new line, start writing a function. For example, if you're in a Python file, write def .

    Gemini Code Assist suggests code in the form of an inline suggestion.

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

  3. Optional: If you prefer to use a different shortcut key to accept the inline suggestion, hold your pointer over the inline suggestion and click the Tabdropdown that appears. Then, select your preferred shortcut or click Customto enter your own shortcut.

    Gemini provides a dropdown menu to change your shortcut to accept an inline suggestion.

Code completion is enabled by default. If you want to disable code completion, in the status bar of your IDE, click spark AI Code Completion: Activeand select Enable AI Code Completion.

Gemini Code Completion button in IntelliJ status bar.

This disables the code completion setting, and Gemini Code Assist no longer makes inline suggestions until you enable the setting again.

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:

  1. In your code file, select a line or block of code.

  2. Right-click the selected code and select a smart action, like Generate unit tests.

    Selecting the smart action will automatically prompt Gemini Code Assist to generate a response to the prompt in the Gemini Code Assisttool window.

Exclude files from context

If you want to exclude any of your files from Gemini Code Assist completions and chat interactions, you can create an .aiexclude file, which works similarly to .gitignore .

For more information on how to configure an .aiexclude file, see Configure context sharing with .aiexclude files .

Known issues

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

JetBrains code completion shortcut overlaps with Gemini Code Assist shortcut

On JetBrains 2024.2 versions, you may experience the Gemini Code Assist shortcut key for code generation ( Alt+Shift+\ for Windows and Linux, or Option+Shift+\ for macOS) overlapping with the JetBrains code completion shortcut that was recently introduced.

To change your shortcut key for Gemini Code Assist, perform the following tasks:

  1. Navigate to settings IDE and Project Settings > Settings > Keymap > Gemini Code Assist.

  2. Right-click Gemini Code Assistand select Add Keyboard Shortcut.

  3. Enter your preferred keyboard shortcut and then click OK.

  4. Right-click Gemini Code Assistand select Remove Alt+Shift+\ (for Windows and Linux), or Remove Option+Shift+\ (for macOS).

    You can now use your new keyboard shortcut to generate code in a code file.

Leave feedback

To leave feedback of your experience, follow these steps:

  1. In the status bar, click spark AI Code Completion, and then in the menu, select Submit feedback.

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

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

  4. Click Submit Feedback.

What's next