Public Function AutoFormatLines() As Object
If Not Settings.bFormatText Then
Return Nothing
End If
pbProgress.Visible = True
Dim iBracketsFound As Integer = 0
Dim iTabIndent As Integer = Settings.iIndent
Dim sFoundWhat As String = ""
bDisableSelectionUpdates = True
Dim sOperators() As String = {"<", ">", "=", "<=", ">=", "&&", "-", "/", "==", "!="}
'Cant use the brackets ( or ), and I can't use ||, +, *
Dim sBrackets() As String = {"(", ")"}
Dim sBracketsLength As Integer = sBrackets.Length - 1
'use regex
Dim sOperatorLength As Integer = sOperators.Length - 1
Dim rRegex(sOperatorLength) As Regex
For i As Integer = 0 To sOperatorLength
rRegex(i) = New Regex("\b" & sOperators(i) & "\b")
Next i
Dim rBrackets(sBracketsLength) As Regex
For i As Integer = 0 To sBracketsLength
'rBrackets(i) = New Regex("\b" & sBrackets(i) & "\b")
Next i
Dim mc As MatchCollection = Nothing
LockWindowUpdate(rtbEdit.Handle)
lStatus.Text = "Status: Formatting Text"
Application.DoEvents()
'Adjust one line at a time
'Search for lines that start with a bracket (only)
Dim iStart As Integer = rtbEdit.SelectionStart
Dim iLength As Integer = rtbEdit.Lines.Length
Dim sLinesText() As String = rtbEdit.Lines
Dim iIndent As Integer = 0
Dim bPass As Boolean = False
Dim bSkip As Boolean = False
pbProgress.PerformStep()
'Dim sRTBText As String = rtbEdit.Text
For i As Integer = 0 To iLength - 1
bPass = False
bSkip = False
If i = CInt(iLength / 4) - 1 Then
pbProgress.PerformStep()
Application.DoEvents()
End If
If i = CInt(iLength / 2) - 1 Then
pbProgress.PerformStep()
Application.DoEvents()
End If
If i = CInt(iLength / 1.3) - 1 Then
pbProgress.PerformStep()
Application.DoEvents()
End If
Dim iCurrentLineLength As Integer = sLinesText(i).Length
If iCurrentLineLength > 0 Then
Dim iSelectStart As Integer = rtbEdit.GetFirstCharIndexFromLine(i)
Dim iSub As String = sLinesText(i).Substring(0, 1)
Dim iTempInteger As Integer = 0
Do Until iSub <> " " AndAlso iSub <> ControlChars.Tab 'tabs!
Dim iLen As Integer = sLinesText(i).Length
If iLen < iTempInteger + 1 Then
Exit Do
End If
iSub = sLinesText(i).Substring(iTempInteger, 1)
iTempInteger += 1
Loop
Dim iTempIndent As Integer = iBracketsFound * iTabIndent
Dim iLineN As Integer = sLinesText(i).Length - 1
For k As Integer = 0 To iLineN
Dim sSubstr As String = sLinesText(i).Substring(k, 1)
If sSubstr = "{" Then
iBracketsFound += 1
sFoundWhat = "{"
bPass = True 'SEARCH in this FOR LOOP for >0 and make it > 0 etc..
End If
If sSubstr = "}" Then
iBracketsFound -= 1
sFoundWhat = "}"
bPass = True
End If
Dim bRan As Boolean = False
Next k
For k As Integer = 0 To sOperatorLength
mc = rRegex(k).Matches(sLinesText(i))
For Each mMatch As Match In mc
If mMatch.Index + sOperators(k).Length < sLinesText(i).Length Then
Dim sB4 As String = sLinesText(i).Substring(mMatch.Index, sOperators(k).Length)
Dim iAFPos As Integer = 0
If sB4 <> " " Then
rtbEdit.SelectionStart = iSelectStart + mMatch.Index
rtbEdit.SelectionLength = 0
rtbEdit.SelectedText = " "
'rtbEdit.Text = rtbEdit.Text.Insert(iSelectStart + mMatch.Index, " ")
iAFPos = 1
End If
If mMatch.Index + iAFPos + sOperators(k).Length < sLinesText(i).Length Then
Dim sAF As String = sLinesText(i).Substring(mMatch.Index + iAFPos, sOperators(k).Length)
If sAF <> " " Then
rtbEdit.SelectionStart = iSelectStart + mMatch.Index + iAFPos + sOperators(k).Length
rtbEdit.SelectionLength = 0
rtbEdit.SelectedText = " "
'rtbEdit.Text = rtbEdit.Text.Insert(iSelectStart + mMatch.Index + iAFPos + sOperators(k).Length, " ")
End If
End If
End If
Next mMatch
Next k
'For k As Integer = 0 To sBracketsLength
' mc = rBrackets(k).Matches(sLinesText(i))
' For Each mMatch As Match In mc
' If mMatch.Index + 1 < sLinesText(i).Length Then
' Dim sB4 As String = sLinesText(i).Substring(mMatch.Index, 1)
' Dim iAFPos As Integer = 0
' If sB4 = " " Then
' rtbEdit.Text = rtbEdit.Text.Remove(iSelectStart + mMatch.Index, 1)
' iAFPos = 1
' End If
' If mMatch.Index + iAFPos + 1 < sLinesText(i).Length Then
' Dim sAF As String = sLinesText(i).Substring(mMatch.Index + iAFPos, sOperators(k).Length)
' If sAF <> " " Then
' rtbEdit.Text = rtbEdit.Text.Remove(iSelectStart + mMatch.Index + iAFPos, 1)
' End If
' End If
' End If
' Next mMatch
'Next k
iIndent = iBracketsFound * iTabIndent
Dim iTempIndent2 As Integer = iIndent
If iLineN + 1 > 1 Then
'this line has more than just a curly bracket!
iIndent = iTempIndent ' we also need to move that curly bracket to the next line!
If sFoundWhat = "}" Then 'coming down, revert to iIndent
iIndent = iTempIndent2
End If
Else
iIndent -= iTabIndent
End If
If iTempInteger > 0 Then
rtbEdit.SelectionStart = rtbEdit.GetFirstCharIndexFromLine(i)
rtbEdit.SelectionLength = iTempInteger - 1
rtbEdit.SelectedText = ""
End If
If Not bSkip Then
rtbEdit.SelectionStart = iSelectStart
For k As Integer = 0 To iIndent - 1
rtbEdit.SelectedText &= " "
Next k
iTabPositions(iTabCount) = iSelectStart + iIndent
iTabCount += 1
End If
End If
Next i
rtbEdit.SelectionStart = iStart
bDisableSelectionUpdates = False
LockWindowUpdate(IntPtr.Zero)
pbProgress.PerformStep()
Threading.Thread.Sleep(250)
lStatus.Text = "Status: Ready"
pbProgress.Visible = False
pbProgress.Value = 0
Return Nothing
End Function