rubberduck-vba / Rubberduck

Every programmer needs a rubberduck. COM add-in for the VBA & VB6 IDE (VBE).
https://rubberduckvba.com
GNU General Public License v3.0
1.91k stars 300 forks source link

Indenter doesn't produce proper indentation #3943

Closed IvenBach closed 6 years ago

IvenBach commented 6 years ago

While reviewing the tweeted article https://twitter.com/rubberduckvba/status/986058407013646336 I pasted the code in the indenter and didn't get correct indentation.

Pre-indent

Sub findbordr()
Dim r As Long
r = 1

Dim c As Long
c = 1
Do Until r > 46
If Cells(r, c).Interior.ColorIndex <> xlNone Then

    If Cells(r, c).Offset(0, 1).Interior.ColorIndex = xlNone Then
    Cells(r, c).Borders(xlEdgeRight).LineStyle = xlContinuous
    End If

    If c = 1 Then
    Cells(r, c).Borders(xlEdgeLeft).LineStyle = xlContinuous
    c = c + 1
    End If

    If Cells(r, c).Offset(0, -1).Interior.ColorIndex = xlNone Then
        Cells(r, c).Borders(xlEdgeLeft).LineStyle = xlContinuous

        c = c + 1
    Else:
        c = c + 1
    If c > 52 Then
        c = 1
        r = r + 1
    End If
    End If
        Else:
        c = c + 1
        If c > 52 Then
            r = r + 1
            c = 1
        End If
    End If
    Loop
Call findbordt
End Sub

Sub findbordt()
Dim r As Long
r = 2

Dim c As Long
c = 2
Do Until r > 46
If Cells(r, c).Interior.ColorIndex <> xlNone Then
    If Cells(r, c).Offset(1, 0).Interior.ColorIndex = xlNone Then
    Cells(r, c).Borders(xlEdgeBottom).LineStyle = xlContinuous
    End If

    If Cells(r, c).Offset(-1, 0).Interior.ColorIndex = xlNone Then
    Cells(r, c).Borders(xlEdgeTop).LineStyle = xlContinuous
    c = c + 1
    Else:
    c = c + 1
    If c > 52 Then
        c = 1
        r = r + 1
        End If
        End If
        Else:
        c = c + 1
        If c > 52 Then
        r = r + 1
        c = 1
        End If
        End If
    Loop
End Sub

Post-Indent

Sub findbordr()
    Dim r As Long
    r = 1

    Dim c As Long
    c = 1
    Do Until r > 46
        If Cells(r, c).Interior.ColorIndex <> xlNone Then

            If Cells(r, c).Offset(0, 1).Interior.ColorIndex = xlNone Then
                Cells(r, c).Borders(xlEdgeRight).LineStyle = xlContinuous
            End If

            If c = 1 Then
                Cells(r, c).Borders(xlEdgeLeft).LineStyle = xlContinuous
                c = c + 1
            End If

            If Cells(r, c).Offset(0, -1).Interior.ColorIndex = xlNone Then
                Cells(r, c).Borders(xlEdgeLeft).LineStyle = xlContinuous

                c = c + 1
            Else:
            c = c + 1
            If c > 52 Then
                c = 1
                r = r + 1
            End If
        End If
    Else:
    c = c + 1
    If c > 52 Then
        r = r + 1
        c = 1
    End If
End If
Loop
Call findbordt
End Sub

Sub findbordt()
    Dim r As Long
    r = 2

    Dim c As Long
    c = 2
    Do Until r > 46
        If Cells(r, c).Interior.ColorIndex <> xlNone Then
            If Cells(r, c).Offset(1, 0).Interior.ColorIndex = xlNone Then
                Cells(r, c).Borders(xlEdgeBottom).LineStyle = xlContinuous
            End If

            If Cells(r, c).Offset(-1, 0).Interior.ColorIndex = xlNone Then
                Cells(r, c).Borders(xlEdgeTop).LineStyle = xlContinuous
                c = c + 1
            Else:
            c = c + 1
            If c > 52 Then
                c = 1
                r = r + 1
            End If
        End If
    Else:
    c = c + 1
    If c > 52 Then
        r = r + 1
        c = 1
    End If
End If
Loop
End Sub

It took me a moment to catch the issue lies with Else:. I think the : causes it to be perceived as a line label but it compiles and does run. Removing : produces correct indentation.

comintern commented 6 years ago

This should be a fairly easy fix. Do we know of any other keywords that are valid with a colon after them?