Orchard CMS Quick Tip–Curling up with Orchard, or brace line breaks without ReSharper

Orchard CMS requires that all commits to the core use the JavaScript style curly brace positioning, where the opening brace is on the same line as the method signature. If you use ReSharper then it comes with an automatic .resharper file that applies this out-of-the-box but non-users can still get this functionality with plain vanilla Visual Studio.

This isn’t a post against ReSharper at all, I think its great, but its not-free so not every developer has it. If you are wanting to contribute to the Orchard core, write a blog post about Orchard with code or just have your custom modules look like they are part of the rest of the code this quick tip will let you get the brace positions the same.

What’s the difference?

Lets take a simple example. A normal install of Visual Studio would format a piece of code like this:

namespace Orchard.LearnOrchard.FeaturedProduct.Models
{
    public class FeaturedProductPartRecord : ContentPartRecord
    {
        public virtual bool IsOnSale { get; set; }
    }
}

The opening curly braces each start on a fresh line. Orchard favours the more terse JavaScript style approach with the curly braces:

namespace Orchard.LearnOrchard.FeaturedProduct.Models {
    public class FeaturedProductPartRecord : ContentPartRecord {
        public virtual bool IsOnSale { get; set; }
    }
}

Update – Orchard also supports the free Rebracer extension

It was pointed out to me (#1, #2) that Orchard actually also supports a free add-on which will manage the settings described below. If you’re using a version of Visual Studio that supports extensions then you should probably install this instead:

It’s a free extension that adds in support for automatically managing the settings for brace line-breaks based on the configuration values located in a rebracer.xml file.

Orchard comes with one of these in the root of the solution so you just install it and your Orchard solution will automatically pick it up, no further configuration required. The benefit of this is twofold:

  1. Zero configuration
  2. Your non-Orchard solutions can have their own (normal) brace line-break settings.

So unless there is some restriction with your environment it makes sense to use this.

How do I do it with pure Visual Studio settings?

It’s just a few checkboxes within the options panel:

  1. Click Tools | Options…
  2. Scroll down to the Text Editor node
  3. Expand the C# node
  4. Expand the Formatting node
  5. Click on the New Lines node
  6. You will see a list of options like in the image below which give you full control over when Visual Studio should put your open brace on a new line. Remove the check’s from all of the checkboxes in the “New line options for braces” section.

    orchard-curlupwithorchard
  7. Click OK

When you change this setting it will change it for all of your projects, so if you switch between Orchard and non-Orchard projects you will have to come back and re-enable these settings.

Older versions of Visual Studio

You might not see these options. Some of the older versions of Visual Studio showed simplified settings by default. It seems like (in Visual Studio Community 2015 at least) that it defaults to showing all the settings now.

If you can’t follow the steps above then look for a Show all settings checkbox in the bottom corner of the Options dialog.

No comments :