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 299 forks source link

Encapsulate Field refactoring does not always insert procedures in correct places #5420

Open bclothier opened 4 years ago

bclothier commented 4 years ago

Rubberduck version information Version 2.5.0.3853 OS: Microsoft Windows NT 10.0.17763.0, x64 Host Product: Microsoft Office x64 Host Version: 16.0.12430.20288 Host Executable: MSACCESS.EXE

Description It seems that Declare statements are not handled correctly and get pushed down into middle of module when adding new property statements as a result of the refactoring.

To Reproduce Start with this code in a class module:

Private foo As String
Private strBar As String
Private bizz As String

Private Declare PtrSafe Function GetFocus Lib "user32" () As LongPtr

Public Property Get Bar() As String
    Bar = strBar
End Property

Public Sub doit()
    Debug.Print "do it"
End Sub

Refactor as shown: image

We end up with this uncompilable code:

Private Type TClass1
    Foo As String
    Bar_1 As String
    Bizz As String
End Type

Private this As TClass1

Public Property Get Foo() As String
    Foo = this.Foo
End Property

Public Property Let Foo(ByVal value As String)
    this.Foo = value
End Property

Public Property Get Bar_1() As String
    Bar_1 = this.Bar_1
End Property

Public Property Let Bar_1(ByVal value As String)
    this.Bar_1 = value
End Property

Public Property Get Bizz() As String
    Bizz = this.Bizz
End Property

Public Property Let Bizz(ByVal value As String)
    this.Bizz = value
End Property

Private Declare PtrSafe Function GetFocus Lib "user32" () As LongPtr

Public Property Get Bar() As String
    Bar = Bar_1
End Property

Public Sub doit()
    Debug.Print "do it"
End Sub

Expected behavior The refactoring should ensure that it actually insert at the end of the code declaration section and that should factor in Declare statements which are technically a part of the declaration section.

retailcoder commented 4 years ago

Note, this refactoring has a tech debt refactor pending - I blocked it in Max' PR because I have a branch with UI & other tweaks to this feature... but I need to get back to it.

I'll wrap up these overdue changes and then this bug can be addressed.