搜索和替换宏(或重用) [英] Search and Replace macros (or reusage)
问题描述
嗨
在VS工作15年后,我意识到我不知道如何存储某个搜索并替换"模式",以便随时快速重复使用我需要它。
After 15 years with VS I realized that I do not know how to store a certain search and replace "pattern", in order to reuse it quickly anytime I need it.
使用"模式"我不一定是指,SR使用Reg exp。
例如,我有一些WPF项目,其自定义DependencyProperties注册为
With "pattern" I do not necessarily mean, the SR uses Reg exp.
For example I have some WPF projects with custom DependencyProperties that are registered like
DependencyProperty.Register("FileLocation", ...);
No I想要确保属性名称中没有拼写错误并使用"nameof" op,like
No I want to ensure that there are no typos in the property names and use the "nameof" op, like
DependencyProperty.Register(nameof(FileLocation), ...);
搜索&安培;替换看起来像
The Search & Replace looks like
/* search: */ (\.Register\w*)\("([^"]+)"
/* replace: */ $1(nameof($2)
/* options: */ Regex: on
现在我想以某种方式存储S& R,所以每当我需要它时我都可以重新使用它。
如何完成?
对于像这样的任务,VS不支持宏吗?
Thx,Chris
Now I want to store the S&R somehow, so I can reuse it qucikly whenever I need it.
How is iit done?
Doesn't VS support macros for tasks like this
Thx, Chris
推荐答案
Chris,
Chris,
您可以使用
DTE.Find.FindReplace 自动执行Search& Replace的方法。例如,作为我的
Visual Commander 扩展名:
You can use DTE.Find.FindReplace method to automate Search & Replace. For example, as the following C# command for my Visual Commander extension:
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
int options = (int)(EnvDTE.vsFindOptions.vsFindOptionsRegularExpression |
EnvDTE.vsFindOptions.vsFindOptionsMatchCase |
EnvDTE.vsFindOptions.vsFindOptionsMatchInHiddenText |
EnvDTE.vsFindOptions.vsFindOptionsSearchSubfolders |
EnvDTE.vsFindOptions.vsFindOptionsKeepModifiedDocumentsOpen);
DTE.Find.FindReplace(EnvDTE.vsFindAction.vsFindActionReplaceAll,
@"(\.Register\w*)\(""([^""]+)""",
options,
@"
1(nameof(
2)" ,
EnvDTE.vsFindTarget.vsFindTargetCurrentDocument);
}
2)", EnvDTE.vsFindTarget.vsFindTargetCurrentDocument); }
这篇关于搜索和替换宏(或重用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!