Working with Copilot Suggestions

Recall that VS Code provides suggestions as you edit code. We discussed this in our chapter about writing code. Copilot suggestions are very similar, they are very supercharged – often spanning multiple lines and entire methods. Copilot provides 2 types of suggestions:

  1. Suggestions. These would be the most relevant code insertions at the current cursor positions.
  2. Next edit suggestions. These are edits that Copilot suggests to multiple non-consecutive lines of code. Let’s understand both in this chapter.

Copilot makes very extensive use of audio cues to convey the presence of these suggestions. This new programming paradigm may pose some learning curve. In an attempt to reduce that, I would like you to review the following audio cues using the “Help: List Signal Sounds” command:

  • Inline suggestion on line
  • Next edit suggestion on line
  • Diff line inserted
  • Diff line modified
  • Diff line deleted

Let’s now first learn about Copilot suggestions.

Copilot Suggestions

These suggestions are very similar to the traditional auto-complete, except that they span multiple consecutive lines and sometimes entire methods and code blocks. I am listing a few key differences to keep in mind as you develop an understanding of Copilot suggestions.

  • Each suggestion could span multiple lines. This means that you may hear screen readers announce entire functions as suggestions. This can sometimes be hard to remember!
  • Similar to code suggestions, Copilot also provides multiple suggestions. However, arrowing up and down will not take you to previous and the next suggestion.
  • There are no new keyboard shortcuts to invoke Copilot suggestions. They appear as you type, often in place of the traditional suggestions. The presence of a Copilot suggestion is indicated by the audio signal “inline suggestion on line.” The screen reader will then start reading the code suggestion. When you hear the suggestion, you can do the following:
    • Accept by pressing Tab
    • Review it line-by-line, character-by-character by pressing Alt+F2 to open it in the accessible view.
    • Go to previous and next suggestion by pressing Control+[ or Control+]
Inline suggestion audio signal indicating that GitHub Copilot has a code suggestion available.

Try it Yourself

  1. Create a new Python file using the “Create: New File” option from the command palette.
  2. Start to type def primality_test:
  3. GitHub Copilot will suggest code. You will hear the “inline suggestion on line” sound and you screen reader will read the entire suggestions if you are not typing. Once you hear the GitHub Copilot suggestion announcement, press Alt+F2 to review it in the accessible view.
  4. Press Shift+Tab to go to the accessibility view toolbar.
  5. Navigate this toolbar using the left and right arrow keys.
  6. Accept the suggestion using the option provided.

Try It Yourself: Working with Copilot Suggestions

  1. In a new file, type def factorial and review the Copilot suggestion.
  2. Copy the code suggestion from the accessible view and paste it.
  3. Copy the second code suggestion and paste it.
  4. What is the keyboard shortcut to accept the suggestion?

Next Edit Suggestions

Next Edit suggestions are a Copilot feature that make edit suggestions at locations beyond your current cursor position. While Copilot suggestions suggest the most relevant code that could be inserted at the current position, next edit suggestions suggest code changes for multiple lines of code that could be several lines below the current cursor position. For example, if you rename a function’s name in its definition, next edit suggestions could include renaming other functions to follow similar naming conventions. Let’s try this.

Try it Yourself

  1. Open a new Python file.
  2. Start typing def add_numbers
  3. Accept the Copilot suggestion.
  4. Press Enter.
  5. Start typing def subtract_numbers
  6. Accept the suggested function definition.
  7. Go back to the add_numbers definition and remove the _. Copilot should play the next edit suggestion on line signal.
  8. Press Alt+F2 to review the suggestion in the accessible view.
  9. The accessible view will contain multiple lines of code. You will hear a “diff line removed” sound when you encounter the next suggestion. This will be your old code line.
  10. The next line will contain the suggested code. You will hear a “diff line added” sound.
  11. Press Shift+Tab to go to the accessible view and accept the suggestion. Depending on the type of suggested change, you may only hear a “diff line modified” sound.

Next Edit Suggestions Practice

  1. What is the keyboard shortcut to accept the suggestion?
  2. What was the suggested next edit?

What’s Next

You now know how to use Copilot to get, review, and apply code suggestions. In the next chapter, we will learn about Copilot’s chat features that allow you to ask questions about code and programming, generate entire projects, and run it.