Tuesday 1 November 2011

Roslyn: Opening the C# Compiler

Just recently Microsoft released the Roslyn CTP. You can read more on this page, but in short this opens the C# compiler to everyone.

Previous IDE's operating on source code were attempted mostly only by the those seeking an intense sanity challenge, as you needed to parse the C# document either with your own or someone elses C# parser or eye watering migraine inducing regex.

But no more.

Roslyn now makes it possible for the man on the street to parse C# and thereby do things like write his own productivity tools as I will demonstrate now and reincarnate an old friend.

Back in the day when I worked in Delphi there was a collection of IDE productivity tool with a product called GExperts.

One of the components was ProcedureList, you would hit CTRL+G and get a popup and can quick search and jump to an object in the document.

I missed this tool in Visual Studio even though there was the navigation bar, I just didn’t like it so much and the ProcedureList just felt faster to use, but I got used to this and forgot about it.

So now this small and simple enough for my first test of Roslyn:

To get started I needed the Visual Studio SDK (I didn’t have it already) (Required by Roslyn)

And then of course Roslyn CTP

Then you can use this in any projects, just add references to the Roslyn assemblies:

image

There is a VB one too, but I will just ignore it for now.

To parse a C# document into an AST is as follows:

SyntaxTree tree = SyntaxTree.ParseCompilationUnit(sourceCode);
var root = (CompilationUnitSyntax)(tree.Root);

Then you can select the classes or any other object type as follows:

var classNodes = root.DescendentNodes()
.OfType<ClassDeclarationSyntax>();

Or any other type you want to use, so now that I can get the AST and play with it I can create the add-in.

I created a Visual Studio 2010 Add-In project , don't worry this is VERY easy just use the template wizard to create a Visual Studio Package and follow the default settings.

Once created:

I built this very rudimentary frontend WinForm:

image

(That’s fine the procedure list is more about function anyway)

Then I wrote a small amount of boiler plate code in my addin's MenuItem callback:

private void MenuItemCallback(object sender, EventArgs e)
{
var app = (EnvDTE.DTE)GetService(typeof(SDTE));
if (app.ActiveDocument != null && app.ActiveDocument.Type == "Text")
{

var selection = (TextSelection)app.ActiveDocument.Selection;
var savedOffset = selection.ActivePoint.AbsoluteCharOffset;
selection.SelectAll();
string text = selection.Text;
selection.Cancel();
selection.MoveToAbsoluteOffset(savedOffset);

var view = new FormProcedureList();
var presenter = new ProcedureListPresenter(view, text);
presenter.Load();
view.ShowDialog();

if (view.SelectedMethod != null)
{
//selection.MoveToAbsoluteOffset(view.SelectedMethod.Position);
var line = OffsetToLineNumber(text, view.SelectedMethod.Position);
selection.GotoLine(line);
}
}

My presenter simply loads the Active visual studio document and converts it to an AST, which I can then use to extract the nodes as above and display the objects:

 

public void Load()
{
List<MethodDto> methods = this.GetMethods(this.Code).ToList();
this.View.BindMethods(methods);
}

I can now quick type anything into the search box and filter and instantly jump to the piece of code

Finally this is just like a normal add-in so by default you can't hit CTRL+G and get this so I set up my hotkey in the settings:

Tools - Options:

image

This basically overwrote my Goto Line, but I was fine with that for now, but you could choose something else.

So as you can see we can build awesome productivity tools with very little effort.

I will definitely be exploring this further, and I'm sure you will see some amazing things coming from Roslyn.

You can access the source via mercurial or browse the repo here:

http://code.google.com/p/procedurelist/downloads/list

Or download the VSIX package here (just double click to install)

http://code.google.com/p/procedurelist/downloads/list

1 comment:

  1. The best Roslyn demo I've seen allows you to copy C# code and paste as VB.NET, and vice versa.

    It is a really cool technology, and centralises some stuff that the VS IDE used to do in two places.

    ReplyDelete