我主要对C++和方法/类名/签名的自动更改感兴趣。
我经常这样做,所以我也在焦急地等待其他回复。
我所知道的唯一技巧是非常基本的。以下是我在Emacs中重构代码时最好的朋友:
M-x query-replace
这允许您进行全局搜索和替换。当您将方法和通常访问的数据移动到其他类或名称空间时,您将要做大量的工作。
C-x 3
这将为您提供一个具有两个并排缓冲区的显示器。然后,您可以继续在其中加载不同的文件,并使用C-x o
将光标从一个文件移动到另一个文件。这是非常基本的东西,但我提到它是因为它能让下一个。。。
C-x (
(type any amount of stuff and/or emacs commands here)
C-x )
这就是在emacs中定义宏的方式。每当你发现自己需要对一堆代码一遍又一遍地做同样的事情时(这对于查询替换来说太复杂了),这就是救命稻草。如果您搞砸了,可以点击C-g
停止宏定义,然后撤消(C-_
),直到回到开始的位置。调用宏的键是C-x e
。如果你想做很多次,你可以点击<code>Esc</code>,然后先输入一个数字。例如:<code>Esc 100 C-x e</code>将尝试调用宏100次。
(注意:在Windows上,您可以通过按Esc键或按住Alt键来获取“Meta”)。
在最近的Emacs版本(24)中,Semantic能够做到这一点。
- Possibly activate semantic mode M-x semantic-mode RET.
- Bring up the Symref buffer with C-c , g.
- Press C-c C-e to open all references.
- Rename with R.
如果你能用elisp编程,你可以从CEDET库-它为该任务提供了所有工具-查找函数的调用方、获取签名等。但您需要使用这些工具自己创建重构工具
对于介于重构工具和简单正则表达式之间的地方,由于Emacs 22,您可以在替换文本中嵌入任意的elisp表达式,这使您能够进行非常强大的文本操作。Steve Yegge写了一篇不久前关于这方面的好文章。
我的一个朋友正在玩xreferry并表示它运行得很好。不过它并不便宜。
构建cscope符号。
查找要重构的符号。
进入cscope窗口,在第一次出现光标后启动宏
- ret
- c-f your symbol start
- navigate to start of your symbol
- modify the word
- c-x o (back to cscope)
- n (for next cscope symbol)
你现在只需要c-x-c-e
我完全同意查找和替换工作很好。然而,cedet的一个非常好的特性是语义符号引用列表。
将光标放在方法上,运行此命令,您将看到一个缓冲区,其中列出了代码中引用此标记的所有位置。
您仍然可以使用查找和替换技巧,这将确认您已经更改了所有引用。
我一直在使用cquery来完成我的C++,它使用Microsoft LSP for IDE<->;工具通信cquery服务器使用clang后端满足LSP协议的请求。
lsp-emacs是位于emacs和cquery后端(cquery-emacs)之间的包,后者公开了lsp-rename
函数。顺便说一句,cquery作为一种完井系统,非常可靠和快速,强烈推荐。
Give it a try, follow the getting-started guide on the cquery github: https://github.com/cquery-project/cquery/wiki/Emacs
一旦您设置了查询:
- Hover your cursor over an identifier (class, var, whatever) you d like to rename.
- M-x
lsp-rename
- Enter the new name for the identifier.
- Do C-x s (save some buffers), which will prompt you to save all the buffers that were touched by the refactor.
您可能应该检查所有修改过的缓冲区,并检查在使用任何工具/语言重构后所做的操作。
- winforms
- combobox
- fogbugz
- java
- date
- internationalization
- asp.net
- iis
- url-rewriting
- urlrewriter
- c#
- enums
- ocaml
- haxe
- algorithm
- string
- viewstate
- .net
- c++
- c
- symbol-table
- mysql
- database
- postgresql
- licensing
- migration
- vb.net
- vb6
- declaration
- vb6-migration
- python
- psycopg2
- backup
- vmware
- virtualization
- gnu-screen
- authentication
- desktop
- excel
- xll
- cultureinfo
- regioninfo
- oracle
- client
- session
- download
- html
- virtual
- constructor
- scenarios
- perl
- full-text-search
- javascript
- ajax
- testing
- oop
- inheritance
- vim
- encapsulation
- information-hiding