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 your IDE:

  • 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 VS Code, Cloud Shell Editor, and Cloud Workstations.

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. 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 Marketplace tab, search for Gemini Code Assist.
  3. Click Install to 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.

Sign into Google and select a Google Cloud project

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 project to continue.

  2. In the Select Google Cloud Project dialog, 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 History in 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.

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 your code 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 with a comment

  1. In your code file, on a new line, enter the comment Function to create a Cloud Storage bucket.

  2. To generate code, press Alt+G (for Windows and Linux) or Option+G (for macOS). Alternatively, you can right-click next to the comment and select Generate Code.

    Gemini Code Assist generates the code below your comment in the form of ghost text.

  3. Optional: To accept the generated code, press Tab.

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. Navigate to settings IDE and Project Settings > Settings > Keymap > Gemini Code Assist.

  2. Right-click Gemini Code Assist and select Add Keyboard Shortcut.

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

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

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

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:

  1. In the activity bar, click spark Gemini Code Assist to open the Gemini Code Assist tool window.

  2. In your code file, select a block of code.

  3. In the Gemini Code Assist tool window 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 Code Assist uses your selected code as reference and responds to your prompt.

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 Tab dropdown that appears. Then, select your preferred shortcut or click Custom to 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 Gemini Code Assist: Active and 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 Assist tool window.

Optional: Exclude files from context with an .aiexclude file

You can exclude files from being considered by Gemini Code Assist for context by creating an .aiexclude file. An .aiexclude file follows the similar syntax as a .gitignore file, with the following differences:

  • An empty or nonexistent .aiexclude file blocks no files in its directory and all sub-directories.
  • An .aiexclude file doesn't support negation, where you prefix patterns with an exclamation point (!).
  • An .aiexclude file matches greedily on the * character. This will match indiscriminately on directories and files. This .aiexclude file doesn't differentiate between ** or * with regards to folders or files.

Only a single .aiexclude file is supported currently. To configure your .aiexclude file, create a file titled .aiexclude in the root of your workspace folder.

Examples

The following examples demonstrate how you can configure an .aiexclude file:

  • Block all files named apikeys.txt at or below the workspace directory:

    apikeys.txt
    
  • Block all files with the .key file extension at or below the workspace directory:

    *.key
    
  • Block only the apikeys.txtfile at the same directory as the .aiexclude file, but not any subdirectories:

    /apikeys.txt
    
  • Block all files in the directory my/sensitive/dir and all subdirectories. The path should be relative to the workspace root directory.

    my/sensitive/dir/*
    

Leave feedback

To leave feedback of your experience, follow these steps:

  1. In the status bar, click spark Gemini Code Assist, 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