csharp4405de34 | 1 | using System.Text; |
| 2 | using System.Text.Json; |
| 3 | |
| 4 | namespace CascadeIDE.Services.Lsp; |
| 5 | |
| 6 | /// <summary>Парсинг <c>Hover.contents</c> из ответа LSP (<see cref="textDocument/hover"/>).</summary> |
| 7 | internal static class LspHoverContentFormatter |
| 8 | { |
| 9 | internal static string? Format(JsonElement contents) |
| 10 | { |
| 11 | return contents.ValueKind switch |
| 12 | { |
| 13 | JsonValueKind.String => contents.GetString(), |
| 14 | JsonValueKind.Array => FormatArray(contents), |
| 15 | JsonValueKind.Object => FormatObject(contents), |
| 16 | _ => null, |
| 17 | }; |
| 18 | } |
| 19 | |
| 20 | private static string? FormatArray(JsonElement arr) |
| 21 | { |
| 22 | var sb = new StringBuilder(); |
| 23 | foreach (var item in arr.EnumerateArray()) |
| 24 | { |
| 25 | var part = item.ValueKind switch |
| 26 | { |
| 27 | JsonValueKind.String => item.GetString(), |
| 28 | JsonValueKind.Object when item.TryGetProperty("value", out var v) => v.GetString(), |
| 29 | _ => null, |
| 30 | }; |
| 31 | if (!string.IsNullOrEmpty(part)) |
| 32 | { |
| 33 | if (sb.Length > 0) |
| 34 | sb.AppendLine(); |
| 35 | sb.Append(part); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | return sb.Length == 0 ? null : sb.ToString(); |
| 40 | } |
| 41 | |
| 42 | private static string? FormatObject(JsonElement obj) |
| 43 | { |
| 44 | if (obj.TryGetProperty("value", out var v)) |
| 45 | return v.GetString(); |
| 46 | return null; |
| 47 | } |
| 48 | } |
| 49 | |
View only · write via MCP/CIDE