07 September 2007

ReSharper 3.0.3 EAP and Solution-wide Error Analysis

The ReSharper team is actively working on next version with C# 3.0 support, but we are still fixing things for ReSharper 3.0 and even add new features!

Among with various bug-fixes, ReSharper 3.0.3 features "Global Error Analysis" feature that finds compilation errors in your solution on-the-fly. It was available in early ReSharper 3.0 EAP builds, but then we decided that it is not mature enough to put it into release. Now, we've improved it and are going to include it in ReSharper 3.0.3 update.

You can get ReSharper 3.0.3 EAP build at download page. Take a look at post about Early Access Program, if you want to learn more about participating.

To enable analysis of all errors in solution, double-click a red crossing lines symbol at the very right of the status bar and check the "Analyze errors in whole solution" checkbox. Note, that it is per-solution setting. It may take quite a while to analyze your solution for the first time (you can continue your work), but once the solution has been analyzed only those files that may be affected by the changes made are reanalyzed. Tool window with all errors in your solution can be opened using ReSharper | Windows | Errors in Solution menu command. Also, next/previous error command walks through all solution errors, if solution-wide error analysis is enabled.

We would like to hear from you about this feature! Do you like it or not? Does it work fine for you? Do you have any problems? You can leave comments to this post, or drop a message in our EAP newsgroup. Thanks in advance!

06 September 2007

C# 3.0 Collection Initializers - Incomplete Feature?

I was really confused when I learned that "collection initializer" is so limited. I thought it was so natural to support extension methods for "Add" method, but it doesn't.

In short, collection initializer is the following syntax:

var list = new List<int> { 1, 2, 3 };
var dic = new Dictionary<int,string> { { 1, "a" }, { 2, "b" }, { 3, "c" } };


Each item in the braces is simply transformed to the call of Add method with appropriate signature.

var list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
var dic = new Dictionary<int,string>();
dic.Add(1,"a");
dic.Add(2,"b");
dic.Add(3,"c");


It's a bit more complex in reality, but it doesn't matter here.
So, when I first saw description of the feature I thought: How cool! I can specify extension method "Add" for "StringBuilder" and get the following:

var sb = new StringBuilder { "Text = ", Text, ";", "Value = ", Value };


But, restrictions are too strong - type being constructed should implement IEnumerable and have instance method "Add". IEnumerable is not of a big deal, but inability to use extension methods for Add is deal breaker.

How about this?

XmlDocument doc = new XmlDocument
{
new Node("person") { Attributes = { { "name", "John" }, { "age", "42" } },
Nodes =
{
new Node("phones") { Nodes =
{
new Node("number") { Value = "123 45 678" },
new Node("number") { Value = "777 77 777" },
new Node("number") { Value = "987 65 432" }
}
}
}


No way. You can't use collection initializer for XmlDocument, because it doesn't have Add methods. It is IEnumerable, though.

May be this?

var set = new PermissionSet( new ZoneIdentityPermission(...), new PrincipalPermission(...) };


No way. PermissionSet is IEnumerable too, but does have AddPermission instead of Add method.

If it only were searching for candidates as follows:

  • If type implements "IEnumerable", look for instance Add method with appropriate parameters,
  • If not found, if type implements "IEnumerable", look for extension method Add with appropriate parameters,
  • If not found, look for extension method AddToSequence with appropriate parameters.


This change, or something similar, would turn the otherwise very limited collection initializers into really powerfull object initialization feature.

04 September 2007

Visualize Right Margin in Visual Studio

ReSharper has formatting option to keep your source code lines below certain limit.
In Options, go to Languages / C# / Formatting Style / Line Breaks and Wrapping, find "Wrap long lines" below "Line Wrapping" group and enable it, then set "Right margin (columns)" to the desired value. That's pretty cool, but how can one see which lines will be wrapped?

Meet Visual Studio secret feature: Guides!

As described in this blog post, you can play a bit with registry and make Visual Studio display nice dotted vertical lines at specific column:

  • Open "HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Text Editor" key

  • Create new string value "Guides"

  • Set its value to something like "RGB(192,192,192) 119" (columns in registry are zero-based)

  • Restart Visual Studio


Now you will be able to see when your lines are too long, and probably make manual formatting before ReSharper will split lines at next reformat.