One way to do this is to create a parser and a beautiful tool. The parser reads the source and builds the AST, fixing the essence of the program structure. A pretty printer takes a tree and regenerates the output based on the structure; thus, it is βeasyβ to get structured output. As a key hint at each level, the language structure (classes, methods, blocks, loops, conventions), prettyprinter can print text with fingerprints to give a good indentation structure.
Parsing and simpling are quite complex topics. Instead of repeating all of this here, you can see my Answer to the question of how to parse, followed by a discussion of how to build an AST . Prepress is not well known, but this SO answer to my question gives a fairly complete description of how to do this.
Then you have the difficulty of determining the actual grammar of VB.net. It takes a lot of work to extract from the reference documentation ... and this is not entirely correct, so you need to check your parser with a lot of code to convince yourself of this. This part, unfortunately, just sweats.
Given the niceprinter program, OP can simply run it as a file formatting process.
If you do all this, then yes, you can format the text of VB.net. Our (stand-alone) VB.net formatter ("DMSFormat ...") does this above to achieve beautiful printing. A.
Given the file "vb_example.net":
Module Test Public Shared Function CanReachPage(page As String) As Boolean Try Using client = New WebClient() Using stream = client.OpenRead(page) Return True End Using End Using Catch Return False End Try End Function End Module
Following:
C:>DMSFormat VisualBasic~VBdotNet C:\temp\vb_example.net
gives:
VisualBasic~VBdotNet Formatter/Obfuscator Version 1.2.1 Copyright (C) 2010 Semantic Designs, Inc Powered by DMS (R) Software Reengineering Toolkit Parsing C:\temp\vb_example.net [encoding ISO-8859-1] Module Test Public Shared Function CanReachPage(page As String) As Boolean Try Using client = New WebClient() Using stream = client.OpenRead(page) Return True End Using End Using Catch Return False End Try End Function End Module
which is identical to what OP wanted in his example.
You can easily redirect the contents of a formatted program to a file.
You can provide the tool with a project file, and it will format as many files as you specify in the project file at once.
The formatter integrates the full VB.net parser, and our own highly printable mechanism. It analyzes the source text accurately (including the strange character encodings). Since it uses a robust parser and prettyprinter, it will not break the code.
The eval version works with files of several hundred lines of code. This may be exactly what you need.
I would provide a link, but SO didn't seem to like it. You can find it through my biography.