visual studio – how to format whole project files

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 could customize formatting style they want.

However, it is not easy to indent whole project.

In visual studio 2010, before VS macro is removed. we could use following script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
 
Public Module Formatting
 
 Dim allowed As List(Of String) = New List(Of String)
 Dim processed As Integer = 0
 Dim ignored As Integer = 0
 Dim errors As StringBuilder = New StringBuilder()
 Dim skippedExtensions As List(Of String) = New List(Of String)
 
 Public Sub FormatProject()
  allowed.Add(".master")
  allowed.Add(".aspx")
  allowed.Add(".ascx")
  allowed.Add(".asmx")
  allowed.Add(".cs")
  allowed.Add(".vb")
  allowed.Add(".config")
  allowed.Add(".css")
  allowed.Add(".htm")
  allowed.Add(".html")
  allowed.Add(".js")
  Try
   recurseSolution(AddressOf processItem)
  Catch ex As Exception
   Debug.Print("error in main loop: " + ex.ToString())
  End Try
  Debug.Print("processed items: " + processed.ToString())
  Debug.Print("ignored items: " + ignored.ToString())
  Debug.Print("ignored extensions: " + String.Join(" ", skippedExtensions.ToArray()))
  Debug.Print(errors.ToString())
 End Sub
 
 Private Sub processItem(ByVal Item As ProjectItem)
  If Not Item.Name.Contains(".") Then
   'Debug.Print("no file extension. ignoring.")
   ignored += 1
   Return
  End If
  Dim ext As String
  ext = Item.Name.Substring(Item.Name.LastIndexOf(".")) 'get file extension
  If allowed.Contains(ext) Then
   formatItem(Item)
   processed += 1
  Else
   'Debug.Print("ignoring file with extension: " + ext)
   If Not skippedExtensions.Contains(ext) Then
    skippedExtensions.Add(ext)
   End If
   ignored += 1
  End If
 End Sub
 
 Private Sub formatItem(ByVal Item As ProjectItem)
  Debug.Print("processing file " + Item.Name)
  Try
   Dim window As EnvDTE.Window
   window = Item.Open()
   window.Activate()
   DTE.ExecuteCommand("Edit.FormatDocument", "")
   window.Document.Save()
   window.Close()
  Catch ex As Exception
   Debug.Print("error processing file." + ex.ToString())
   errors.Append("error processing file " + Item.Name + "  " + ex.ToString())
  End Try
 End Sub
 
 Private Delegate Sub task(ByVal Item As ProjectItem)
 
 Private Sub recurseSolution(ByVal taskRoutine As task)
  For Each Proj As Project In DTE.Solution.Projects
   Debug.Print("project " + Proj.Name)
   For Each Item As ProjectItem In Proj.ProjectItems
    recurseItems(Item, 0, taskRoutine)
   Next
  Next
 End Sub
 
 Private Sub recurseItems(ByVal Item As ProjectItem, ByVal depth As Integer, ByVal taskRoutine As task)
  Dim indent As String = New String("-", depth)
  Debug.Print(indent + " " + Item.Name)
  If Not Item.ProjectItems Is Nothing Then
   For Each Child As ProjectItem In Item.ProjectItems
    taskRoutine(Child)
    recurseItems(Child, depth + 1, taskRoutine)
   Next
  End If
 End Sub
 
End Module

for visual studio 2015, I recommend plugin
https://visualstudiogallery.msdn.microsoft.com/68076712-aea1-4317-ba71-ecf987da415f/view/Reviews

This entry was posted in Uncategorized. Bookmark the permalink.