Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let AddComponentService optionally take a component name #5360

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 29 additions & 6 deletions Rubberduck.VBEEditor/Utility/AddComponentService.cs
@@ -1,4 +1,6 @@
using Rubberduck.VBEditor.ComManagement;
using System.Runtime.InteropServices;
using NLog;
using Rubberduck.VBEditor.ComManagement;
using Rubberduck.VBEditor.SafeComWrappers;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
using Rubberduck.VBEditor.SourceCodeHandling;
Expand All @@ -11,6 +13,8 @@ public class AddComponentService : IAddComponentService
private readonly IComponentSourceCodeHandler _codePaneSourceCodeHandler;
private readonly IComponentSourceCodeHandler _attributeSourceCodeHandler;

private static ILogger _logger = LogManager.GetCurrentClassLogger();

public AddComponentService(
IProjectsProvider projectsProvider,
IComponentSourceCodeHandler codePaneComponentSourceCodeProvider,
Expand All @@ -21,17 +25,17 @@ public class AddComponentService : IAddComponentService
_attributeSourceCodeHandler = attributesComponentSourceCodeProvider;
}

public void AddComponent(string projectId, ComponentType componentType, string code = null, string additionalPrefixInModule = null)
public void AddComponent(string projectId, ComponentType componentType, string code = null, string additionalPrefixInModule = null, string componentName = null)
{
AddComponent(_codePaneSourceCodeHandler, projectId, componentType, code, additionalPrefixInModule);
AddComponent(_codePaneSourceCodeHandler, projectId, componentType, code, additionalPrefixInModule, componentName);
}

public void AddComponentWithAttributes(string projectId, ComponentType componentType, string code, string prefixInModule = null)
public void AddComponentWithAttributes(string projectId, ComponentType componentType, string code, string prefixInModule = null, string componentName = null)
{
AddComponent(_attributeSourceCodeHandler, projectId, componentType, code, prefixInModule);
AddComponent(_attributeSourceCodeHandler, projectId, componentType, code, prefixInModule, componentName);
}

public void AddComponent(IComponentSourceCodeHandler sourceCodeHandler, string projectId, ComponentType componentType, string code = null, string prefixInModule = null)
public void AddComponent(IComponentSourceCodeHandler sourceCodeHandler, string projectId, ComponentType componentType, string code = null, string prefixInModule = null, string componentName = null)
{
using (var newComponent = CreateComponent(projectId, componentType))
{
Expand All @@ -45,17 +49,36 @@ public void AddComponent(IComponentSourceCodeHandler sourceCodeHandler, string p
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)
Expand Down
4 changes: 2 additions & 2 deletions Rubberduck.VBEEditor/Utility/IAddComponentService.cs
Expand Up @@ -4,7 +4,7 @@ namespace Rubberduck.VBEditor.Utility
{
public interface IAddComponentService
{
void AddComponent(string projectId, ComponentType componentType, string code = null, string additionalPrefixInModule = null);
void AddComponentWithAttributes(string projectId, ComponentType componentType, string code, string prefixInModule = null);
void AddComponent(string projectId, ComponentType componentType, string code = null, string additionalPrefixInModule = null, string componentName = null);
void AddComponentWithAttributes(string projectId, ComponentType componentType, string code, string prefixInModule = null, string componentName = null);
}
}