vim-go与coc.nvim:如何重命名一个包?

huangapple go评论103阅读模式
英文:

vim-go with coc.nvim: how to rename a package?

问题

我有一个名为work.go的文件在vim-go中打开。它位于oldpackagename包中。

我想要重构并将其重命名为newpackagename。有没有相应的工具可以做到这一点?原因是,对于oldpackagename的最初假设不再适用。

我知道我可以进行搜索和替换,但通常要么使用GoRename,要么使用nmap <leader>rn <Plug>(coc-rename)可以自动重命名所有出现的地方。

但是对于包名,我得到了以下信息:

vim-go: [rename] SUCCESS vim-go: cannot rename the identifier at the requested position
对于GoRename,以及:

[coc.nvim] Error on rename: The element can't be renamed.
对于coc.nvim

包重命名是否不同?我是否必须使用搜索/替换,或者我的配置可能已损坏?

英文:

I have a file work.go opened in vim-go. It's in package oldpackagename.

I want to refactor and rename it to newpackagename. Is there some tooling for that? The reason is, the initial assumptions for oldpackagename do not apply anymore.

I know I can do search and replace stuff, but usually either

  • GoRename resp.
  • nmap &lt;leader&gt;rn &lt;Plug&gt;(coc-rename)

work just fine on automatically renaming all occurrences.

But for the package name, I get:

> vim-go: [rename] SUCCESS vim-go: cannot rename the identifier at the
> requested position
for GoRename and:

> [coc.nvim] Error on rename: The element can't be renamed.
for coc.nvim.

Is package renaming different? Do I have to use search/replace or are my configs maybe corrupted?

答案1

得分: 1

在底层,vim-go使用gorenamegopls来运行:GoRename命令;默认情况下使用的是gopls

这两个工具都不支持重命名包:

至于coc.nvim,我不确定,但它似乎与gopls集成,因此同样缺乏支持。

英文:

Under the hood, vim-go uses either gorename or gopls to run :GoRename; the default is now gopls.

Both of these don't support renaming packages:

For coc.nvim, I'm not sure, but it seems to integrate with gopls, so the same lack of support would apply.

答案2

得分: 1

gopls 应该很快就会支持包重命名(2022年10月)。一旦该功能发布,它与 vim-go 的集成应该会利用这个功能。

Issue 41567 提到了 CL 420958

> ## gopls/internal/lsp: 添加对包重命名的支持
>
> 用户现在可以通过对包声明中的包名进行重命名来重命名包。
>
> 编辑器将提示用户使用对话文本字段来更改包名。
然后,此重命名功能将执行以下操作:
> - 重命名所有重命名包的外部导入。如果文件内存在重命名冲突,该功能将重复尝试使用附加新包名和迄今为止尝试次数的数字构造的新名称,直到成功为止。
> - 重命名来自其文件的对重命名包的所有内部引用。
> - 重命名重命名包的目录,并更新重命名目录下任何嵌套包的导入路径。
> - 使用新的包名和后缀“_test”重命名测试包,如果当前测试包名以“_test”结尾,否则只使用新的包名。

然而:

> ## 待办事项:
>
> - 为重命名包的路径包含“internal”作为段的情况添加一个测试。
> - 允许编辑整个包路径,而不仅仅是包路径的最后一个段。
> - 如果重命名影响到任何go.mod文件的位置,拒绝重命名,除非重命名的子包属于与重命名包相同的模块。
> - 检查工作区中的go.mod文件,以查看是否需要修复任何替换指令,如果重命名影响到任何go.mod文件的位置。

英文:

gopls should support package renaming soon (Oct. 2022).
Its integration with vim-go should take advantage of that as soon as the feature is released.

Issue 41567 mentions CL 420958

> ## gopls/internal/lsp: add support for package renaming
>
> Users can now rename packages by performing a rename on the package name in package declarations.
>
> The editor will prompt user with a dialogue text field to change the package name.
This rename feature then do the following:
> - Rename all the external imports of the renaming package. In case there is renaming conflicts within a file, the feature repeatedly try fresh names constructed by appending new package name with number of try times so far until succeed.
> - Rename all the internal references to the renaming package from its files.
> - Rename the directory of the renamed package and update the imports' paths of any packages nested under the renamed directory.
> - Rename the test package with the new package name and suffix "_test" with the current test package name ends with "_test", or just the new package name otherwise.

However:

> ## Todo:
>
> - Add a test for the case when the renaming package's path contains "internal" as a segment.
> - Allow edit to the whole package path not just only the last segment of the package path
> - Reject renaming if the renamed subpackages don't belong to the same module with the renaming package
> - Check the go.mod files in the workspace to see if any replace directives need to be fixed if the renaming affects the locations of any go.mod files

huangapple
  • 本文由 发表于 2021年8月5日 05:46:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/68658351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定