这是开始使用 Github Copilot 的快速参考指南
创建一个新文件时,在文件顶部添加注释,描述你的需求。这对 Copilot 很有帮助。
* 下面说明将使用...表示copilot开始生成的位置
# Download file from an URL and analyze its content
# Details:
# * Download the file from an URL
# * Save the downloaded files into `./download` folder
# * Use `filetype` of the file to specify how to parse
# * Filetype can be `.pdf`, `.html`, `.epub`, `.md` and `.txt`
# * Use NLP or OCR to get the file content
# * Tokenize the file content and get the statistics result
import ...
有时候当你已经拥有详细设计文档,但未编写功能代码时,可以直接使用 docstring 中的描述来让 copilot 生成代码。
def send_email(to_address: Email, subject: str, content: HTML): -> StatusCode:
"""
Send email to specified address
Parameters
----------
to_address : Email
The email address to send to
subject : str
The email subject
content : HTML
The email content
Returns
-------
StatusCode
The sending result
"""
...
提供片段代码示例,以帮助 Copilot 更好地开始新的开发任务。
- 使用的框架与库
- 代码风格
- 算法逻辑
from typing import List
from typing import Optional
from sqlalchemy import ForeignKey
from sqlalchemy import String
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "user_account"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(30))
fullname: Mapped[Optional[str]]
addresses: Mapped[List["Address"]] = relationship(
back_populates="user", cascade="all, delete-orphan"
)
def __repr__(self) -> str:
return f"User(id={self.id!r}, name={self.name!r}, fullname={self.fullname!r})"
# Email Address
...
提供片段数据示例,以帮助 Copilot 更好地开始新的开发任务。
- 数据结构与类型
- 命名
- 值处理逻辑
dailogs = [
{
"timestamp": "May 1, 2023 11:00:00",
"text": "Hello, World!",
"speaker": "Jack",
},
{
"timestamp": "May 1, 2023 11:01:00",
"text": "Hello, Copilot!",
"speaker": "Copilot",
},
]
# Parse the json object into `Dialog` object
...
对于mac用户建议修改alt相关的快捷键,因为mac上的alt+字母键有可能被输入法使用了。若有自定义过输入法keylayout,则忽略这句话。
另外没有快捷键的命令,可以唤起命令面板后输入查询关键字筛选后执行。
| 命令 | 说明 | 快捷键 | Mac 快捷键 |
|---|---|---|---|
editor.action.inlineSuggest.trigger | 触发内联建议 | alt+\ | alt+\ |
editor.action.inlineSuggest.showPrevious | 显示上一个内联建议 | alt+[ | alt+[ |
editor.action.inlineSuggest.showNext | 显示下一个内联建议 | alt+] | alt+] |
editor.action.inlineSuggest.acceptNextWord | 接受内联建议的下一个字 | ctl+right | cmd+right |
editor.action.inlineSuggest.commit | 接受内联建议 | Tab | Tab |
editor.action.inlineSuggest.hide | 隐藏内联建议 | Esc | Esc |
editor.action.inlineSuggest.acceptNextLine | 接受内联建议的下一行 | - | - |
| 命令 | 说明 | 快捷键 | Mac 快捷键 |
|---|---|---|---|
github.copilot.generate | 打开 Completions Panel | ctrl+enter | ctrl+enter |
github.copilot.acceptCursorPanelSolution | 接受Completions Panel光标所在的建议 | ctrl+/ | ctrl+/ |
github.copilot.previousPanelSolution | 查看上一个建议 | alt+[ | alt+[ |
github.copilot.nextPanelSolution | 查看下一个建议 | alt+] | alt+] |
| 命令 | 说明 | 快捷键 | Mac 快捷键 |
|---|---|---|---|
github.copilot.toggleCopilot | 启用/禁用 Copilot 补全提示 | - | - |
github.copilot.collectDiagnostics | 收集诊断信息 | - | - |
github.copilot.openLogs | 打开日志窗口 | - | - |
github.copilot.sendFeedback | 打开社区网站 | - | - |
github.copilot.signIn | 登陆 | - | - |
| 命令 | 说明 | 快捷键 | Mac 快捷键 |
|---|---|---|---|
github.copilot.interactiveEditor.explain | 进行解释(选中内容或光标所在的文件) | - | - |
github.copilot.terminal.explainTerminalSelection | 对此进行解释(需要在终端中使用) | - | - |
github.copilot.terminal.explainTerminalSelectionContextMenu | Copilot: 对此进行解释(需要在终端中使用) | 鼠标右键菜单 | 鼠标右键菜单 |
github.copilot.terminal.explainTerminalLastCommand | 对终端中最后一个命令进行解释(需要在终端中使用) | - | - |
| 命令 | 说明 | 快捷键 | Mac 快捷键 |
|---|---|---|---|
inlineChat.start | 代码内聊天 | - | - |
github.copilot.interactiveEditor.generate | 在此生成(在光标所在位置唤起inline chat的/generate功能) | - | - |
github.copilot.interactiveEditor.generateDocs | 生成文档 | - | - |
github.copilot.interactiveEditor.generateTests | 生成测试 | - | - |
github.copilot.interactiveEditor.fix | 修复此 | - | - |
| 命令 | 说明 | 快捷键 | Mac 快捷键 |
|---|---|---|---|
workbench.action.quickchat.toggle | 开启/关闭 Quick Chat | shift+cmd+i | shift+cmd+i |
github.copilot.terminal.suggestCommand | 建议终端命令 | ctrl+i(仅在终端起作用) | cmd+i |
| 命令 | 说明 | 快捷键 | Mac 快捷键 |
|---|---|---|---|
workbench.action.openChat.copilot | 打开编辑器聊天 | - | - |
| 命令 | 说明 | 快捷键 | Mac 快捷键 |
|---|---|---|---|
github.copilot.interactiveSession.feedback | 打开github Issues | - | - |
github.copilot.debug.workbenchState | 日志工作台状态 | - | - |
github.copilot.ghpr.applySuggestion | 为Github Pull Request提供代码建议 | - | - |
打开 VSCode 命令面板,输入 Preferences: Open Settings 来打开配置文件。在文件模式下,您可以配置相关参数。要获取完整的参数说明,请查看 copilot 和 copilot chat 两个插件目录下的 package.json 文件,具体位置可以参考 VSCode 扩展市场。
// settings.json
{
// ...
"github.copilot.chat.welcomeMessage": "always",
"github.copilot.chat.localeOverride": "zh-CN",
"github.copilot.editor.enableCodeActions": true,
"github.copilot.editor.iterativeFixing": true,
"github.copilot.editor.enableAutoCompletions": true,
"github.copilot.enable": {
"plaintext": false,
"ini": false,
"markdown": true,
"*": true
},
"github.copilot.advanced": {
"length": 4000,
"inlineSuggestCount": 5,
"top_p": 1,
"temperature": "0.8",
"listCount": 10,
"stops": {
"*": [
"\n\n\n"
],
"python": [
"\ndef ",
"\nclass ",
"\nif ",
"\n\n#"
]
},
"debug.showScores": true,
"indentationMode": {
"python": false,
"javascript": false,
"javascriptreact": false,
"jsx": false,
"typescript": false,
"typescriptreact": false,
"go": false,
"ruby": false,
"*": true
}
}
// ...
}
| 设置参数 | 值类型 | 说明 |
|---|---|---|
"http.proxy" | string | 配置网络代理地址 |
| 设置参数 | 值类型 | 说明 |
|---|---|---|
"github.copilot.chat.localeOverride" | string | 设置Copilot本地语言 |
"github.copilot.chat.welcomeMessage" | string | Copilot Chat 是否显示欢迎语first: 仅第一次启动时, always: 总是, never: 从不 |
| 设置参数 | 值类型 | 说明 |
|---|---|---|
"editor.inlineSuggest.enabled" | boolean | 启用内联建议 |
"github.copilot.editor.iterativeFixing" | boolean | 允许 Copilot 提供迭代修复建议 |
"github.copilot.editor.enableAutoCompletions" | boolean | 允许 Copilot 提供自动补全 |
"github.copilot.editor.enableCodeActions" | boolean | 允许 Copilot 提供代码操作建议,包括重构和优化代码结构、修复错误等 |
| 设置参数 | 值类型 | 说明 |
|---|---|---|
"github.copilot.enable" | json | 请将 "*": true 放到末尾 语言后设置 false 表示禁用copilot, 设置 true 表示启用 |
github.copilot.advanced 可以控制模型参数,最终影响到代码生成。其值为json
| 设置参数 | 值类型 | 说明 |
|---|---|---|
"length" | 整数 integer | 生成代码的字数,默认为 500 |
"top_p" | 数字 number | 控制模型候选范围,默认值为 1,范围为 0.0~1.0 |
"temperature" | 字符串 string | 控制模型创造性,默认值为空,值越大越不可预测,范围为 0.0~1.0 |
"inlineSuggestCount" | 整数 integer | 内联提示的个数,默认为 3 |
"listCount" | 整数 integer | Completions Panel 中建议的个数,默认为 10 |
"stops" | JSON | 控制模型代码生成时的停止标志,可按语言控制 |
"indentationMode" | JSON | 指定语言是否使用该语言的缩进模式,可能与 stops 冲突 |
"debug.showScores" | 布尔值 boolean | 在代码建议列表中显示每个建议的分数 |
欢迎大家添加新内容,校对、错误请指正。📮邮箱: jussker@outlook.com
[1]: GitHub Copilot in VS Code
[2]: How to use GitHub Copilot: Prompts, tips, and use cases
[3]: GitHub Copilot Official Website
[4]: GitHub Copilot Series (Youtube)
[5]: Pragmatic techniques to get the most out of GitHub Copilot (Youtube)
[6]: How I used GitHub Copilot to build a browser extension
[7]: Visual Studio Code, Where are extensions installed?
[8]: Visual Studio Code, Language Identifiers