Smart Inline Completions: Use Cases, Examples, and Exercises
Automate Repetitive Patterns
Imagine you need to write code that repeats the same pattern several times. That's the perfect moment to let Copilot generate the boilerplate for you.
As an example, you need to write code that executes a function based on the value of a variable. When the variable is A, you need to execute the function function_A(). When the variable is B, you need to execute the function function_B(), and so on until Z. Writing this code manually can be time-consuming and error-prone. Copilot excels at this type of task and can save you enormous amounts of repetitive work. By giving the following starting code, Copilot will understand that it should generate the code for the rest of the alphabet.
if input == "A":
function_A()
elif input == "B":
function_B()
All you have to do is use the TAB key to accept the completion and let Copilot finish the job for you.
The same technique can be used to generate data that could be used as a configuration or a test case. Let's say you need to use a JSON object to store the names of the months and their corresponding numbers. You can start by writing the first two months and let Copilot generate the rest of the months for you.
months = {
"January": 1,
"February": 2,
# Press ENTER here and wait for the suggestion
Try It Yourself: You are developing a text editor, and you need to implement keyboard shortcuts for various functions. Write code that reads the keyboard shortcut that the user presses and executes the corresponding function. Here are some examples:
- When the user presses
Ctrl + A, the functionselect_all()should be executed.
Building with GitHub Copilot
From Autocomplete to Autonomous AgentsEnroll now to unlock all content and receive all future updates for free.
