Skip to content

Commit

Permalink
Adds function returning only fn signature in function code blocks (F…
Browse files Browse the repository at this point in the history
…uelLabs#3545)

This PR adds a function `trim_fn_body()` that returns only the function
signature for documentable function code blocks.

Result:
![Screen Shot 2022-12-08 at 1 25 46
PM](https://user-images.githubusercontent.com/57543709/206549104-1b5b1e79-d573-422b-9bb2-eaa1af6bfa37.png)

Closes FuelLabs#3540

Co-authored-by: Mohammad Fawaz <[email protected]>
  • Loading branch information
eureka-cpu and mohammadfawaz authored Dec 9, 2022
1 parent 8101a64 commit edd77fb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions forc-plugins/forc-doc/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ pub(crate) enum Descriptor {

impl Descriptor {
pub(crate) fn from_typed_decl(
d: &TyDeclaration,
ty_decl: &TyDeclaration,
module_prefix: Vec<String>,
document_private_items: bool,
) -> Result<Self> {
use TyDeclaration::*;
match d {
match ty_decl {
StructDeclaration(ref decl_id) => {
let struct_decl = de_get_struct(decl_id.clone(), &decl_id.span())?;
if !document_private_items && struct_decl.visibility.is_private() {
Expand Down
9 changes: 8 additions & 1 deletion forc-plugins/forc-doc/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,13 @@ fn attrsmap_to_html_string(attributes: &AttributesMap) -> String {
options.parse.default_info_string = Some("sway".into());
markdown_to_html(&format_docs(&docs), &options)
}
/// Takes a formatted String fn and returns only the function signature.
fn trim_fn_body(f: String) -> String {
match f.find('{') {
Some(index) => f.split_at(index).0.to_string(),
None => f,
}
}

trait Renderable {
fn render(&self, module: String, module_depth: usize, decl_ty: String) -> Box<dyn RenderBox>;
Expand Down Expand Up @@ -615,7 +622,7 @@ impl Renderable for TyFunctionDeclaration {
visibility: _,
} = &self;
let name = name.as_str().to_string();
let code_str = parse::parse_format::<sway_ast::ItemFn>(span.as_str());
let code_str = trim_fn_body(parse::parse_format::<sway_ast::ItemFn>(span.as_str()));
let function_attributes = attrsmap_to_html_string(attributes);
box_html! {
: html_head(module_depth, module.clone(), decl_ty.clone(), name.clone());
Expand Down

0 comments on commit edd77fb

Please sign in to comment.