Skip to content

Commit 2ff180d

Browse files
committed
Return IVBComponents in various Import methods
This allows the calling method to continue work with the imported component. However, it also shifts the responsibility for disposal to the the caller.
1 parent dbeab72 commit 2ff180d

File tree

12 files changed

+67
-51
lines changed

12 files changed

+67
-51
lines changed

Rubberduck.Main/Root/RubberduckIoCInstaller.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ public void Install(IWindsorContainer container, IConfigurationStore store)
130130
.LifestyleSingleton());
131131
container.Register(Component.For<IAddComponentService>()
132132
.ImplementedBy<AddComponentService>()
133-
.DependsOn(Dependency.OnComponent("codePaneSourceCodeProvider", typeof(CodeModuleComponentSourceCodeHandler)),
134-
Dependency.OnComponent("attributesSourceCodeProvider", typeof(SourceFileHandlerComponentSourceCodeHandlerAdapter)))
133+
.DependsOn(Dependency.OnComponent("codePaneComponentSourceCodeProvider", typeof(CodeModuleComponentSourceCodeHandler)),
134+
Dependency.OnComponent("attributesComponentSourceCodeProvider", typeof(SourceFileHandlerComponentSourceCodeHandlerAdapter)))
135135
.LifestyleSingleton());
136136

137137
container.Register(Component.For<TestExplorerModel>()

Rubberduck.VBEEditor/SafeComWrappers/VB/Abstract/IVBComponents.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public interface IVBComponents : ISafeEventedComWrapper, IComCollection<IVBCompo
2020
IVBComponent Add(ComponentType type);
2121
IVBComponent Import(string path);
2222
IVBComponent AddCustom(string progId);
23-
void ImportSourceFile(string path);
23+
IVBComponent ImportSourceFile(string path);
2424

2525
/// <summary>
2626
/// Safely removes the specified VbComponent from the collection.

Rubberduck.VBEEditor/SourceCodeHandling/CodeModuleComponentSourceCodeHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ public string SourceCode(IVBComponent module)
1212
}
1313
}
1414

15-
public void SubstituteCode(IVBComponent module, string newCode)
15+
public IVBComponent SubstituteCode(IVBComponent module, string newCode)
1616
{
1717
using (var codeModule = module.CodeModule)
1818
{
1919
codeModule.Clear();
2020
codeModule.InsertLines(1, newCode);
2121
}
22+
23+
return module;
2224
}
2325
}
2426
}

Rubberduck.VBEEditor/SourceCodeHandling/ComponentSourceCodeHandlerSourceCodeHandlerAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void SubstituteCode(QualifiedModuleName module, string newCode)
3232
return;
3333
}
3434

35-
_componentSourceCodeHandler.SubstituteCode(component, newCode);
35+
using (_componentSourceCodeHandler.SubstituteCode(component, newCode)){} //We do nothing; we just need to guarantee that the returned SCW gets disposed.
3636
}
3737
}
3838
}

Rubberduck.VBEEditor/SourceCodeHandling/IComponentSourceCodeHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace Rubberduck.VBEditor.SourceCodeHandling
55
public interface IComponentSourceCodeHandler
66
{
77
string SourceCode(IVBComponent module);
8-
void SubstituteCode(IVBComponent module, string newCode);
8+
IVBComponent SubstituteCode(IVBComponent module, string newCode);
99
}
1010
}

Rubberduck.VBEEditor/SourceCodeHandling/ITempSourceFileHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Rubberduck.VBEditor.SourceCodeHandling
55
public interface ITempSourceFileHandler
66
{
77
string Export(IVBComponent component);
8-
void ImportAndCleanUp(IVBComponent component, string fileName);
8+
IVBComponent ImportAndCleanUp(IVBComponent component, string fileName);
99

1010
string Read(IVBComponent component);
1111
}

Rubberduck.VBEEditor/SourceCodeHandling/SourceFileHandlerComponentSourceCodeHandlerAdapter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ public string SourceCode(IVBComponent module)
1818
return _tempSourceFileHandler.Read(module) ?? string.Empty;
1919
}
2020

21-
public void SubstituteCode(IVBComponent module, string newCode)
21+
public IVBComponent SubstituteCode(IVBComponent module, string newCode)
2222
{
2323
if (module.Type == ComponentType.Document)
2424
{
2525
//We cannot substitute the code of a document module via the file.
26-
return;
26+
return module;
2727
}
2828

2929
var fileName = _tempSourceFileHandler.Export(module);
3030
if (fileName == null || !File.Exists(fileName))
3131
{
32-
return;
32+
return module;
3333
}
3434
File.WriteAllText(fileName, newCode);
35-
_tempSourceFileHandler.ImportAndCleanUp(module, fileName);
35+
return _tempSourceFileHandler.ImportAndCleanUp(module, fileName);
3636
}
3737
}
3838
}

Rubberduck.VBEEditor/Utility/AddComponentService.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public class AddComponentService : IAddComponentService
1313

1414
public AddComponentService(
1515
IProjectsProvider projectsProvider,
16-
IComponentSourceCodeHandler codePaneSourceCodeHandler,
17-
IComponentSourceCodeHandler attributeSourceCodeHandler)
16+
IComponentSourceCodeHandler codePaneComponentSourceCodeProvider,
17+
IComponentSourceCodeHandler attributesComponentSourceCodeProvider)
1818
{
1919
_projectsProvider = projectsProvider;
20-
_codePaneSourceCodeHandler = codePaneSourceCodeHandler;
21-
_attributeSourceCodeHandler = attributeSourceCodeHandler;
20+
_codePaneSourceCodeHandler = codePaneComponentSourceCodeProvider;
21+
_attributeSourceCodeHandler = attributesComponentSourceCodeProvider;
2222
}
2323

2424
public void AddComponent(string projectId, ComponentType componentType, string code = null, string additionalPrefixInModule = null)
@@ -39,19 +39,31 @@ public void AddComponent(IComponentSourceCodeHandler sourceCodeHandler, string p
3939
{
4040
return;
4141
}
42-
43-
if (code != null)
44-
{
45-
sourceCodeHandler.SubstituteCode(newComponent, code);
46-
}
4742

48-
if (prefixInModule != null)
43+
if (code != null)
4944
{
50-
using (var codeModule = newComponent.CodeModule)
45+
using (var loadedComponent = sourceCodeHandler.SubstituteCode(newComponent, code))
5146
{
52-
codeModule.InsertLines(1, prefixInModule);
47+
AddPrefix(loadedComponent, prefixInModule);
5348
}
5449
}
50+
else
51+
{
52+
AddPrefix(newComponent, prefixInModule);
53+
}
54+
}
55+
}
56+
57+
private static void AddPrefix(IVBComponent module, string prefix)
58+
{
59+
if (prefix == null || module == null)
60+
{
61+
return;
62+
}
63+
64+
using (var codeModule = module.CodeModule)
65+
{
66+
codeModule.InsertLines(1, prefix);
5567
}
5668
}
5769

Rubberduck.VBEditor.VB6/ExternalFileTempSourceFileHandlerEmulator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ public string Export(IVBComponent component)
1313
return component.GetFileName(1);
1414
}
1515

16-
public void ImportAndCleanUp(IVBComponent component, string fileName)
16+
public IVBComponent ImportAndCleanUp(IVBComponent component, string fileName)
1717
{
1818
// VB6 source code can be written directly in-place, without needing to import it, hence no-op.
19+
return component;
1920
}
2021

2122
public string Read(IVBComponent component)

Rubberduck.VBEditor.VB6/SafeComWrappers/VB/VBComponents.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public override int GetHashCode()
7070
return IsWrappingNullReference ? 0 : HashCode.Compute(Target);
7171
}
7272

73-
public void ImportSourceFile(string path)
73+
public IVBComponent ImportSourceFile(string path)
7474
{
7575
throw new NotSupportedException("ImportSourceFile not supported in VB6");
7676
}

0 commit comments

Comments
 (0)