Skip to content

Commit 9811522

Browse files
authored
Merge pull request #5360 from MDoerner/EnhanceAddComponentToProvideTheName
Let AddComponentService optionally take a component name
2 parents 9536b3c + caa1666 commit 9811522

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

Rubberduck.VBEEditor/Utility/AddComponentService.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Rubberduck.VBEditor.ComManagement;
1+
using System.Runtime.InteropServices;
2+
using NLog;
3+
using Rubberduck.VBEditor.ComManagement;
24
using Rubberduck.VBEditor.SafeComWrappers;
35
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
46
using Rubberduck.VBEditor.SourceCodeHandling;
@@ -11,6 +13,8 @@ public class AddComponentService : IAddComponentService
1113
private readonly IComponentSourceCodeHandler _codePaneSourceCodeHandler;
1214
private readonly IComponentSourceCodeHandler _attributeSourceCodeHandler;
1315

16+
private static ILogger _logger = LogManager.GetCurrentClassLogger();
17+
1418
public AddComponentService(
1519
IProjectsProvider projectsProvider,
1620
IComponentSourceCodeHandler codePaneComponentSourceCodeProvider,
@@ -21,17 +25,17 @@ public AddComponentService(
2125
_attributeSourceCodeHandler = attributesComponentSourceCodeProvider;
2226
}
2327

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)
2529
{
26-
AddComponent(_codePaneSourceCodeHandler, projectId, componentType, code, additionalPrefixInModule);
30+
AddComponent(_codePaneSourceCodeHandler, projectId, componentType, code, additionalPrefixInModule, componentName);
2731
}
2832

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)
3034
{
31-
AddComponent(_attributeSourceCodeHandler, projectId, componentType, code, prefixInModule);
35+
AddComponent(_attributeSourceCodeHandler, projectId, componentType, code, prefixInModule, componentName);
3236
}
3337

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)
3539
{
3640
using (var newComponent = CreateComponent(projectId, componentType))
3741
{
@@ -45,17 +49,36 @@ public void AddComponent(IComponentSourceCodeHandler sourceCodeHandler, string p
4549
using (var loadedComponent = sourceCodeHandler.SubstituteCode(newComponent, code))
4650
{
4751
AddPrefix(loadedComponent, prefixInModule);
52+
RenameComponent(loadedComponent, componentName);
4853
ShowComponent(loadedComponent);
4954
}
5055
}
5156
else
5257
{
5358
AddPrefix(newComponent, prefixInModule);
59+
RenameComponent(newComponent, componentName);
5460
ShowComponent(newComponent);
5561
}
5662
}
5763
}
5864

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+
5982
private static void ShowComponent(IVBComponent component)
6083
{
6184
if (component == null)

Rubberduck.VBEEditor/Utility/IAddComponentService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Rubberduck.VBEditor.Utility
44
{
55
public interface IAddComponentService
66
{
7-
void AddComponent(string projectId, ComponentType componentType, string code = null, string additionalPrefixInModule = null);
8-
void AddComponentWithAttributes(string projectId, ComponentType componentType, string code, string prefixInModule = null);
7+
void AddComponent(string projectId, ComponentType componentType, string code = null, string additionalPrefixInModule = null, string componentName = null);
8+
void AddComponentWithAttributes(string projectId, ComponentType componentType, string code, string prefixInModule = null, string componentName = null);
99
}
1010
}

0 commit comments

Comments
 (0)