-
Notifications
You must be signed in to change notification settings - Fork 516
Expand file tree
/
Copy pathMatchResult.cs
More file actions
135 lines (122 loc) · 4.9 KB
/
MatchResult.cs
File metadata and controls
135 lines (122 loc) · 4.9 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
/*
* Copyright 2026 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Linq;
using OpenRewrite.Java;
namespace OpenRewrite.CSharp.Template;
/// <summary>
/// Result of a successful pattern match. Provides typed access to captured AST nodes.
/// </summary>
public sealed class MatchResult
{
private readonly Dictionary<string, object> _captures;
private readonly IReadOnlyDictionary<string, NullSafe>? _nullSafeCaptures;
internal MatchResult(Dictionary<string, object> captures,
IReadOnlyDictionary<string, NullSafe>? nullSafeCaptures = null)
{
_captures = captures;
_nullSafeCaptures = nullSafeCaptures;
}
/// <summary>
/// Create a <see cref="MatchResult"/> from manually-assembled capture values.
/// This allows recipe authors to use <see cref="CSharpTemplate.Apply"/> with
/// substitution values that come from imperative extraction rather than
/// <see cref="CSharpPattern.Match"/>.
/// </summary>
/// <example>
/// <code>
/// var values = MatchResult.Of(("reason", skipLiteral));
/// var result = template.Apply(cursor, values: values);
/// </code>
/// </example>
public static MatchResult Of(params (string name, J value)[] captures)
{
var dict = new Dictionary<string, object>(captures.Length);
foreach (var (name, value) in captures)
{
dict[name] = value;
}
return new MatchResult(dict);
}
/// <summary>
/// Create a <see cref="MatchResult"/> from <see cref="ICapture"/> keys.
/// This allows using unnamed captures (with auto-generated names) as keys,
/// avoiding the need to manually synchronize string names between template
/// creation and value binding.
/// </summary>
/// <example>
/// <code>
/// var left = Capture.Expression();
/// var right = Capture.Expression();
/// var template = CSharpTemplate.Expression($"{left} && {right}");
/// var values = MatchResult.Of((left, outerCond), (right, innerCond));
/// var result = template.Apply(cursor, values: values);
/// </code>
/// </example>
public static MatchResult Of(params (ICapture capture, J value)[] captures)
{
var dict = new Dictionary<string, object>(captures.Length);
foreach (var (capture, value) in captures)
{
dict[capture.Name] = value;
}
return new MatchResult(dict);
}
/// <summary>
/// Get a captured value by name.
/// </summary>
public T? Get<T>(string name) where T : class, J
=> _captures.TryGetValue(name, out var value) ? value as T : default;
/// <summary>
/// Get a captured value by its <see cref="Capture{T}"/> object.
/// </summary>
public T? Get<T>(Capture<T> capture) where T : class, J
=> Get<T>(capture.Name);
/// <summary>
/// Get a captured value by its <see cref="ICapture"/> object.
/// Useful when the capture's type parameter doesn't match the desired return type.
/// </summary>
public T? Get<T>(ICapture capture) where T : class, J
=> Get<T>(capture.Name);
/// <summary>
/// Get a variadic capture result as a list.
/// Handles both IReadOnlyList<T> and IReadOnlyList<object> (from pattern matcher).
/// </summary>
public IReadOnlyList<T> GetList<T>(string name) where T : class, J
{
if (!_captures.TryGetValue(name, out var value))
return [];
if (value is IReadOnlyList<T> typedList)
return typedList;
if (value is IReadOnlyList<object> objectList)
return objectList.Cast<T>().ToList();
return [];
}
/// <summary>
/// Check if a capture with the given name was bound.
/// </summary>
public bool Has(string name) => _captures.ContainsKey(name);
/// <summary>
/// Check if a capture was bound.
/// </summary>
public bool Has(ICapture capture) => _captures.ContainsKey(capture.Name);
/// <summary>
/// Get the NullSafe marker associated with a capture, if any.
/// Present when the capture was the Select of a null-conditional MI/FA in the matched tree.
/// </summary>
internal NullSafe? GetNullSafe(string name) =>
_nullSafeCaptures != null && _nullSafeCaptures.TryGetValue(name, out var ns) ? ns : null;
internal Dictionary<string, object> AsDict() => _captures;
}