Skip to content

Commit ea9af0a

Browse files
committed
loading serialized built-in references from file (WIP - not working)
1 parent 39ee905 commit ea9af0a

File tree

5 files changed

+135
-53
lines changed

5 files changed

+135
-53
lines changed

RetailCoder.VBE/UI/Command/MenuItems/CommandBars/SerializeDeclarationsCommandMenuItem.cs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System;
22
using System.IO;
3-
using System.Runtime.Serialization;
4-
using System.Text;
5-
using System.Xml;
63
using NLog;
74
using Rubberduck.Parsing.Symbols;
85
using Rubberduck.Parsing.VBA;
@@ -54,41 +51,4 @@ protected override void ExecuteImpl(object parameter)
5451
}
5552
}
5653
}
57-
58-
public class XmlPersistableDeclarations : IPersistable<SerializableDeclarationTree>
59-
{
60-
public void Persist(string path, SerializableDeclarationTree tree)
61-
{
62-
if (string.IsNullOrEmpty(path)) { throw new InvalidOperationException(); }
63-
64-
var xmlSettings = new XmlWriterSettings
65-
{
66-
NamespaceHandling = NamespaceHandling.OmitDuplicates,
67-
Encoding = Encoding.UTF8,
68-
//Indent = true
69-
};
70-
71-
using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
72-
using (var xmlWriter = XmlWriter.Create(stream, xmlSettings))
73-
using (var writer = XmlDictionaryWriter.CreateDictionaryWriter(xmlWriter))
74-
{
75-
writer.WriteStartDocument();
76-
var settings = new DataContractSerializerSettings {RootNamespace = XmlDictionaryString.Empty};
77-
var serializer = new DataContractSerializer(typeof (SerializableDeclarationTree), settings);
78-
serializer.WriteObject(writer, tree);
79-
}
80-
}
81-
82-
public SerializableDeclarationTree Load(string path)
83-
{
84-
if (string.IsNullOrEmpty(path)) { throw new InvalidOperationException(); }
85-
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
86-
using (var xmlReader = XmlReader.Create(stream))
87-
using (var reader = XmlDictionaryReader.CreateDictionaryReader(xmlReader))
88-
{
89-
var serializer = new DataContractSerializer(typeof(SerializableDeclarationTree));
90-
return (SerializableDeclarationTree)serializer.ReadObject(reader);
91-
}
92-
}
93-
}
9454
}

Rubberduck.Parsing/Rubberduck.Parsing.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<Reference Include="System.Core" />
5555
<Reference Include="Microsoft.CSharp" />
5656
<Reference Include="System.Runtime.Serialization" />
57+
<Reference Include="System.Xml" />
5758
</ItemGroup>
5859
<ItemGroup>
5960
<Compile Include="Annotations\AnnotationBase.cs" />
@@ -269,6 +270,7 @@
269270
<Compile Include="Symbols\IdentifierReference.cs" />
270271
<Compile Include="Symbols\IdentifierReferenceListener.cs" />
271272
<Compile Include="Symbols\ConstantDeclaration.cs" />
273+
<Compile Include="Symbols\XmlPersistableDeclarations.cs" />
272274
<Compile Include="Syntax\SyntaxTree.cs" />
273275
<Compile Include="Syntax\TextSpan.cs" />
274276
<Compile Include="VBA\AttributeParser.cs" />
@@ -309,6 +311,10 @@
309311
<None Include="Preprocessing\VBALike.g4" />
310312
</ItemGroup>
311313
<ItemGroup>
314+
<ProjectReference Include="..\Rubberduck.SettingsProvider\Rubberduck.SettingsProvider.csproj">
315+
<Project>{E85E1253-86D6-45EE-968B-F37348D44132}</Project>
316+
<Name>Rubberduck.SettingsProvider</Name>
317+
</ProjectReference>
312318
<ProjectReference Include="..\Rubberduck.VBEEditor\Rubberduck.VBEditor.csproj">
313319
<Project>{8ce35eb3-8852-4ba1-84dd-df3f5d2967b0}</Project>
314320
<Name>Rubberduck.VBEditor</Name>

Rubberduck.Parsing/Symbols/SerializableDeclaration.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using System.Runtime.Serialization;
4+
using Rubberduck.Parsing.Annotations;
45
using Rubberduck.Parsing.VBA;
56
using Rubberduck.VBEditor;
67

@@ -71,6 +72,14 @@ public SerializableDeclaration(Declaration declaration)
7172
ProjectName = declaration.QualifiedName.QualifiedModuleName.ProjectName;
7273
ProjectPath = declaration.QualifiedName.QualifiedModuleName.ProjectPath;
7374
ComponentName = declaration.QualifiedName.QualifiedModuleName.ComponentName;
75+
76+
var param = declaration as ParameterDeclaration;
77+
if (param != null)
78+
{
79+
IsOptionalParam = param.IsOptional;
80+
IsByRefParam = param.IsByRef;
81+
IsParamArray = param.IsParamArray;
82+
}
7483
}
7584

7685
public List<SerializableMemberAttribute> Attributes { get; set; }
@@ -95,14 +104,43 @@ public SerializableDeclaration(Declaration declaration)
95104
public Accessibility Accessibility { get; set; }
96105
public DeclarationType DeclarationType { get; set; }
97106

107+
public bool IsOptionalParam { get; set; }
108+
public bool IsByRefParam { get; set; }
109+
public bool IsParamArray { get; set; }
110+
98111
public Declaration Unwrap(Declaration parent)
99112
{
113+
var annotations = Enumerable.Empty<IAnnotation>();
100114
var attributes = new Attributes();
101115
foreach (var attribute in Attributes)
102116
{
103117
attributes.Add(attribute.Name, attribute.Values);
104118
}
105-
return new Declaration(QualifiedMemberName, parent, ParentScope, AsTypeName, TypeHint, IsSelfAssigned, IsWithEvents, Accessibility, DeclarationType, null, Selection.Empty, IsArray, null, IsBuiltIn, null, attributes);
119+
120+
switch (DeclarationType)
121+
{
122+
case DeclarationType.Project:
123+
return new ProjectDeclaration(QualifiedMemberName, IdentifierName, true);
124+
case DeclarationType.ClassModule:
125+
return new ClassModuleDeclaration(QualifiedMemberName, parent, IdentifierName, true, annotations, attributes);
126+
case DeclarationType.ProceduralModule:
127+
return new ProceduralModuleDeclaration(QualifiedMemberName, parent, IdentifierName, true, annotations, attributes);
128+
case DeclarationType.Procedure:
129+
return new SubroutineDeclaration(QualifiedMemberName, parent, parent, AsTypeName, Accessibility, null, Selection.Empty, true, annotations, attributes);
130+
case DeclarationType.Function:
131+
return new FunctionDeclaration(QualifiedMemberName, parent, parent, AsTypeName, null, TypeHint, Accessibility, null, Selection.Empty, IsArray, true, annotations, attributes);
132+
case DeclarationType.PropertyGet:
133+
return new PropertyGetDeclaration(QualifiedMemberName, parent, parent, AsTypeName, null, TypeHint, Accessibility, null, Selection.Empty, IsArray, true, annotations, attributes);
134+
case DeclarationType.PropertyLet:
135+
return new PropertyLetDeclaration(QualifiedMemberName, parent, parent, AsTypeName, Accessibility, null, Selection.Empty, true, annotations, attributes);
136+
case DeclarationType.PropertySet:
137+
return new PropertySetDeclaration(QualifiedMemberName, parent, parent, AsTypeName, Accessibility, null, Selection.Empty, true, annotations, attributes);
138+
case DeclarationType.Parameter:
139+
return new ParameterDeclaration(QualifiedMemberName, parent, AsTypeName, null, TypeHint, IsOptionalParam, IsByRefParam, IsArray, IsParamArray);
140+
141+
default:
142+
return new Declaration(QualifiedMemberName, parent, ParentScope, AsTypeName, TypeHint, IsSelfAssigned, IsWithEvents, Accessibility, DeclarationType, null, Selection.Empty, IsArray, null, IsBuiltIn, null, attributes);
143+
}
106144
}
107145
}
108146
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.IO;
3+
using System.Runtime.Serialization;
4+
using System.Text;
5+
using System.Xml;
6+
using Rubberduck.SettingsProvider;
7+
8+
namespace Rubberduck.Parsing.Symbols
9+
{
10+
public class XmlPersistableDeclarations : IPersistable<SerializableDeclarationTree>
11+
{
12+
public void Persist(string path, SerializableDeclarationTree tree)
13+
{
14+
if (string.IsNullOrEmpty(path)) { throw new InvalidOperationException(); }
15+
16+
var xmlSettings = new XmlWriterSettings
17+
{
18+
NamespaceHandling = NamespaceHandling.OmitDuplicates,
19+
Encoding = Encoding.UTF8,
20+
//Indent = true
21+
};
22+
23+
using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
24+
using (var xmlWriter = XmlWriter.Create(stream, xmlSettings))
25+
using (var writer = XmlDictionaryWriter.CreateDictionaryWriter(xmlWriter))
26+
{
27+
writer.WriteStartDocument();
28+
var settings = new DataContractSerializerSettings {RootNamespace = XmlDictionaryString.Empty};
29+
var serializer = new DataContractSerializer(typeof (SerializableDeclarationTree), settings);
30+
serializer.WriteObject(writer, tree);
31+
}
32+
}
33+
34+
public SerializableDeclarationTree Load(string path)
35+
{
36+
if (string.IsNullOrEmpty(path)) { throw new InvalidOperationException(); }
37+
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
38+
using (var xmlReader = XmlReader.Create(stream))
39+
using (var reader = XmlDictionaryReader.CreateDictionaryReader(xmlReader))
40+
{
41+
var serializer = new DataContractSerializer(typeof(SerializableDeclarationTree));
42+
return (SerializableDeclarationTree)serializer.ReadObject(reader);
43+
}
44+
}
45+
}
46+
}

Rubberduck.Parsing/VBA/RubberduckParser.cs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -432,18 +432,20 @@ private void SyncComReferences(IReadOnlyList<IVBProject> projects)
432432
{
433433
try
434434
{
435-
var comReflector = new ReferencedDeclarationsCollector(State);
436-
SerializableDeclarationTree tree;
437-
var items = comReflector.GetDeclarationsForReference(localReference, out tree);
438-
if (tree != null)
439-
{
440-
State.BuiltInDeclarationTrees.Add(tree);
441-
}
442-
443-
foreach (var declaration in items)
444-
{
445-
State.AddDeclaration(declaration);
446-
}
435+
LoadSerializedBuiltInReferences(State);
436+
//var comReflector = new ReferencedDeclarationsCollector(State);
437+
//SerializableDeclarationTree tree;
438+
439+
//var items = comReflector.GetDeclarationsForReference(localReference, out tree);
440+
//if (tree != null)
441+
//{
442+
// State.BuiltInDeclarationTrees.Add(tree);
443+
//}
444+
445+
//foreach (var declaration in items)
446+
//{
447+
// State.AddDeclaration(declaration);
448+
//}
447449
}
448450
catch (Exception exception)
449451
{
@@ -486,6 +488,36 @@ private void SyncComReferences(IReadOnlyList<IVBProject> projects)
486488
}
487489
}
488490

491+
private void LoadSerializedBuiltInReferences(RubberduckParserState state)
492+
{
493+
var basePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Rubberduck", "Declarations");
494+
var files = Directory.GetFiles(basePath, "*.xml");
495+
var reader = new XmlPersistableDeclarations();
496+
foreach (var file in files)
497+
{
498+
var tree = reader.Load(file);
499+
foreach (var declaration in UnwrapTree(tree))
500+
{
501+
state.AddDeclaration(declaration);
502+
}
503+
}
504+
}
505+
506+
private IEnumerable<Declaration> UnwrapTree(SerializableDeclarationTree tree, Declaration parent = null)
507+
{
508+
var current = tree.Node.Unwrap(parent);
509+
yield return current;
510+
511+
foreach (var serializableDeclarationTree in tree.Children)
512+
{
513+
var unwrapped = UnwrapTree(serializableDeclarationTree, current);
514+
foreach (var declaration in unwrapped)
515+
{
516+
yield return declaration;
517+
}
518+
}
519+
}
520+
489521
private void UnloadComReference(IReference reference, IReadOnlyList<IVBProject> projects)
490522
{
491523
var referencedProjectId = GetReferenceProjectId(reference, projects);

0 commit comments

Comments
 (0)