Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 40 additions & 30 deletions tools/package-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,46 +410,56 @@ pub struct DocItem {
pub struct DocExtractor;

impl DocExtractor {
fn extract_symbol_name(line: &str) -> Option<String> {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() <= 1 {
return None;
}

let sym_name = parts[1]
.split('(')
.next()
.unwrap_or("")
.split('{')
.next()
.unwrap_or("")
.split('=')
.next()
.unwrap_or("")
.trim();

if sym_name.is_empty() {
None
} else {
Some(sym_name.to_string())
}
}

pub fn extract_comments(content: &str) -> Vec<DocItem> {
let mut items = Vec::new();
let mut active_doc = Vec::new();

for line in content.lines() {
let line = line.trim();

if let Some(doc) = line.strip_prefix("///") {
active_doc.push(doc.trim().to_string());
} else if line.starts_with("function")
|| line.starts_with("make")
|| line.starts_with("model")
|| line.starts_with("struct")
{
if !active_doc.is_empty() {
// Extract name of symbol following keyword
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() > 1 {
let raw_name = parts[1];
let sym_name = raw_name
.split('(')
.next()
.unwrap_or("")
.split('{')
.next()
.unwrap_or("")
.split('=')
.next()
.unwrap_or("")
.trim();

if !sym_name.is_empty() {
items.push(DocItem {
name: sym_name.to_string(),
doc: active_doc.join("\n"),
});
}
continue;
}

if !active_doc.is_empty() {
if line.starts_with("function")
|| line.starts_with("make")
|| line.starts_with("model")
|| line.starts_with("struct")
{
if let Some(name) = Self::extract_symbol_name(line) {
items.push(DocItem {
name,
doc: active_doc.join("\n"),
});
}
active_doc.clear();
}
} else {
active_doc.clear();
}
}
Expand Down
Loading