Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make paths displed on left nav bar if duplicated #1592

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 10 additions & 2 deletions extensions/scarb-doc/src/docs_generation/markdown/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::docs_generation::markdown::traits::TopLevelMarkdownDocItem;
use crate::docs_generation::markdown::BASE_HEADER_LEVEL;
use crate::docs_generation::TopLevelItems;

use super::traits::mark_items_if_duplicated_name;

pub fn generate_summary_file_content(top_level_items: &TopLevelItems) -> String {
let header = str::repeat("#", BASE_HEADER_LEVEL);

Expand Down Expand Up @@ -51,8 +53,14 @@ fn generate_markdown_list_summary_for_top_level_subitems<T: TopLevelMarkdownDocI
T::ITEMS_SUMMARY_FILENAME
)
.unwrap();
for item in subitems {
writeln!(&mut markdown, " {}", item.generate_markdown_list_item()).unwrap();
let marked_duplicated_items = mark_items_if_duplicated_name(subitems);
for (item, is_duplicated) in marked_duplicated_items {
writeln!(
&mut markdown,
" {}",
item.generate_markdown_list_item(is_duplicated)
)
.unwrap();
}
}

Expand Down
54 changes: 48 additions & 6 deletions extensions/scarb-doc/src/docs_generation/markdown/traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use itertools::Itertools;
use std::collections::HashMap;
use std::fmt::Write;

use crate::docs_generation::{DocItem, PrimitiveDocItem, TopLevelDocItem};
Expand All @@ -14,12 +15,27 @@ pub trait TopLevelMarkdownDocItem: MarkdownDocItem + TopLevelDocItem {
format!("{}.md", self.full_path().replace("::", "-"))
}

fn md_ref(&self) -> String {
format!("[{}](./{})", self.name(), self.filename())
fn parent_path(&self) -> String {
let mut path_tree_elements: Vec<_> = self.full_path().split("::").collect();
path_tree_elements.pop();
path_tree_elements.join("::")
}

fn generate_markdown_list_item(&self) -> String {
format!("- {}\n", self.md_ref())
fn md_ref(&self, display_parent_path: bool) -> String {
if display_parent_path {
format!(
"[{} ({})](./{})",
maciektr marked this conversation as resolved.
Show resolved Hide resolved
self.name(),
self.parent_path(),
maciektr marked this conversation as resolved.
Show resolved Hide resolved
self.filename(),
)
} else {
format!("[{}](./{})", self.name(), self.filename())
}
}

fn generate_markdown_list_item(&self, display_parent_path: bool) -> String {
format!("- {}\n", self.md_ref(display_parent_path))
}
}

Expand Down Expand Up @@ -153,6 +169,26 @@ impl MarkdownDocItem for Trait {
}
}

pub fn mark_items_if_duplicated_name<'a, T: TopLevelMarkdownDocItem + 'a>(
items: &'a [&'a T],
) -> Vec<(&&'a T, bool)> {
let mut names_counter = HashMap::<String, u32>::new();
for item in items {
*names_counter.entry(item.name().to_string()).or_insert(0) += 1;
}

items
.iter()
.map(|item| {
let is_duplicated = match names_counter.get(item.name()) {
Some(value) => *value > 1,
maciektr marked this conversation as resolved.
Show resolved Hide resolved
_ => false,
};
(item, is_duplicated)
})
.collect::<Vec<_>>()
}

pub fn generate_markdown_list_for_top_level_subitems<T: TopLevelMarkdownDocItem>(
subitems: &[&T],
header_level: usize,
Expand All @@ -163,8 +199,14 @@ pub fn generate_markdown_list_for_top_level_subitems<T: TopLevelMarkdownDocItem>
let header = str::repeat("#", header_level);

writeln!(&mut markdown, "{header} {}\n", T::HEADER).unwrap();
for item in subitems {
writeln!(&mut markdown, "{}", item.generate_markdown_list_item()).unwrap();
let marked_duplicated_items = mark_items_if_duplicated_name(subitems);
for (item, is_duplicated) in marked_duplicated_items {
writeln!(
&mut markdown,
"{}",
item.generate_markdown_list_item(is_duplicated)
)
.unwrap();
}
}

Expand Down
19 changes: 19 additions & 0 deletions extensions/scarb-doc/tests/data/duplicated_item_names/book.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[book]
authors = ["<unknown>"]
language = "en"
multilingual = false
src = "src"
title = "hello_world - Cairo"

[output.html]
no-section-label = true

[output.html.playground]
runnable = false

[output.html.fold]
enable = true
level = 0

[output.html.code.hidelines]
cairo = "#"
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Summary

- [Modules](./modules.md)

- [hello_world](./hello_world.md)

- [sub_module](./hello_world-sub_module.md)

- [Free functions](./free_functions.md)

- [main](./hello_world-main.md)

- [unique_function_name](./hello_world-unique_function_name.md)

- [duplicated_function_name (hello_world)](./hello_world-duplicated_function_name.md)

- [duplicated_function_name (hello_world::sub_module)](./hello_world-sub_module-duplicated_function_name.md)

- [Enums](./enums.md)

- [DuplicatedEnumName (hello_world)](./hello_world-DuplicatedEnumName.md)

- [DuplicatedEnumName (hello_world::sub_module)](./hello_world-sub_module-DuplicatedEnumName.md)

- [Traits](./traits.md)

- [DuplicatedTraitName (hello_world)](./hello_world-DuplicatedTraitName.md)

- [DuplicatedTraitName (hello_world::sub_module)](./hello_world-sub_module-DuplicatedTraitName.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Enums

- [DuplicatedEnumName (hello_world)](./hello_world-DuplicatedEnumName.md)

- [DuplicatedEnumName (hello_world::sub_module)](./hello_world-sub_module-DuplicatedEnumName.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Free functions

- [main](./hello_world-main.md)

- [unique_function_name](./hello_world-unique_function_name.md)

- [duplicated_function_name (hello_world)](./hello_world-duplicated_function_name.md)

- [duplicated_function_name (hello_world::sub_module)](./hello_world-sub_module-duplicated_function_name.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# DuplicatedEnumName

Fully qualified path: `hello_world::DuplicatedEnumName`

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# DuplicatedTraitName

Fully qualified path: `hello_world::DuplicatedTraitName`

```rust
trait DuplicatedTraitName
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# duplicated_function_name

Fully qualified path: `hello_world::duplicated_function_name`

```rust
fn duplicated_function_name()
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# main

Fully qualified path: `hello_world::main`

```rust
fn main()
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# DuplicatedEnumName

Fully qualified path: `hello_world::sub_module::DuplicatedEnumName`

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# DuplicatedTraitName

Fully qualified path: `hello_world::sub_module::DuplicatedTraitName`

```rust
trait DuplicatedTraitName
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# duplicated_function_name

Fully qualified path: `hello_world::sub_module::duplicated_function_name`

```rust
fn duplicated_function_name()
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# sub_module

Fully qualified path: `hello_world::sub_module`

## Free functions

- [duplicated_function_name](./hello_world-sub_module-duplicated_function_name.md)

## Enums

- [DuplicatedEnumName](./hello_world-sub_module-DuplicatedEnumName.md)

## Traits

- [DuplicatedTraitName](./hello_world-sub_module-DuplicatedTraitName.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# unique_function_name

Fully qualified path: `hello_world::unique_function_name`

```rust
fn unique_function_name()
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# hello_world

Fully qualified path: `hello_world`

## Modules

- [sub_module](./hello_world-sub_module.md)

## Free functions

- [main](./hello_world-main.md)

- [unique_function_name](./hello_world-unique_function_name.md)

- [duplicated_function_name](./hello_world-duplicated_function_name.md)

## Enums

- [DuplicatedEnumName](./hello_world-DuplicatedEnumName.md)

## Traits

- [DuplicatedTraitName](./hello_world-DuplicatedTraitName.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Modules

- [hello_world](./hello_world.md)

- [sub_module](./hello_world-sub_module.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Traits

- [DuplicatedTraitName (hello_world)](./hello_world-DuplicatedTraitName.md)

- [DuplicatedTraitName (hello_world::sub_module)](./hello_world-sub_module-DuplicatedTraitName.md)

73 changes: 73 additions & 0 deletions extensions/scarb-doc/tests/duplicated_names.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use assert_fs::TempDir;
use indoc::indoc;
use scarb_test_support::command::Scarb;
use scarb_test_support::project_builder::ProjectBuilder;

pub mod markdown_target;
use markdown_target::MarkdownTargetChecker;

#[test]
fn test_duplicated_items_names() {
let root_dir = TempDir::new().unwrap();

ProjectBuilder::start()
.name("hello_world")
.lib_cairo(indoc! {r#"
mod sub_module;

fn main() {
println!("hellow")
}

fn unique_function_name() {
// pass
}

fn duplicated_function_name() {
// pass
}

enum DuplicatedEnumName {
// pass
}

trait DuplicatedTraitName {
// pass
}
"#})
.src(
"src/sub_module.cairo",
indoc! {r#"
fn duplicated_function_name() {
// pass
}

enum DuplicatedEnumName {
// pass
}

trait DuplicatedTraitName {
// pass
}
"#},
)
.build(&root_dir);

Scarb::quick_snapbox()
.arg("doc")
.args(["--document-private-items"])
.current_dir(&root_dir)
.assert()
.success();

MarkdownTargetChecker::default()
.actual(
root_dir
.path()
.join("target/doc/hello_world")
.to_str()
.unwrap(),
)
.expected("tests/data/duplicated_item_names")
.assert_all_files_match();
}
Loading