github Copilot的使用总结

1. 代码建议和补全

GitHub Copilot 的基本使用涉及编写代码时的实时代码建议和补全。一旦你已经安装并配置好 GitHub Copilot 插件,你可以在支持的编辑器(如 Visual Studio Code)中开始使用 Copilot。以下是一些基本的使用步骤:

编写代码:

复制代码
uint32_t add_number

按tap 键后,代码会自动补全如下图:

复制代码
uint32_t add_number(uint32_t a, uint32_t b)
{
    return a + b;
}

在编写check_vector_value接口的时候,你在接口内输入if,会自动看到提示建议的代码,当前这个代码大部分可能不能完全符合你的需求,但是可能会有一定借鉴意义。

2. 解读代码

在你的代码文件中,用鼠标选中需要解读的代码如下图:

然后在左侧chat 窗口输入/explain, copilot 将会解读你选中的代码,如下图:

3. 删除重复代码

假如你写了这样一段代码,里面包含了一些重复的工作,比如下面:

复制代码
void check_vector_value(std::vector<uint16_t> v1,std::vector<uint16_t> v2,std::vector<uint16_t> v3)
{
    auto it1 = std::find_if(v1.begin(), v1.end(), [value](auto val)
    {
        return val == value;
    });

    auto it2 = std::find_if(v2.begin(), v3.end(), [value](auto val)
    {
        return val == value;
    });

    auto it3 = std::find_if(v3.begin(), v3.end(), [value](auto val)
    {
        return val == value;
    });

}

然后你可以在chat 窗口,输入:remove duplicate code

那么copilot 将会为你重写接口,解决重复代码问题,如下:

复制代码
auto find_value_in_vector(const std::vector<uint16_t>& vec, uint16_t value)
{
    return std::find_if(vec.begin(), vec.end(), [value](auto val)
    {
        return val == value;
    });
}

void check_vector_value(std::vector<uint16_t> v1, std::vector<uint16_t> v2, std::vector<uint16_t> v3, uint16_t value)
{
    auto it1 = find_value_in_vector(v1, value);
    auto it2 = find_value_in_vector(v2, value);
    auto it3 = find_value_in_vector(v3, value);
}

4. 帮助编写接口注释

首先鼠标选中一个接口或者一个文件都可以,然后右键copilot->Generate docs,变回生成接口的comments,如下图:

5. 帮助写UT case

比如想给一个接口写一个UT case,首先鼠标选中这个接口,然后右键copilot->Generate Tests,生成的UT case 如下:

复制代码
TEST_F(functionTests, CheckVectorValue)
{
    std::vector<uint16_t> v1 = {1, 2, 3, 4, 5};
    std::vector<uint16_t> v2 = {6, 7, 8, 9, 10};
    std::vector<uint16_t> v3 = {11, 12, 13, 14, 15};

    // Test case 1: value exists in v1
    uint16_t value1 = 3;
    check_vector_value(v1, v2, v3);
    auto it1 = std::find_if(v1.begin(), v1.end(), [value1](auto val) {
        return val == value1;
    });
    EXPECT_NE(it1, v1.end());

    // Test case 2: value exists in v2
    uint16_t value2 = 8;
    check_vector_value(v1, v2, v3);
    auto it2 = std::find_if(v2.begin(), v2.end(), [value2](auto val) {
        return val == value2;
    });
    EXPECT_NE(it2, v2.end());

    // Test case 3: value exists in v3
    uint16_t value3 = 14;
    check_vector_value(v1, v2, v3);
    auto it3 = std::find_if(v3.begin(), v3.end(), [value3](auto val) {
        return val == value3;
    });
    EXPECT_NE(it3, v3.end());

    // Test case 4: value does not exist in any vector
    uint16_t value4 = 20;
    check_vector_value(v1, v2, v3);
    auto it4 = std::find_if(v1.begin(), v1.end(), [value4](auto val) {
        return val == value4;
    });
    EXPECT_EQ(it4, v1.end());
    auto it5 = std::find_if(v2.begin(), v2.end(), [value4](auto val) {
        return val == value4;
    });
    EXPECT_EQ(it5, v2.end());
    auto it6 = std::find_if(v3.begin(), v3.end(), [value4](auto val) {
        return val == value4;
    });
    EXPECT_EQ(it6, v3.end());
}

///

快捷按键:

Tab 键: 接受当前选中的建议,将其插入到代码中。

Enter 键: 将当前选中的建议应用到代码中。

Ctrl + Space 或 Cmd + Space(Mac): 打开建议框,显示Copilot的建议列表。

Ctrl + . 或 Cmd + .(Mac): 打开建议框,显示更多关于建议的选项,如查看更多建议、查看文档等。

Ctrl + / 或 Cmd + /(Mac): 注释或取消注释选定的行或块。

相关推荐
逛逛GitHub14 小时前
慢慢吃掉你的 Claude Code,在终端里养一只黑洞。
github
jump_jump20 小时前
为了重玩金庸群侠传,我研究了一下 Ruffle 怎么复活 Flash
游戏·rust·github
LinXunFeng1 天前
Obsidian - 使用 Share Note 分享笔记并自部署
前端·笔记·github
DayDaydream2 天前
7 天涨了 8000+ Star,Agent Reach 想给 AI 装上互联网眼睛
github
天衍四九3 天前
Git从0到实战(四):冲突解决与版本回退 —— 别怕,出错了也能救
github
大刚测试开发实战3 天前
如何内网穿透访问本地私有化部署的TestHub
前端·后端·github
uhakadotcom4 天前
在python 的 工程化架构中 ,什么是 薄包装器层?
后端·面试·github
Avan_菜菜4 天前
AI 能写代码了,为什么我反而开始要求它先写文档?
前端·github·ai编程
逛逛GitHub5 天前
这个爆红的 GitHub 项目让 token 直接省 60–95%。
github
iccb10135 天前
5年,一个程序员是如何把私有化在线客服系统做到第一名的
前端·后端·github