Skip to content

Commit 21c6822

Browse files
committed
Add local node help lookup as fallback for DNA
1 parent 76f92d4 commit 21c6822

4 files changed

Lines changed: 99 additions & 2 deletions

File tree

src/DynamoCore/Models/DynamoModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2431,7 +2431,7 @@ private void SetPeriodicEvaluation(WorkspaceModel ws)
24312431
}
24322432
}
24332433

2434-
private bool OpenJsonFile(
2434+
internal bool OpenJsonFile(
24352435
string filePath,
24362436
string fileContents,
24372437
DynamoPreferencesData dynamoPreferences,

src/DynamoUtilities/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@
3131
[assembly: InternalsVisibleTo("LibraryViewExtensionWebView2")]
3232
[assembly: InternalsVisibleTo("Notifications")]
3333
[assembly: InternalsVisibleTo("SystemTestServices")]
34+
[assembly: InternalsVisibleTo("NodeAutoCompleteViewExtension")]

src/NodeAutoCompleteViewExtension/ViewModels/NodeAutoCompleteBarViewModel.cs

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,8 @@ private IEnumerable<SingleResultItem> GetNodeAutocompleMLResults()
557557
AutocompleteMLTitle = Resources.AutocompleteNoRecommendationsTitle;
558558
AutocompleteMLMessage = Resources.AutocompleteNoRecommendationsMessage;
559559
Analytics.TrackEvent(Actions.View, Categories.NodeAutoCompleteOperations, "NoRecommendation");
560-
return new List<SingleResultItem>();
560+
561+
return TryGetLocalAutoCompleteResult();
561562
}
562563
ServiceVersion = MLresults.Version;
563564
var results = new List<SingleResultItem>();
@@ -630,6 +631,90 @@ private IEnumerable<SingleResultItem> GetNodeAutocompleMLResults()
630631

631632
return results;
632633
}
634+
635+
/// <summary>
636+
/// Use local node help files to at least return one result that will work.
637+
/// </summary>
638+
/// <returns></returns>
639+
private List<SingleResultItem> TryGetLocalAutoCompleteResult()
640+
{
641+
var dynamoModel = this.dynamoViewModel.Model;
642+
643+
var nodeHelpDocPath = new DirectoryInfo(Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName,
644+
Configurations.DynamoNodeHelpDocs));
645+
646+
var selectedNode = this.PortViewModel.NodeViewModel.NodeModel;
647+
var selectedPortModel = this.PortViewModel.PortModel;
648+
649+
string nodeFullName;
650+
651+
switch (selectedNode)
652+
{
653+
case DSFunction dsFunction:
654+
string fullSignature = dsFunction.FunctionSignature;
655+
nodeFullName = fullSignature.Split('@')[0];
656+
break;
657+
case Function function:
658+
string fullFunctionSignature = function.FunctionSignature.ToString();
659+
nodeFullName = fullFunctionSignature.Split('@')[0];
660+
break;
661+
default:
662+
nodeFullName = selectedNode.GetType().ToString();
663+
break;
664+
}
665+
666+
string sampleDynPath = Path.Combine(nodeHelpDocPath.FullName, $"{nodeFullName}.dyn");
667+
668+
if (!File.Exists(sampleDynPath))
669+
{
670+
var minimumQualifiedName = dynamoViewModel.GetMinimumQualifiedName(selectedNode);
671+
var shortName = Hash.GetHashFilenameFromString(minimumQualifiedName);
672+
sampleDynPath = Path.Combine(nodeHelpDocPath.FullName, $"{shortName}.dyn");
673+
}
674+
675+
//no help file found, return nothing
676+
if (!File.Exists(sampleDynPath))
677+
{
678+
return new List<SingleResultItem>();
679+
}
680+
681+
Exception ex;
682+
DynamoUtilities.PathHelper.isValidJson(sampleDynPath, out var fileContents, out ex);
683+
684+
//get the workspace model from the sample to lookup the node
685+
var workspace = WorkspaceModel.FromJson(fileContents, dynamoModel.LibraryServices,
686+
dynamoModel.EngineController, dynamoModel.Scheduler,
687+
dynamoModel.NodeFactory, false, false,
688+
dynamoModel.CustomNodeManager,
689+
dynamoModel.LinterManager);
690+
691+
var matchingNode = workspace.Nodes
692+
.FirstOrDefault(n => n.CreationName == selectedNode.CreationName);
693+
694+
//no matching node found, return nothing
695+
if (matchingNode is null) return new List<SingleResultItem>();
696+
697+
//find the port model and the node to place
698+
NodeModel predictedNode = null;
699+
PortModel matchingPortModel = null;
700+
switch (selectedPortModel.PortType)
701+
{
702+
case PortType.Input:
703+
matchingPortModel = matchingNode.InPorts[selectedPortModel.Index];
704+
predictedNode = matchingPortModel.Connectors.First().Start.Owner;
705+
break;
706+
case PortType.Output:
707+
matchingPortModel = matchingNode.OutPorts[selectedPortModel.Index];
708+
predictedNode = matchingPortModel.Connectors.First().End.Owner;
709+
break;
710+
}
711+
712+
if (predictedNode is null) return new List<SingleResultItem>();
713+
714+
SingleResultItem singleResultItem = new SingleResultItem(predictedNode, matchingPortModel.Index);
715+
return [singleResultItem];
716+
}
717+
633718
private T GetGenericAutocompleteResult<T>(string endpoint)
634719
{
635720
var requestDTO = GenerateRequestForMLAutocomplete();

src/NodeAutoCompleteViewExtension/ViewModels/SingleResultItem.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Dynamo.Graph.Nodes;
12
using Dynamo.Search.SearchElements;
23
using Dynamo.Wpf.ViewModels;
34

@@ -17,6 +18,16 @@ public SingleResultItem(NodeSearchElement model, double score = 1.0)
1718
Score = score;
1819
}
1920

21+
public SingleResultItem(NodeModel nodeModel, int portToConnect, double score = 1.0)
22+
{
23+
Assembly = nodeModel.GetType().Assembly.GetName().Name;
24+
IconName = nodeModel.GetType().Name;
25+
Description = nodeModel.Name;
26+
CreationName = nodeModel.CreationName;
27+
PortToConnect = portToConnect;
28+
Score = score;
29+
}
30+
2031
public SingleResultItem(NodeSearchElementViewModel x) : this(x.Model)
2132
{
2233
//Convert percent to probability

0 commit comments

Comments
 (0)