Skip to content

Commit

Permalink
members of structs
Browse files Browse the repository at this point in the history
  • Loading branch information
piotmag769 committed Jun 27, 2024
1 parent 275f797 commit 7dc6501
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ zip = { version = "0.6", default-features = false, features = ["deflate"] }
zstd = "0.13"

[profile.release]
lto = true
#lto = true

[profile.ci]
inherits = "test"
Expand Down
56 changes: 52 additions & 4 deletions extensions/scarb-doc/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@

use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::diagnostic_utils::StableLocation;
use cairo_lang_defs::ids::{
ConstantId, EnumId, ExternFunctionId, ExternTypeId, FreeFunctionId, ImplAliasId,
ImplConstantDefId, ImplDefId, ImplFunctionId, ImplItemId, ImplTypeDefId, LookupItemId,
MemberId, ModuleId, ModuleItemId, ModuleTypeAliasId, StructId, TopLevelLanguageElementId,
TraitConstantId, TraitFunctionId, TraitId, TraitItemId, TraitTypeId, UseId, VariantId,
ImplConstantDefId, ImplDefId, ImplFunctionId, ImplItemId, ImplTypeDefId, LanguageElementId,
LookupItemId, MemberId, ModuleId, ModuleItemId, ModuleTypeAliasId, StructId,
TopLevelLanguageElementId, TraitConstantId, TraitFunctionId, TraitId, TraitItemId, TraitTypeId,
UseId, VariantId,
};
use cairo_lang_filesystem::ids::CrateId;
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_syntax::node::{ast, TypedStablePtr, TypedSyntaxNode};
use itertools::Itertools;

#[derive(Clone, Debug)]
pub struct Crate {
Expand Down Expand Up @@ -264,6 +267,16 @@ pub struct Struct {
impl Struct {
pub fn new(db: &RootDatabase, id: StructId) -> Self {
let members = db.struct_members(id).unwrap();
let members_stable_locations = members
.iter()
.map(|(_, member)| member.id.stable_location(db))
.collect_vec();

let members_docs = members_stable_locations
.iter()
.map(|x| get_item_documentation(db, x))
.collect_vec();

let members = members
.iter()
.map(|(_name, semantic_member)| Member::new(db, semantic_member.id))
Expand All @@ -290,7 +303,7 @@ pub struct Member {

pub item_data: ItemData,
// TODO: no LookupItemId enum variant for struct / enum items
// pub lookup_item_data: LookupItemData,
pub lookup_item_data: LookupItemData,
}

impl Member {
Expand All @@ -300,6 +313,7 @@ impl Member {
id,
node,
item_data: ItemData::new(db, id, node),
lookup_item_data: LookupItemData {},
}
}
}
Expand Down Expand Up @@ -692,3 +706,37 @@ impl ExternFunction {
}
}
}

/// Gets the documentation above an item definition.
fn get_item_documentation(db: &dyn DefsGroup, stable_location: &StableLocation) -> Option<String> {
// Get the text of the item (trivia + definition)
let doc = stable_location.syntax_node(db).get_text(db.upcast());
// Only get the doc comments (start with `///` or `//!`) above the function.
let doc = doc
.lines()
.take_while_ref(|line| {
!line
.trim_start()
.chars()
.next()
.map_or(false, |c| c.is_alphabetic())
})
.filter_map(|line| {
// Remove indentation.
let dedent = line.trim_start();
// Check if this is a doc comment.
for prefix in ["///", "//!"] {
if let Some(content) = dedent.strip_prefix(prefix) {
// TODO(mkaput): The way how removing this indentation is performed is probably
// wrong. The code should probably learn how many spaces are used at the first
// line of comments block, and then remove the same amount of spaces in the
// block, instead of assuming just one space.
// Remove inner indentation if one exists.
return Some(content.strip_prefix(' ').unwrap_or(content));
}
}
None
})
.collect::<Vec<&str>>();
(!doc.is_empty()).then(|| doc.join("\n"))
}
7 changes: 7 additions & 0 deletions extensions/scarb-doc/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ fn test_workspace() {
fn hello_world() -> u32 {
1
}
/// Nice essa.
struct Essa {
/// Nice field.
field: u8,
}
"#})
.build(&hello);

Expand Down Expand Up @@ -208,6 +214,7 @@ fn test_workspace() {
.assert()
.success();
let stdout = std::str::from_utf8(&output.get_output().stdout).unwrap();

assert!(stdout.contains("Module: \"hello_world\""));
assert!(!stdout.contains("Module: \"goodbye_world\""));
}

0 comments on commit 7dc6501

Please sign in to comment.