B4J Code Snippet [Class] Code Beautifier/Formatter

Hi there

I just finished writing this piece of code to format the source code generated by one of the apps I'm working on.

Usage:
B4X:
dim clsCF as clsCodeModule
clsCF.initialize(File.Combine(File.DirApp,"my.bas"))
clsCF.Code_Format
clsCF.Save

NB: Please backup your source code before using this. This is provided as an "As Is" class code.
 

Attachments

  • clsCodeModule.bas
    20.4 KB · Views: 498
Last edited:

wonder

Expert
Licensed User
Longtime User

Mashiane

Expert
Licensed User
Longtime User
Discovered that in case of case sensitivity of the starting and ending keywords, this didnt work... an updated version. Will also add more keywords as I find them, but this is more better that before when I tested it.

B4X:
Sub FormatSourceCode(targetFile As String)
    Dim startKeywords As List
    Dim endKeyWords As List
    Dim lastPos As Int
    Dim fileContents As List
    Dim fileCnt As Int
    Dim fileTot As Int
    Dim fileStr As String
    Dim newFormat As StringBuilder
    Dim lastFound As Boolean
    Dim keyCnt As Int
    Dim keyTot As Int
    Dim keyStr As String
    Dim fileStr1 As String
    Dim casePos As Int
    Dim selectCasePos As Int
    ' initialize startkeywords
    startKeywords.Initialize
    startKeywords.Add("Sub ")
    startKeywords.Add("Public Sub ")
    startKeywords.Add("Private Sub ")
    startKeywords.Add("Select Case ")
    startKeywords.Add("Case ")
    startKeywords.Add("If ")
    startKeywords.Add("Try")
    startKeywords.Add("Catch")
    startKeywords.Add("For ")
    startKeywords.Add("Do While ")
    startKeywords.Add("Else")
    ' initialize endKeywords
    endKeyWords.Initialize
    endKeyWords.Add("End Sub")
    endKeyWords.Add("End Select")
    endKeyWords.Add("End If")
    endKeyWords.Add("Case ")
    endKeyWords.Add("End Try")
    endKeyWords.Add("Next")
    endKeyWords.Add("Loop")
    endKeyWords.Add("Else")
    endKeyWords.Add("Catch")
    ' initiliaze final file
    newFormat.Initialize
    ' define last position
    lastPos = 0
    casePos = 0
    lastFound = False
    ' read the contents of the file
    fileContents = File.ReadList("", targetFile)
    fileTot = fileContents.Size - 1
    fileStr = fileContents.Get(0)
       
    For fileCnt = 1 To fileTot
        '**If casePos <> 0 Then lastPos = casePos
        ' get the file contents
        fileStr = fileContents.Get(fileCnt)
        ' get the last line
        fileStr1 = fileContents.Get(fileCnt-1)
        fileStr1 = fileStr1.trim
        ' trim the file content
        fileStr = fileStr.trim
        lastFound = False
        ' start with starting keywords
        keyTot = startKeywords.Size - 1
        For keyCnt = 0 To keyTot
            keyStr = startKeywords.Get(keyCnt)
            If fileStr1.ToLowerCase.StartsWith(keyStr.tolowercase) = True Then
                Select Case keyStr.tolowercase
                Case "select case "
                    selectCasePos = lastPos + 1
                End Select
                '    casePos = 0
                'Case "Case "
                '    If casePos = 0 Then casePos = lastPos
                'Case Else
                '    casePos = 0
                'End Select
                lastPos = lastPos + 1
                lastFound = True
                If keyStr.ToLowerCase = "if " Then
                    If fileStr1.ToLowerCase.Contains(" then ") = True Then
                        lastPos = lastPos - 1
                        lastFound = False
                    End If
                End If
                Exit
            End If
        Next
        ' start with the ending keywords
        keyTot = endKeyWords.Size - 1
        For keyCnt = 0 To keyTot
            keyStr = endKeyWords.Get(keyCnt)
            If fileStr.tolowercase.StartsWith(keyStr.tolowercase) = True Then
                Select Case keyStr.tolowercase
                Case "end select"
                 lastPos = selectCasePos
                End Select
                '    casePos = 0
                'Case "Case Else"
                '    lastPos = casePos + 1
                '    casePos = 0
                'Case "End Sub"
                '    casePos = 0
                'End Select
                lastPos = lastPos - 1
                Exit
            End If
        Next
        fileStr = Space(lastPos * 4) & fileStr
        fileContents.Set(fileCnt,fileStr)
    Next
    File.WriteList("",targetFile,fileContents)
End Sub
 
Top