keystroke mapper layer — intents, presets, when-clauses (T-117)

Build the upstream of every keyboard-driven feature: widgets bind
to typed Intents, the keymap resolves chord+context to an Intent,
and Flutter's Actions dispatches. The widget never touches a key.

Layers (low → high precedence):
  1. preset YAML in assets/keymaps/<preset>.yaml
  2. extension-registered command bindings (via
     KeymapService.registerCommandBinding from ExtensionManager)
  3. user file at <appDir>/keybindings.yaml
  4. settings JSON overlay at app.keymap.overrides

The when-clause grammar is a tiny recursive-descent parser over
boolean expressions on a named context bag — VS-Code style
`palette.open && !textInputFocused`. Producing services publish
scope flags via setScopeFlag.

Keys reference LogicalKeyboardKey.keyId (stable across keyboard
layouts), not the locale-aware keyLabel the consultant flagged.

Ships:
  - lib/kernel/src/keymap/{key_chord, when_clause, intents, keymap,
    keymap_service}.dart
  - assets/keymaps/default.yaml (the baseline preset)
  - 90+ unit tests covering parser precedence, layering precedence,
    scope evaluation, register/unregister, settings overlay,
    malformed-input tolerance
  - app.dart root handler routes through KeymapService → Actions
  - ExtensionManager mirrors every legacy defaultBinding into the
    keymap as a contribution layer

KeybindingResolver kept temporarily as a back-compat shim for
callers we haven't migrated yet; safe to delete once the last
caller goes through Actions.

Closes T-110 (consultant: scoped Shortcuts/Actions; off keyLabel).
Annotates T-23 with what's left for T-100. Unblocks T-64 / T-65 /
T-66 (preset data tickets).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-17 21:40:02 +02:00
co-authored by Claude Opus 4.7
parent a8729db893
commit 798ba524f1
18 changed files with 1944 additions and 47 deletions
+154
View File
@@ -0,0 +1,154 @@
/// Unit tests for KeyChord parsing, equality, canonicalisation, and
/// fromKeyEvent.
library;
import 'package:clide/kernel/src/keymap/key_chord.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('KeyChord.parse', () {
test('single key without modifiers', () {
final c = KeyChord.parse('enter');
expect(c.modifiers, isEmpty);
expect(c.key, LogicalKeyboardKey.enter);
expect(c.canonical, 'enter');
});
test('modifier order is canonicalised', () {
final a = KeyChord.parse('shift+ctrl+p');
final b = KeyChord.parse('ctrl+shift+p');
expect(a, b);
expect(a.canonical, 'ctrl+shift+p');
});
test('cmd/meta/command/super/win all alias the meta modifier', () {
for (final spec in ['cmd+a', 'meta+a', 'command+a', 'super+a', 'win+a']) {
expect(KeyChord.parse(spec).modifiers, [KeyModifier.meta], reason: spec);
}
});
test('punctuation keys are recognised by name or character', () {
expect(KeyChord.parse('ctrl+slash').key, LogicalKeyboardKey.slash);
expect(KeyChord.parse('ctrl+/').key, LogicalKeyboardKey.slash);
expect(KeyChord.parse('ctrl+equal').key, LogicalKeyboardKey.equal);
expect(KeyChord.parse('ctrl+=').key, LogicalKeyboardKey.equal);
});
test('parse is case-insensitive on modifiers + key name', () {
final c = KeyChord.parse('CTRL+SHIFT+P');
expect(c.modifiers, [KeyModifier.ctrl, KeyModifier.shift]);
expect(c.key, LogicalKeyboardKey.keyP);
});
});
group('KeyChord.parse — errors', () {
test('empty string throws', () {
expect(() => KeyChord.parse(''), throwsFormatException);
});
test('unknown modifier throws', () {
expect(() => KeyChord.parse('hyper+a'), throwsFormatException);
});
test('unknown key throws', () {
expect(() => KeyChord.parse('ctrl+definitely-not-a-key'), throwsFormatException);
});
test('trailing + (missing key) throws', () {
expect(() => KeyChord.parse('ctrl+'), throwsFormatException);
});
});
group('KeyChord display + canonical', () {
test('display capitalises modifiers + key, joined with +', () {
expect(KeyChord.parse('ctrl+shift+p').display, 'Ctrl+Shift+P');
expect(KeyChord.parse('enter').display, 'ENTER');
});
test('each modifier renders its own display string', () {
expect(KeyChord.parse('ctrl+a').display, 'Ctrl+A');
expect(KeyChord.parse('alt+a').display, 'Alt+A');
expect(KeyChord.parse('shift+a').display, 'Shift+A');
expect(KeyChord.parse('meta+a').display, 'Cmd+A');
});
test('toString embeds the canonical form', () {
expect(KeyChord.parse('ctrl+shift+p').toString(), 'KeyChord(ctrl+shift+p)');
});
});
group('KeyChord equality + hashing', () {
test('equal chords have equal hash codes', () {
final a = KeyChord.parse('ctrl+shift+p');
final b = KeyChord.parse('shift+ctrl+p');
expect(a, b);
expect(a.hashCode, b.hashCode);
});
test('different keys are not equal', () {
expect(KeyChord.parse('ctrl+a'), isNot(KeyChord.parse('ctrl+b')));
});
test('different modifier sets are not equal', () {
expect(KeyChord.parse('ctrl+a'), isNot(KeyChord.parse('alt+a')));
});
});
group('KeyChord.fromKeyEvent', () {
final kb = HardwareKeyboard.instance;
tearDown(() => kb.clearState());
test('returns null for non-KeyDown / non-Repeat events', () {
final up = KeyUpEvent(
physicalKey: PhysicalKeyboardKey.keyA,
logicalKey: LogicalKeyboardKey.keyA,
timeStamp: Duration.zero,
);
expect(KeyChord.fromKeyEvent(up, kb), isNull);
});
test('returns null for a bare modifier press', () {
final down = KeyDownEvent(
physicalKey: PhysicalKeyboardKey.controlLeft,
logicalKey: LogicalKeyboardKey.controlLeft,
timeStamp: Duration.zero,
);
expect(KeyChord.fromKeyEvent(down, kb), isNull);
});
test('maps a plain key down to a modifier-free chord', () {
final down = KeyDownEvent(
physicalKey: PhysicalKeyboardKey.keyA,
logicalKey: LogicalKeyboardKey.keyA,
timeStamp: Duration.zero,
);
final chord = KeyChord.fromKeyEvent(down, kb)!;
expect(chord.modifiers, isEmpty);
expect(chord.key, LogicalKeyboardKey.keyA);
});
test('records every held modifier in the resulting chord', () {
// Press all four modifiers, then a non-modifier key.
for (final m in [
(PhysicalKeyboardKey.controlLeft, LogicalKeyboardKey.controlLeft),
(PhysicalKeyboardKey.altLeft, LogicalKeyboardKey.altLeft),
(PhysicalKeyboardKey.shiftLeft, LogicalKeyboardKey.shiftLeft),
(PhysicalKeyboardKey.metaLeft, LogicalKeyboardKey.metaLeft),
]) {
kb.handleKeyEvent(KeyDownEvent(physicalKey: m.$1, logicalKey: m.$2, timeStamp: Duration.zero));
}
final down = KeyDownEvent(
physicalKey: PhysicalKeyboardKey.keyP,
logicalKey: LogicalKeyboardKey.keyP,
timeStamp: Duration.zero,
);
final chord = KeyChord.fromKeyEvent(down, kb)!;
expect(chord.key, LogicalKeyboardKey.keyP);
expect(chord.modifiers.toSet(), {KeyModifier.ctrl, KeyModifier.alt, KeyModifier.shift, KeyModifier.meta});
expect(chord.canonical, 'ctrl+alt+shift+meta+p');
});
});
}