• github Copilot的使用总结


    1. 代码建议和补全

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

    uint32_t add_number

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

    1. uint32_t add_number(uint32_t a, uint32_t b)
    2. {
    3. return a + b;
    4. }

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

    2. 解读代码

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

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

    3. 删除重复代码

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

    1. void check_vector_value(std::vector<uint16_t> v1,std::vector<uint16_t> v2,std::vector<uint16_t> v3)
    2. {
    3. auto it1 = std::find_if(v1.begin(), v1.end(), [value](auto val)
    4. {
    5. return val == value;
    6. });
    7. auto it2 = std::find_if(v2.begin(), v3.end(), [value](auto val)
    8. {
    9. return val == value;
    10. });
    11. auto it3 = std::find_if(v3.begin(), v3.end(), [value](auto val)
    12. {
    13. return val == value;
    14. });
    15. }

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

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

    1. auto find_value_in_vector(const std::vector<uint16_t>& vec, uint16_t value)
    2. {
    3. return std::find_if(vec.begin(), vec.end(), [value](auto val)
    4. {
    5. return val == value;
    6. });
    7. }
    8. void check_vector_value(std::vector<uint16_t> v1, std::vector<uint16_t> v2, std::vector<uint16_t> v3, uint16_t value)
    9. {
    10. auto it1 = find_value_in_vector(v1, value);
    11. auto it2 = find_value_in_vector(v2, value);
    12. auto it3 = find_value_in_vector(v3, value);
    13. }

    4. 帮助编写接口注释

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

    5. 帮助写UT case

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

    1. TEST_F(functionTests, CheckVectorValue)
    2. {
    3. std::vector<uint16_t> v1 = {1, 2, 3, 4, 5};
    4. std::vector<uint16_t> v2 = {6, 7, 8, 9, 10};
    5. std::vector<uint16_t> v3 = {11, 12, 13, 14, 15};
    6. // Test case 1: value exists in v1
    7. uint16_t value1 = 3;
    8. check_vector_value(v1, v2, v3);
    9. auto it1 = std::find_if(v1.begin(), v1.end(), [value1](auto val) {
    10. return val == value1;
    11. });
    12. EXPECT_NE(it1, v1.end());
    13. // Test case 2: value exists in v2
    14. uint16_t value2 = 8;
    15. check_vector_value(v1, v2, v3);
    16. auto it2 = std::find_if(v2.begin(), v2.end(), [value2](auto val) {
    17. return val == value2;
    18. });
    19. EXPECT_NE(it2, v2.end());
    20. // Test case 3: value exists in v3
    21. uint16_t value3 = 14;
    22. check_vector_value(v1, v2, v3);
    23. auto it3 = std::find_if(v3.begin(), v3.end(), [value3](auto val) {
    24. return val == value3;
    25. });
    26. EXPECT_NE(it3, v3.end());
    27. // Test case 4: value does not exist in any vector
    28. uint16_t value4 = 20;
    29. check_vector_value(v1, v2, v3);
    30. auto it4 = std::find_if(v1.begin(), v1.end(), [value4](auto val) {
    31. return val == value4;
    32. });
    33. EXPECT_EQ(it4, v1.end());
    34. auto it5 = std::find_if(v2.begin(), v2.end(), [value4](auto val) {
    35. return val == value4;
    36. });
    37. EXPECT_EQ(it5, v2.end());
    38. auto it6 = std::find_if(v3.begin(), v3.end(), [value4](auto val) {
    39. return val == value4;
    40. });
    41. EXPECT_EQ(it6, v3.end());
    42. }

    ///

    快捷按键:


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

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

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

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

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

  • 相关阅读:
    LeetCode【65. 有效数字】
    java计算机毕业设计教师职称评定系统源码+mysql数据库+系统+lw文档+部署
    【Python入门】Python的tuple容器
    SpringSecurity
    Ceph入门到精通-Macvlan网络模式
    刷题学习记录
    微信小程序自动化发布
    python数据可视化-matplotlib入门(5)-饼图和堆叠图
    什么是REST API
    我国数据安全治理研究
  • 原文地址:https://blog.csdn.net/nihao_2014/article/details/138162444