如何使用 emacs 重构 C++ 源代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/673554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:39:36  来源:igfitidea点击:

How can I refactor C++ source code using emacs?

c++emacsrefactoring

提问by ?ukasz Lew

I'm interested mostly in C++ and method/class name/signature automatic changes.

我最感兴趣的是 C++ 和方法/类名/签名自动更改。

采纳答案by T.E.D.

I do this a lot, so I'm axiously awaiting other replies too.

我经常这样做,所以我也在焦急地等待其他答复。

The only tricks I know are really basic. Here are my best friends in Emacs when refactoring code:

我知道的唯一技巧是非常基本的。以下是我在 Emacs 中重构代码时最好的朋友:

M-x query-replace

This allows you to do a global search and replace. You'll be doing this a ton when you move methods and commonly-accessed data to other classes or namespaces.

这允许您进行全局搜索和替换。当您将方法和经常访问的数据移动到其他类或命名空间时,您将大量执行此操作。

C-x 3

This gives you a display with two buffers side-by side. You can then proceed to load different files in them, and move your cursor from one to the other with C-x o. This is pretty basic stuff, but I mention it because of how powerful it makes the next one...

这为您提供并排显示两个缓冲区。然后,您可以继续在其中加载不同的文件,并使用 将光标从一个移动到另一个C-x o。这是非常基本的东西,但我提到它是因为它使下一个变得多么强大......

C-x (
(type any amount of stuff and/or emacs commands here)
C-x )

This is how you define a macro in emacs. Any time you find yourself needing to do the same thing over and over to a bunch of code (and it is too complex for query-replace), this is a lifesaver. If you mess up, you can hit C-gto stop the macro definition, and then undo (C-_) until you are back to where you started. The keys to invoke the macro are C-x e. If you want to do it a bunch of times, you can hit Escand type in a number first. Eg: Esc 100 C-x ewill try to invoke your macro 100 times.

这就是在 emacs 中定义宏的方式。任何时候你发现自己需要对一堆代码一遍又一遍地做同样的事情(而且查询替换太复杂了),这是一个救星。如果你搞砸了,你可以点击C-g停止宏定义,然后撤消 ( C-_) 直到你回到开始的地方。调用宏的键是C-x e. 如果你想多次这样做,你可以先点击Esc并输入一个数字。例如:Esc 100 C-x e将尝试调用您的宏 100 次。

(Note: On Windows you can get "Meta" by hitting the Esc key, or holding down Alt).

(注意:在 Windows 上,您可以通过按 Esc 键或按住 Alt 来获得“Meta”)。

回答by muede

In recent Emacs versions (24), Semantic is able to this.

在最近的 Emacs 版本 (24) 中,Semantic 能够做到这一点。

  1. Possibly activate semantic mode M-x semantic-mode RET.
  2. Bring up the Symref buffer with C-c , g.
  3. Press C-c C-eto open all references.
  4. Rename with R.
  1. 可能激活语义模式M-x semantic-mode RET
  2. 使用 调出 Symref 缓冲区C-c , g
  3. C-c C-e打开所有参考。
  4. 重命名R

回答by Alex Ott

If you can program in elisp, you can look to combination of cedet + srecode from CEDET libraries- it provide all instruments for this task - find callers of functions, get signature, etc. But you need to create refactory tool yourself, using these instruments

如果你可以在 elisp 中编程,你可以从CEDET 库中寻找 cedet + srecode 的组合- 它提供了这个任务的所有工具 - 找到函数的调用者,获取签名等。但是你需要自己创建重构工具,使用这些工具

回答by jamesnvc

For somewhere in between refactoring tools and simple regex, since Emacs 22 you can embed arbitrary elisp expressions in your replacement text, which allows you to do incredibly powerful text manipulation. Steve Yegge wrote a good articleon this a while ago.

对于介于重构工具和简单正则表达式之间的某个地方,从 Emacs 22 开始,您可以在替换文本中嵌入任意 elisp 表达式,这使您可以进行非常强大的文本操作。Steve Yegge不久前写了一篇关于此的好文章

回答by TheJJ

With the cclsor cquery, which provide the "language server protocol" (lsp), you can refactor names with:

使用cclsorcquery提供“语言服务器协议”(lsp),您可以使用以下命令重构名称:

M-x lsp-rename

M-x lsp-rename

Alternative: srefactoruses the emacs semantic-mode framework: https://github.com/tuhdo/semantic-refactor/blob/master/srefactor-demos/demos.org

替代方案:srefactor使用 emacs 语义模式框架:https: //github.com/tuhdo/semantic-refactor/blob/master/srefactor-demos/demos.org

You can get it with M-x package-installfrom MELPA or at https://github.com/tuhdo/semantic-refactor/.

您可以M-x package-install从 MELPA 或 https://github.com/tuhdo/semantic-refactor/获取它。

回答by Greg Rogers

A friend of mine was playing with xrefactoryand said it worked pretty well. It isn't cheap though.

我的一个朋友在玩xrefactory并说它运行得很好。不过也不便宜。

回答by Vijayender

Build cscope symbols.

构建 cscope 符号。

lookup the symbol you want to refactor.

查找要重构的符号。

get into the cscope window, and start a macro after placing cursor on first occurence

进入 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)
  • 回复
  • 比照你的符号开始
  • 导航到符号的开头
  • 修改单词
  • cx o(回到 cscope)
  • n(用于下一个 cscope 符号)

you have to just c-x c-e now

你现在只需要 cx ce

回答by user7957

I totally agree that find-and-replace work fine. However , a really nice feature of cedet is 'semantic-symref-list'.

我完全同意查找和替换工作正常。然而,cedet 的一个非常好的特性是“语义符号引用列表”。

With the cursor on a method, run this command, and you will be presented with a buffer that lists all of the places in your code that reference this tag.

将光标放在方法上,运行此命令,您将看到一个缓冲区,其中列出了代码中引用此标记的所有位置。

You can still use find-and-replace tricks, and this will confirm that you have changed all the references.

您仍然可以使用查找和替换技巧,这将确认您已更改所有引用。

回答by nenchev

I've been using cqueryfor my C++ completion which uses Microsoft LSP for IDE <-> Tool communication. cqueryserver satisfies the requests of the LSP protocol using a clang backend.

我一直在使用cquery来完成我的 C++ 完成,它使用 Microsoft LSP 进行 IDE <-> 工具通信。cquery服务器使用 clang 后端满足 LSP 协议的请求。

lsp-emacsis the package that sits between emacs and the cquerybackend (cquery-emacs) which exposes an lsp-renamefunction. As a completion system, cqueryhas been very reliable and fast by the way, highly recommended.

lsp-emacs是位于 emacs 和暴露函数的cquery后端 (cquery-emacs)之间的包lsp-rename顺便说一下cquery作为一个补全系统非常可靠和快速,强烈推荐。

Give it a try, follow the getting-started guide on the cquerygithub: https://github.com/cquery-project/cquery/wiki/Emacs

试试看,按照cquerygithub上的入门指南进行操作https: //github.com/cquery-project/cquery/wiki/Emacs

Once you've got cquerysetup:

一旦你有了cquery设置:

  1. Hover your cursor over an identifier (class, var, whatever) you'd like to rename.
  2. M-x lsp-rename
  3. Enter the newname for the identifier.
  4. Do C-x s(save somebuffers), which will prompt you to save all the buffers that were touched by the refactor.
  1. 将光标悬停在要重命名的标识符(类、变量等)上。
  2. MX lsp-rename
  3. 输入标识符的名称。
  4. Do Cx s(保存一些缓冲区),它会提示你保存所有被重构触及的缓冲区。

You should probably go through all modified buffers and check what was done after refactoring with any tool/language.

您可能应该检查所有修改过的缓冲区并检查在使用任何工具/语言重构后做了什么。