Skip to content

Commit f3bbf59

Browse files
committed
Implement external Xml file IntelliSense provider
1 parent d5b1ec1 commit f3bbf59

File tree

6 files changed

+77
-52
lines changed

6 files changed

+77
-52
lines changed

Source/ExcelDna.IntelliSense/Providers/IIntelliSenseProvider.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,22 @@ public class FunctionInfo
4444
{
4545
public class ArgumentInfo
4646
{
47+
[XmlAttribute]
4748
public string Name;
49+
[XmlAttribute]
4850
public string Description;
49-
public string HelpTopic;
5051
}
5152

53+
[XmlAttribute]
5254
public string Name;
55+
[XmlAttribute]
5356
public string Description;
57+
[XmlAttribute]
5458
public string HelpTopic;
5559
[XmlElement("Argument", typeof(ArgumentInfo))]
5660
public List<ArgumentInfo> ArgumentList;
57-
public string SourcePath; // XllPath for .xll, Workbook Name for Workbook
61+
[XmlIgnore]
62+
public string SourcePath; // XllPath for .xll, Workbook Name for Workbook, .xml file path for Xml file
5863
}
5964

6065
}

Source/ExcelDna.IntelliSense/Providers/WorkbookIntelliSenseProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ void RegisterWithXmlProvider(Workbook wb)
204204
var xmlPath = GetXmlPath(path);
205205
_xmlProvider.RegisterXmlFunctionInfo(xmlPath); // Will check if file exists
206206

207-
var customXmlParts = wb.CustomXMLParts.SelectByNamespace(XmlIntelliSenseProvider.XmlRegistrationInfo.XmlIntelliSense.Namespace);
207+
var customXmlParts = wb.CustomXMLParts.SelectByNamespace(XmlIntelliSense.Namespace);
208208
if (customXmlParts.Count > 0)
209209
{
210-
// We just take the first one - register against the workbook name
210+
// We just take the first one - register against the Bworkbook name
211211
_xmlProvider.RegisterXmlFunctionInfo(path, customXmlParts[1].XML);
212212
}
213213
}

Source/ExcelDna.IntelliSense/Providers/XmlIntelliSenseProvider.cs

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public void Refresh()
3939
xml = File.ReadAllText(_fileName);
4040
}
4141
_intelliSense = XmlIntelliSense.Parse(xml);
42+
if (_intelliSense?.XmlFunctionInfo?.FunctionsList != null)
43+
{
44+
// Fix up SourcePath (is this used?)
45+
foreach (var func in _intelliSense.XmlFunctionInfo.FunctionsList)
46+
{
47+
func.SourcePath = _fileName;
48+
}
49+
}
4250
}
4351
catch // (Exception ex)
4452
{
@@ -55,50 +63,6 @@ public IEnumerable<FunctionInfo> GetFunctionInfos()
5563
return _intelliSense.XmlFunctionInfo.FunctionsList;
5664
}
5765

58-
#region Serialized Xml structure
59-
[Serializable]
60-
[XmlType(AnonymousType = true)]
61-
[XmlRoot("IntelliSense", Namespace = XmlIntelliSense.Namespace, IsNullable = false)]
62-
public class XmlIntelliSense
63-
{
64-
[XmlElement("FunctionInfo")]
65-
public XmlFunctionInfo XmlFunctionInfo;
66-
67-
// returns XmlIntelliSense.Empty on failure
68-
public static XmlIntelliSense Parse(string xmlFunctionInfo)
69-
{
70-
Initialize();
71-
try
72-
{
73-
using (var stringReader = new StringReader(xmlFunctionInfo))
74-
{
75-
return (XmlIntelliSense)_serializer.Deserialize(stringReader);
76-
}
77-
}
78-
catch // (Exception ex)
79-
{
80-
// TODO: Log errors
81-
return Empty;
82-
}
83-
}
84-
85-
public static void Initialize()
86-
{
87-
if (_serializer == null)
88-
_serializer = new XmlSerializer(typeof(XmlIntelliSense));
89-
}
90-
static XmlSerializer _serializer;
91-
public static XmlIntelliSense Empty { get; } = new XmlIntelliSense { XmlFunctionInfo = new XmlFunctionInfo { FunctionsList = new List<FunctionInfo>() } };
92-
public const string Namespace = "http://schemas.excel-dna.net/intellisense/";
93-
}
94-
95-
public class XmlFunctionInfo
96-
{
97-
[XmlElement("Function", typeof(FunctionInfo))]
98-
public List<FunctionInfo> FunctionsList;
99-
}
100-
101-
#endregion
10266

10367
}
10468

@@ -134,11 +98,12 @@ public void RegisterXmlFunctionInfo(string fileName, string xmlIntelliSense = nu
13498
}
13599
}
136100

137-
// Safe to call it wasn't registered
101+
// Safe to call even if it wasn't registered
138102
public void UnregisterXmlFunctionInfo(string fileName)
139103
{
140104
if (_xmlRegistrationInfos.Remove(fileName))
141105
_isDirty = true;
106+
// Not Invalidating - we're not really worried about keeping the extra information around a bit longer
142107
}
143108

144109
public void Initialize()
@@ -188,4 +153,49 @@ public void Dispose()
188153
}
189154
}
190155

156+
#region Serialized Xml structure
157+
[Serializable]
158+
[XmlType(AnonymousType = true)]
159+
[XmlRoot("IntelliSense", Namespace = XmlIntelliSense.Namespace, IsNullable = false)]
160+
public class XmlIntelliSense
161+
{
162+
[XmlElement("FunctionInfo")]
163+
public XmlFunctionInfo XmlFunctionInfo;
164+
165+
// returns XmlIntelliSense.Empty on failure
166+
public static XmlIntelliSense Parse(string xmlFunctionInfo)
167+
{
168+
Initialize();
169+
try
170+
{
171+
using (var stringReader = new StringReader(xmlFunctionInfo))
172+
{
173+
return (XmlIntelliSense)_serializer.Deserialize(stringReader);
174+
}
175+
}
176+
catch // (Exception ex)
177+
{
178+
// TODO: Log errors
179+
return Empty;
180+
}
181+
}
182+
183+
public static void Initialize()
184+
{
185+
if (_serializer == null)
186+
_serializer = new XmlSerializer(typeof(XmlIntelliSense));
187+
}
188+
static XmlSerializer _serializer;
189+
public static XmlIntelliSense Empty { get; } = new XmlIntelliSense { XmlFunctionInfo = new XmlFunctionInfo { FunctionsList = new List<FunctionInfo>() } };
190+
public const string Namespace = "http://schemas.excel-dna.net/intellisense/1.0";
191+
}
192+
193+
[Serializable]
194+
public class XmlFunctionInfo
195+
{
196+
[XmlElement("Function", typeof(FunctionInfo))]
197+
public List<FunctionInfo> FunctionsList;
198+
}
199+
200+
#endregion
191201
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<IntelliSense xmlns="http://excel-dna.net/intellisense/1.0/">
22
<FunctionInfo>
3-
<Function Name="" Description="" HelpTopic="" >
4-
<Argument Name="" Description="" />
5-
<Argument Name="" Description="" />
3+
<Function Name="MyVBAFunction" Description="A function described by an external XML file"
4+
HelpTopic="http://www.bing.com" >
5+
<Argument Name="FirstArg" Description="Whatever you want to put in here" />
6+
<Argument Name="AnotherArg" Description="Actually the second arg" />
67
</Function>
78
</FunctionInfo>
89
</IntelliSense>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<IntelliSense xmlns="http://schemas.excel-dna.net/intellisense/1.0">
2+
<FunctionInfo>
3+
<Function Name="MyVBAFunction" Description="A function described by an external XML file"
4+
HelpTopic="http://www.bing.com" >
5+
<Argument Name="FirstArg" Description="Whatever you want to put in here" />
6+
<Argument Name="AnotherArg" Description="Actually the second arg" />
7+
</Function>
8+
</FunctionInfo>
9+
</IntelliSense>

VBASamples/VBAFunctionsNoIS.xlsm

11.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)