buildRipgrepArgs function

List<String> buildRipgrepArgs({
  1. required String pattern,
  2. String? glob,
  3. String? type,
  4. String outputMode = 'files_with_matches',
  5. int? contextBefore,
  6. int? contextAfter,
  7. int? contextC,
  8. int? context,
  9. bool showLineNumbers = true,
  10. bool caseInsensitive = false,
  11. bool multiline = false,
})

Build ripgrep command-line arguments from tool input.

Implementation

List<String> buildRipgrepArgs({
  required String pattern,
  String? glob,
  String? type,
  String outputMode = 'files_with_matches',
  int? contextBefore,
  int? contextAfter,
  int? contextC,
  int? context,
  bool showLineNumbers = true,
  bool caseInsensitive = false,
  bool multiline = false,
}) {
  final args = <String>['--hidden'];

  // Exclude VCS directories to avoid noise
  for (final dir in vcsDirectoriesToExclude) {
    args.addAll(['--glob', '!$dir']);
  }

  // Limit line length to prevent base64/minified content from cluttering output
  args.addAll(['--max-columns', '500']);

  // Only apply multiline flags when explicitly requested
  if (multiline) {
    args.addAll(['-U', '--multiline-dotall']);
  }

  // Case insensitive
  if (caseInsensitive) {
    args.add('-i');
  }

  // Output mode flags
  if (outputMode == 'files_with_matches') {
    args.add('-l');
  } else if (outputMode == 'count') {
    args.add('-c');
  }

  // Line numbers for content mode
  if (showLineNumbers && outputMode == 'content') {
    args.add('-n');
  }

  // Context flags: -C/context takes precedence over -B/-A
  if (outputMode == 'content') {
    if (context != null) {
      args.addAll(['-C', context.toString()]);
    } else if (contextC != null) {
      args.addAll(['-C', contextC.toString()]);
    } else {
      if (contextBefore != null) {
        args.addAll(['-B', contextBefore.toString()]);
      }
      if (contextAfter != null) {
        args.addAll(['-A', contextAfter.toString()]);
      }
    }
  }

  // If pattern starts with dash, use -e flag to prevent ripgrep from
  // interpreting it as a command-line option
  if (pattern.startsWith('-')) {
    args.addAll(['-e', pattern]);
  } else {
    args.add(pattern);
  }

  // Type filter
  if (type != null && type.isNotEmpty) {
    args.addAll(['--type', type]);
  }

  // Glob patterns
  if (glob != null && glob.isNotEmpty) {
    final globPatterns = parseGlobPatterns(glob);
    for (final gp in globPatterns) {
      args.addAll(['--glob', gp]);
    }
  }

  return args;
}