Use EnvDTE automatic format entire project

Source: Internet
Author: User

It's not a good practice to see the source code for a C # Open source project today, because the author uses C + + coding style to organize the code, VS only the automatic format of single file code (Edit-> Advanced-> format document), There is no automatic format for the entire project options, no way, you can only write a macro.

First Open Marcos Explorer (Tools-> marcors-> Macro Explorer), and then in Marcos Explorer, select MyMacros-> Module1, double hit open Module1, add Add the following code to the public module Module1 and end module and save, back to Marcos Explorer, we see autoformatsolution has been in Module1, right key to run on it.

The simple program is to call Autoformatprojectitem recursively on each project's ProjectItem (because ProjectItem may be a folder), I simply say Dte.executecommand (" Edit.formatdocument "), this command can be obtained in the following ways, relatively 1 single, but sometimes we can not record through the macro to get the things we are interested in, only through the 2,3 to hard find ...

1. Open the macro recording, and then manually perform a formatdocument, turn off the macro recording, in Macros Explorer in RecordingModule you can see the corresponding information

2. Open Command Window (View-> other Windows-> Command window), and then try to find out what you need command,command generally have one by one correspondence with what you want to do. For example, if we are going to execute format Document under the Edit menu, then you can play edit first, and then look for the commands you need in the pop-up completionlist.

3. Query in the Tools-> Options-> Environment-> Keyboard window, the Query method is basically similar to 2.

Dim Solution as Solution = DTE. Solution
Sub autoformatsolution ()
for i-as Integer = 1 to Solution. Projects.Count
Autoformatproject (solution.          Projects.item (i))
Next
End Sub
Private Sub autoformatproject (ByVal proj as Project)
For I as the Integer = 1 to proj. Projectitems.count
Autoformatprojectitem (proj. Projectitems.item (i))
Next
End Sub
Private Sub Autoformatprojectitem (ByVal ProjectItem as P Rojectitem)
If projectitem.kind = Constants.vsprojectitemkindphysicalfile Then
If Projectite                  M.name.endswith (". cs") Then
Dim window as window = Projectitem.open (constants.vsviewkindcode)
Window. Activate ()
ProjectItem.Document.DTE.ExecuteCommand ("edit.formatdocument")
Window . Close (vssavechanges.vssavechangesyes)
End If
End if
' If the ProjectItem is a fold, apply autoformatprojectitem to it recusivly
If not PROJECTITEM.PR Ojectitems is no Then
for i as Integer = 1 to ProjectItem.ProjectItems.Count
Au Toformatprojectitem (ProjectItem.ProjectItems.Item (i))
Next
End If
End Sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.