using System.Linq; using JetBrains.Annotations; using OpenScadGraphEditor.Library; using OpenScadGraphEditor.Nodes; using OpenScadGraphEditor.Utils; namespace OpenScadGraphEditor.Refactorings { /// /// Refactoring for nodes which implement . This removes an input from the node /// and fixes all connections. /// [UsedImplicitly] public class RemoveVariableInputRefactoring : NodeRefactoring { public RemoveVariableInputRefactoring(ScadGraph holder, ScadNode node) : base(holder, node) { } public override void PerformRefactoring(RefactoringContext context) { var asVariableInputSize = (IHaveVariableInputSize) Node; // when decreasing input port count, we loose connections (incoming and outgoing) // incoming connections Holder.GetAllConnections() .Where(it => it.IsTo(Node, asVariableInputSize.InputPortOffset + asVariableInputSize.CurrentInputSize - 1)) .ToList() // make a new list, so we don't change the collection while iterating over it .ForAll(it => Holder.RemoveConnection(it)); if (asVariableInputSize.OutputPortsMatchVariableInputs) { // outgoing connections Holder.GetAllConnections() .Where(it => it.IsFrom(Node, asVariableInputSize.OutputPortOffset + asVariableInputSize.CurrentInputSize - 1)) .ToList() // make a new list, so we don't change the collection while iterating over it .ForAll(it => Holder.RemoveConnection(it)); } // and tell the node to kill the ports asVariableInputSize.RemoveVariableInputPort(); } } }