csharpdeeb25a2 | 1 | #nullable enable |
| 2 | |
| 3 | using System.Text.Json; |
| 4 | |
| 5 | namespace CascadeIDE.Models.Intercom; |
| 6 | |
| 7 | /// <summary>Синтаксический scope внутри члена (ADR 0128 §3): kind + indexInParent + optional parentMemberKey.</summary> |
| 8 | public sealed record AttachmentSyntaxScope( |
| 9 | string Kind, |
| 10 | int IndexInParent, |
| 11 | string? ParentMemberKey) |
| 12 | { |
| 13 | public static bool TryParse(JsonElement? element, out AttachmentSyntaxScope? scope) |
| 14 | { |
| 15 | scope = null; |
| 16 | if (element is not { } el || el.ValueKind != JsonValueKind.Object) |
| 17 | return false; |
| 18 | |
| 19 | var kind = readString(el, "kind"); |
| 20 | if (string.IsNullOrWhiteSpace(kind)) |
| 21 | return false; |
| 22 | |
| 23 | var index = 1; |
| 24 | if (el.TryGetProperty("indexInParent", out var idxEl) && idxEl.ValueKind == JsonValueKind.Number && idxEl.TryGetInt32(out var i)) |
| 25 | index = i; |
| 26 | else if (el.TryGetProperty("index_in_parent", out var idx2) && idx2.ValueKind == JsonValueKind.Number && idx2.TryGetInt32(out var i2)) |
| 27 | index = i2; |
| 28 | |
| 29 | if (index < 1) |
| 30 | index = 1; |
| 31 | |
| 32 | var parent = readString(el, "parentMemberKey") ?? readString(el, "parent_member_key"); |
| 33 | scope = new AttachmentSyntaxScope(kind.Trim(), index, string.IsNullOrWhiteSpace(parent) ? null : parent.Trim()); |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | private static string? readString(JsonElement obj, string name) => |
| 38 | obj.TryGetProperty(name, out var el) && el.ValueKind == JsonValueKind.String ? el.GetString() : null; |
| 39 | } |
| 40 | |
View only · write via MCP/CIDE