Astyle msvc

From PSwiki
Jump to navigation Jump to search

Add a macro in Visual Studio with the following content. Change the fileName and shellCmd paths.

 
    Public Sub formatDocWithAStyle()
        Dim fileName As String
        Dim textDocument As TextDocument
        Dim startPoint As EnvDTE.EditPoint
        Dim endPoint As EnvDTE.EditPoint
        Dim text As String
        Dim shellCmd As String
        Dim procId As Integer
        If DTE.ActiveDocument Is Nothing Then
            Return
        End If
        fileName = "c:\temp\formattedCode"
        shellCmd = """D:\Program Files\AStyle\bin\AStyle" & """" & " --style=allman --align-reference=name --align-pointer=type --indent-switches --unpad-paren " & """" & fileName & """"
        textDocument = DTE.ActiveDocument.Object
        startPoint = textDocument.StartPoint.CreateEditPoint
        endPoint = textDocument.EndPoint.CreateEditPoint
        text = startPoint.GetText(endPoint)
        My.Computer.FileSystem.WriteAllText(fileName, text, False)
        procId = Shell(shellCmd, AppWinStyle.Hide, True, 30000)
        If procId = 0 Then
            text = My.Computer.FileSystem.ReadAllText(fileName)
            startPoint.ReplaceText(endPoint, text, vsEPReplaceTextOptions.vsEPReplaceTextTabsSpaces)
        End If
    End Sub