{"id":169,"date":"2017-02-26T11:10:41","date_gmt":"2017-02-26T11:10:41","guid":{"rendered":"http:\/\/windows.emacslisp.com\/?p=169"},"modified":"2017-02-26T11:10:41","modified_gmt":"2017-02-26T11:10:41","slug":"visual-studio-how-to-format-whole-project-files","status":"publish","type":"post","link":"http:\/\/windows.emacslisp.com\/index.php\/2017\/02\/26\/visual-studio-how-to-format-whole-project-files\/","title":{"rendered":"visual studio &#8211; how to format whole project files"},"content":{"rendered":"<p>formatting one single file is simple, find Edit.FormatDocument command in visual studio and click to apply for it. or Ctrl+K Ctrl+D.<\/p>\n<p>customizing C# indent, for example, going Tools | Options | Text Editor | C# | Tabs, on UI user could customize formatting style they want.<\/p>\n<p>However, it is not easy to indent whole project. <\/p>\n<p>In visual studio 2010, before VS macro is removed. we could use following script.<\/p>\n<pre lang=\"VB\" line=\"1\">\r\nImports System\r\nImports EnvDTE\r\nImports EnvDTE80\r\nImports EnvDTE90\r\nImports System.Collections.Generic\r\nImports System.Diagnostics\r\nImports System.Text\r\n\r\nPublic Module Formatting\r\n\r\n Dim allowed As List(Of String) = New List(Of String)\r\n Dim processed As Integer = 0\r\n Dim ignored As Integer = 0\r\n Dim errors As StringBuilder = New StringBuilder()\r\n Dim skippedExtensions As List(Of String) = New List(Of String)\r\n\r\n Public Sub FormatProject()\r\n  allowed.Add(\".master\")\r\n  allowed.Add(\".aspx\")\r\n  allowed.Add(\".ascx\")\r\n  allowed.Add(\".asmx\")\r\n  allowed.Add(\".cs\")\r\n  allowed.Add(\".vb\")\r\n  allowed.Add(\".config\")\r\n  allowed.Add(\".css\")\r\n  allowed.Add(\".htm\")\r\n  allowed.Add(\".html\")\r\n  allowed.Add(\".js\")\r\n  Try\r\n   recurseSolution(AddressOf processItem)\r\n  Catch ex As Exception\r\n   Debug.Print(\"error in main loop: \" + ex.ToString())\r\n  End Try\r\n  Debug.Print(\"processed items: \" + processed.ToString())\r\n  Debug.Print(\"ignored items: \" + ignored.ToString())\r\n  Debug.Print(\"ignored extensions: \" + String.Join(\" \", skippedExtensions.ToArray()))\r\n  Debug.Print(errors.ToString())\r\n End Sub\r\n\r\n Private Sub processItem(ByVal Item As ProjectItem)\r\n  If Not Item.Name.Contains(\".\") Then\r\n   'Debug.Print(\"no file extension. ignoring.\")\r\n   ignored += 1\r\n   Return\r\n  End If\r\n  Dim ext As String\r\n  ext = Item.Name.Substring(Item.Name.LastIndexOf(\".\")) 'get file extension\r\n  If allowed.Contains(ext) Then\r\n   formatItem(Item)\r\n   processed += 1\r\n  Else\r\n   'Debug.Print(\"ignoring file with extension: \" + ext)\r\n   If Not skippedExtensions.Contains(ext) Then\r\n    skippedExtensions.Add(ext)\r\n   End If\r\n   ignored += 1\r\n  End If\r\n End Sub\r\n\r\n Private Sub formatItem(ByVal Item As ProjectItem)\r\n  Debug.Print(\"processing file \" + Item.Name)\r\n  Try\r\n   Dim window As EnvDTE.Window\r\n   window = Item.Open()\r\n   window.Activate()\r\n   DTE.ExecuteCommand(\"Edit.FormatDocument\", \"\")\r\n   window.Document.Save()\r\n   window.Close()\r\n  Catch ex As Exception\r\n   Debug.Print(\"error processing file.\" + ex.ToString())\r\n   errors.Append(\"error processing file \" + Item.Name + \"  \" + ex.ToString())\r\n  End Try\r\n End Sub\r\n\r\n Private Delegate Sub task(ByVal Item As ProjectItem)\r\n\r\n Private Sub recurseSolution(ByVal taskRoutine As task)\r\n  For Each Proj As Project In DTE.Solution.Projects\r\n   Debug.Print(\"project \" + Proj.Name)\r\n   For Each Item As ProjectItem In Proj.ProjectItems\r\n    recurseItems(Item, 0, taskRoutine)\r\n   Next\r\n  Next\r\n End Sub\r\n\r\n Private Sub recurseItems(ByVal Item As ProjectItem, ByVal depth As Integer, ByVal taskRoutine As task)\r\n  Dim indent As String = New String(\"-\", depth)\r\n  Debug.Print(indent + \" \" + Item.Name)\r\n  If Not Item.ProjectItems Is Nothing Then\r\n   For Each Child As ProjectItem In Item.ProjectItems\r\n    taskRoutine(Child)\r\n    recurseItems(Child, depth + 1, taskRoutine)\r\n   Next\r\n  End If\r\n End Sub\r\n\r\nEnd Module\r\n <\/pre>\n<p><strong>for visual studio 2015, I recommend plugin<br \/>\nhttps:\/\/visualstudiogallery.msdn.microsoft.com\/68076712-aea1-4317-ba71-ecf987da415f\/view\/Reviews<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>formatting one single file is simple, find Edit.FormatDocument command in visual studio and click to apply for it. or Ctrl+K Ctrl+D. customizing C# indent, for example, going Tools | Options | Text Editor | C# | Tabs, on UI user &hellip; <a href=\"http:\/\/windows.emacslisp.com\/index.php\/2017\/02\/26\/visual-studio-how-to-format-whole-project-files\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/posts\/169"}],"collection":[{"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":1,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":170,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/posts\/169\/revisions\/170"}],"wp:attachment":[{"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}