MCPcopy Create free account
hub / github.com/amber-lang/amber / insert_usage_import_statement

Method insert_usage_import_statement

src/modules/function/declaration.rs:525–558  ·  view source on GitHub ↗
(
        &self,
        meta: &ParserMetadata,
        mut comment_text: String,
    )

Source from the content-addressed store, hash-verified

523
524impl 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: &regex::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}

Callers 1

documentMethod · 0.80

Calls 1

to_stringMethod · 0.45

Tested by

no test coverage detected