Skip to content

Commit e09a69f

Browse files
committed
overloads AddComponent method
Allows adding a module from module text.
1 parent c6a33ba commit e09a69f

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

Rubberduck.Core/UI/CodeExplorer/Commands/AddComponentCommand.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System.Runtime.InteropServices;
1+
using System.IO;
2+
using System.Runtime.InteropServices;
23
using Rubberduck.Navigation.CodeExplorer;
34
using Rubberduck.Parsing.Symbols;
5+
using Rubberduck.Resources;
46
using Rubberduck.VBEditor.SafeComWrappers;
57
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
68

@@ -51,30 +53,51 @@ public void AddComponent(CodeExplorerItemViewModel node, ComponentType type)
5153
}
5254
}
5355

54-
public void AddComponent(CodeExplorerItemViewModel node, string fileName)
56+
public void AddComponent(CodeExplorerItemViewModel node, string moduleText)
5557
{
5658
var nodeProject = GetDeclaration(node)?.Project;
5759
if (node != null && nodeProject == null)
5860
{
5961
return; //The project is not available.
6062
}
6163

64+
string optionCompare = string.Empty;
65+
using (IHostApplication hostApp = _vbe.HostApplication())
66+
{
67+
optionCompare = hostApp.ApplicationName == "Microsoft Access" ? "Option Compare Database" :
68+
string.Empty;
69+
}
70+
6271
using (var components = node != null
6372
? nodeProject.VBComponents
6473
: ComponentsCollectionFromActiveProject())
6574
{
6675
var folderAnnotation = $"'@Folder(\"{GetFolder(node)}\")";
76+
string fileName = createTempTextFile(moduleText);
6777

6878
using (var newComponent = components.Import(fileName))
6979
{
7080
using (var codeModule = newComponent.CodeModule)
7181
{
72-
codeModule.InsertLines(1, folderAnnotation);
82+
var delarationLines = string.Concat(folderAnnotation, optionCompare);
83+
codeModule.InsertLines(1, delarationLines);
7384
}
7485
}
7586
}
7687
}
7788

89+
private string createTempTextFile(string moduleText)
90+
{
91+
string tempFolder = ApplicationConstants.RUBBERDUCK_TEMP_PATH;
92+
if (!Directory.Exists(tempFolder))
93+
{
94+
Directory.CreateDirectory(tempFolder);
95+
}
96+
string filePath = Path.Combine(tempFolder, Path.GetRandomFileName());
97+
File.WriteAllText(filePath, moduleText);
98+
return filePath;
99+
}
100+
78101
private IVBComponents ComponentsCollectionFromActiveProject()
79102
{
80103
using (var activeProject = _vbe.ActiveVBProject)

0 commit comments

Comments
 (0)