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:

CidTek

Active Member
Licensed User
Longtime User
One really cool feature I would like to see is one I use in an addin for VB and that is align all declaration statements (Dim) at a certain column (user configuarable).

This results in all the "As" keywords aligning to the same column resulting in a much tidier and more readable block of declares.

You probably have to strip both tabs and spaces and then reconstuct the code line.

Dim X As String
Dim XYZ As String

If the "As" columns were aligned to a user configerable column it would be a much cleaner look.
 
Last edited:

metrick

Active Member
Licensed User
Longtime User
Tom: Thank you for sharing.
I have one request, can you make a check box so user can has option to comment out unused variable on User Settings. Eg. Check to comment unused variable.

This is awesome!
 

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
Neat utility. Thanks.

I have a couple of comments - keep in mind I receive negative opinions about my code formatting style, but for what it's worth…

For the application interface, I think a tabbed editor UI might be more useful. One tab for the B4A file and one tab for each BAS file. A menu item would allow "Apply Formatting". Another menu item could be "Settings". The formatted files would be reloaded to their corresponding editor tabs after formatting so you could see the results.

Personally, for indentation, I prefer to use spaces rather than tabs. I use 2 spaces for each indentation level. Why? Different editors use different spacing for tabs – some use 4 spaces, some 8, some variable. If the source is viewed in a different editor, using tabs for indentation can become messy. An option to use a fixed number of spaces or a tab character for indentation would be nice.

Comments are an important part of documentation and the formatting of comments is important for meaning.
- The blank lines or lack of blank lines surrounding comments should be preserved.
- If a comment starts at character position one, it should remain at character position one.
- If a comment starts after character position one, it should be relocated to start at the current level of indentation.
- If a comment is at the end of a line of code, it should remain at the end of the line of code – maybe cleaned up; one space after the end of code and one space after the ' before the comment begins.

I frequently use exponential notation to represent floating-point numbers (1e-3 for 0.001). I think I noticed in my test that 1e-3 was reformatted to 1e - 3.

Just my $2e-2. Very useful utility.

Barry.
 
Last edited:

CidTek

Active Member
Licensed User
Longtime User
- If I comment is at the end of a line of code, it should remain at the end of the line of code – maybe cleaned up; one space after the end of code and one space after the ' before the comment begins.

Personally I like my end of line comments to be aligned to the same column far enough to the right of most code lines so that they are quickly scanable when reading code. The preferred column would be a user setting.
 

coslad

Well-Known Member
Licensed User
Longtime User
Hi ,

i got an error , the screenshot is attached
 

Attachments

  • Image1.jpg
    Image1.jpg
    139.5 KB · Views: 186

Tom Law

Active Member
Licensed User
Longtime User
Hi ,

i got an error , the screenshot is attached

Hi Coslad,

Thank you for reporting this - I'm sorry if you encountered an error (this is a new one) but its probably something simple.

The code formatter makes a backup copy of the original file so you should be able to recover that without any problems.
Unfortunately the error is shown in a foreign language so I'm not sure what it refers to. Could you translate it?

If you would like to provide me with a copy of your code I will investigate the problem and fix this for you.

Regards

Tom
 

Tom Law

Active Member
Licensed User
Longtime User
Hi Coslad,

Thank you for reporting this - I'm sorry if you encountered an error (this is a new one) but its probably something simple.

The code formatter makes a backup copy of the original file so you should be able to recover that without any problems.
Unfortunately the error is shown in a foreign language so I'm not sure what it refers to. Could you translate it?

If you would like to provide me with a copy of your code I will investigate the problem and fix this for you.

Regards

Tom
Unfortunately I have been unable to get a reply from Coslad, so for now must assume that the problem has been resolved.
 

ValDog

Active Member
Licensed User
Longtime User
I just ran the program with my project - seems to have handled all 28 files Ok - but, when I now go to compile I see the following:


Parsing code. 0.25
Compiling code. 1.35
Compiling layouts code. 0.32
Generating R file. Error
ERROR: resource directory 'C:\Program Files\Android\sdk\extras\google\google_play_services\libproject\google - play - services_lib\res' does not exist


The resource directory does exist, so what is going on? I did not have this issue before reformatting.
 

ValDog

Active Member
Licensed User
Longtime User
maybe the spaces in foldername are the problem? "\google - play - services_lib\" -> "\google-play-services_lib\"

Yes, that was the issue. In fact, I found related issues. For instance, when writing numbers in exponential notation: "1E-3" was reformatted as "1E - 3", and if I had set anything to a value of say "-1" it was reformatted as "- 1", and another glitch was searching a string for a "=" all of sudden became " = " which was also problemation.

Other than those issues I'm pretty happy with the results.
 

Tom Law

Active Member
Licensed User
Longtime User
Hi Valdog,

Its been a while since I wrote this utility - but thank you for letting me know about the problem that you encountered. The formatter can occasionally get it wrong. To handle this and to prevent it happening again you can insert a manual break around the affected code i.e.:

'#Coderoff

your code here

'#Coderon

This will stop the specific code portion from being modified.

Another option is to change the first user setting i.e.:
Check to Space operators i.e "+ - < >" chars - Indeterminate removes spaces
This should prevent exponential numbers and negative values from being modified.


Regards
Tom Law
 
Last edited:

ValDog

Active Member
Licensed User
Longtime User
Thanks Tom,

I was aware of the #Coderoff functionality - just didn't realize until I ran B4ACoder the impact it had on my application. Thanks for your app and feedback!
 

Daniel Joseph

New Member
Licensed User
Longtime User
Hi Tom,
This is a fantastic bit of software - to start each day with neat code is such a pleasure. Many thanks for sharing.

Two comments. The first time I used the software I was surprised that there wasn't a box that came up to say "Here are the files I'm going to format - is this ok?" but instead jumped straight in. I'm ok with this now, but it was just a bit of a surprise. Second thing is, once one project is tidied, to tidy a second the software has to be restarted. I happily live with both of these things though, and I'd just like to say many thanks to you again.

: o )
 

Harris

Expert
Licensed User
Longtime User
Like I said in a post many months ago, "so many users, so many options" - or something to that effect.

It is still great to format my messy code so I (and others) can read it.
It sure would be nice to have this as an IDE option - "Format my code"... (this would require a .net be installed - which installer takes care of).

Thanks again Tom, maybe someday I will learn to indent and tab properly (but don't count on it).
 

Tom Law

Active Member
Licensed User
Longtime User
Hi gentlemen, thank you for your kind comments, very much appreciated. perhaps if enough people come up with good ideas I might consider upgrading this at some point.
 

CidTek

Active Member
Licensed User
Longtime User
I still would like to see inline comments align in a user settable column as it really help the code look tidy.
 

Marco Nissen

Active Member
Licensed User
Longtime User
Here is an error from my side .. any idea?

B4X:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
  at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
  at B4ACoder.Routines.ConfirmEnd(String Tempstring)
  at B4ACoder.Routines.Translator()
  at B4ACoder.Routines.Reader()
  at B4ACoder.Routines.Reader()
  at B4ACoder.MainScreen.Button1_Click(Object sender, EventArgs e)
  at System.Windows.Forms.Control.OnClick(EventArgs e)
  at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
  at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
  at System.Windows.Forms.Control.WndProc(Message& m)
  at System.Windows.Forms.ButtonBase.WndProc(Message& m)
  at System.Windows.Forms.Button.WndProc(Message& m)
  at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5483 (Win7SP1GDR.050727-5400)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v2.0.50727/mscorlib.dll
----------------------------------------
B4ACoder
    Assembly Version: 1.8.0.0
    Win32 Version: 1.8.0.0
    CodeBase: file:///C:/Program%20Files%20(x86)/B4ACoder/B4ACoder.exe
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 8.0.0.0
    Win32 Version: 8.0.50727.5483 (Win7SP1GDR.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5467 (Win7SP1GDR.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5483 (Win7SP1GDR.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5483 (Win7SP1GDR.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Runtime.Remoting
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5483 (Win7SP1GDR.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------
System.Configuration
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5483 (Win7SP1GDR.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5483 (Win7SP1GDR.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Data
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5483 (Win7SP1GDR.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_64/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
 

Tom Law

Active Member
Licensed User
Longtime User
Hi Marco, it looks like some kind of string handling problem. If this is reproducible would you like to send me a copy of the program code so that I can fix it?

Email [email protected]
 
Top