Resolve cylinder, robot and connection name references across workflows

This commit is contained in:
wanglei 2026-12-05 22:22:37 +08:00
parent 5712d069ce
commit dc5463b3bb
2 changed files with 20 additions and 4 deletions

View File

@ -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.")]

View File

@ -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<object>();
var diagnostics = new List<object>();
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<InvocationExpressionSyntax>())
{
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 &&