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 properly handle line-continuation in If statement #4342

Open daFreeMan opened 6 years ago

daFreeMan commented 6 years ago

Win10, Excel2016(desktop), RD .3739

Initial code:

Public Function TotalVeryDissatisfied(ByRef year As YearFlag, ByRef printStateFlag As PrintState) As Long

  Dim myClinicItem As ClinicItem
  Dim Sum As Long

  Sum = 0
  On Error GoTo ErrHandle
  For Each myClinicItem In this.ClinicItems
    If myClinicItem.GenerateSatSurvey Then
      If (printStateFlag = psPrintState) _
    Or (printStateFlag = psDoNotPrintState And myClinicItem.Name <> STATE_EXCLUSION) Then
        If year = yfLastYear Then
          Sum = Sum + myClinicItem.LastYearVD
        Else
          Sum = Sum + myClinicItem.YtdVD
        End If
      End If
    End If
  Next

ExitHere:
  TotalVeryDissatisfied = Sum
  Exit Function

ErrHandle:
  Sum = -1
  Resume ExitHere

End Function

Note the position of the line beginning with Or. After indenting the procedure, I get:

Public Function TotalVeryDissatisfied(ByRef year As YearFlag, ByRef printStateFlag As PrintState) As Long

  Dim myClinicItem As ClinicItem
  Dim Sum As Long

  Sum = 0
  On Error GoTo ErrHandle
  For Each myClinicItem In this.ClinicItems
    If myClinicItem.GenerateSatSurvey Then
      If (printStateFlag = psPrintState) _
    Or (printStateFlag = psDoNotPrintState And myClinicItem.Name <> STATE_EXCLUSION) Then
        If year = yfLastYear Then
          Sum = Sum + myClinicItem.LastYearVD
        Else
          Sum = Sum + myClinicItem.YtdVD
        End If
      End If
    End If
  Next

ExitHere:
  TotalVeryDissatisfied = Sum
  Exit Function

ErrHandle:
  Sum = -1
  Resume ExitHere

End Function

Again, note the position of the line starting with Or... - it's in the exact same column. I do know that the indenter ran because it also gobbled up my leading/trailing blank line that is supposed to leave.

However, 2 MCVEs of:

Private Sub glarp()

  Dim foo As Boolean
  Dim bar As Boolean
  Dim baz As Boolean

  If foo Then
    If bar _
       Or baz Then
      Debug.Print "glarp"
    End If
  End If

End Sub
Private Sub glarp()

  Dim foo As Boolean
  Dim bar As Long
  Dim baz As Long
  Dim blam As Long

  If foo Then
    If bar = 1 _
       Or (baz = 2 And blam = 3) Then
      Debug.Print "glarp"
    End If
  End If

End Sub

both indent properly.

daFreeMan commented 6 years ago

Screenshots of applicable intenter settings:

2018-08-31 12_08_54-settings 2018-08-31 12_08_32-settings

After thinking about it, I decided that the Ignore Operators setting was the issue, so I cleared it. 2018-08-31 12_12_42-settings That did not resolve the issue.

comintern commented 6 years ago

Can you explain the comment "because it also gobbled up my leading/trailing blank line that is supposed to leave"? I can't see that in the samples that were provided above.

comintern commented 6 years ago

BTW, it's the parentheses around (printStateFlag = psPrintState) that is causing the issue. Verified with:

Private Sub glarp()

    Dim foo As Boolean
    Dim bar As Boolean
    Dim baz As Boolean

    If foo Then
        If (bar) _
        Or baz Then
            Debug.Print "glarp"
        End If
    End If

End Sub
daFreeMan commented 6 years ago

Sorry, @comintern, I failed to provide an example of that.

Private Sub Foo
  'do stuff
End Sub

Private Sub Bar
  'do other stuff
End Sub

After indentation it became:

Private Sub Foo
  'do stuff
End Sub
Private Sub Bar
  'do other stuff
End Sub

Note there's no blank line between the End Sub and the Private Sub Bar even though Maintain vertical spacing is set to 1. I thought this might need to be a separate issue, but it's inconsistent and I've only noticed it recently in combination with the line continuation issue noted here.