diff --git a/PLCSharp.Mcp/ProjectTools.cs b/PLCSharp.Mcp/ProjectTools.cs index 0be84b6..e22e3f6 100644 --- a/PLCSharp.Mcp/ProjectTools.cs +++ b/PLCSharp.Mcp/ProjectTools.cs @@ -30,7 +30,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 or Workflows ID within its recipe. Dynamic hits require human review.")] + [McpServerTool(Name = "find_references", ReadOnly = true), Description("Find C# references to a Variables, SystemVariables, Workflows, AxisPoints or RobotPoints ID within its recipe. 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 0e02340..b25d824 100644 --- a/PLCSharpWpf/Models/WorkflowReferences.cs +++ b/PLCSharpWpf/Models/WorkflowReferences.cs @@ -2,6 +2,9 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using PLCSharp.VVMs.GlobalVariables; +using PLCSharp.VVMs.MotionController; +using PLCSharp.VVMs.Robots; +using Microsoft.CodeAnalysis.Operations; using PLCSharp.VVMs.Workflows; using System.IO; using System.Text.Json; @@ -25,10 +28,13 @@ internal static class WorkflowReferences internal static JsonNode Find(ProjectDirectory project, string kind, string id, bool forRename = false) { var targetType = kind switch { "Variables" => typeof(Variable), "SystemVariables" => typeof(SystemVariable), - "Workflows" => typeof(Workflow), _ => throw new ArgumentException("Expected Variables, SystemVariables or Workflows.") }; + "AxisPoints" => typeof(AxisPoint), "RobotPoints" => typeof(RobotPoint), + "Workflows" => typeof(Workflow), _ => throw new ArgumentException("Unsupported reference target.") }; var target = ProjectInspection.Query(project, "get", new() { ["kind"] = kind, ["id"] = id }); var recipeId = 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 workflow in project.Flows(recipeId)) @@ -49,11 +55,39 @@ internal static class WorkflowReferences column = position.Character + 1, start = value.SpanStart, length = value.Span.Length, expression = location.ToString(), confidence = !constant.HasValue ? "dynamic" : bound ? "confirmed" : "possible" }); } + // Return null when the receiver cannot be tied to a configured robot. + bool? IsTargetRobot(ExpressionSyntax receiver) + { + if (receiver is not InvocationExpressionSyntax get || + model.GetSymbolInfo(get).Symbol is not IMethodSymbol { Name: "GetRobot" } method || + method.ContainingType.ToDisplayString() != typeof(GlobalModel).FullName || + get.ArgumentList.Arguments.Count != 1 || + model.GetConstantValue(get.ArgumentList.Arguments[0].Expression) is not { HasValue: true, Value: string robotName }) return null; + var matches = robots.Where(r => r.Name == robotName).ToArray(); + return matches.Length == 1 ? matches[0].ID == robotId : null; + } + var lookup = kind switch { "Variables" => "GetVariable", "Workflows" => "GetTask", _ => 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 == "RobotPoints" && methodName is "RunPoint" or "Jog") + { + if (model.GetOperation(call) is IInvocationOperation invocation) + { + var method = invocation.TargetMethod; + while (method.OverriddenMethod != null) method = method.OverriddenMethod; + if (method.ContainingType.ToDisplayString() != typeof(Robot).FullName) continue; + var argument = invocation.Arguments.FirstOrDefault(a => a.Parameter?.Name == "pointName"); + var owner = IsTargetRobot((call.Expression as MemberAccessExpressionSyntax)?.Expression); + if (owner != false && argument?.Syntax is ArgumentSyntax syntax) + Record(syntax.Expression, call, owner == true); + } + else if (call.ArgumentList.Arguments.FirstOrDefault() is { } unresolved) + Record(unresolved.Expression, call, false); + continue; + } if (methodName != lookup || call.ArgumentList.Arguments.Count != 1) continue; var symbol = model.GetSymbolInfo(call).Symbol; if (symbol != null && symbol.ContainingType?.ToDisplayString() != typeof(GlobalModel).FullName) continue; @@ -65,13 +99,17 @@ internal static class WorkflowReferences { if (model.GetSymbolInfo(member).Symbol is IPropertySymbol property && property.Name == "Name" && property.ContainingType.ToDisplayString() == targetType.FullName) - Record(value, comparison, !forRename || IsRecipePredicate(member, model, kind)); + { + bool? bound = IsRecipePredicate(member, model, kind, IsTargetRobot); + if (bound != false || kind != "RobotPoints") + Record(value, comparison, bound == true || (!forRename && kind is not ("AxisPoints" or "RobotPoints"))); + } } } } return JsonSerializer.SerializeToNode(new { recipeId, targetId = id, name, hits, diagnostics }); } - private static bool IsRecipePredicate(ExpressionSyntax expression, SemanticModel model, string kind) + private static bool? IsRecipePredicate(ExpressionSyntax expression, SemanticModel model, string kind, Func robotOwner) { if (expression is not MemberAccessExpressionSyntax access || model.GetSymbolInfo(access.Expression).Symbol is not IParameterSymbol parameter || @@ -80,10 +118,15 @@ internal static class WorkflowReferences lambda.Parent?.Parent?.Parent is not InvocationExpressionSyntax call || call.Expression is not MemberAccessExpressionSyntax method || model.GetSymbolInfo(call).Symbol is not IMethodSymbol resolved || - resolved.ReducedFrom?.ContainingType.ToDisplayString() != "System.Linq.Enumerable") return false; - if (model.GetSymbolInfo(method.Expression).Symbol is not IPropertySymbol source) return false; + resolved.ReducedFrom?.ContainingType.ToDisplayString() != "System.Linq.Enumerable") return null; + if (model.GetSymbolInfo(method.Expression).Symbol is not IPropertySymbol source) return null; return kind switch { + "AxisPoints" => source.Name == nameof(ControllersModel.AxisPoints) && + source.ContainingType.ToDisplayString() == typeof(ControllersModel).FullName, + "RobotPoints" when source.Name == nameof(Robot.Points) && source.ContainingType.ToDisplayString() == typeof(Robot).FullName && + method.Expression is MemberAccessExpressionSyntax points => robotOwner(points.Expression), + "RobotPoints" => null, "Workflows" => source.Name == nameof(WorkflowsModel.Workflows) && source.ContainingType.ToDisplayString() == typeof(WorkflowsModel).FullName, "Variables" => source.Name == nameof(VariablesModel.Variables) &&