1
- using Rubberduck . VBEditor . ComManagement ;
1
+ using System . Runtime . InteropServices ;
2
+ using NLog ;
3
+ using Rubberduck . VBEditor . ComManagement ;
2
4
using Rubberduck . VBEditor . SafeComWrappers ;
3
5
using Rubberduck . VBEditor . SafeComWrappers . Abstract ;
4
6
using Rubberduck . VBEditor . SourceCodeHandling ;
@@ -11,6 +13,8 @@ public class AddComponentService : IAddComponentService
11
13
private readonly IComponentSourceCodeHandler _codePaneSourceCodeHandler ;
12
14
private readonly IComponentSourceCodeHandler _attributeSourceCodeHandler ;
13
15
16
+ private static ILogger _logger = LogManager . GetCurrentClassLogger ( ) ;
17
+
14
18
public AddComponentService (
15
19
IProjectsProvider projectsProvider ,
16
20
IComponentSourceCodeHandler codePaneComponentSourceCodeProvider ,
@@ -21,17 +25,17 @@ public AddComponentService(
21
25
_attributeSourceCodeHandler = attributesComponentSourceCodeProvider ;
22
26
}
23
27
24
- public void AddComponent ( string projectId , ComponentType componentType , string code = null , string additionalPrefixInModule = null )
28
+ public void AddComponent ( string projectId , ComponentType componentType , string code = null , string additionalPrefixInModule = null , string componentName = null )
25
29
{
26
- AddComponent ( _codePaneSourceCodeHandler , projectId , componentType , code , additionalPrefixInModule ) ;
30
+ AddComponent ( _codePaneSourceCodeHandler , projectId , componentType , code , additionalPrefixInModule , componentName ) ;
27
31
}
28
32
29
- public void AddComponentWithAttributes ( string projectId , ComponentType componentType , string code , string prefixInModule = null )
33
+ public void AddComponentWithAttributes ( string projectId , ComponentType componentType , string code , string prefixInModule = null , string componentName = null )
30
34
{
31
- AddComponent ( _attributeSourceCodeHandler , projectId , componentType , code , prefixInModule ) ;
35
+ AddComponent ( _attributeSourceCodeHandler , projectId , componentType , code , prefixInModule , componentName ) ;
32
36
}
33
37
34
- public void AddComponent ( IComponentSourceCodeHandler sourceCodeHandler , string projectId , ComponentType componentType , string code = null , string prefixInModule = null )
38
+ public void AddComponent ( IComponentSourceCodeHandler sourceCodeHandler , string projectId , ComponentType componentType , string code = null , string prefixInModule = null , string componentName = null )
35
39
{
36
40
using ( var newComponent = CreateComponent ( projectId , componentType ) )
37
41
{
@@ -45,17 +49,36 @@ public void AddComponent(IComponentSourceCodeHandler sourceCodeHandler, string p
45
49
using ( var loadedComponent = sourceCodeHandler . SubstituteCode ( newComponent , code ) )
46
50
{
47
51
AddPrefix ( loadedComponent , prefixInModule ) ;
52
+ RenameComponent ( loadedComponent , componentName ) ;
48
53
ShowComponent ( loadedComponent ) ;
49
54
}
50
55
}
51
56
else
52
57
{
53
58
AddPrefix ( newComponent , prefixInModule ) ;
59
+ RenameComponent ( newComponent , componentName ) ;
54
60
ShowComponent ( newComponent ) ;
55
61
}
56
62
}
57
63
}
58
64
65
+ private static void RenameComponent ( IVBComponent newComponent , string componentName )
66
+ {
67
+ if ( componentName == null )
68
+ {
69
+ return ;
70
+ }
71
+
72
+ try
73
+ {
74
+ newComponent . Name = componentName ;
75
+ }
76
+ catch ( COMException ex )
77
+ {
78
+ _logger . Debug ( ex , $ "Unable to rename component to { componentName } .") ;
79
+ }
80
+ }
81
+
59
82
private static void ShowComponent ( IVBComponent component )
60
83
{
61
84
if ( component == null )
0 commit comments