从变量名中删除 m_ 前缀

发布于 2024-11-15 15:37:41 字数 254 浏览 2 评论 0原文

最初,我们计划采用旧的约定,为类变量添加 m_ 前缀。但现在要求将所有m_VaribaleName替换为this.variableName,即删除m_并使m_后面的第一个字符小写。

我可以搜索并用它替换 m_ 。但这不会将变量的第一个字符重命名为小写。搜索和替换后,如果我使用重构工具将 VariableName 重命名为variableName,这也会重命名已存在的 VariableName 属性。

我想知道是否有任何正则表达式、工具、宏可以使此任务自动化。

Initially we planned to have old convention of having m_ prefix for class variables. But now requirement has come to replace all the m_VaribaleName to this.variableName, i.e. remove the m_ and make the first character after m_ lowercase.

I can search and replace m_ with this. but this doesn't rename the variable's first character to lowercase. After search and replace if I use re-factoring tool to rename VariableName to variableName this also renames the property already exists with VariableName.

I am wondering is there any regex, tool, macro to make this task automated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒岛晴空 2024-11-22 15:37:41

Resharper 将突出显示解决方案中的所有此类错误,并单独修复它们,但我认为它无法通过单个命令修复所有这些错误。尽管如此,在它发现的错误之间导航还是很容易的。

Resharper will highlight all such errors in a solution, and fix them individually, but I don't think it can fix all of them with a single command. Still, it's easy enough to navigate between errors that it finds.

清风挽心 2024-11-22 15:37:41

您可以在 emacs 中执行此操作。

打开您的文件 (Cx Cf) 并执行 Mx Replace-regexp
假设变量的名称是Variable

您的正则表达式查询会将 \(V\)ariable 替换为 \,(downcase \1)ariable

\, 告诉 emacs 下面的语句是一个 lisp 表达式。

此外,如果您想同时替换 m_,您可以将 m_\(V\)ariable 替换为 \,(downcase \1)ariable

同时处理文件中的所有实例,并且 emacs 会记住您的最后一个替换正则表达式查询,因此您不必为多个文件重新键入它。此外,还有一种方法使用 DIRED 模式同时对多个文件进行替换正则表达式。

在 DIRED 模式下打开一个目录 (Cx Cf DIR_NAME),通过浏览标记您想要的文件(您可以使用 np 进行导航>) 按 m。一旦标记了要处理的文件,请按Q(大写字母)。您将获得与执行单个文件相同的正则表达式提示,输入正则表达式、RET、替换内容和 RET。它将一次打开每个文件,对每个文件按 ! 来替换该文件中的所有 reg-exp。

之后,您仍然需要保存文件。您可以使用 Cs 然后使用 ! 来完成此操作。

You can do this in emacs.

Open your file (C-x C-f) and do M-x replace-regexp.
Let's say the name of the variable is Variable.

Your regexp query would replace \(V\)ariable for \,(downcase \1)ariable.

The \, tells emacs the following statement is a lisp expression.

Additionally, if you wanted to replace the m_ at the same time you could do replace m_\(V\)ariable for \,(downcase \1)ariable.

This will take care of all instances in a file at the same time and emacs does remember your last replace-regexp query so you do not have to retype it for multiple files. Furthermore, there is a way using DIRED mode to do the replace-regexp on multiple files at the same time.

Open up a directory on DIRED mode (C-x C-f DIR_NAME), mark the files you want by going over them (you can navigate using n and p) by pressing m. Once the files you want to process are marked press Q (capital-case). You will get the same regexp prompt as if you did a single file, enter the regexp, RET, the replacement, and RET. It will open up each file at a time, press ! for every file to replace all reg-exps in that file.

After that, you still have to save the files. Which you can do with C-s and then !.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文