Tool B4a Source Code Formatter (now open source)

SOURCE CODE FORMATTER
Cleans up your Basic4Android project source code

As a programmer I tend to write code without too much regard for neatness with the intention of cleaning it up at a later date. As my projects became longer it could take quite a while to correctly space and indent my code - so I wrote a VB program to take care of this for me.

The program is available free of charge and all comments and suggestions are welcomed. Erel has since uploaded this to the website, you can download this to a pc from here:

www.b4x.com/android/files/B4ACoderSetup.zip

************************** Version 1.9 now available. ****************************

B4aCoder.jpg


1) Source code is formatted neatly in a user configurable manner.
2) Wrongly scoped and unused declared variables are listed.
3) Dead code is listed.
4) Fault listings appear in a datagridview on screen and can be printed to the printer.

So far we have tested it on a 22 module program and it has worked faultlessly. The interface has recently been updated to reduce flickering and additional functions handle public and private subs. Wrongly scoped variables are now noted and recommendations given to reduce a variables scope where appropriate.
Please leave a comment if you find the program useful.

Hope you like it.

UPDATE: Visual Studio (VB 2008) original source code is now available for programmers who would like to go on to improve this program for the benefit of the B4A community. www.b4x.com/android/files/B4ACoder.zip. Download the zip file and place the contents in your Visual Studio projects directory.
 
Last edited:
First of all, Thank you for your work. Your B4A Coder has the potential to become a very useful tool for all of us.

I have installed version 1.2 and now the installation under windows 7 works fine. I have tested your app with a very large application and could compile it without any errors.

Regarding the reformatted sourcecode, there remain some points on on my wishlist. Let me point out three issues:

1. Existing indentations should never be removed. Your app often strips Tabs from the source code, for example in Select blocks and Type Declarations. This makes the code less readable (to me).

2. >= and <= are not recognized correctly. See this example:

the following line of code:

Code: Copy
If(a>=b AND c<=d)Then...

should be reformatted to

Code: Copy
If (a >= b AND c <= d) Then ...

But currently the code line looks like this:

Code: Copy
If (a> = b AND c< = d) Then ...

3. Comments should not be reformatted:

Currently the following Comment
Code: Copy
'-------
'bla bla
'-------
is changed to:
Code: Copy
'- - - - - - -
'bla bla
'- - - - - - -
 
Six different users, six different preferences....

I had not thought about code within the line, my issue was with proper indentation.

I have many modules in my project that need formatting. Even to select a file (any file) and format it would be helpful at present.

If / End If (with nested) and Case statements format quite nicely.
Comments follow the same indent - nice...

Line space after Sub dec and before End Sub works well.

All else I can live with for now. :sign0098:
 
Version 1.3 is now available on the site, thanks to everyone that suggested improvements and for the encouragement to continue development of this program.

Changes since version 1.2:

1) The program now detects when a project has .bas modules and reformats them also. Thanks to Harris and klaus for that suggestion.

2) The interface has been updated a little to include a scrollbar alongside the counter and a label indicates which file is being formatted (thanks again klaus)

3) I have started to make some attempt to handle inline formatting i.e. inserting a space after a comma and to place spaces around + - < > etc. cheers for that peacemaker.

4) Additional lines under a sub now correctly handle a split line situation where a line is terminated with _ then recontinued below.

Incidently if the spacing around -+ - < > is not to your liking you can remove it by running the program again and resetting the spacing checkbox to indeterminate i.e. neither checked or unchecked.

Further points to consider:
Some changes need to be made to correctly recognise >= and =< and to not reformat comments (thank you Djembefola - good points)

Keep the suggestions coming.

Tom Law
 
Existing indentations should never be removed.

To explain this a little bit better:

Currently a multiline type declaration like
Code: Copy
Type myStructure( _
   a As String, _
   b As Int, _
   c As Int)
Dim d As Int
...
is reformatted to
Code: Copy
Type myStructure( _
a As String, _
b As Int, _
c As Int)
Dim d As Int

the same happens to Select Blocks:
Code: Copy
Select True
Case Condition1
   DoThis
Case Condition2
   DoThat
End Select
is reformatted to:
Code: Copy
Select True
Case Condition1
DoThis
Case Condition2
DoThat
End Select

My suggestion is, not to strip existing indentations.
 
Version 1.3 will only format the first 10 .bas files of my project which (currently) contains 23.

I first thought it was doing just lower case named files - but that is what they get renamed to after formatting.

Again, :sign0098:
Thanks
 
Hi Harris,

23 .bas files - well I will see what I can do - never expected anyone to have that many. In the meantime I am waiting for Erel to post version 1.4. This will further improve formatting, however it also highlights a number of programming errors (it found several in my main b4a project).

Your large project should be an excellent test for this (but please backup first). If this works OK I will see if I can accommodate your .bas files.

Tom
 
Hi Djembefola,

I see what you mean about the select case statements - I will see if I can improve this. In the meantime I have taken on board your recommendation to correctly handle <= and >=, also the program no longer modifies comments.


Tom
 
...in the meantime I have taken on board your recommendation to correctly handle <= and >=, also the program no longer modifies comments.

Much appreciated. Here comes another suggestion:

Strings should be excluded from formatting. Currently this line of code:

Code: Copy
s = BytesToString(buffer, 0, 3, "8859-1")

is changed to:

Code: Copy
s = BytesToString(buffer, 0, 3, "8859 - 1")

this leads to an error message.
 
Code: Copy
   If ((scrW > = 800 AND scrW < = 810 AND scrH > = 475 AND scrH < = 485) OR (scrW > = 475 AND scrW < = 485 AND scrH > = 800 AND scrH < = 810)) AND (D = 1) Then ' 7" low density 800x480
   DeviceType = Dim800x480x160
End If

If a comment follows if statement (example above), then the following line(s) are not indented properly.

Thanks
 
Much appreciated Klaus - If I get sufficient feedback I will consider building in some user configurable settings to allow people to adjust the formatting to suit their own personal preferences.

I think i went to the same code formatting schol as Klaus

Thanks for your work tom a great tool
 
Last edited:
Thanks for your comments Smee. The final version (1.7) has been uploaded today.
 
I have tested 1.7 and it works great!

The new error reporting feature is quite handy to find dead code (like unused vars) in each module and sub.

Everyone try this out - there is no harm in trying...

Excellent work my friend!!!!
 
1.7 tried

I just tried v1.7, just one thing this does not do for me..........that is write my code for me!!!!!!..lol. Great job a big help to me.
 
Good one tremara1... Must agree.

Sure is nice to work with well formatted code.
 
Works fine,

There is a lot of flickering of the display page.

Cheers
 
Spaces around operators in literals

The final version (1.7) has been uploaded today.

Thank you Tom for your nice tool! Works great.

One thing though. I like spaces around operators, but after the first compilation I had to solve a small riddle.

It took me a while to realize the difference between before:
Code: Copy
IndexOf("&")
and after:
Code: Copy
IndexOf(" & ")

But no problem - I appreciate your useful contribution to the community.

Best regards,
fredo
 
Hi Fredo. Thank you for that. If you have a line that the program cannot format correctly just surround it with:

'#Coderoff

your code here

'#Coderon

That will prevent the formatter from changing a line or section.

Tom
 
Hi Azlan,

The flickering you experienced can happen on some screens, mainly due to resizing panels under .net 3.5. It doesn't happen on my machine but I am aware that some setups may be more prone to it. I hope that it didn't detract from your use of the program. Maybe it can be improved with double buffering - I will take a look at this.

Tom
 
Would it be possible to call this app from the IDE and format the current project?

Will add above to wish list...

Thanks
 
Back
Top