B4J Question make Clear all comments codes

SMOOTSARA

Active Member
Licensed User
Longtime User
Hello friends
How can I write a program whit B4J for clears the all comments line and free line in B4A?
In fact, an app that makes our writing cleaner and more beautiful?;)

For example, convert 4 empty lines to 1 empty line
or
clears the all comments line

thank
 

udg

Expert
Licensed User
Longtime User
You may want to have a look at Regex.
As a simple starting point, a pattern to look for comments could be: ('.*)
It will work on code like:
B4X:
' this is a comment
dim abc as string
dim def as string = "test" 'inline comment
but will fail for something like:
B4X:
dim angle as string = "Angle(degrees, primes): 20 45' " 'inline comment
Sorry, I'm not at my PC now so I can't help you further with complete code, but spending some time on Regex (there are quite a few examples on the forum) won't result in a waste of time. Guaranteed.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Start with this non-ui project.
Change the path. Make sure to backup your project before you test it.
B4X:
#CommandLineArgs: "C:\Users\H\Documents\B4A Samples\B4AServer"
Sub Process_Globals
End Sub

Sub AppStart (Args() As String)
   Dim dir As String = Args(0)
   Dim rx As RegexBuilder
   rx.Initialize.StartNonCapture.AppendEscaped(".bas").EndNonCapture.AppendOr.StartNonCapture.AppendEscaped(".b4").Append(rx.CharAny).EndNonCapture
   rx.AppendEndString
   For Each f As String In File.ListFiles(dir)
       If Regex.Matcher2(rx.Pattern, Regex.CASE_INSENSITIVE, f).Find Then
           RemoveComments(dir, f)
       End If
   Next
End Sub

Sub RemoveComments (Dir As String, FileName As String)
   Dim NewLines As List
   NewLines.Initialize
   Dim lines As List = File.ReadList(Dir, FileName)
   Dim rx As RegexBuilder
   rx.Initialize.AppendStartString.Append(rx.CharWhitespace).AppendZeroOrMore.AppendEscaped("'")
   For Each line As String In lines
       If Regex.Matcher(rx.Pattern, line).Find Then
           Continue
       End If
       NewLines.Add(line)
   Next
   File.WriteList(Dir, FileName, NewLines)
   Log($"File: ${FileName}, removed ${lines.Size - NewLines.Size} lines."$)
End Sub
 
Upvote 0
Top