Software Geek

March 25, 2008

VS.NET Macro To Group and Sort Your Using Statements

Filed under: Software

Also see: Access to old blogs

I try to follow a coding standard for organizing my using statements. System.* goes at the top and then other namespaces grouped together like this:

 using System;
 using System.Collections.Generic;
 using System.Configuration;
 using System.Data;
 using System.Data.SqlClient;
 using System.Web;
 using System.Web.Script.Services;
 using System.Web.Services;
 using System.Web.Services.Protocols;
	
 using Microsoft;
 using Microsoft.CSharp;
	
 using MyCompany;
 using MyCompany.Web;

I finally got tired enough of keeping this all sorted out that I made a VS.NET macro to do it for me. This macro will take the current selection, parse it for using statements, group and sort them like the above example.

Also see: A Couple of My Rules for Startups

Also see: Blogging and Newspapers, a Lesson in How Not to Brand and Market

Also see: A quick update on me.

Also see: LINQ - The Uber FindControl

Also see: Determining Whether a File Is an Assembly

Also see: Load(AssemblyName)

Also see: VS.NET Macro To Group and Sort Your Using Statements

Also see: LINQ - The Uber FindControl

Here’s the macro code:

 Dim _usingPattern As Regex = New Regex( _
 “\s*(?<using>using\s*(?<group>\w+)[^;]*);”, _
 RegexOptions.IgnoreCase _
 Or RegexOptions.Multiline _
 Or RegexOptions.ExplicitCapture _
 Or RegexOptions.CultureInvariant _
 Or RegexOptions.Compiled _
 )
	
 Public Sub SortUsing()
 If Not DTE.ActiveDocument Is Nothing Then
 Dim sel As TextSelection = DTE.ActiveDocument.Selection
	
 If sel.Text.Contains(vbCrLf) Then
 If sel.ActivePoint Is sel.BottomPoint Then sel.SwapAnchor()
 sel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn, True)
 sel.SwapAnchor()
 sel.EndOfLine(True)
	
 Dim groups As New SortedList(Of String, List(Of String))()
 For Each match As Match In _usingPattern.Matches(sel.Text)
 Dim u As String = match.Groups(“using”).Value
 Dim g As String = match.Groups(“group”).Value
	
 ‘ System usings sort at the top
 If g = “System” Then g = “_” + g
	
 Dim list As List(Of String)
 If Not groups.TryGetValue(g, list) Then
 list = New List(Of String)()
 groups.Add(g, list)
 End If
 list.Add(u)
 Next
	
 Dim builder As New StringBuilder()
 For Each group As KeyValuePair(Of String, List(Of String)) In groups
 If builder.Length > 0 Then builder.AppendLine()
 group.Value.Sort()
 For Each line As String In group.Value
 builder.Append(line)
 builder.AppendLine(“;”)
 Next
 Next
	
 sel.DeleteLeft()
 sel.Insert(builder.ToString())
 End If
 End If
 End Sub


Multisoft Group: Custom Software Development and Consulting Service.

Also see: DevWeek 2008 Cross Platform Silverlight Demos

Also see: What Are You Destined to Be ?

Also see: Note to self: Blog about using Service Broker

Also see: Tagspace, Meet Claimspace

Also see: Avoid DevPath

Also see: 2,433 Unread Emails, I feel your pain..

Also see: JSR-203 more New I/O APIs - NIO.2

Also see: Apartments and Pumping in the CLR

Also see: Blogging and Newspapers, a Lesson in How Not to Brand and Market

Also see: Tagspace: Social Bookmarking for the Whole Web…from Microsoft

Also see: Access to old blogs

Also see: 2,433 Unread Emails, I feel your pain..

Also see: Generics and .NET

Also see: Channel 9 Interview

Also see: Help John Baez and Mike Stay!

Also see: 2,433 Unread Emails, I feel your pain..

Also see: Eriskay: a Programming Language Based on Game Semantics

Also see: Should “Membership Stores” Be Permitted in Redmond’s Manufacturing Park Zone?

Also see: Tagspace, Meet Claimspace

Also see: Determining Whether a File Is an Assembly


http://weblogs.asp.net/dfindley/archive/2007/02/07/vs-net-macro-to-group-and-sort-your-using-statements.aspx

Comments »

The URI to TrackBack this entry is: http://annil12.blogsome.com/2008/03/25/vsnet-macro-to-group-and-sort-your-using-statements-2/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome
Theme designed by Jay of onefinejay.com