Forge
csharp551a3611
1#nullable enable
2
3namespace RoslynMcp.ServiceLayer.WorkspaceNavigation;
4
5/// <summary>
6/// Фильтр по видам связей: <c>include_kinds</c> — белый список (если задан и непустой);
7/// <c>exclude_kinds</c> — вычитание. Неизвестные токены в списках игнорируются.
8/// </summary>
9public readonly struct WorkspaceNavigationKindFilter
10{
11 private readonly HashSet<string>? _include;
12 private readonly HashSet<string> _exclude;
13
14 private WorkspaceNavigationKindFilter(HashSet<string>? include, HashSet<string> exclude)
15 {
16 _include = include;
17 _exclude = exclude;
18 }
19
20 /// <summary><c>null</c> — без белого списка (все виды, кроме исключённых).</summary>
21 public IReadOnlyList<string>? EffectiveIncludeKinds =>
22 _include is null ? null : _include.OrderBy(x => x, StringComparer.Ordinal).ToList();
23
24 /// <summary>Канонические исключённые виды (может быть пустым).</summary>
25 public IReadOnlyList<string> EffectiveExcludeKinds =>
26 _exclude.Count == 0 ? Array.Empty<string>() : _exclude.OrderBy(x => x, StringComparer.Ordinal).ToList();
27
28 public static WorkspaceNavigationKindFilter Create(IReadOnlyList<string>? includeKinds, IReadOnlyList<string>? excludeKinds)
29 {
30 var exclude = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
31 if (excludeKinds is not null)
32 {
33 foreach (var t in excludeKinds)
34 {
35 var c = WorkspaceNavigationRelatedKinds.TryCanonicalKind(t);
36 if (c is not null)
37 exclude.Add(c);
38 }
39 }
40
41 HashSet<string>? include = null;
42 if (includeKinds is not null && includeKinds.Count > 0)
43 {
44 include = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
45 foreach (var t in includeKinds)
46 {
47 var c = WorkspaceNavigationRelatedKinds.TryCanonicalKind(t);
48 if (c is not null)
49 include.Add(c);
50 }
51
52 if (include.Count == 0)
53 include = null;
54 }
55
56 return new WorkspaceNavigationKindFilter(include, exclude);
57 }
58
59 public bool Allows(string kind)
60 {
61 if (string.IsNullOrEmpty(kind))
62 return false;
63 if (_include is not null && !_include.Contains(kind))
64 return false;
65 if (_exclude.Contains(kind))
66 return false;
67 return true;
68 }
69}
70
View only · write via MCP/CIDE