An advent to GitHub Copilot The use of the Plugin for Neovim


Humanity has come a long way in its technological journey. We have reached the cusp of an age in which the concepts we have collectively known as science fiction are quickly becoming science fact. The concept of artificial intelligence is highly anticipated yet controversial. Parts of society are apprehensive about its use, and perhaps for good reason. There are valid concerns about the safe and ethical use of this new technology. Regardless of any societal contention, the robots have arrived! This particular robot’s name is GitHub Copilot; if you’re a software developer, or learning to be one, it may be your new best friend.

What is GitHub Copilot?

GitHub Copilot is an AI code assistant tool that integrates with your IDE. It was developed by OpenAI in cooperation with GitHub. It uses the OpenAI Codex language model as its source to suggest generated code in real-time as you type based on the content of your editor’s buffer. There are currently plugins available for Visual Studio, Visual Studio Code, JetBrains, and Neovim. As a Vim user, I created this article to provide real examples of what these suggestions look like using the GitHub Copilot plugin for Neovim.

How does it work?

The OpenAI Codex parses public code sources in order to train its engine to transform natural language into runnable code. It’s currently in private beta, so if you’re interested, you can join their waitlist for access.

Getting Started with GitHub Copilot

Installing GitHub Copilot is a simple process that involves the following steps:

  1. Join the technical preview waitlist
  2. Download and install the plugin
  3. Activate the plugin

Join the Technical Preview Waitlist

At the time of this writing, GitHub Copilot is only available to a small group of developers. Before you can use it, you will need to apply for access to the technical preview in order to activate the plugin. Sign up for the wait list at https://github.com/features/copilot/signup.

Install the Plugin

GitHub Copilot has plugins available for the most common code editors. This article focuses on the Neovim plugin, but if you’d like to try out one of the others, read the documentation at https://github.com/github/copilot-docs, otherwise follow along to install the GitHub Copilot Neovim plugin.

  1. Install the plugin by following the official documentation available in the plugin’s GitHub repository https://github.com/github/copilot.vim I highly recommend that you use a plugin manager such as vim-plug to install the GitHub Copilot plugin for Neovim, and all your other Neovim plugins as well.

  2. Run the :Copilot setup command, and you will be prompted to enter your activation code.

  3. Agree to the telemetry terms and conditions, and you’re ready to start receiving AI-generated code directly in Neovim.

Configure the Plugin

By default, the following key bindings are configured:

Key Action
Tab Accept the suggestion
Ctrl-] Dismiss the current suggestion
Alt-[ Cycle to the next suggestion
Alt-] Cycle to the previous suggestion

I have the Alt key on my system reserved for another application, so I chose to remap these to Ctrl-\Ctrl-j, and Ctrl-k respectively. I did this by adding the following to my Neovim config. The Neovim config file is typically located at ~/.config/nvim/config.

imap <silent> <C-j> <Plug>(copilot-next)
imap <silent> <C-k> <Plug>(copilot-previous)
imap <silent> <C-\> <Plug>(copilot-dismiss)

As I was editing my Neovim config I was already getting suggestions! Line 137 in the image below is an auto-suggestion from the GitHub Copilot plugin.

I do find it funny that Copilot would give us the GitHub API as its suggestion for the API URL. I really like how it was able to populate the import block for me based solely on the comments. Thanks Copilot!

Now I’m just going to use the suggestions for each line and see what Copilot can do on its own.

Pretty handy. This is essentially a valid program. Copilot understood we are working with user data and injected a User type in there, but the definition doesn’t exist. Copilot will need to know the structure of our data to give us suggestions on parsing it. Let’s get that in there and then continue with the logic to parse the data.

But wait! There is another issue, if you caught it. The response from the GitHub API will return a single model, while Copilot set us up to parse an array; however, for the sake of this example, I’m going to leave it as is. I’m not planning on running this code anyway.

Neat! Copilot even gave us some fields to work with, though they aren’t the actual fields that are returned from the GitHub API – I know, I know: I’m expecting a lot from a robot. Next, let’s jump into some parsing logic. I’m just going to add some comments to ask Copilot to suggest some solutions for me and see what happens.

Well… I’m impressed. I can absolutely see how this can increase velocity for development. This is especially true for a language like Go that can require more explicit instructions to perform common operations, such as deleting an element from an array of models.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *