Skip to content

Commit

Permalink
docs: Generate links for Class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelmoussaoui committed Apr 30, 2023
1 parent 073063e commit 04b66b8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/analysis/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum LocationInObject {
VirtualExt,
VirtualExtManual,
ClassExt,
ClassExtManual,
Ext,
ExtManual,
Builder,
Expand Down Expand Up @@ -95,6 +96,7 @@ impl Info {
/// Returns the location of the function within this object
pub fn function_location(&self, fn_info: &functions::Info) -> LocationInObject {
if fn_info.kind == FunctionKind::ClassMethod {
// TODO: Fix location here once we can auto generate virtual methods
LocationInObject::ClassExt
} else if fn_info.kind == FunctionKind::VirtualMethod {
// TODO: Fix location here once we can auto generate virtual methods
Expand Down Expand Up @@ -139,10 +141,13 @@ impl Info {
trait_name.into(),
)
}
LocationInObject::ClassExt => (
format!("subclass::prelude::{}", self.trait_name).into(),
self.trait_name.as_str().into(),
),
LocationInObject::ClassExt | LocationInObject::ClassExtManual => {
let trait_name = format!("{}SubclassExt", self.trait_name.trim_end_matches("Ext"));
(
format!("subclass::prelude::{}", trait_name).into(),
trait_name.into(),
)
}
LocationInObject::Builder => {
panic!("C documentation is not expected to link to builders (a Rust concept)!")
}
Expand Down
21 changes: 19 additions & 2 deletions src/codegen/doc/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ fn find_method_or_function_by_ctype(
|r| c_type.map_or(true, |t| r.type_(&env.library).c_type == t),
|r| c_type.map_or(true, |t| r.type_(&env.library).c_type == t),
c_type.map_or(false, |t| t.ends_with("Class")),
false,
)
}

Expand All @@ -418,8 +419,9 @@ pub(crate) fn find_method_or_function(
search_enum: impl Fn(&crate::analysis::enums::Info) -> bool + Copy,
search_flag: impl Fn(&crate::analysis::flags::Info) -> bool + Copy,
is_class_method: bool,
is_virtual_method: bool,
) -> Option<String> {
if is_class_method {
if is_virtual_method {
if let Some((obj_info, fn_info)) = env
.analysis
.find_object_by_virtual_method(env, search_obj, search_fn)
Expand All @@ -434,6 +436,21 @@ pub(crate) fn find_method_or_function(
} else {
None
}
} else if is_class_method {
if let Some((record_info, fn_info)) =
env.analysis
.find_record_by_function(env, search_record, search_fn)
{
let object = env.config.objects.get(&record_info.full_name);
let visible_parent = object
.and_then(|o| o.trait_name.clone())
.unwrap_or_else(|| format!("{}SubclassExt", record_info.name));
let parent = format!("subclass::prelude::{}", visible_parent);
let is_self = in_type == Some((&record_info.type_id, None));
Some(fn_info.doc_link(Some(&parent), Some(&visible_parent), is_self))
} else {
None
}
// if we can find the function in an object
} else if let Some((obj_info, fn_info)) = env
.analysis
Expand Down Expand Up @@ -509,7 +526,7 @@ pub(crate) fn gen_object_fn_doc_link(
let sym = symbols.by_tid(obj_info.type_id).unwrap();
let is_self = in_type == Some((&obj_info.type_id, Some(obj_info.function_location(fn_info))));

if fn_info.kind == FunctionKind::VirtualMethod {
if fn_info.kind == FunctionKind::VirtualMethod || fn_info.kind == FunctionKind::ClassMethod {
let (type_name, visible_type_name) = obj_info.generate_doc_link_info(fn_info);
fn_info.doc_link(
Some(&sym.full_rust_name().replace(visible_name, &type_name)),
Expand Down
2 changes: 2 additions & 0 deletions src/codegen/doc/gi_docgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ fn find_virtual_method(
|_| false,
|_| false,
|_| false,
false,
true,
)
}
Expand Down Expand Up @@ -263,6 +264,7 @@ fn find_method_or_function_by_name(
})
},
is_class_method,
false,
)
}

Expand Down

0 comments on commit 04b66b8

Please sign in to comment.