Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleBreak code lines at a specified width
Keywordsstrings, code, break code
CategoriesStrings
 
Subroutine BreakCodeLine breaks a line of code into strings no longer than a specified length.

The routine starts by determining how much the line is indented. If the line is a comment, subsequent lines get the same indentation. Otherwise subsequent lines are indented four additional spaces.

While the remaining code is too long, the routine searches for the space character nearest to the line length limit. It will brak the line at that space. If it cannot find a good space to use, the routine outputs the rest of the code at once.

The subroutine then examines the code to see if a comment or quoted string is open at the selected break point. If the break is inside a comment, the routine adds the code up to that point to the result and adds a ' to the remaining code so the subsequent line is in a comment.

If the break is not in a comment but is in a quoted string, the routine adds the code up to that point followed by a close " and a line continuation character to the result. It adds a " to the remaining code so the subsequent line is in a quoted string.

If the break is not in a comment or a quoted string, the routine adds the code up to that point followed by a line continuation character to the result. It loops to display the rest of the line normally.

Once the remaining code is short enough, the subroutine adds the remainder to the output.

 
' Break the code line so it isn't too long.
Private Function BreakCodeLine(ByVal code_line As String, _
    ByVal max_len As Integer) As String
Dim indent As String
Dim good_space As Long
Dim result As String
Dim quote_open As Boolean
Dim comment_open As Boolean
Dim j As Long
Dim ch As String
Dim min_length As Integer

    ' See how much the line is indented.
    indent = Space$(Len(code_line) - Len(Trim$(code_line)))

    ' If this line is not a comment, indent subsequent
    ' lines.
    If Left$(Trim$(code_line), 1) <> "'" Then
        indent = indent + Space$(4)
    End If

    ' Don't break within this many characters of
    ' the start of the string.
    min_length = Len(indent) + 4

    ' Trim the line until it fits.
    Do While Len(code_line) > max_len
        ' Find the space closest to the maximum character.
        good_space = InStrRev(code_line, " ", max_len)

        ' Do not break too soon.
        If good_space <= min_length Then good_space = 0

        ' If there are no spaces before MAX_LEN, look after.
        If good_space = 0 Then
            good_space = InStr(max_len, code_line, " ")
        End If

        If good_space = 0 Then
            ' No good spaces. Display the rest at once.
            Exit Do
        Else
            ' Break at this space.
            ' See if the space is in a quoted string or
            ' comment.
            comment_open = False
            quote_open = False
            For j = 1 To good_space
                ch = Mid$(code_line, j, 1)
                If ch = "'" Then
                    If Not quote_open Then comment_open = _
                        True
                ElseIf ch = """" Then
                    quote_open = Not quote_open
                End If
            Next j

            If comment_open Then
                ' The space is in a comment.
                ' Start the new line with a comment.
                result = result & vbCrLf & _
                    Left$(code_line, good_space - 1)
                code_line = indent & "' " & _
                    Trim$(Mid$(code_line, good_space))
            ElseIf quote_open Then
                ' The space is in a quoted string.
                ' Break the string.
                result = result & vbCrLf & _
                    Left$(code_line, good_space) & """ & _"
                code_line = indent & """" & _
                    Mid$(code_line, good_space + 1)
            Else
                ' The space is within code.
                ' Break the code.
                result = result & vbCrLf & _
                    Left$(code_line, good_space - 1) & " _"
                code_line = indent & _
                    Trim$(Mid$(code_line, good_space))
            End If
        End If
    Loop

    ' Add the last piece of code.
    result = result & vbCrLf & code_line

    If Len(result) > 0 Then
        BreakCodeLine = Mid$(result, Len(vbCrLf) + 1)
    Else
        BreakCodeLine = result
    End If
End Function
 
The BreakCodeLines subroutine breaks the code into lines and uses subroutine BreakCodeLine to format them.
 
' Break all of the code's lines.
Public Function BreakCodeLines(ByVal code As String, ByVal _
    max_len As Integer) As String
Dim lines As Variant
Dim i As Integer
Dim result As String

    ' Break the code into lines.
    lines = Split(code, vbCrLf)

    ' Break the lines.
    For i = LBound(lines) To UBound(lines)
        result = result & vbCrLf & BreakCodeLine(lines(i), _
            max_len)
    Next i

    ' Remove the leading vbCrLf.
    If Len(result) > 0 Then result = Mid$(result, _
        Len(vbCrLf) + 1)

    ' Return the result.
    BreakCodeLines = result
End Function
 
Note that the program that generated this Web page used these routines to format itself.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated