Skip to content

Commit

Permalink
Merge branch 'main' into diagnostics/diagnostics-without-lowering-group
Browse files Browse the repository at this point in the history
  • Loading branch information
wawel37 authored Oct 2, 2024
2 parents 3b46ff3 + 323ea7e commit 79c42fb
Show file tree
Hide file tree
Showing 8 changed files with 414 additions and 108 deletions.
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
95 changes: 78 additions & 17 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 @@ -63,44 +71,97 @@ enum Answer {
No
}

//! > Item #1
//! > Item signature #1

//! > Item documentation #1
This comment refers to the crate.

//! > Item #2
//! > Item signature #2
fn main()

//! > Item documentation #2
Main function comment outside. Main function comment inside.

//! > Item #3
//! > Item signature #3
trait TraitTest

//! > Item documentation #3
Trait containing abc function.

//! > Item #4
//! > Item signature #4
fn abc() -> u32

//! > Item documentation #4
abc function returning u32. Default impl of abc TraitTest function. Default impl of abc TraitTest function inner comment.

//! > Item #5
//! > Item signature #5
impl TraitTestImpl of TraitTest

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

//! > Item #6
//! > Item signature #6
fn abc() -> u32

//! > Item documentation #6
Default impl of abc TraitTest function. Default impl of abc TraitTest function inner comment.

//! > Item #7
//! > Item signature #7

//! > Item documentation #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
//! > Item signature #8
pub fn inner_test_module_function() -> ()

//! > Item documentation #8
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.
//! > Item signature #9
#[derive(Drop)]
#[derive(Copy)]
struct Point {
pub x: u32,
y: u32
}

//! > Item #10
//! > Item documentation #9
Point struct representing a point in a 2d space. Example usage:
```cairo
fn new_Point() {
Point {x: 12, y: 14}
}
```

//! > Item signature #10
pub x: u32

//! > Item documentation #10
X coordinate.

//! > Item #11
//! > Item signature #11
y: u32

//! > Item documentation #11
Y coordinate.

//! > Item #12
//! > Item signature #12
enum Answer {
Yes,
No
}

//! > Item documentation #12
Answer Enum representing an answer to a yes/no question.

//! > Item #13
//! > Item signature #13
Yes

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

//! > Item #14
//! > Item signature #14
No

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

//! > Item #1
//! > Item signature #1

//! > Item documentation #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
//! > Item signature #2

//! > Item documentation #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
//! > Item signature #3

//! > Item documentation #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
//! > Item signature #4
fn hello()

//! > Item documentation #4
Hello function inside the inner module.

//! > Item #5
//! > Item signature #5
fn main()

//! > Item documentation #5
Main function. Empty code example.
```rust
```
Loading

0 comments on commit 79c42fb

Please sign in to comment.