Files
pipelines/app/lib/features/search/domain/search_query.dart
T
alan 51140cd3b7 feat(app): add native omni-search screen over the search gateway
Pure request/parse/row logic in lib/features/search/domain, a
gateway-backed repository, and Material plus Cupertino bodies.
The header and rows share one scroll view, and the server's
truncation, scan and cap facts stay visible even when a bounded
scan returns no matches.

Tests cover the exact search envelope, token parsing, stale replies,
retry, truncation bounds and platform layouts. A Web-only fixture
and CDP probe support rendered verification; the screen is not yet
registered in the shell.
2026-10-21 16:41:30 +01:00

83 lines
3.0 KiB
Dart

import 'search_models.dart';
/// Wording for a server that cannot answer `search`: an explicit rejection
/// naming an unknown/unsupported command, or a reply without
/// `search_results`. Kept verbatim from the web client's AppViewModel copy.
const unsupportedSearchCopy =
'This server does not support search. Update the Pipelines server, then try again.';
/// Unquoted, whitespace-delimited `t:`/`type:` tokens; the last supported token
/// wins. Unknown tokens and quoted text stay literal, including their whitespace.
/// The selector is a fallback; explicitly changing it removes tokens in the view.
///
/// This is the same token grammar as `web/src/search.js`, so `"t:run"`,
/// `t:unknown`, `prefix"t:run"` and `\t:run` all remain searchable text.
ParsedSearchQuery parseSearchQuery(
String value, [
SearchKind selectedKind = SearchKind.all,
]) {
var kind = selectedKind;
var hasType = false;
final query = value.replaceAllMapped(_tokenPattern, (match) {
final token = match[0] ?? '';
final type = _typePattern.firstMatch(token);
if (type == null) return token;
final candidate = SearchKind.fromWire(type.group(1)!.toLowerCase());
if (candidate == SearchKind.all) return token;
kind = candidate;
hasType = true;
return '';
});
return ParsedSearchQuery(query, kind, hasType);
}
final _tokenPattern = RegExp(
r'''(?:[^\s"'\\]|\\[\s\S]|"(?:[^"\\]|\\[\s\S])*(?:"|$)|'(?:[^'\\]|\\[\s\S])*(?:'|$))+''',
);
final _typePattern = RegExp(r'^(?:t|type):([a-z]+)$', caseSensitive: false);
/// Text searches and valid kind-only browsing are sendable. Empty text with no
/// supported kind clears locally, without an all-kind scan.
bool searchQuerySendable(String query, SearchKind kind) =>
query.trim().isNotEmpty || kind != SearchKind.all;
/// The message for a failed search: the unsupported sentence for a missing
/// payload or an unknown-command rejection, otherwise the server's own text.
String searchFailureText(Object? error) {
if (error == null) return unsupportedSearchCopy;
final message = error.toString().trim();
if (message.isEmpty) return unsupportedSearchCopy;
if (_unsupportedCommand.hasMatch(message)) return unsupportedSearchCopy;
return message;
}
final _unsupportedCommand = RegExp(
r'\b(unknown|unsupported|unrecognised|unrecognized)\b[\s\S]*\bcommand\b',
caseSensitive: false,
);
/// One recorded edit being debounced.
class SearchEdit {
const SearchEdit(this.token, this.query);
final int token;
final String query;
}
/// Query sequencing: a burst of keystrokes collapses into one request, and a
/// reply is accepted only for the newest recorded edit, so a stale reply can
/// never overwrite a newer one. Recording an edit (a keystroke, or an empty
/// edit when the field clears) is enough to invalidate an in-flight reply.
class SearchSequencer {
int _newest = 0;
SearchEdit edit(String query) => SearchEdit(++_newest, query);
SearchEdit? begin(SearchEdit? edit) {
if (edit == null || edit.token != _newest) return null;
return edit;
}
bool accepts(int token) => token == _newest;
}