Integrating uncrustify into Visual Studio

I like clean code and I cannot lie. Visual Studio’s native code formatter is quite basic — it indents code, but doesn’t clean up spacing etc. While the Resharper extension does a great job for C#, there isn’t a good integrated tool for VC++.

Uncrustify is a command line tool that does a remarkable job on C / C++ and other languages. Running it from a command prompt (or even as an external tool) can get tedious rather quickly. At RemoteReality, we use a VS macro to launder code through uncrustify with a single keyboard shortcut.

    Sub Uncrustify()
        Dim proc As New System.Diagnostics.Process()

        If (DTE.ActiveDocument IsNot Nothing) Then
            Dim doc As Document = DTE.ActiveDocument.Object("Document")
            Dim procStart As New System.Diagnostics.ProcessStartInfo()
            Dim outputFile As String = doc.FullName + ".uncrustify"

            procStart.Arguments = "-c \rrdeV\Vperi\repos\unCrustify\rr.cfg -o " + outputFile + " -f " + doc.FullName
            procStart.FileName = "C:\Program Files\uncrustify\uncrustify.exe"
            procStart.CreateNoWindow = False

            Dim txt As TextDocument = DTE.ActiveDocument.Object("TextDocument")
            Dim ep As EditPoint = txt.StartPoint.CreateEditPoint

            Dim endPoint As TextPoint = ep

            Dim sp As EditPoint = txt.StartPoint.CreateEditPoint

        End If
    End Sub

The macro runs uncrustify on the document currently active in the editor and sends the formatted output to a temporary file. It deletes the text in the editor and replaces it text from the generated file.

The macro and our favorite configuration files can be found here.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s