Skip to content

Commit

Permalink
improve node autocomplete results for custom node output ports (Dynam…
Browse files Browse the repository at this point in the history
…oDS#14218)

* update tests
dont use type string, use name
dont use name with spaces, it will never be correct.

* add a new test

* comments

* review comments and new test
  • Loading branch information
mjkkirschner authored and sm6srw committed Mar 22, 2024
1 parent 07de74e commit 74fc972
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ internal IEnumerable<NodeSearchElement> GetMatchingSearchElements()
else if (PortViewModel.PortModel.PortType == PortType.Output)
{
portType = PortViewModel.PortModel.GetOutPortType();
//if the custom node output name contains spaces, try using the first word.
if (PortViewModel.PortModel.Owner is Graph.Nodes.CustomNodes.Function && portType.Any(char.IsWhiteSpace))
{
portType = string.Concat(portType.TrimStart().TakeWhile(char.IsLetterOrDigit));
}
}

//List of input types that are skipped temporarily, and will display list of default suggestions instead.
Expand All @@ -198,7 +203,6 @@ internal IEnumerable<NodeSearchElement> GetMatchingSearchElements()
var ast = parseResult?.CodeBlockNode.Children().FirstOrDefault() as IdentifierNode;
//if parsing the type failed, revert to original string.
portType = ast != null ? ast.datatype.Name : portType;

//check if the input port return type is in the skipped input types list
if (skippedInputTypes.Any(s => s == portType))
{
Expand Down Expand Up @@ -237,7 +241,8 @@ internal IEnumerable<NodeSearchElement> GetMatchingSearchElements()
{
foreach (var inputParameter in ztSearchElement.Descriptor.Parameters.Select((value, index) => new { value, index }))
{
if (inputParameter.value.Type.ToString() == portType || DerivesFrom(inputParameter.value.Type.ToString(), portType, core))
var ZTparamName = inputParameter.value.Type.Name ?? inputParameter.value.Type.ToString();
if (ZTparamName == portType || DerivesFrom(ZTparamName, portType, core))
{
ztSearchElement.AutoCompletionNodeElementInfo.PortToConnect = ztSearchElement.Descriptor.Type == FunctionType.InstanceMethod ? inputParameter.index + 1 : inputParameter.index;
elements.Add(ztSearchElement);
Expand Down
42 changes: 39 additions & 3 deletions test/DynamoCoreWpfTests/NodeAutoCompleteSearchTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Dynamo;
using Dynamo.Controls;
using Dynamo.Graph.Nodes;
using Dynamo.Models;
Expand Down Expand Up @@ -134,9 +135,44 @@ public void NodeSuggestions_CanAutoCompleteOnCustomNodesOutPort()
var searchViewModel = ViewModel.CurrentSpaceViewModel.NodeAutoCompleteSearchViewModel;
searchViewModel.PortViewModel = outPorts[1];

// The initial list will fill the FilteredResults with a few options - all basic input types
// Results will be nodes that take color or color[] etc as params.
searchViewModel.PopulateAutoCompleteCandidates();
Assert.AreEqual(10, searchViewModel.FilteredResults.Count());
}

[Test]
public void NodeSuggestions_CanAutoCompleteOnCustomNodesOutPort_WithSpaceInPortName()
{
var outputNode = new Dynamo.Graph.Nodes.CustomNodes.Output();
outputNode.Symbol = "Line Nonsense";
var cnm = new Dynamo.Graph.Nodes.CustomNodes.Function(
new CustomNodeDefinition(Guid.NewGuid(), "mock", new List<NodeModel>() { outputNode })
, "mock", "mock", "mock");
var cnvm = new NodeViewModel(ViewModel.CurrentSpaceViewModel, cnm);

var searchViewModel = ViewModel.CurrentSpaceViewModel.NodeAutoCompleteSearchViewModel;
searchViewModel.PortViewModel = cnvm.OutPorts.First();

// Results will be nodes that accept Line as parameter.
searchViewModel.PopulateAutoCompleteCandidates();
Assert.AreEqual(44, searchViewModel.FilteredResults.Count());
}
[Test]
public void NodeSuggestions_CanAutoCompleteOnCustomNodesOutPort_WithWhiteSpaceStartingPortName()
{
var outputNode = new Dynamo.Graph.Nodes.CustomNodes.Output();
outputNode.Symbol = " Line Nonsense";
var cnm = new Dynamo.Graph.Nodes.CustomNodes.Function(
new CustomNodeDefinition(Guid.NewGuid(), "mock", new List<NodeModel>() { outputNode })
, "mock", "mock", "mock");
var cnvm = new NodeViewModel(ViewModel.CurrentSpaceViewModel, cnm);

var searchViewModel = ViewModel.CurrentSpaceViewModel.NodeAutoCompleteSearchViewModel;
searchViewModel.PortViewModel = cnvm.OutPorts.First();

// Results will be nodes that accept Line as parameter.
searchViewModel.PopulateAutoCompleteCandidates();
Assert.AreEqual(7, searchViewModel.FilteredResults.Count());
Assert.AreEqual(44, searchViewModel.FilteredResults.Count());
}

[Test]
Expand Down Expand Up @@ -275,7 +311,7 @@ public void NodeSuggestions_OutputPortBuiltInNode_AreCorrect()
var searchViewModel = ViewModel.CurrentSpaceViewModel.NodeAutoCompleteSearchViewModel;
searchViewModel.PortViewModel = outPorts[0];
var suggestions = searchViewModel.GetMatchingSearchElements();
Assert.AreEqual(29, suggestions.Count());
Assert.AreEqual(44, suggestions.Count());
}

[Test]
Expand Down

0 comments on commit 74fc972

Please sign in to comment.