forked from rubberduck-vba/Rubberduck
/
AddComponentService.cs
127 lines (111 loc) · 4.38 KB
/
AddComponentService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System.Runtime.InteropServices;
using NLog;
using Rubberduck.VBEditor.ComManagement;
using Rubberduck.VBEditor.SafeComWrappers;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
using Rubberduck.VBEditor.SourceCodeHandling;
namespace Rubberduck.VBEditor.Utility
{
public class AddComponentService : IAddComponentService
{
private readonly IProjectsProvider _projectsProvider;
private readonly IComponentSourceCodeHandler _codePaneSourceCodeHandler;
private readonly IComponentSourceCodeHandler _attributeSourceCodeHandler;
private static ILogger _logger = LogManager.GetCurrentClassLogger();
public AddComponentService(
IProjectsProvider projectsProvider,
IComponentSourceCodeHandler codePaneComponentSourceCodeProvider,
IComponentSourceCodeHandler attributesComponentSourceCodeProvider)
{
_projectsProvider = projectsProvider;
_codePaneSourceCodeHandler = codePaneComponentSourceCodeProvider;
_attributeSourceCodeHandler = attributesComponentSourceCodeProvider;
}
public void AddComponent(string projectId, ComponentType componentType, string code = null, string additionalPrefixInModule = null, string componentName = null)
{
AddComponent(_codePaneSourceCodeHandler, projectId, componentType, code, additionalPrefixInModule, componentName);
}
public void AddComponentWithAttributes(string projectId, ComponentType componentType, string code, string prefixInModule = null, string componentName = null)
{
AddComponent(_attributeSourceCodeHandler, projectId, componentType, code, prefixInModule, componentName);
}
public void AddComponent(IComponentSourceCodeHandler sourceCodeHandler, string projectId, ComponentType componentType, string code = null, string prefixInModule = null, string componentName = null)
{
using (var newComponent = CreateComponent(projectId, componentType))
{
if (newComponent == null)
{
return;
}
if (code != null)
{
using (var loadedComponent = sourceCodeHandler.SubstituteCode(newComponent, code))
{
AddPrefix(loadedComponent, prefixInModule);
RenameComponent(loadedComponent, componentName);
ShowComponent(loadedComponent);
}
}
else
{
AddPrefix(newComponent, prefixInModule);
RenameComponent(newComponent, componentName);
ShowComponent(newComponent);
}
}
}
private static void RenameComponent(IVBComponent newComponent, string componentName)
{
if (componentName == null)
{
return;
}
try
{
newComponent.Name = componentName;
}
catch (COMException ex)
{
_logger.Debug(ex, $"Unable to rename component to {componentName}.");
}
}
private static void ShowComponent(IVBComponent component)
{
if (component == null)
{
return;
}
using (var codeModule = component.CodeModule)
{
if (codeModule == null)
{
return;
}
using (var codePane = codeModule.CodePane)
{
codePane.Show();
}
}
}
private static void AddPrefix(IVBComponent module, string prefix)
{
if (prefix == null || module == null)
{
return;
}
using (var codeModule = module.CodeModule)
{
codeModule.InsertLines(1, prefix);
}
}
private IVBComponent CreateComponent(string projectId, ComponentType componentType)
{
var componentsCollection = _projectsProvider.ComponentsCollection(projectId);
if (componentsCollection == null)
{
return null;
}
return componentsCollection.Add(componentType);
}
}
}