checkDuplicates function

List<KeybindingWarning> checkDuplicates(
  1. List<KeybindingBlock> blocks
)

Check for duplicate bindings within the same context.

Implementation

List<KeybindingWarning> checkDuplicates(List<KeybindingBlock> blocks) {
  final warnings = <KeybindingWarning>[];
  final seenByContext = <String, Map<String, String>>{};

  for (final block in blocks) {
    final contextMap = seenByContext.putIfAbsent(block.context, () => {});
    for (final entry in block.bindings.entries) {
      final normalizedKey = normalizeKeyForComparison(entry.key);
      final existingAction = contextMap[normalizedKey];
      if (existingAction != null && existingAction != (entry.value ?? 'null')) {
        warnings.add(
          KeybindingWarning(
            type: KeybindingWarningType.duplicate,
            severity: ReservedSeverity.warning,
            message:
                'Duplicate binding "${entry.key}" in ${block.context} context',
            key: entry.key,
            context: block.context,
            action: entry.value ?? 'null (unbind)',
            suggestion:
                'Previously bound to "$existingAction". Only the last binding will be used.',
          ),
        );
      }
      contextMap[normalizedKey] = entry.value ?? 'null';
    }
  }

  return warnings;
}