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

Fixed REFRESH for .intellisense.xml file and Workbook.CustomXMLParts #142

Merged
merged 1 commit into from
Feb 23, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -353,21 +353,7 @@ void RegisterWithXmlProvider(Workbook wb)
var path = wb.FullName;
var xmlPath = GetXmlPath(path);
_xmlProvider.RegisterXmlFunctionInfo(xmlPath); // Will check if file exists

Logger.Provider.Verbose($"WorkbookIntelliSenseProvider.RegisterWithXmlProvider - Checking CustomXMLParts");

var customXmlParts = wb.CustomXMLParts.SelectByNamespace(XmlIntelliSense.Namespace);
if (customXmlParts.Count > 0)
{
Logger.Provider.Verbose($"WorkbookIntelliSenseProvider.RegisterWithXmlProvider - CustomXMLPart found");
// We just take the first one - register against the Bworkbook name
_xmlProvider.RegisterXmlFunctionInfo(path, customXmlParts[1].XML);
}
else
{
Logger.Provider.Verbose($"WorkbookIntelliSenseProvider.RegisterWithXmlProvider - No CustomXMLPart found");
}

_xmlProvider.RegisterXmlFunctionInfo(path, () => GetIntelliSenseFromCustomXMLParts(wb));
}

void UnregisterWithXmlProvider(Workbook wb)
Expand Down Expand Up @@ -404,6 +390,27 @@ public void Dispose()
}
}

string GetIntelliSenseFromCustomXMLParts(Workbook wb)
{
string result = null;

Logger.Provider.Verbose($"WorkbookIntelliSenseProvider.RegisterWithXmlProvider - Checking CustomXMLParts");

var customXmlParts = wb.CustomXMLParts.SelectByNamespace(XmlIntelliSense.Namespace);
if (customXmlParts.Count > 0)
{
Logger.Provider.Verbose($"WorkbookIntelliSenseProvider.RegisterWithXmlProvider - CustomXMLPart found");
// We just take the first one - register against the workbook name
result = customXmlParts[1].XML;
}
else
{
Logger.Provider.Verbose($"WorkbookIntelliSenseProvider.RegisterWithXmlProvider - No CustomXMLPart found");
}

return result;
}

string GetXmlPath(string wbPath) => Path.ChangeExtension(wbPath, ".intellisense.xml");
}
}
23 changes: 10 additions & 13 deletions Source/ExcelDna.IntelliSense/Providers/XmlIntelliSenseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,25 @@ class XmlIntelliSenseProvider : IIntelliSenseProvider
{
public class XmlRegistrationInfo
{
string _fileName; // Might be .xml file or Workbook path. Use only if _xmlIntelliSense is null.
string _xmlIntelliSense; // Might be null
string _fileName; // Might be .xml file or Workbook path. Use only if _xmlIntelliSenseSource is null.
Func<string> _xmlIntelliSenseSource; // Might be null
XmlIntelliSense _intelliSense; // Might be null - lazy parsed
string _path; // Directory of file, used to expand HelpTopic

public XmlRegistrationInfo(string fileName, string xmlIntelliSense)
public XmlRegistrationInfo(string fileName, Func<string> xmlIntelliSenseSource)
{
_fileName = fileName;
_xmlIntelliSense = xmlIntelliSense;
_xmlIntelliSenseSource = xmlIntelliSenseSource;
_path = Path.GetDirectoryName(fileName);
}

// Called in a macro context
public void Refresh()
{
if (_intelliSense != null)
return; // Already done

try
{
// Parse first
var xml = _xmlIntelliSense;
var xml = _xmlIntelliSenseSource?.Invoke();
if (xml == null)
{
xml = File.ReadAllText(_fileName);
Expand Down Expand Up @@ -87,15 +84,15 @@ public XmlIntelliSenseProvider()
// May be called on the main Excel thread or on another thread (e.g. our automation thread)
// Pass in the xmlFunctionInfo if available (from inside document), else file will be read
// We make the parsing lazy...
public void RegisterXmlFunctionInfo(string fileName, string xmlIntelliSense = null)
public void RegisterXmlFunctionInfo(string fileName, Func<string> xmlIntelliSenseSource = null)
{
if (!File.Exists(fileName) && xmlIntelliSense == null)
if (!File.Exists(fileName) && xmlIntelliSenseSource == null)
{
Logger.Provider.Verbose($"XmlIntelliSenseProvider.RegisterXmlFunctionInfo - Not IntelliSense file at {fileName}");
return;
}
var regInfo = new XmlRegistrationInfo(fileName, xmlIntelliSense);

var regInfo = new XmlRegistrationInfo(fileName, xmlIntelliSenseSource);
Logger.Provider.Verbose($"XmlIntelliSenseProvider.RegisterXmlFunctionInfo - Created XmlRegistrationInfo info for {fileName}");
lock (_xmlRegistrationInfos)
{
Expand Down Expand Up @@ -157,7 +154,7 @@ public IList<FunctionInfo> GetFunctionInfos()
Logger.Provider.Verbose("XmlIntelliSenseProvider.GetFunctionInfos - End");
return functionInfos;
}

void OnInvalidate(object _unused_)
{
Logger.Provider.Verbose($"XmlIntelliSenseProvider.OnInvalidate - Invoking Invalidate event");
Expand Down