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

Doc/remove trivia from signatures #6393

Merged
79 changes: 61 additions & 18 deletions crates/cairo-lang-doc/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ fn get_item_signature(db: &dyn DocGroup, item_id: DocumentableItemId) -> String

let syntax_node = item_id.stable_location(db.upcast()).unwrap().syntax_node(db.upcast());
let definition = match syntax_node.green_node(db.upcast()).kind {
SyntaxKind::ItemConstant
| SyntaxKind::TraitItemFunction
| SyntaxKind::ItemTypeAlias
| SyntaxKind::ItemImplAlias => syntax_node.clone().get_text_without_trivia(db.upcast()),
SyntaxKind::FunctionWithBody | SyntaxKind::ItemExternFunction => {
SyntaxKind::ItemConstant | SyntaxKind::ItemTypeAlias | SyntaxKind::ItemImplAlias => {
syntax_node.clone().get_text_without_all_comment_trivia(db.upcast())
}
SyntaxKind::FunctionWithBody
| SyntaxKind::ItemExternFunction
| SyntaxKind::TraitItemFunction => {
let children = db.get_children(syntax_node);
children[1..]
.iter()
Expand All @@ -83,11 +84,14 @@ fn get_item_signature(db: &dyn DocGroup, item_id: DocumentableItemId) -> String
if kind == SyntaxKind::VisibilityPub
|| kind == SyntaxKind::TerminalExtern
{
node.clone().get_text_without_trivia(db.upcast()).trim().to_owned()
node.clone()
.get_text_without_all_comment_trivia(db.upcast())
.trim()
.to_owned()
+ " "
} else {
node.clone()
.get_text_without_trivia(db.upcast())
.get_text_without_all_comment_trivia(db.upcast())
.lines()
.map(|line| line.trim())
.collect::<Vec<&str>>()
Expand All @@ -98,13 +102,50 @@ fn get_item_signature(db: &dyn DocGroup, item_id: DocumentableItemId) -> String
.collect::<Vec<String>>()
.join("")
}
SyntaxKind::ItemEnum | SyntaxKind::ItemExternType | SyntaxKind::ItemStruct => db
.get_children(syntax_node)
.iter()
.skip(1)
.map(|node| node.clone().get_text(db.upcast()))
.collect::<Vec<String>>()
.join(""),
SyntaxKind::ItemEnum | SyntaxKind::ItemStruct => {
let children = db.get_children(syntax_node);

let item_content_children_without_trivia = db
.get_children(children[6].clone())
.iter()
.map(|node| node.clone().get_text_without_all_comment_trivia(db.upcast()))
.join("");

let [attributes, visibility, keyword, name, generic_types, left_brace, _, right_brace] =
&children
.iter()
.map(|node| node.clone().get_text_without_all_comment_trivia(db.upcast()))
.collect::<Vec<_>>()[..]
else {
return "".to_owned();
};

format!(
"{}\n{} {} {}{} {}\n {}{}",
attributes,
visibility,
keyword,
name,
generic_types,
left_brace,
item_content_children_without_trivia,
right_brace
)
}
SyntaxKind::ItemExternType => {
let [attributes, visibility, extern_keyword, keyword, name, generic_types, _] = &db
.get_children(syntax_node)
.iter()
.map(|node| node.clone().get_text_without_all_comment_trivia(db.upcast()))
.collect::<Vec<_>>()[..]
else {
return "".to_owned();
};
format!(
"{}\n{} {} {} {}{}",
attributes, visibility, extern_keyword, keyword, name, generic_types
)
}
SyntaxKind::ItemTrait | SyntaxKind::ItemImpl => {
let children = db.get_children(syntax_node);
children[1..]
Expand All @@ -115,7 +156,7 @@ fn get_item_signature(db: &dyn DocGroup, item_id: DocumentableItemId) -> String
if kind != SyntaxKind::ImplBody && kind != SyntaxKind::TraitBody {
let text = node
.clone()
.get_text_without_trivia(db.upcast())
.get_text_without_all_comment_trivia(db.upcast())
.lines()
.map(|line| line.trim())
.collect::<Vec<&str>>()
Expand All @@ -135,18 +176,20 @@ fn get_item_signature(db: &dyn DocGroup, item_id: DocumentableItemId) -> String
}
SyntaxKind::TraitItemConstant | SyntaxKind::TraitItemType => {
let children = db.get_children(syntax_node.clone());
let get_text = |node: &SyntaxNode| node.clone().get_text_without_trivia(db.upcast());
let get_text =
|node: &SyntaxNode| node.clone().get_text_without_all_comment_trivia(db.upcast());
format!("{} {}", get_text(&children[1]), children[2..].iter().map(get_text).join(""))
}
SyntaxKind::Member => {
let children_text = db
.get_children(syntax_node)
.iter()
.map(|node| node.clone().get_text_without_trivia(db.upcast()))
.map(|node| node.clone().get_text_without_all_comment_trivia(db.upcast()))
.collect::<Vec<String>>();
// Returning straight away as we don't want to format it.
return format!("{} {}", children_text[1], children_text[2..].join(""));
return children_text[1..].join("").trim().into();
}
SyntaxKind::Variant => syntax_node.get_text_without_all_comment_trivia(db.upcast()),
_ => "".to_owned(),
};
fmt(definition)
Expand Down
41 changes: 38 additions & 3 deletions crates/cairo-lang-doc/src/tests/test-data/basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,24 @@ impl TraitTestImpl of TraitTest {
pub mod test_module {
//! Test module used to check if the documentation is being attached to the nodes correctly.
/// Just a function outside the test_module.
pub fn inner_test_module_function() {
pub fn inner_test_module_function() -> () {
//! Just a function inside the test_module.
println!("inside inner test module inner function");
}
}

/// Point struct representing a point in a 2d space.
/// Example usage:
/// ```
/// fn new_Point() {
/// Point {x: 12, y: 14}
/// }
/// ```
#[derive(Drop)]
#[derive(Copy)]
struct Point {
/// X coordinate.
x: u32,
pub x: u32,
/// Y coordinate.
y: u32
}
Expand All @@ -64,43 +72,70 @@ enum Answer {
}

//! > Item #1

This comment refers to the crate.

//! > Item #2
fn main()
Main function comment outside. Main function comment inside.

//! > Item #3
trait TraitTest
Trait containing abc function.

//! > Item #4
fn abc() -> u32
abc function returning u32. Default impl of abc TraitTest function. Default impl of abc TraitTest function inner comment.

//! > Item #5
impl TraitTestImpl of TraitTest
Implementation of TraitTest's abc function.

//! > Item #6
fn abc() -> u32
Default impl of abc TraitTest function. Default impl of abc TraitTest function inner comment.

//! > Item #7

Test module used to check if the documentation is being attached to the nodes correctly. Test module used to check if the documentation is being attached to the nodes correctly.

//! > Item #8
pub fn inner_test_module_function() -> ()
Just a function outside the test_module. Just a function inside the test_module.

//! > Item #9
Point struct representing a point in a 2d space.
#[derive(Drop)]
#[derive(Copy)]
struct Point {
pub x: u32,
y: u32
}
Point struct representing a point in a 2d space. Example usage:
```cairo
fn new_Point() {
Point {x: 12, y: 14}
}
```

//! > Item #10
pub x: u32
X coordinate.

//! > Item #11
y: u32
Y coordinate.

//! > Item #12
enum Answer {
Yes,
No
}
Answer Enum representing an answer to a yes/no question.

//! > Item #13
Yes
Yes answer variant.

//! > Item #14
No
No answer variant.
5 changes: 5 additions & 0 deletions crates/cairo-lang-doc/src/tests/test-data/submodule.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,31 @@ mod inner_sub_module {
}

//! > Item #1

This is a testing crate file. It's for the tests purposes only.
```cairo
let a = 5;
```
This is also testing crate. After the code example. We don't take responsibility for compiling this file. So don't even try.

//! > Item #2

This is a submodule regarding the module_level_comments. It's used to make sure crate / module level comments are parsed in a correct way. Testing purposes only! This one is just a prefix comment for a module.
```rust
let a = String::from("This also works fine");
```
As mentioned above.

//! > Item #3

This comment just proves that it won't be considered as a file-module comment. It just refers to the inner_sub_module

//! > Item #4
fn hello()
Hello function inside the inner module.

//! > Item #5
fn main()
Main function. Empty code example.
```rust
```
106 changes: 106 additions & 0 deletions crates/cairo-lang-doc/src/tests/test-data/trivia.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//! > Documentation with a lot of trivia

//! > test_runner_name
documentation_test_runner

//! > cairo_project.toml
[crate_roots]
hello = "src"

//! > cairo_code
/// Const comment
const a: u32 = 123;

/// Test Pair
#[derive(Drop)]
#[derive(Clone)]
pub struct Pair<T, U> // T: Display + Debug,
// U: Display + Debug,
{
/// first field
first: T,
/// second field
second: U,
}
#[derive(Clone)] // Some comment3.
/// A second struct defining a pair.
#[derive(Drop)] // Some comment2.
/// As above.
struct Pair2<T, U> // T: Display + Debug,
// U: Display + Debug,
{
// This one is a little useless comment.
/// First field
pub first: T,
// Don't use the struct. The code is bad.
/// Second field
second: U,
}

pub enum TestEnum {
/// First variant
Var1,
/// Second variant
Var2
}

/// extern type comment
// yup
pub extern type S<T>;

//! > Item #1

//! > Item #2
const a: u32 = 123;
Const comment

//! > Item #3
#[derive(Drop)]
#[derive(Clone)]
pub struct Pair<T, U> {
first: T,
second: U,
}
Test Pair

//! > Item #4
first: T
first field

//! > Item #5
second: U
second field

//! > Item #6
#[derive(Clone)]
#[derive(Drop)]
struct Pair2<T, U> {
pub first: T,
second: U,
}

//! > Item #7
pub first: T
First field

//! > Item #8
second: U
Second field

//! > Item #9
pub enum TestEnum {
Var1,
Var2
}

//! > Item #10
Var1
First variant

//! > Item #11
Var2
Second variant

//! > Item #12
pub extern type S<T>
extern type comment
Loading