-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodCreate.cs
More file actions
184 lines (158 loc) · 6.73 KB
/
Copy pathMethodCreate.cs
File metadata and controls
184 lines (158 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Cdp.ScriptableIde;
/// <summary>Create a method on an existing type (Anchor M:TypeName).</summary>
public sealed class MethodFacade(IScriptToolBus bus, PlanContext plan)
{
public MethodBuilder Create(string name) => new(bus, plan, name);
}
public sealed class MethodBuilder(IScriptToolBus bus, PlanContext plan, string name)
{
private string? _typeAnchor;
private TypeIntent? _returns;
private bool _static;
private readonly List<(string Name, TypeIntent Type)> _params = [];
public MethodBuilder In(string typeAnchor)
{
ArgumentException.ThrowIfNullOrWhiteSpace(typeAnchor);
_typeAnchor = typeAnchor.Trim();
return this;
}
public MethodBuilder In(Anchor typeAnchor) => In(typeAnchor.ToWire());
public MethodBuilder Static(bool value = true)
{
_static = value;
return this;
}
public MethodBuilder Returns(TypeIntent type)
{
ArgumentNullException.ThrowIfNull(type);
_returns = type;
return this;
}
public MethodBuilder Param(string paramName, TypeIntent type)
{
ArgumentException.ThrowIfNullOrWhiteSpace(paramName);
ArgumentNullException.ThrowIfNull(type);
_params.Add((paramName.Trim(), type));
return this;
}
public Task<StepResponse> ApplyAsync(CancellationToken ct = default) =>
MethodCreateRunner.ApplyAsync(bus, plan, name, _typeAnchor, _returns, _static, _params, ct);
}
internal static class MethodCreateRunner
{
public const string Kind = "method.create";
public static Task<StepResponse> ApplyAsync(
IScriptToolBus bus,
PlanContext plan,
string name,
string? typeAnchor,
TypeIntent? returns,
bool isStatic,
IReadOnlyList<(string Name, TypeIntent Type)> parameters,
CancellationToken ct)
{
_ = ct;
if (string.IsNullOrWhiteSpace(name))
return Task.FromResult(StepResponse.Fail(Kind, "Method.Create(name) required"));
if (string.IsNullOrWhiteSpace(typeAnchor))
return Task.FromResult(StepResponse.Fail(Kind, "In(typeAnchor) required"));
if (!IsIdent(name))
return Task.FromResult(StepResponse.Fail(Kind, "method name must be an identifier"));
var lang = (plan.Language ?? "csharp").Trim().ToLowerInvariant();
if (lang is not "csharp")
return Task.FromResult(StepResponse.Fail(Kind, "Method.Create csharp-only v1"));
if (!AnchorLocus.TryResolveFile(plan, typeAnchor!, Kind, out var file, out var span, out var fail))
return Task.FromResult(fail!);
if (!file.EndsWith(".cs", StringComparison.OrdinalIgnoreCase))
return Task.FromResult(StepResponse.Fail(Kind, "csharp only (.cs)", new { file }));
if (!TryFindType(file, span, out var typeDecl, out var typeName, out var detail))
return Task.FromResult(StepResponse.Fail(Kind, "type locate failed: " + detail, new { anchor = typeAnchor }));
if (typeDecl.Members.OfType<MethodDeclarationSyntax>()
.Any(m => m.Identifier.Text.Equals(name, StringComparison.Ordinal)
&& m.ParameterList.Parameters.Count == parameters.Count))
{
var skip = StepResponse.Success(Kind, "already:" + name, new { path = file, type = typeName });
bus.RecordLocal("method", Kind, ScriptArgs.From(new { name, path = file }), skip.ToJson());
return Task.FromResult(skip);
}
string retWire;
if (returns is null || returns is InferTypeIntent)
retWire = "void";
else if (!TypeProjection.TryProject(lang, returns, out retWire!, out var terr))
return Task.FromResult(StepResponse.Fail(Kind, terr ?? "returns project failed"));
var paramParts = new List<string>();
foreach (var (pn, pt) in parameters)
{
if (!IsIdent(pn))
return Task.FromResult(StepResponse.Fail(Kind, "param name must be identifier: " + pn));
if (!TypeProjection.TryProject(lang, pt, out var tw, out var perr))
return Task.FromResult(StepResponse.Fail(Kind, perr ?? "param type project failed"));
paramParts.Add($"{tw} {pn}");
}
var mods = isStatic ? "public static" : "public";
var sig = $"{mods} {retWire} {name}({string.Join(", ", paramParts)})";
var methodText = $"\n {sig}\n {{\n }}\n";
var text = File.ReadAllText(file);
var close = typeDecl.CloseBraceToken.Span.Start;
var before = text[..close];
var gap = before.EndsWith('\n') ? "" : "\n";
var newText = before + gap + methodText + text[close..];
var args = ScriptArgs.From(new { name, path = file, type = typeName, static_member = isStatic, returns = retWire });
if (bus.IsDryRun)
{
var dry = StepResponse.Success(Kind, "dry_run", new { dry_run = true, path = file, preview = methodText });
bus.RecordLocal("method", Kind, args, dry.ToJson(), skippedDryRun: true);
return Task.FromResult(dry);
}
File.WriteAllText(file, newText);
var ok = StepResponse.Success(Kind, "created:" + name, new { path = file, type = typeName, method = name });
bus.RecordLocal("method", Kind, args, ok.ToJson());
return Task.FromResult(ok);
}
private static bool TryFindType(
string file,
BracketLocate.Span span,
out TypeDeclarationSyntax type,
out string typeName,
out string detail)
{
type = null!;
typeName = "";
detail = "";
var tree = CSharpSyntaxTree.ParseText(File.ReadAllText(file));
var root = tree.GetCompilationUnitRoot();
if (!string.IsNullOrWhiteSpace(span.MemberKey))
{
var hit = root.DescendantNodes()
.OfType<TypeDeclarationSyntax>()
.FirstOrDefault(t => t.Identifier.Text.Equals(span.MemberKey, StringComparison.Ordinal));
if (hit is null)
{
detail = "type_not_found";
return false;
}
type = hit;
typeName = hit.Identifier.Text;
detail = "member";
return true;
}
detail = "need_M_TypeName";
return false;
}
private static bool IsIdent(string s)
{
if (string.IsNullOrWhiteSpace(s))
return false;
if (!(char.IsLetter(s[0]) || s[0] == '_'))
return false;
for (var i = 1; i < s.Length; i++)
{
if (!(char.IsLetterOrDigit(s[i]) || s[i] == '_'))
return false;
}
return true;
}
}