TypechoJoeTheme

MetMan's Blog

网站页面

提高代码开发效率:Github Copilot初体验

MetMan博 主神仙
2024-12-30
/
0 评论
/
145 阅读
/
312 个字
/
百度已收录
12/30
本文最后更新于 2024年12月30日,已超过 61天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!

Github Copilot是Github推出的一款使用AI技术在代码编辑器中自动生成代码的工具,可以大幅提高编程效率。

下面真实体验一下neovim + Github Copilot。

配置

  • Github Copilot账号开启
  • Astronvim配置

neovim使用的是Astronvim 4配置,在~/.config/nvim/lua/community.lua中添加

{ import = "astrocommunity.completion.copilot-lua-cmp" },

保存退出,然后重新打开neovim,在neovim中运行命令:Copilot auth安装Copilot。

测试

写一个Fortran版本的快排算法。

通过注释介绍程序意图,Copilot会自动提供代码建议。如果同意接收代码建议,按TAB键确认。

完成快排程序后,再写一个注释生成测试程序。

程序完成后编译测试。

$ gfortran test.f90
test.f90:41:18:

   41 |     call quicksort(arr, 1, size(arr))
      |                  1
Error: Explicit interface required for ‘quicksort’ at (1): assumed-shape argument

可以看出,编译出错,问题在于没有quicksort子程序interface。

neovim重新编辑代码文件,写一条生成fortran interface的注释生成接口代码。

保存,编译成功,测试结果也正常。

$ gfortran test.f90 
$ ./a.out 
           1
           2
           3
           4
           5
           6
           7
           8
           9
          10

小结

虽然Copilot代码建议还不够完美,但已经能够大幅提高日常代码开发效率。

Github Copilot支持多种IDE,除了上面介绍的neovim,还支持VSCODE、Visual Studio、JetBrains IDE。

Github Copilot提供30天试用,之后就需要付费使用。如果希望使用免费的AI代码建议功能,可以在VSCODE中尝试阿里云的通义灵码插件。

附完整代码

! write a quicksort algorithm in fortran90. The quicksort algorithm is a sorting algorithm that uses a divide-and-conquer strategy to sort a list of elements. It works by selecting a pivot element from the list and partitioning the list into two sublists based on the pivot element. The sublists are then recursively sorted until the entire list is sorted. Here is an example implementation of the quicksort algorithm in Fortran90:

recursive subroutine quicksort(arr, low, high)
    integer, intent(inout) :: arr(:)
    integer, intent(in) :: low, high
    integer :: i, j, pivot, temp

    if (low < high) then
        pivot = arr((low + high) / 2)
        i = low
        j = high

        do while (i <= j)
            do while (arr(i) < pivot)
                i = i + 1
            end do

            do while (arr(j) > pivot)
                j = j - 1
            end do

            if (i <= j) then
                temp = arr(i)
                arr(i) = arr(j)
                arr(j) = temp
                i = i + 1
                j = j - 1
            end if
        end do

        call quicksort(arr, low, j)
        call quicksort(arr, i, high)
    end if
end subroutine quicksort

! write a main program to test the quicksort algorithm
program main
    integer :: arr(10) = [9, 4, 7, 2, 8, 3, 5, 1, 6, 10]
    integer :: i
    ! write quicksort fortran interface
    interface
        recursive subroutine quicksort(arr, low, high)
            integer, intent(inout) :: arr(:)
            integer, intent(in) :: low, high
        end subroutine quicksort
    end interface

    call quicksort(arr, 1, size(arr))

    do i = 1, size(arr)
        print *, arr(i)
    end do
end program main

参考资料

https://www.mikecoutermarsh.com/how-to-use-copilot-with-astrovim-4/

注:GIF动图使用LICEcap软件生成。
copilot
朗读
赞(0)
赞赏
感谢您的支持,我会继续努力哒!
版权属于:

MetMan's Blog

本文链接:

https://blog.metman.top/index.php/archives/172/(转载时请注明本文出处及文章链接)

评论 (0)

互动读者

标签云

最新回复

  1. tqymnonccc打酱油
    2024-09-27
  2. toibdpojay打酱油
    2024-09-22
  3. yvctxyevvw打酱油
    2024-09-22
  4. frezhwzwuq打酱油
    2024-09-22
登录
X
用户名
密码