Skip to content

Commit 5d6357c

Browse files
authored
Merge pull request #4263 from MDoerner/ParserFileHandlingFix
More safety around handling the temp files in the parser
2 parents e2c1d6c + c339d4a commit 5d6357c

File tree

19 files changed

+72
-54
lines changed

19 files changed

+72
-54
lines changed

Rubberduck.API/VBA/Parser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ internal Parser(object vbe) : this()
9797
_state = new RubberduckParserState(_vbe, projectRepository, declarationFinderFactory, _vbeEvents);
9898
_state.StateChanged += _state_StateChanged;
9999

100-
var sourceFileHandler = _vbe.SourceFileHandler;
100+
var sourceFileHandler = _vbe.TempSourceFileHandler;
101101
var vbeVersion = double.Parse(_vbe.Version, CultureInfo.InvariantCulture);
102102
var predefinedCompilationConstants = new VBAPredefinedCompilationConstants(vbeVersion);
103103
var typeLibProvider = new TypeLibWrapperProvider(projectRepository);

Rubberduck.Main/Root/RubberduckIoCInstaller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ private void RegisterConstantVbeAndAddIn(IWindsorContainer container)
886886
container.Register(Component.For<ICommandBars>().Instance(_vbe.CommandBars));
887887
container.Register(Component.For<IUiContextProvider>().Instance(UiContextProvider.Instance()).LifestyleSingleton());
888888
container.Register(Component.For<IVBEEvents>().Instance(VBEEvents.Initialize(_vbe)).LifestyleSingleton());
889-
container.Register(Component.For<ISourceFileHandler>().Instance(_vbe.SourceFileHandler));
889+
container.Register(Component.For<ITempSourceFileHandler>().Instance(_vbe.TempSourceFileHandler));
890890
}
891891

892892
private void RegisterHotkeyFactory(IWindsorContainer container)

Rubberduck.Parsing/VBA/Parsing/ModuleParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private ModuleParseResults ParseInternal(QualifiedModuleName module, Cancellatio
9999

100100
Logger.Trace($"ParseTaskID {taskId} begins attributes pass.");
101101
var (attributesParseTree, attributesTokenStream) = AttributesPassResults(module, cancellationToken);
102-
var attributesRewriter = _moduleRewriterFactory.AttributesRewriter(module, attributesTokenStream ?? codePaneTokenStream);
102+
var attributesRewriter = _moduleRewriterFactory.AttributesRewriter(module, attributesTokenStream);
103103
Logger.Trace($"ParseTaskID {taskId} finished attributes pass.");
104104
cancellationToken.ThrowIfCancellationRequested();
105105

@@ -153,7 +153,7 @@ private IEnumerable<CommentNode> QualifyAndUnionComments(QualifiedModuleName qua
153153
private (IParseTree tree, ITokenStream tokenStream) AttributesPassResults(QualifiedModuleName module, CancellationToken token)
154154
{
155155
token.ThrowIfCancellationRequested();
156-
var code = _attributesSourceCodeProvider.SourceCode(module) ?? string.Empty;
156+
var code = _attributesSourceCodeProvider.SourceCode(module);
157157
token.ThrowIfCancellationRequested();
158158
var attributesParseResults = _parser.Parse(module.ComponentName, module.ProjectId, code, token, CodeKind.AttributesCode);
159159
return attributesParseResults;

Rubberduck.VBEEditor/Rubberduck.VBEditor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
<Compile Include="SourceCodeHandling\CodePaneSourceCodeHandler.cs" />
8383
<Compile Include="SourceCodeHandling\ISourceCodeHandler.cs" />
8484
<Compile Include="SourceCodeHandling\ISourceCodeProvider.cs" />
85-
<Compile Include="SourceCodeHandling\ISourceFileHandler.cs" />
85+
<Compile Include="SourceCodeHandling\ITempSourceFileHandler.cs" />
8686
<Compile Include="CommandBarLocation.cs" />
8787
<Compile Include="SafeComWrappers\Abstract\HostApplicationBase.cs" />
8888
<Compile Include="SafeComWrappers\Abstract\ISafeComWrapper.cs" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public interface IVBComponent : ISafeComWrapper, IEquatable<IVBComponent>
2020
IWindow DesignerWindow();
2121
void Activate();
2222
void Export(string path);
23-
string ExportAsSourceFile(string folder, bool tempFile = false);
23+
string ExportAsSourceFile(string folder, bool tempFile = false, bool specialCaseDocumentModules = true);
2424
int FileCount { get; }
2525
string GetFileName(short index);
2626
IVBProject ParentProject { get; }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ public interface IVBE : ISafeComWrapper, IEquatable<IVBE>
2525

2626
bool IsInDesignMode { get; }
2727
int ProjectsCount { get; }
28-
ISourceFileHandler SourceFileHandler { get; }
28+
ITempSourceFileHandler TempSourceFileHandler { get; }
2929
}
3030
}

Rubberduck.VBEEditor/SourceCodeHandling/CodePaneSourceCodeHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public string SourceCode(QualifiedModuleName module)
2222

2323
using (var codeModule = component.CodeModule)
2424
{
25-
return codeModule.Content();
25+
return codeModule.Content() ?? string.Empty;
2626
}
2727
}
2828

Rubberduck.VBEEditor/SourceCodeHandling/ISourceFileHandler.cs renamed to Rubberduck.VBEEditor/SourceCodeHandling/ITempSourceFileHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Rubberduck.VBEditor.SourceCodeHandling
44
{
5-
public interface ISourceFileHandler
5+
public interface ITempSourceFileHandler
66
{
77
string Export(IVBComponent component);
8-
void Import(IVBComponent component, string fileName);
8+
void ImportAndCleanUp(IVBComponent component, string fileName);
99

1010
string Read(IVBComponent component);
1111
}

Rubberduck.VBEEditor/SourceCodeHandling/SourceFileHandlerSourceCodeHandlerAdapter.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ namespace Rubberduck.VBEditor.SourceCodeHandling
77
public class SourceFileHandlerSourceCodeHandlerAdapter : ISourceCodeHandler
88
{
99
private readonly IProjectsProvider _projectsProvider;
10-
private readonly ISourceFileHandler _sourceFileHandler;
10+
private readonly ITempSourceFileHandler _tempSourceFileHandler;
1111

12-
public SourceFileHandlerSourceCodeHandlerAdapter(ISourceFileHandler sourceFileHandler, IProjectsProvider projectsProvider)
12+
public SourceFileHandlerSourceCodeHandlerAdapter(ITempSourceFileHandler tempSourceFileHandler, IProjectsProvider projectsProvider)
1313
{
1414
_projectsProvider = projectsProvider;
15-
_sourceFileHandler = sourceFileHandler;
15+
_tempSourceFileHandler = tempSourceFileHandler;
1616
}
1717

1818
public string SourceCode(QualifiedModuleName module)
@@ -23,7 +23,7 @@ public string SourceCode(QualifiedModuleName module)
2323
return string.Empty;
2424
}
2525

26-
return _sourceFileHandler.Read(component);
26+
return _tempSourceFileHandler.Read(component) ?? string.Empty;
2727
}
2828

2929
public void SubstituteCode(QualifiedModuleName module, string newCode)
@@ -40,9 +40,14 @@ public void SubstituteCode(QualifiedModuleName module, string newCode)
4040
return;
4141
}
4242

43-
var file = _sourceFileHandler.Export(component);
44-
File.WriteAllText(file, newCode);
45-
_sourceFileHandler.Import(component, file);
43+
var fileName = _tempSourceFileHandler.Export(component);
44+
if (fileName == null || !File.Exists(fileName))
45+
{
46+
return;
47+
}
48+
File.WriteAllText(fileName, newCode);
49+
_tempSourceFileHandler.ImportAndCleanUp(component, fileName);
50+
4651
}
4752
}
4853
}
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
11
using System.IO;
22
using System.Text;
3-
using Rubberduck.VBEditor.SafeComWrappers;
43
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
54
using Rubberduck.VBEditor.SourceCodeHandling;
65

76
namespace Rubberduck.VBEditor.VB6
87
{
9-
public class SourceFileHandler : ISourceFileHandler
8+
public class ExternalFileTempSourceFileHandlerEmulator : ITempSourceFileHandler
109
{
1110
public string Export(IVBComponent component)
1211
{
1312
// VB6 source code is already external, and should be in the first associated file.
1413
return component.GetFileName(1);
1514
}
1615

17-
public void Import(IVBComponent component, string fileName)
16+
public void ImportAndCleanUp(IVBComponent component, string fileName)
1817
{
1918
// VB6 source code can be written directly in-place, without needing to import it, hence no-op.
2019
}
2120

2221
public string Read(IVBComponent component)
2322
{
2423
var fileName = Export(component);
25-
if (fileName == null)
24+
if (fileName == null || !File.Exists(fileName))
2625
{
2726
return null;
2827
}
2928

30-
var encoding = component.QualifiedModuleName.ComponentType == ComponentType.Document
31-
? Encoding.UTF8
32-
: Encoding.Default;
33-
34-
return File.ReadAllText(fileName, encoding);
29+
return File.ReadAllText(fileName, Encoding.Default);
3530
}
3631
}
3732
}

0 commit comments

Comments
 (0)