| 1 | using CascadeIDE.Services; |
| 2 | |
| 3 | namespace CascadeIDE.Models; |
| 4 | |
| 5 | /// <summary>Подписи положительной/отрицательной ветви IF на графе control-flow (ADR 0053).</summary> |
| 6 | public static class CodeNavigationMapConditionBranchLabels |
| 7 | { |
| 8 | public const string PresetPlusMinus = "plus_minus"; |
| 9 | public const string PresetTrueFalse = "true_false"; |
| 10 | public const string PresetOneZero = "one_zero"; |
| 11 | public const string PresetCustom = "custom"; |
| 12 | |
| 13 | public sealed record Pair(string Positive, string Negative); |
| 14 | |
| 15 | public static Pair Resolve(CodeNavigationMapSettings? settings, string? solutionPath = null) |
| 16 | { |
| 17 | var map = settings ?? new CodeNavigationMapSettings(); |
| 18 | var presetId = NormalizePresetId(map.ConditionBranchLabelPreset); |
| 19 | if (presetId == PresetCustom) |
| 20 | { |
| 21 | return new Pair( |
| 22 | Sanitize(map.ConditionBranchPositive, "+"), |
| 23 | Sanitize(map.ConditionBranchNegative, "-")); |
| 24 | } |
| 25 | |
| 26 | var merged = CodeNavigationMapConditionBranchPresetsLoader.GetEffectivePresets(map, solutionPath); |
| 27 | foreach (var entry in merged) |
| 28 | { |
| 29 | if (!string.Equals(entry.Id, presetId, StringComparison.OrdinalIgnoreCase)) |
| 30 | continue; |
| 31 | return PairFromEntry(entry, fallbackPositive: "+", fallbackNegative: "-"); |
| 32 | } |
| 33 | |
| 34 | foreach (var entry in merged) |
| 35 | { |
| 36 | if (!string.Equals(entry.Id, PresetPlusMinus, StringComparison.OrdinalIgnoreCase)) |
| 37 | continue; |
| 38 | return PairFromEntry(entry, "+", "-"); |
| 39 | } |
| 40 | |
| 41 | return new Pair("+", "-"); |
| 42 | } |
| 43 | |
| 44 | public static string? ResolveDisplayLabel(string? edgeProvenance, Pair labels) |
| 45 | { |
| 46 | if (!CodeNavigationMapConditionBranchProvenance.TryParseDisplayPolarity(edgeProvenance, out var isTrue)) |
| 47 | return null; |
| 48 | return isTrue ? labels.Positive : labels.Negative; |
| 49 | } |
| 50 | |
| 51 | public static string NormalizePresetId(string? value) |
| 52 | { |
| 53 | var v = (value ?? "").Trim(); |
| 54 | if (v.Length == 0) |
| 55 | return PresetPlusMinus; |
| 56 | return v.ToLowerInvariant() switch |
| 57 | { |
| 58 | PresetTrueFalse or "true-false" => PresetTrueFalse, |
| 59 | PresetOneZero or "one-zero" or "01" => PresetOneZero, |
| 60 | PresetCustom => PresetCustom, |
| 61 | _ => v |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | private static Pair PairFromEntry( |
| 66 | CodeNavigationMapConditionBranchPresetEntry entry, |
| 67 | string fallbackPositive, |
| 68 | string fallbackNegative) => |
| 69 | new( |
| 70 | Sanitize(entry.Positive, fallbackPositive), |
| 71 | Sanitize(entry.Negative, fallbackNegative)); |
| 72 | |
| 73 | private static string Sanitize(string? text, string fallback) |
| 74 | { |
| 75 | var s = (text ?? "").Trim(); |
| 76 | return s.Length == 0 ? fallback : s.Length <= 8 ? s : s[..8]; |
| 77 | } |
| 78 | } |
| 79 | |