当前位置:AIGC资讯 > AIGC > 正文

vscode结合GitHub Copilot编码

已集成工具

Azure Data Studio JetBrains IDEs Vim/Neovim Visual Studio Visual Studio Code

目录

GitHub Copilot & Visual Studio Code

前提条件

Getting code suggestions

Showing alternative suggestions

Showing multiple suggestions in a new tab

Accepting partial suggestions

Next steps

支持试用一个月

GitHub Copilot & Visual Studio Code

在vscode中利用github copilot支持 Python, JavaScript, TypeScript, Ruby, Go, C# and C++等多种语言, GitHub Copilot 支持sql语句生成,框架使用,api建议等

前提条件

订阅Copilot. 参见 "About GitHub Copilot."

安装Visual Studio Code. 参见 Visual Studio Code download page.

安装Copilot 插件. 参见 "Set up GitHub Copilot in Visual Studio Code" .

GitHub Copilot offers coding suggestions as you type. For example, type this function header in a JavaScript file:

JavaScript

function calculateDaysBetweenDates(begin, end) {

GitHub Copilot will automatically suggest the rest of the function. To accept the suggestion, press Tab.

You can also describe something you want to do using natural language within a comment, and Copilot will suggest the code to accomplish your goal. For example, type this comment in a JavaScript file:

JavaScript

// write a function to
// find all images without alternate text
// and give them a red border

GitHub Copilot will automatically suggest code. To accept the suggestion, press Tab.

Tip

If you receive limited or no suggestions from Copilot, you may have duplication detection enabled. For more information about duplication detection, see "Managing Copilot policies as an individual subscriber."

Showing alternative suggestions

For any given input, GitHub Copilot may offer multiple suggestions. You can select which suggestion to use, or reject all suggestions.

For example, type this function header in a JavaScript file, and press Enter:

JavaScript

function calculateDaysBetweenDates(begin, end) {

GitHub Copilot will show you a suggestion.

Now hover over the suggestion to show the GitHub Copilot control for choosing suggestions. To display next or previous suggestions, click the forward or back arrow button in the control.

You can also use keyboard shortcuts to show alternative suggestions:

OS See next suggestion See previous suggestion macOS Option (⌥) or Alt+] Option (⌥) or Alt+[ Windows or Linux Alt+] Alt+[

To accept a suggestion, click "Accept" in the Copilot command palette, or press Tab. To reject all suggestions, press Esc.

Showing multiple suggestions in a new tab

If you don't want to use any of the initial suggestions GitHub Copilot offers, you can show multiple suggestions in a new tab.

For example, type this function header in a JavaScript file, and press Enter:

JavaScript

function calculateDaysBetweenDates(begin, end) {

GitHub Copilot will show you a suggestion. Now press Ctrl+Enter to open a new tab with multiple additional options.

To accept a suggestion, below the suggestion, click Accept suggestion NUMBER. To reject all suggestions, close the tab.

Accepting partial suggestions

If you don't want to accept an entire suggestion from GitHub Copilot, you can accept the next word or the next line of a suggestion.

For example, type this function header in a JavaScript file, and press Enter:

JavaScript

function calculateDaysBetweenDates(begin, end) {

GitHub Copilot will automatically suggest an entire function body in grayed text. The exact suggestion may vary.

Now hover over the suggestion to show the GitHub Copilot control for choosing suggestions. To accept only the next word of the suggestion, click Accept Word in the control.

Alternatively, you can use a keyboard shortcut to accept the next word of a suggestion:

OS Accept Next Word macOS Command+→ Windows or Linux Control+→

If you want to accept the next line of a suggestion, you will need to set a custom keyboard shortcut for the command editor.action.inlineSuggest.acceptNextLine. For more information on setting custom keyboard shortcuts, see "Configuring GitHub Copilot in your environment."

总结

### GitHub Copilot与Visual Studio Code集成详解
#### 已集成工具概览
Azure Data Studio、JetBrains IDEs、Vim/Neovim、Visual Studio、以及Visual Studio Code等开发工具已为广大开发者所采纳,其中Visual Studio Code因其强大功能和开放性深受用户喜爱。
#### GitHub Copilot介绍
GitHub Copilot是GitHub推出的强大代码辅助工具,它与Visual Studio Code完美集成,支持Python、JavaScript、TypeScript、Ruby、Go、C#和C++等多种语言。Copilot不仅可以帮助生成SQL语句和框架使用建议,还能为API编写提供智能建议。
#### 前提条件
1. **订阅GitHub Copilot**:访问GitHub官网了解订阅详情。
2. **安装Visual Studio Code**:从官网下载并安装你所需版本的VS Code。
3. **安装Copilot插件**:在VS Code的扩展商店中搜索并安装Copilot插件,或者参考官方文档进行设置。
#### 使用GitHub Copilot
1. **获取代码建议**:在编写函数时,如`function calculateDaysBetweenDates(begin, end) {`,Copilot会自动建议函数体剩余部分的代码。按下Tab键即可接受建议。
2. **展示替代建议**:针对同一输入,Copilot可能提供多个建议。通过鼠标悬停在建议上方可以显示建议选择控件,通过点击前后箭头或使用键盘快捷键来显示不同的建议。
3. **新标签页中查看多个建议**:如果初始建议都不符合需求,可以按下Ctrl+Enter打开新标签页查看更多建议。
4. **接受部分建议**:若不想接受完整建议,可选择仅接受下一个单词或下一行建议。通过鼠标点击“Accept Word”或使用快捷键Command+→(macOS)/ Control+→(Windows/Linux)接受下一个单词。要接受下一行,需配置自定义快捷键。
5. **支持试用**:GitHub Copilot提供一个月试用期,助力开发者体验代码智能辅助功能。
#### 总结
GitHub Copilot与Visual Studio Code的深度集成极大地提高了编程效率。它不仅能够在编写代码时实时提供建议,还允许用户自由选择和组合建议内容。无论是专业开发者还是初学者,都能从此工具中获益良多。不妨试试看吧!

更新时间 2024-07-20