Rubberduck icon indicating copy to clipboard operation
Rubberduck copied to clipboard

How does Rubberduck imports?

Open Alfonsomazzarella opened this issue 2 years ago • 2 comments

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 :)

Alfonsomazzarella avatar Jun 01 '22 14:06 Alfonsomazzarella

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.

Vogel612 avatar Jun 02 '22 08:06 Vogel612

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

Greedquest avatar Jun 03 '22 23:06 Greedquest