Thursday, March 12, 2009

Update to the Code Formatter Plugin

Version of the Code Formatter Plugin is now available.

New in this version:

  1. Enhanced support for Syntax Highlighter 2x
    You can now specify things such as tab size, show ruler, collapseshow toolbar, show line numbers, starting line number, and highlighting specific line numbers
  2. Configuration screens for the different providers
    You can now add and remove languages and generally configure each provider.  You can also edit the files manually – they are called SyntaxHightlighter2xConfiguration.xml and ActiProConfiguration.xml.  They are located in the Plugins folder.
  3. Preview window for Syntax Highlighter formatted code.
    This was added because the Preview tab in Windows Live Writer doesn’t include the scripts and styles used in your blog engine’s templates and hence previewing Syntax Highlighted code means it looks plain.  Click the Show Preview button to see this window.

Code Formatter Plugin home-page
Direct download link

Friday, March 06, 2009

IDisposable alerts

Types that implement IDisposable usually do so for a reason.  They probably consume resources that should be released as early as possible.

In a recent project, I came across a very neat idea.  In the destructor/finalizer/finaliser of your IDisposable type, do something to alert the consumer that you’re being collected by the Garbage Collector and hence you haven’t been disposed of correctly.

But how does this type know it’s not been disposed correctly?  Well, if you follow the IDisposable pattern to the letter (described in the excellent book Effective C# (Item 18), and about 3,000 places around t’internet), in your Dispose method, you’ll call GC.SupressFinalize(this);  meaning the Garbage Collector won’t call your finalizer.  So if you ever end up in the finalizer, the naughty user hasn’t called Dispose or hasn’t put the construction of your type in a using block.

There’s two bits to this

The constructor:

public MyResourceHungryType( )
  _stackTrace = new StackTrace( ) ;

The finalizer:
~MyResourceHungryType( )
  Debug.WriteLine( _stackTrace.ToString( ) ) ;

Then, if the finalizer is ever called, you’ll get a call stack printed up to the point where you created this type – something like:
at Namespace.MyResourceHungryType..ctor()
   at Namespace.MyType.DoSomething()
   at SomeNamespace.SomeMethod()


New version of the Code Formatter Plugin

Version 2.0 of the Code Formatter Plugin for Windows Live Writer is now available.

New in this version is the ability to use different formatting engines – in this version: ActiPro and SyntaxHighlighter 2.0

Also new is the ability to output either formatted code as text or as a bitmap.

A massive thank you to ActiPro for donating the ActiPro Syntax Editor component – the best code editor available!

If you’re reading this in an aggregator, the following code snippets may look unformatted (apart from the bitmap), but if you’re not, then it should be all nicely formatted.

Here’s some output from ActiPro as text:

public int TabWidth
      return _content.GetInt(@"TabWidth", 4);
      _content.SetInt(@"TabWidth", value);

Here it is again as a bitmap:


and here it is using SyntaxHighlighter 2.0

public int TabWidth
      return _content.GetInt(@"TabWidth", 4);
      _content.SetInt(@"TabWidth", value);

Please feel free to read more and download it.

Thursday, March 05, 2009

Online mind maps in Silverlight

I’m a big fan of mind maps.  I use MindManager, but there’s now an online Silverlight alternative from DropMind

It’s currently in beta, so there’s a few things that need addressing to make it as nice to use as MindManager (such as keyboard navigation).  Despite this, it looks feature packed compared to other online mind mapping tools I’ve tried.

There’s also a desktop version.  It has none of the above keyboard problems.  It also looks and feels very similar to MindManager:





It’s nice to see Silverlight apps like this.  I’d probably switch to it once all the glitches are resolved.  MindManager’s great, but it comes with a lot of fluff I don’t need, like Office integration and RSS features.

Wednesday, March 04, 2009

Halt, this is StyleCop. You are in violation of SA1201!

StyleCop has a law rule named SA1201. It says that you should put various parts of your code in the correct order.  For the contents of a class this goes like:

  1. Fields
  2. Constructors
  3. Finalizers (Destructors)
  4. Delegates
  5. Events
  6. Enums
  7. Interfaces
  8. Properties
  9. Indexers
  10. Methods
  11. Structs
  12. Classes

Doing all this by hand is tedious, but as with most things, ReSharper (R#) makes it easier!

R# has a couple of features that, when combined, will mean you are fully SA1201 compliant (and, quite possibly well within the 20 seconds compliance window!)  Firstly, there is a feature called Code Cleanup:


You can get R# to do various things during code clean-up.  One of them is Reorder Type Members.  R# does a decent job of moving stuff around by default, but it’s not perfect and doesn’t stop all warnings about SA1201.  Thankfully, Reorder Type Members is configurable.  The interface is straight XML in the R# Options screen.  It’d be nice to have a GUI over it, but as yet (in R# 4.1 and 4.5), there isn’t one.  Thankfully, you can’t bugger things up too much as the XML is validated against an XSD before it’s saved.

I was going through a legacy project recently and wanted a way to automatically reorder things, so I modified the R# XML configuration.  To get your environment set-up, download the new XML configuration, go to ReSharper/Options and under languages/C# there an entry named Type Members Layout.


Deselect Use Default Patterns check-box and past in the new XML and click OK.

If you want a C# class that contains most of the things that should be reorganised, download it here.  Mash it up a bit and then run StyleCop on it to ensure no SA1201 errors.

Tuesday, March 03, 2009

Hiding your privates from StyleCop

I like StyleCop but I didn’t like the fact it produced warnings on private fields and methods.  I was wrong. Not wrong in that I didn’t like warnings on privates, but wrong that I thought there was no way to tell it to stop it.

Apparently there is.  Jason Allor kindly left a comment saying that it can be configured through the StyleCop settings dialog in Visual Studio.

I took a look and initially didn’t find it.  I didn’t find it because I assumed there’d be different rules for public and private entities.  But there’s not, you can decide at the rule type level:


It’d be nice to have separate rules for public and private.  Currently, I’ve turned off all Documentation rules because I don’t like being warned that I haven’t commented private methods.  However, I’d still like it to give me all the other great warnings if I do decide to comment a particular private method.

Programs that launch (or should launch) Visual Studio

Programs such as FxCop provide links to source files that can be clicked and then edited in Visual Studio. 

FxCop recently (probably since I went 64bit, but also possibly a side-effect of running Windows 7) started failing when clicking these links.  It said:

Could not start Microsoft Visual Studio. Try specifying an alternate source code editor in Application Settings.

Maybe it just looks in \Program Files\Microsoft Visual Studio\[whatever version].  Anyway, I couldn’t see anything in the event log or any local FxCop logs, so I just changed the setting under Tools\Settings to use an explicit path to devenv.exe.

This worked, although it started a new instance of the IDE for every file.  To get around that, you need to use the /Edit argument.  Here’s what it looks like (click to make it bigger):


There’s other handy switches, some of which I knew about, like building a particular project/solution (to get the full list, run devenv.exe /?).   It now uses an existing instance of Visual Studio if one’s present, otherwise it starts one up.