Skip to content

Commit d09a4be

Browse files
committed
Improve overloaded function documentation generation
1 parent d819390 commit d09a4be

File tree

2 files changed

+24
-36
lines changed

2 files changed

+24
-36
lines changed

binding-generator/src/func.rs

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
229229
desc.cpp_name = ancestor.cpp_name(CppNameStyle::Reference).into();
230230
}
231231
if config.doc_comment {
232-
desc.doc_comment = ancestor.doc_comment_overloaded().into();
232+
desc.doc_comment = ancestor.doc_comment().into();
233233
}
234234
if config.arguments {
235235
desc.arguments = ancestor.arguments().into();
@@ -349,39 +349,6 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
349349
}
350350
}
351351

352-
/// Like [Self::doc_comment], but processes `@overload` and `@copybrief` directives if possible by copying the corresponding
353-
/// doccomment
354-
pub fn doc_comment_overloaded(&self) -> Cow<'_, str> {
355-
let mut out = self.doc_comment();
356-
match self {
357-
Func::Clang { gen_env, .. } => {
358-
let line = self.file_line_name().location.as_file().map_or(0, |(_, line)| line);
359-
const OVERLOAD: &str = "@overload";
360-
if let Some(idx) = out.find(OVERLOAD) {
361-
let rep = if let Some(copy) = gen_env.get_func_comment(line, self.cpp_name(CppNameStyle::Reference).as_ref()) {
362-
Owned(format!("{copy}\n\n## Overloaded parameters\n"))
363-
} else {
364-
"This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.".into()
365-
};
366-
out.to_mut().replace_range(idx..idx + OVERLOAD.len(), &rep);
367-
}
368-
static COPY_BRIEF: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"@copybrief\s+(\w+)").unwrap());
369-
out.to_mut().replace_in_place_regex_cb(&COPY_BRIEF, |comment, caps| {
370-
let copy_name = caps.get(1).map(|(s, e)| &comment[s..e]).expect("Impossible");
371-
let mut copy_full_name = self.cpp_namespace().into_owned();
372-
copy_full_name.extend_sep("::", copy_name);
373-
if let Some(copy) = gen_env.get_func_comment(line, &copy_full_name) {
374-
Some(copy.into())
375-
} else {
376-
Some("".into())
377-
}
378-
});
379-
}
380-
Func::Desc(_) => {}
381-
}
382-
out
383-
}
384-
385352
pub fn return_kind(&self) -> ReturnKind {
386353
match self {
387354
Self::Clang { entity, gen_env, .. } => {
@@ -690,7 +657,28 @@ impl Element for Func<'_, '_> {
690657

691658
fn doc_comment(&self) -> Cow<'_, str> {
692659
match self {
693-
Self::Clang { entity, .. } => entity.doc_comment(),
660+
Self::Clang { entity, gen_env, .. } => {
661+
// Process `@overload` and `@copybrief` directives if possible by copying the corresponding doccomment
662+
let mut out = entity.doc_comment();
663+
let line = self.file_line_name().location.as_file().map_or(0, |(_, line)| line);
664+
const OVERLOAD: &str = "@overload";
665+
if let Some(idx) = out.find(OVERLOAD) {
666+
let rep = if let Some(copy) = gen_env.get_func_comment(line, self.cpp_name(CppNameStyle::Reference).as_ref()) {
667+
Owned(format!("{copy}\n\n## Overloaded parameters\n"))
668+
} else {
669+
"This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.".into()
670+
};
671+
out.to_mut().replace_range(idx..idx + OVERLOAD.len(), &rep);
672+
}
673+
static COPY_BRIEF: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"@copybrief\s+(\w+)").unwrap());
674+
out.to_mut().replace_in_place_regex_cb(&COPY_BRIEF, |comment, caps| {
675+
let copy_name = caps.get(1).map(|(s, e)| &comment[s..e]).expect("Impossible");
676+
let mut copy_full_name = self.cpp_namespace().into_owned();
677+
copy_full_name.extend_sep("::", copy_name);
678+
Some(gen_env.get_func_comment(line, &copy_full_name).unwrap_or("").into())
679+
});
680+
out
681+
}
694682
Self::Desc(desc) => desc.doc_comment.as_ref().into(),
695683
}
696684
}

binding-generator/src/writer/rust_native/func.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl RustElement for Func<'_, '_> {
195195
}
196196

197197
fn rust_doc_comment(&self, comment_marker: &str, opencv_version: &str) -> String {
198-
let mut comment = RenderComment::new(self.doc_comment_overloaded().into_owned(), opencv_version);
198+
let mut comment = RenderComment::new(self.doc_comment().into_owned(), opencv_version);
199199
let args = self.arguments();
200200
let (_, default_args) = split_default_args(&args);
201201
let default_args_comment = comment::render_cpp_default_args(default_args);

0 commit comments

Comments
 (0)