execute method
Execute the tool with the given input.
Implementation
@override
Future<ToolResult> execute(Map<String, dynamic> input) async {
final pattern = input['pattern'] as String?;
if (pattern == null || pattern.isEmpty) {
return ToolResult.error('Missing required parameter: pattern');
}
final searchPath = input['path'] as String?;
final glob = input['glob'] as String?;
final type = input['type'] as String?;
final outputMode = input['output_mode'] as String? ?? 'files_with_matches';
final contextBefore = _toInt(input['-B']);
final contextAfter = _toInt(input['-A']);
final contextC = _toInt(input['-C']);
final context = _toInt(input['context']);
final showLineNumbers = input['-n'] as bool? ?? true;
final caseInsensitive = input['-i'] as bool? ?? false;
final headLimit = _toInt(input['head_limit']);
final offset = _toInt(input['offset']) ?? 0;
final multiline = input['multiline'] as bool? ?? false;
final absolutePath = searchPath != null && searchPath.isNotEmpty
? expandPath(searchPath)
: Directory.current.path;
try {
// Build ripgrep arguments
final args = buildRipgrepArgs(
pattern: pattern,
glob: glob,
type: type,
outputMode: outputMode,
contextBefore: contextBefore,
contextAfter: contextAfter,
contextC: contextC,
context: context,
showLineNumbers: showLineNumbers,
caseInsensitive: caseInsensitive,
multiline: multiline,
);
// Run ripgrep
final results = await _runRipgrep(args, absolutePath);
// Route to appropriate output mode handler
switch (outputMode) {
case 'content':
return _handleContentMode(results, headLimit, offset);
case 'count':
return _handleCountMode(results, headLimit, offset);
default:
return await _handleFilesWithMatchesMode(results, headLimit, offset);
}
} on RipgrepTimeoutException catch (e) {
return ToolResult.error(
'Search timed out after ${e.timeoutMs}ms. Try narrowing your '
'search with a more specific pattern or path.',
);
} catch (e) {
return ToolResult.error('Search error: $e');
}
}