Introduction
This is a simple command line utility to sort methods in VB source code, it can be used as an external tool from Visual Studio.
Background
I just needed something to sort my methods but not anything else, so I created this simple utility to do the work for me. It could be extended into a fully integrated Visual Studio Extension if someone wanted to do that.
Using the Code
The program is a simple EXE that runs from a command line. The options are:
-f"fullpath to your vb code"
Full path to the source code file e.g. -f"c:\my files\my code.vb"
This can be provided as a command line parameter from Visual Studio as described below:
-sAAB
Sort order for Subs, Functions, and Properties.
When sorting, each character is used to prefix the method name so that Subs, Functions and Properties can be grouped together. e.g. CBA will group Properties, then Functions, then Subs. Specifying -sAAA
will sort all methods by name with no grouping.
-d
Displays the sorted file, but doesn't change the original.
-r
Restores the original file after sorting. When a file is sorted, a copy is made, this option will restore the file to the original. Note that there is only one backup copy.
To set this up in Visual Studio, add three external tools from Tools>External Tools.
Command: path to sort Method Sorting.exe.
Arguments for sorting:
-f$(ItemPath}" -sAAB
Arguments for restoring:
-f"$(ItemPath)" -r
Arguments to display only:
-f"$(ItemPath)" -d -sAAB
This will add a menu item to the Tools Menu.
When you sort the file, Visual Studio will detect that it has been changed externally and prompt you reload it, click Yes to load the sorted file. Note: This clears all previous Undo actions.
Points of Interest
There is no template or settings for this utility and it should work with most VB code, it will ignore all Structures, Interfaces and Declares that can contain methods, including DllImport
directives.
It has been tested on a wide variety of native and interop code, but not with #Regions
.
I haven't allowed for #Regions
because I don't use them, but also, I have assumed (maybe incorrectly) that if you use #Regions
you already have your code grouped and sorted as you want it to be.