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; }