From dc5463b3bb466f087dcf858db69f0c9189a9a697 Mon Sep 17 00:00:00 2001 From: wanglei Date: Sat, 5 Dec 2026 22:22:37 +0800 Subject: [PATCH] Resolve cylinder, robot and connection name references across workflows --- PLCSharp.Mcp/ProjectTools.cs | 2 +- PLCSharpWpf/Models/WorkflowReferences.cs | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/PLCSharp.Mcp/ProjectTools.cs b/PLCSharp.Mcp/ProjectTools.cs index 0c90a38..f13a727 100644 --- a/PLCSharp.Mcp/ProjectTools.cs +++ b/PLCSharp.Mcp/ProjectTools.cs @@ -46,7 +46,7 @@ internal sealed class ProjectTools(ProjectGateway gateway) [McpServerTool(Name = "get_change_status", ReadOnly = true), Description("Inspect a pending, rejected or saved draft.")] public string GetChangeStatus(string draftId) => gateway.Call("status", new() { ["draftId"] = draftId }); - [McpServerTool(Name = "find_references", ReadOnly = true), Description("Find C# references to a Variables, SystemVariables, Workflows, VisionFunctions, AxisPoints or RobotPoints ID within its recipe, or an Axes, DI or DQ ID across recipes. Dynamic hits require human review.")] + [McpServerTool(Name = "find_references", ReadOnly = true), Description("Find C# references to a Variables, SystemVariables, Workflows, VisionFunctions, AxisPoints or RobotPoints ID within its recipe, or an Axes, DI, DQ, Cylinders, Robots or Connects ID across recipes. Dynamic hits require human review.")] public string FindReferences(string kind, string id) => gateway.Call("references", new() { ["kind"] = kind, ["id"] = id }); [McpServerTool(Name = "get_project_summary", ReadOnly = true), Description("Read the bound PLCSharp project, default recipe and available object kinds.")] diff --git a/PLCSharpWpf/Models/WorkflowReferences.cs b/PLCSharpWpf/Models/WorkflowReferences.cs index 3fa2fa6..2ebbbb3 100644 --- a/PLCSharpWpf/Models/WorkflowReferences.cs +++ b/PLCSharpWpf/Models/WorkflowReferences.cs @@ -4,6 +4,8 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using PLCSharp.VVMs.GlobalVariables; using PLCSharp.VVMs.MotionController; using PLCSharp.VVMs.Robots; +using PLCSharp.VVMs.Cylinders; +using PLCSharp.VVMs.Connects; using Microsoft.CodeAnalysis.Operations; using PLCSharp.VVMs.Workflows; using PLCSharp.VVMs.Vision; @@ -30,15 +32,17 @@ internal static class WorkflowReferences { var targetType = kind switch { "Variables" => typeof(Variable), "SystemVariables" => typeof(SystemVariable), "VisionFunctions" => typeof(VisionFunction), "DI" => typeof(DI), "DQ" => typeof(DQ), "Axes" => typeof(Axis), "AxisPoints" => typeof(AxisPoint), "RobotPoints" => typeof(RobotPoint), + "Cylinders" => typeof(Cylinder), "Robots" => typeof(Robot), "Connects" => typeof(Connect), "Workflows" => typeof(Workflow), _ => throw new ArgumentException("Unsupported reference target.") }; var target = ProjectInspection.Query(project, "get", new() { ["kind"] = kind, ["id"] = id }); - var recipeId = kind is "Axes" or "DI" or "DQ" ? Guid.Empty : Guid.Parse((string)target["RecipeID"]); + bool projectWide = kind is "Axes" or "DI" or "DQ" or "Cylinders" or "Robots" or "Connects"; + var recipeId = projectWide ? Guid.Empty : Guid.Parse((string)target["RecipeID"]); var name = (string)target["Name"]; var robotId = kind == "RobotPoints" ? Guid.Parse((string)target["RobotID"]) : Guid.Empty; var robots = kind == "RobotPoints" ? project.Data.Robots.Select(r => new { r.ID, r.Name }).ToList() : []; var hits = new List(); var diagnostics = new List(); - foreach (var recipe in project.Recipes.Where(r => kind is "Axes" or "DI" or "DQ" || r.ID == recipeId)) + foreach (var recipe in project.Recipes.Where(r => projectWide || r.ID == recipeId)) foreach (var workflow in project.Flows(recipe.ID)) { var tree = CSharpSyntaxTree.ParseText(workflow.Code ?? ""); @@ -70,11 +74,20 @@ internal static class WorkflowReferences } var lookup = kind switch { "Variables" => "GetVariable", "Workflows" => "GetTask", + "Robots" => nameof(GlobalModel.GetRobot), "Connects" => nameof(GlobalModel.GetConnect), "VisionFunctions" => nameof(GlobalModel.GetVisionFunction), _ => null }; foreach (var call in root.DescendantNodes().OfType()) { var methodName = call.Expression switch { MemberAccessExpressionSyntax m => m.Name.Identifier.ValueText, MemberBindingExpressionSyntax b => b.Name.Identifier.ValueText, IdentifierNameSyntax n => n.Identifier.ValueText, _ => "" }; + if (kind == "Connects" && methodName is nameof(ConnectsModel.ApplyAllDataToServer) or nameof(ConnectsModel.ApplyDataItemToServer)) + { + if (model.GetOperation(call) is IInvocationOperation operation && + operation.TargetMethod.ContainingType.ToDisplayString() == typeof(ConnectsModel).FullName && + operation.Arguments.FirstOrDefault(a => a.Parameter?.Name == "connectName")?.Syntax is ArgumentSyntax argument) + Record(argument.Expression, call, true); + continue; + } if (kind == "RobotPoints" && methodName is "RunPoint" or "Jog") { if (model.GetOperation(call) is IInvocationOperation invocation) @@ -105,7 +118,7 @@ internal static class WorkflowReferences { bool? bound = IsRecipePredicate(member, model, kind, IsTargetRobot); if (bound != false || kind != "RobotPoints") - Record(value, comparison, bound == true || (!forRename && kind is not ("DI" or "DQ" or "Axes" or "AxisPoints" or "RobotPoints" or "VisionFunctions"))); + Record(value, comparison, bound == true || (!forRename && !projectWide && kind is not ("AxisPoints" or "RobotPoints" or "VisionFunctions"))); } } } @@ -125,6 +138,9 @@ internal static class WorkflowReferences if (model.GetSymbolInfo(method.Expression).Symbol is not IPropertySymbol source) return null; return kind switch { + "Cylinders" => source.Name == kind && source.ContainingType.ToDisplayString() == typeof(CylinderModel).FullName, + "Robots" => source.Name == kind && source.ContainingType.ToDisplayString() == typeof(RobotModel).FullName, + "Connects" => source.Name == kind && source.ContainingType.ToDisplayString() == typeof(ConnectsModel).FullName, "VisionFunctions" => source.Name == nameof(VisionsModel.VisionFunctions) && source.ContainingType.ToDisplayString() == typeof(VisionsModel).FullName, "DI" or "DQ" or "Axes" => source.Name == kind &&