add workspace grep engine + search.grep/cancel, editor.open --line
The pure-Dart content-search engine behind find-in-files (D-79): walks the ignore-pruned workspace, fans files across worker isolates (Isolate.run) for parallelism, matches each line with a literal indexOf fast-path or a RegExp, and streams match batches with cooperative cancellation. No ripgrep dependency; the search.grep IPC contract is engine-agnostic so an rg accelerator can slot in later. search.grep returns a searchId and streams search.match / search.done (or search.error) events, mirroring files.watch; search.cancel stops an in-flight search. The service reuses the files service's resolved ignore set so both honour the same ignore_files: layering. editor.open gains an optional 1-based line argument: it converts the line to a byte offset and sets the initial selection, enabling click-to-line from search results. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -47,6 +47,28 @@ void main() {
|
||||
expect(act['path'], 'doc.md');
|
||||
});
|
||||
|
||||
test('editor.open with a 1-based line jumps the initial selection (T-52)', () async {
|
||||
await File('${sandbox.path}/multi.txt').writeAsString('one\ntwo\nthree\n');
|
||||
final r = await call('editor.open', {'path': 'multi.txt', 'line': 3});
|
||||
expect(r.ok, isTrue);
|
||||
final sel = r.data['selection'] as Map;
|
||||
// Line 3 starts after 'one\n' + 'two\n' = 8 characters.
|
||||
expect(sel['start'], 8);
|
||||
expect(sel['end'], 8);
|
||||
});
|
||||
|
||||
test('editor.open without a line opens at the top', () async {
|
||||
await File('${sandbox.path}/multi.txt').writeAsString('one\ntwo\n');
|
||||
final r = await call('editor.open', {'path': 'multi.txt'});
|
||||
expect((r.data['selection'] as Map)['start'], 0);
|
||||
});
|
||||
|
||||
test('editor.open with an out-of-range line clamps to the content end', () async {
|
||||
await File('${sandbox.path}/multi.txt').writeAsString('one\ntwo\n'); // 8 chars
|
||||
final r = await call('editor.open', {'path': 'multi.txt', 'line': 999});
|
||||
expect((r.data['selection'] as Map)['start'], 8);
|
||||
});
|
||||
|
||||
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 '});
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/// Tests for the `search.*` command handlers (T-52 / D-79).
|
||||
library;
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:clide/clide.dart';
|
||||
import 'package:clide/src/daemon/search_commands.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
late Directory dir;
|
||||
late RecordingEventSink sink;
|
||||
late DaemonDispatcher d;
|
||||
|
||||
setUp(() async {
|
||||
dir = await Directory.systemTemp.createTemp('clide-search-cmd-');
|
||||
File('${dir.path}/a.dart').writeAsStringSync('final answer = 42;\n');
|
||||
File('${dir.path}/b.dart').writeAsStringSync('// no hits here\n');
|
||||
sink = RecordingEventSink();
|
||||
final service = SearchService(
|
||||
root: dir,
|
||||
ignore: IgnoreSet([]),
|
||||
events: sink,
|
||||
useIsolates: false,
|
||||
);
|
||||
d = DaemonDispatcher();
|
||||
registerSearchCommands(d, service);
|
||||
});
|
||||
tearDown(() async => dir.delete(recursive: true));
|
||||
|
||||
Future<IpcResponse> call(String cmd, Map<String, Object?> args) => d.dispatch(IpcRequest(id: '1', cmd: cmd, args: args));
|
||||
|
||||
test('search.grep returns a searchId and streams match + done', () async {
|
||||
// Subscribe before dispatching: the result events are broadcast and
|
||||
// can fire before a post-call listener would attach.
|
||||
final doneFuture = sink.stream.firstWhere((e) => e.kind == 'search.done');
|
||||
final r = await call('search.grep', const {'pattern': 'answer'});
|
||||
expect(r.ok, isTrue);
|
||||
final id = r.data['searchId'] as String;
|
||||
|
||||
final done = await doneFuture;
|
||||
expect(done.data['searchId'], id);
|
||||
expect(done.data['cancelled'], isFalse);
|
||||
|
||||
final matches = sink.events.where((e) => e.kind == 'search.match').toList();
|
||||
expect(matches, isNotEmpty);
|
||||
final batch = (matches.first.data['matches'] as List).cast<Map>();
|
||||
expect(batch.first['path'], 'a.dart');
|
||||
expect(batch.first['line'], 1);
|
||||
});
|
||||
|
||||
test('search.grep with no matches still emits done', () async {
|
||||
final doneFuture = sink.stream.firstWhere((e) => e.kind == 'search.done');
|
||||
await call('search.grep', const {'pattern': 'zzz-not-present'});
|
||||
final done = await doneFuture;
|
||||
expect(done.data['cancelled'], isFalse);
|
||||
expect(sink.events.where((e) => e.kind == 'search.match'), isEmpty);
|
||||
});
|
||||
|
||||
test('empty pattern is a userError', () async {
|
||||
final r = await call('search.grep', const {'pattern': ''});
|
||||
expect(r.ok, isFalse);
|
||||
expect(r.error!.kind, IpcErrorKind.userError);
|
||||
});
|
||||
|
||||
test('invalid regex emits a search.error event', () async {
|
||||
final errFuture = sink.stream.firstWhere((e) => e.kind == 'search.error');
|
||||
final r = await call('search.grep', const {'pattern': '(unclosed', 'regex': true});
|
||||
expect(r.ok, isTrue); // the request is accepted; the error streams
|
||||
final err = await errFuture;
|
||||
expect(err.data['message'], contains('invalid regex'));
|
||||
});
|
||||
|
||||
test('search.cancel requires a searchId', () async {
|
||||
final r = await call('search.cancel', const {});
|
||||
expect(r.ok, isFalse);
|
||||
expect(r.error!.kind, IpcErrorKind.userError);
|
||||
});
|
||||
|
||||
test('search.cancel acks a (possibly finished) id', () async {
|
||||
final r = await call('search.cancel', const {'searchId': 'search-0'});
|
||||
expect(r.ok, isTrue);
|
||||
expect(r.data['cancelled'], 'search-0');
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user