Files
clide/test/builtin/editor/vim_editor_test.dart
T
jpmschweitzerandClaude Opus 4.8 caca816233 make the editor modal with Vim motions and edits
T-206. A pure motion/edit engine (vim_edit_ops.dart) operates on
(text, selection, register) and returns the new value plus an
insert-mode request — hjkl/w/b/e/0/^/$/gg/G motions, x/dd/D/dw/yy/p/P/
cc/cw/o/O edits, i/a/I/A insert entries, and d/y/c over a visual range.
It's headless, so the whole grammar is unit-tested in isolation.

The editor wires it in: in normal/visual mode bare keys feed the
SequenceMatcher (modified chords bubble to the global handler for the
palette etc.), a fired editor.vim.* intent applies the op count times
and persists through the existing edit path, and vim.mode.* intents go
to the registry. Crucially the EditableText is read-only in command
mode — on desktop printable keys arrive over the TextInput channel
separately from KeyEvents, so swallowing the key event alone wouldn't
stop them typing; read-only does, while our edits still drive the
controller directly.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-01 21:27:15 +02:00

123 lines
4.0 KiB
Dart

/// T-206: the editor's modal wiring. With a `vim.normal` scope flag set
/// and a sequence binding registered, a bare key drives the buffer (and
/// the editor goes read-only so the key can't also type); under no Vim
/// mode the editor types normally.
library;
import 'package:clide/builtin/editor/src/editor_view.dart';
import 'package:clide/clide.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import '../../helpers/kernel_fixture.dart';
import '../../helpers/widget_harness.dart';
IpcResponse _ok(Map<String, Object?> data) => IpcResponse.ok(id: '', data: data);
void main() {
late KernelFixture f;
setUp(() async => f = await KernelFixture.create());
tearDown(() => f.dispose());
void stubOneBuffer(String content) {
f.ipc.stub(
'editor.list',
(_) async => _ok({
'buffers': [
{'id': 'b_1', 'path': 'lib/a.dart', 'dirty': false}
]
}));
f.ipc.stub(
'editor.active',
(_) async => _ok({
'active': {'id': 'b_1'}
}));
f.ipc.stub(
'editor.read',
(_) async => _ok({
'id': 'b_1',
'path': 'lib/a.dart',
'content': content,
'selection': {'start': 0, 'end': 0},
'dirty': false,
}));
}
Future<void> pumpEditor(WidgetTester tester) async {
await tester.pumpWidget(harness(f, const EditorView()));
await tester.pumpAndSettle();
// Tap the very start so the caret lands at offset 0 (a centred tap
// would put it at end-of-text and make column-sensitive ops no-ops).
await tester.tapAt(tester.getTopLeft(find.byType(EditableText)) + const Offset(1, 1));
await tester.pump();
}
testWidgets('normal-mode x deletes the char under the caret', (tester) async {
String? sentText;
f.ipc.stub('editor.set-content', (a) async {
sentText = a['text'] as String?;
return _ok(const {});
});
f.services.keymap.registerCommandBinding('x', 'editor.vim.deleteChar', when: 'vim.normal');
f.services.keymap.setScopeFlag('vim.normal', true);
stubOneBuffer('hello');
await pumpEditor(tester);
await tester.sendKeyEvent(LogicalKeyboardKey.keyX);
await tester.pumpAndSettle();
expect(sentText, 'ello');
});
testWidgets('dd sequence deletes the line', (tester) async {
String? sentText;
f.ipc.stub('editor.set-content', (a) async {
sentText = a['text'] as String?;
return _ok(const {});
});
f.services.keymap.registerCommandBinding('d d', 'editor.vim.deleteLine', when: 'vim.normal');
f.services.keymap.setScopeFlag('vim.normal', true);
stubOneBuffer('one\ntwo');
await pumpEditor(tester);
await tester.sendKeyEvent(LogicalKeyboardKey.keyD);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.keyD);
await tester.pumpAndSettle();
expect(sentText, 'two');
});
testWidgets('the editor is read-only in normal mode, writable otherwise', (tester) async {
f.services.keymap.setScopeFlag('vim.normal', true);
stubOneBuffer('hello');
await pumpEditor(tester);
expect(tester.widget<EditableText>(find.byType(EditableText)).readOnly, isTrue);
f.services.keymap.setScopeFlag('vim.normal', false);
await tester.pumpAndSettle();
expect(tester.widget<EditableText>(find.byType(EditableText)).readOnly, isFalse);
});
testWidgets('an unbound bare key in normal mode is swallowed (no edit)', (tester) async {
String? lastText;
f.ipc.stub('editor.set-content', (a) async {
lastText = a['text'] as String?;
return _ok(const {});
});
f.services.keymap.setScopeFlag('vim.normal', true);
stubOneBuffer('hello');
await pumpEditor(tester);
await tester.sendKeyEvent(LogicalKeyboardKey.keyZ);
await tester.pumpAndSettle();
// The buffer text is never altered by an unbound normal-mode key.
expect(lastText, anyOf(isNull, 'hello'));
});
}