MCPcopy Create free account
hub / github.com/ChrisFeldmeier/OpenCodeRust / parse_bash_command

Function parse_bash_command

crates/opencode-tool/src/bash.rs:338–376  ·  view source on GitHub ↗
(command: &str)

Source from the content-addressed store, hash-verified

336];
337
338fn parse_bash_command(command: &str) -> ParsedCommand {
339 let mut result = ParsedCommand {
340 patterns: HashSet::new(),
341 always: HashSet::new(),
342 directories: Vec::new(),
343 };
344
345 let mut parser = tree_sitter::Parser::new();
346 let language = tree_sitter_bash::LANGUAGE;
347 if parser.set_language(&language.into()).is_err() {
348 // Fallback: treat entire command as a single pattern
349 let tokens: Vec<String> = command.split_whitespace().map(String::from).collect();
350 result.patterns.insert(command.to_string());
351 let prefix = BashArity::prefix(&tokens);
352 result.always.insert(format!("{} *", prefix.join(" ")));
353 return result;
354 }
355
356 let Some(tree) = parser.parse(command, None) else {
357 let tokens: Vec<String> = command.split_whitespace().map(String::from).collect();
358 result.patterns.insert(command.to_string());
359 let prefix = BashArity::prefix(&tokens);
360 result.always.insert(format!("{} *", prefix.join(" ")));
361 return result;
362 };
363
364 let root = tree.root_node();
365 collect_commands(root, command.as_bytes(), &mut result);
366
367 // If tree-sitter found no commands (e.g. variable assignment only), use full command
368 if result.patterns.is_empty() && !command.trim().is_empty() {
369 let tokens: Vec<String> = command.split_whitespace().map(String::from).collect();
370 result.patterns.insert(command.to_string());
371 let prefix = BashArity::prefix(&tokens);
372 result.always.insert(format!("{} *", prefix.join(" ")));
373 }
374
375 result
376}
377
378fn collect_commands(node: tree_sitter::Node, source: &[u8], result: &mut ParsedCommand) {
379 if node.kind() == "command" {

Callers 1

executeMethod · 0.85

Calls 4

newFunction · 0.85
collect_commandsFunction · 0.85
is_emptyMethod · 0.80
parseMethod · 0.45

Tested by

no test coverage detected