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.92k stars 302 forks source link

How does Rubberduck imports? #5984

Closed Alfonsomazzarella closed 1 year ago

Alfonsomazzarella commented 2 years ago

Hello,

i have a script that export one full project with Applications.saveastext (every type of file .bas, .cls, .form, .qry, ecc) and it does it right. Then i have another script that should import everything back with Application.loadfromtext and it works, but some attributes are deleted and i don't understand why. Not every attribute, just some of them and i'm talking about the attributes description.

'@Description "Converte una data (tipo VBA 'Date') in una stringa in formato standard a seconda del dialetto SQL"
      Public Function DateToString(ByVal DateToFormat As Date, ByVal SQLdialect As SQLdialectEnum) As String
      Attribute DateToString.VB_Description = "Converte una data (tipo VBA 'Date') in una stringa in formato standard a seconda del dialetto SQL"
End Function

So when i use loadfromtext this Attribute is deleted.

BUT

When i use the "Add-> Existing Files" offered by rubberduck ide it does work and it does keep the attribute. That's why i'll be happy to understand how rubberduck can do that.

Thanks in advance and sorry for my english :)

Vogel612 commented 2 years ago

The import logic is in the following file: https://github.com/rubberduck-vba/Rubberduck/blob/next/Rubberduck.Core/UI/CodeExplorer/Commands/ImportCommand.cs

As far as I can tell from my phone (and before drinking my first coffee) we explicitly call Import on the relevant VBComponent obtained from the underlying VBProject obtained from the VBE we're hooked into. Special component files (i.e. documents and some others) are imported using the ImportSourceFile function instead.

Greedquest commented 2 years ago

To expand on what @Vogel612 said, that basically means change:

Application.loadfromtext content

To the richer:

Application.VBE.ActiveVBProject.VBComponents.Import "C:/foo/file.bas"

That works for classes and standard modules.

Documents/forms need some special handling though (as pointed out above, in the ImportSourceFile function:

https://github.com/rubberduck-vba/Rubberduck/blob/25728b3a8757315a0e23939c5f96b6dca03bdc06/Rubberduck.VBEditor.VBA/SafeComWrappers/VB/VBComponents.cs#L82-L159

And ultimately falls back to:

VBComponent.CodeModule.AddFromString content