suggestIds function

List<String> suggestIds(
  1. Iterable<String> candidates,
  2. String wanted, {
  3. int max = 3,
})

Closest glintIds to a stale or mistyped one, so a failed lookup names the likely replacement instead of sending the agent back to a full re-read. Same base name (before #hash) wins, then small edit distance, then prefix.

Implementation

List<String> suggestIds(Iterable<String> candidates, String wanted, {int max = 3}) {
  final want = wanted.trim();
  if (want.isEmpty) return const [];
  final wantBase = _base(want);
  final scored = <(int, String)>[];
  for (final c in candidates) {
    if (c == want) continue;
    final cBase = _base(c);
    int? score;
    if (cBase == wantBase) {
      score = 0;
    } else {
      final d = editDistance(cBase, wantBase);
      if (d <= 3) {
        score = 10 + d;
      } else if (cBase.startsWith(wantBase) || wantBase.startsWith(cBase)) {
        score = 20 + (cBase.length - wantBase.length).abs();
      }
    }
    if (score != null) scored.add((score, c));
  }
  scored.sort((a, b) {
    final s = a.$1.compareTo(b.$1);
    return s != 0 ? s : a.$2.compareTo(b.$2);
  });
  return [for (final e in scored.take(max)) e.$2];
}