| 523 | |
| 524 | impl FunctionDeclaration { |
| 525 | fn insert_usage_import_statement( |
| 526 | &self, |
| 527 | meta: &ParserMetadata, |
| 528 | mut comment_text: String, |
| 529 | ) -> String { |
| 530 | if meta.doc_usage { |
| 531 | let lib_name = meta |
| 532 | .context |
| 533 | .path |
| 534 | .as_ref() |
| 535 | .map(Path::new) |
| 536 | .and_then(Path::file_name) |
| 537 | .and_then(OsStr::to_str) |
| 538 | .map(|s| s.trim_end_matches(".ab")) |
| 539 | .map(String::from) |
| 540 | .unwrap_or_default(); |
| 541 | let import_stmt = format!("import {{ {} }} from \"std/{}\"\n", self.name, lib_name); |
| 542 | |
| 543 | let usage_pattern = regex::Regex::new(r"#\s*Usage\s+```ab").unwrap(); |
| 544 | if usage_pattern.is_match(&comment_text) { |
| 545 | // Insert import statement after "# Usage" |
| 546 | return usage_pattern |
| 547 | .replace(&comment_text, |caps: ®ex::Captures| { |
| 548 | format!("{}\n{}", &caps[0], import_stmt) |
| 549 | }) |
| 550 | .to_string(); |
| 551 | } else { |
| 552 | comment_text += "```ab\n"; |
| 553 | comment_text += &import_stmt; |
| 554 | comment_text += "```"; |
| 555 | } |
| 556 | } |
| 557 | comment_text |
| 558 | } |
| 559 | } |