add editor subsystem — daemon state + editor.* IPC

EditorBuffer + Selection + EditorRegistry hold the daemon-side
active-file model (D-006 subsystem 'editor'). Active buffer
tracking means `clide insert "…"` and `clide replace-selection
"…"` target the UI's focused file without the caller supplying an
id. Mutations mark buffers dirty; editor.save writes back to disk
through the workspace root; events fire on every state change so
subscribers can mirror.

IPC surface matches CLAUDE.md's tier-2 list + the natural extras
(list, read, activate, set-selection, set-content, close). Tests
cover open-idempotence, insert at caret, replace-selection range
swap, dirty→save→clean round-trip, close picks a new active
buffer, out-of-range selection clamping.

69 core tests pass.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-04-22 10:10:42 +02:00
co-authored by Claude
parent 37108230f2
commit d32e8fdc17
8 changed files with 705 additions and 0 deletions
+110
View File
@@ -0,0 +1,110 @@
import 'dart:io';
import 'package:clide/clide.dart';
import 'package:clide/src/daemon/editor_commands.dart';
import 'package:clide/src/editor/registry.dart';
import 'package:test/test.dart';
void main() {
late Directory sandbox;
late DaemonDispatcher dispatcher;
late EditorRegistry reg;
setUp(() async {
sandbox = await Directory.systemTemp.createTemp('clide-ed-cmd-test-');
await File('${sandbox.path}/doc.md').writeAsString('alpha beta');
final sink = RecordingEventSink();
reg = EditorRegistry(events: sink, workspaceRoot: sandbox);
dispatcher = DaemonDispatcher();
registerEditorCommands(dispatcher, reg);
});
tearDown(() async {
await reg.shutdown();
if (sandbox.existsSync()) sandbox.deleteSync(recursive: true);
});
Future<IpcResponse> call(String cmd, [Map<String, Object?> args = const {}]) {
return dispatcher.dispatch(IpcRequest(id: '1', cmd: cmd, args: args));
}
test('editor.open requires a path', () async {
final r = await call('editor.open');
expect(r.ok, isFalse);
expect(r.error!.kind, 'user_error');
});
test('editor.open + editor.active round-trip', () async {
final open = await call('editor.open', {'path': 'doc.md'});
expect(open.ok, isTrue);
final id = open.data['id']! as String;
expect(id, startsWith('b_'));
final active = await call('editor.active');
expect(active.ok, isTrue);
final act = active.data['active']! as Map;
expect(act['id'], id);
expect(act['path'], 'doc.md');
});
test('editor.insert without id targets the active buffer', () async {
await call('editor.open', {'path': 'doc.md'});
final r = await call('editor.insert', {'text': 'X '});
expect(r.ok, isTrue);
expect(r.data['inserted'], 2);
final read = await call('editor.read');
expect((read.data['content'] as String).startsWith('X '), isTrue);
});
test('editor.replace-selection swaps selected range', () async {
final open = await call('editor.open', {'path': 'doc.md'});
final id = open.data['id'] as String?;
await call('editor.set-selection', {
'id': id,
'selection': {'start': 0, 'end': 5}, // 'alpha'
});
final r = await call('editor.replace-selection', {'text': 'gamma'});
expect(r.ok, isTrue);
final read = await call('editor.read');
expect(read.data['content'], 'gamma beta');
});
test('editor.save persists to disk', () async {
await call('editor.open', {'path': 'doc.md'});
await call('editor.insert', {'text': 'Z '});
final save = await call('editor.save');
expect(save.ok, isTrue);
final disk = await File('${sandbox.path}/doc.md').readAsString();
expect(disk.startsWith('Z '), isTrue);
});
test('editor.close removes the buffer', () async {
final open = await call('editor.open', {'path': 'doc.md'});
final id = open.data['id'] as String?;
final r = await call('editor.close', {'id': id});
expect(r.ok, isTrue);
final list = await call('editor.list');
expect((list.data['buffers'] as List), isEmpty);
});
test('editor.list includes all open buffers', () async {
await File('${sandbox.path}/a.md').writeAsString('a');
await File('${sandbox.path}/b.md').writeAsString('b');
await call('editor.open', {'path': 'a.md'});
await call('editor.open', {'path': 'b.md'});
final r = await call('editor.list');
final names = [
for (final b in (r.data['buffers'] as List).cast<Map>()) b['path'],
];
expect(names, containsAll(['a.md', 'b.md']));
});
test('insert on unknown id returns not-found', () async {
final r = await call('editor.insert', {'id': 'b_404', 'text': 'x'});
expect(r.ok, isFalse);
expect(r.error!.code, IpcExitCode.notFound);
});
}
+97
View File
@@ -0,0 +1,97 @@
import 'dart:io';
import 'package:clide/clide.dart';
import 'package:clide/src/editor/registry.dart';
import 'package:test/test.dart';
void main() {
late Directory sandbox;
late RecordingEventSink sink;
late EditorRegistry reg;
setUp(() async {
sandbox = await Directory.systemTemp.createTemp('clide-editor-test-');
await File('${sandbox.path}/README.md').writeAsString('# Hello\n\nbody\n');
sink = RecordingEventSink();
reg = EditorRegistry(events: sink, workspaceRoot: sandbox);
});
tearDown(() async {
await reg.shutdown();
if (sandbox.existsSync()) sandbox.deleteSync(recursive: true);
});
test('open loads file content + emits editor.opened + active-changed', () async {
final buf = await reg.open('README.md');
expect(buf.content, contains('Hello'));
expect(buf.dirty, isFalse);
expect(reg.active, same(buf));
expect(sink.ofKind('editor.opened'), hasLength(1));
expect(sink.ofKind('editor.active-changed'), hasLength(1));
});
test('opening the same path returns the existing buffer', () async {
final a = await reg.open('README.md');
final b = await reg.open('README.md');
expect(b.id, a.id);
// Still only one open event — re-open is an activate, not a reload.
expect(sink.ofKind('editor.opened'), hasLength(1));
});
test('insert at caret appends + advances cursor', () async {
final buf = await reg.open('README.md');
// caret at 0
reg.insert(buf.id, 'PREFIX ');
expect(buf.content.startsWith('PREFIX '), isTrue);
expect(buf.selection.isCollapsed, isTrue);
expect(buf.selection.start, 'PREFIX '.length);
expect(buf.dirty, isTrue);
expect(sink.ofKind('editor.edited'), hasLength(1));
});
test('replace-selection swaps selected text + resets cursor', () async {
final buf = await reg.open('README.md');
reg.setSelection(buf.id, const Selection(start: 2, end: 7)); // 'Hello'
reg.replaceSelection(buf.id, 'WORLD');
expect(buf.content.substring(2, 7), 'WORLD');
expect(buf.selection, const Selection(start: 7, end: 7));
});
test('set-selection clamps out-of-range offsets', () async {
final buf = await reg.open('README.md');
reg.setSelection(buf.id, const Selection(start: -5, end: 99999));
expect(buf.selection.start, 0);
expect(buf.selection.end, buf.content.length);
});
test('save writes the content back + clears dirty', () async {
final buf = await reg.open('README.md');
reg.insert(buf.id, 'X');
expect(buf.dirty, isTrue);
final ok = await reg.save(buf.id);
expect(ok, isTrue);
expect(buf.dirty, isFalse);
final onDisk = await File('${sandbox.path}/README.md').readAsString();
expect(onDisk.startsWith('X'), isTrue);
expect(sink.ofKind('editor.saved'), hasLength(1));
});
test('close picks a new active buffer when the active one closes',
() async {
final a = await reg.open('README.md');
await File('${sandbox.path}/b.txt').writeAsString('two');
final b = await reg.open('b.txt');
expect(reg.active, same(b));
reg.close(b.id);
expect(reg.active, same(a));
expect(sink.ofKind('editor.closed'), hasLength(1));
// Active changed at least twice: a→b (on open), b→a (after close)
expect(sink.ofKind('editor.active-changed').length, greaterThanOrEqualTo(2));
});
test('opening a non-existent path creates an empty buffer', () async {
final buf = await reg.open('NEW.md');
expect(buf.content, isEmpty);
expect(buf.dirty, isFalse);
});
}