开发者

removing #region

I had to take over a c# project. The guy who developed the software in the first place was deeply in love with #region because he wrapped everything with regions. It makes me almost crazy and I was looking for a tool or addon to remove all #region from the project. Is there somethi开发者_Python百科ng around?


Just use Visual Studio's built-in "Find and Replace" (or "Replace in Files", which you can open by pressing Ctrl + Shift + H).

To remove #region, you'll need to enable Regular Expression matching; in the "Replace In Files" dialog, check "Use: Regular Expressions". Then, use the following pattern: "\#region .*\n", replacing matches with "" (the empty string).

To remove #endregion, do the same, but use "\#endregion .*\n" as your pattern. Regular Expressions might be overkill for #endregion, but it wouldn't hurt (in case the previous developer ever left comments on the same line as an #endregion or something).


Note: Others have posted patterns that should work for you as well, they're slightly different than mine but you get the general idea.


Use one regex ^[ \t]*\#[ \t]*(region|endregion).*\n to find both: region and endregion. After replacing by empty string, the whole line with leading spaces will be removed.

[ \t]* - finds leading spaces

\#[ \t]*(region|endregion) - finds #region or #endregion (and also very rare case with spaces after #)

.*\n - finds everything after #region or #endregion (but in the same line)

EDIT: Answer changed to be compatible with old Visual Studio regex syntax. Was: ^[ \t]*\#(end)?region.*\n (question marks do not work for old syntax)

EDIT 2: Added [ \t]* after # to handle very rare case found by @Volkirith


In Find and Replace use {[#]<region[^]*} for Find what: and replace it with empty string. #EndRegion is simple enough to replace.


Should you have to cooperate with region lovers (and keep regions untouched ), then I would recommend "I hate #Regions" Visual Studio extension. It makes regions tolerable - all regions are expanded by default and #region directives are rendered with very small font.


For anyone using ReSharper it's just a simple Atr-Enter on the region line. You will then have the option to remove regions in file, in project, or in solution.

More info on JetBrains.


To remove #region with a newline after it, replace following with empty string:

^(?([^\r\n])\s)*\#region\ ([^\r\n])*\r?\n(?([^\r\n])\s)*\r?\n

To replace #endregion with a leading empty line, replace following with an empty string:

^(?([^\r\n])\s)*\r?\n(?([^\r\n])\s)*\#endregion([^\r\n])*\r?\n


How about writing your own program for it, to replace regions with nothing in all *.cs files in basePath recursively ?
(Hint: Careful with reading files as UTF8 if they aren't.)

public static void StripRegions(string fileName, System.Text.RegularExpressions.Regex re)
{
    string input = System.IO.File.ReadAllText(fileName, System.Text.Encoding.UTF8);
    string output = re.Replace(input, "");
    System.IO.File.WriteAllText(fileName, output, System.Text.Encoding.UTF8);
}


public static void StripRegions(string basePath)
{
    System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(@"(^[ \t]*\#[ \t]*(region|endregion).*)(\r)?\n", System.Text.RegularExpressions.RegexOptions.Multiline);

    foreach (string file in System.IO.Directory.GetFiles(basePath, "*.cs", System.IO.SearchOption.AllDirectories))
    {
        StripRegions(file, re);
    }
}

Usage:

StripRegions(@"C:\sources\TestProject")


You can use the wildcard find/replace:

*\#region *
*\#endregion

And replace with no value. (Note the # needs to be escaped, as visual stuido uses it to match "any number")

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜