Skip to content

Commit

Permalink
Correct definition node of modules and test goto def on crate keyword
Browse files Browse the repository at this point in the history
commit-id:93c713c0
  • Loading branch information
mkaput committed Jan 16, 2025
1 parent ad46a22 commit 7d4527c
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 36 deletions.
52 changes: 31 additions & 21 deletions src/lang/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,22 +657,26 @@ fn resolved_generic_item_def(
db: &AnalysisDatabase,
item: ResolvedGenericItem,
) -> Option<SyntaxStablePtrId> {
let defs_db = db.upcast();
Some(match item {
ResolvedGenericItem::GenericConstant(item) => item.untyped_stable_ptr(defs_db),
ResolvedGenericItem::GenericConstant(item) => item.untyped_stable_ptr(db.upcast()),
ResolvedGenericItem::Module(module_id) => {
// Check if the module is an inline submodule.
if let ModuleId::Submodule(submodule_id) = module_id {
if let ast::MaybeModuleBody::Some(submodule_id) =
submodule_id.stable_ptr(defs_db).lookup(db.upcast()).body(db.upcast())
{
// Inline module.
return Some(submodule_id.stable_ptr().untyped());
match module_id {
ModuleId::CrateRoot(_) => {
// For crate root files (src/lib.cairo), the definition node is the file itself.
let module_file = db.module_main_file(module_id).ok()?;
let file_syntax = db.file_module_syntax(module_file).ok()?;
file_syntax.as_syntax_node().stable_ptr()
}
ModuleId::Submodule(submodule_id) => {
// For submodules, the definition node is the identifier in `mod <ident> .*`.
submodule_id
.stable_ptr(db.upcast())
.lookup(db.upcast())
.name(db.upcast())
.stable_ptr()
.untyped()
}
}
let module_file = db.module_main_file(module_id).ok()?;
let file_syntax = db.file_module_syntax(module_file).ok()?;
file_syntax.as_syntax_node().stable_ptr()
}
ResolvedGenericItem::GenericFunction(item) => {
let title = match item {
Expand All @@ -684,18 +688,24 @@ fn resolved_generic_item_def(
}
GenericFunctionId::Trait(id) => FunctionTitleId::Trait(id.trait_function(db)),
};
title.untyped_stable_ptr(defs_db)
title.untyped_stable_ptr(db.upcast())
}
ResolvedGenericItem::GenericType(generic_type) => {
generic_type.untyped_stable_ptr(db.upcast())
}
ResolvedGenericItem::GenericTypeAlias(type_alias) => {
type_alias.untyped_stable_ptr(db.upcast())
}
ResolvedGenericItem::GenericImplAlias(impl_alias) => {
impl_alias.untyped_stable_ptr(db.upcast())
}
ResolvedGenericItem::GenericType(generic_type) => generic_type.untyped_stable_ptr(defs_db),
ResolvedGenericItem::GenericTypeAlias(type_alias) => type_alias.untyped_stable_ptr(defs_db),
ResolvedGenericItem::GenericImplAlias(impl_alias) => impl_alias.untyped_stable_ptr(defs_db),
ResolvedGenericItem::Variant(variant) => variant.id.stable_ptr(defs_db).untyped(),
ResolvedGenericItem::Trait(trt) => trt.stable_ptr(defs_db).untyped(),
ResolvedGenericItem::Impl(imp) => imp.stable_ptr(defs_db).untyped(),
ResolvedGenericItem::Variant(variant) => variant.id.stable_ptr(db.upcast()).untyped(),
ResolvedGenericItem::Trait(trt) => trt.stable_ptr(db.upcast()).untyped(),
ResolvedGenericItem::Impl(imp) => imp.stable_ptr(db.upcast()).untyped(),
ResolvedGenericItem::TraitFunction(trait_function) => {
trait_function.stable_ptr(defs_db).untyped()
trait_function.stable_ptr(db.upcast()).untyped()
}
ResolvedGenericItem::Variable(var) => var.untyped_stable_ptr(defs_db),
ResolvedGenericItem::Variable(var) => var.untyped_stable_ptr(db.upcast()),
})
}

Expand Down
1 change: 1 addition & 0 deletions tests/e2e/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cairo_lang_test_utils::test_file_test!(
enum_variants: "enum_variants.txt",
inline_macros: "inline_macros.txt",
items: "items.txt",
kw_crate: "kw_crate.txt",
modules: "modules.txt",
struct_members: "struct_members.txt",
variables: "variables.txt",
Expand Down
47 changes: 47 additions & 0 deletions tests/test_data/goto/kw_crate.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! > Test goto definition on `crate` keyword in `use` statement.

//! > test_runner_name
test_goto_definition

//! > cairo_code
use cra<caret>te::foo::func;

mod foo {
pub fn func() {}
}

//! > Goto definition #0
use cra<caret>te::foo::func;
---
<sel>use crate::foo::func;

mod foo {
pub fn func() {}
}</sel>

//! > ==========================================================================

//! > Test goto definition on `crate` keyword used in expressions.

//! > test_runner_name
test_goto_definition

//! > cairo_code
fn main() {
let _ = cr<caret>ate::foo::func();
}

mod foo {
pub fn func() {}
}

//! > Goto definition #0
let _ = cr<caret>ate::foo::func();
---
<sel>fn main() {
let _ = crate::foo::func();
}

mod foo {
pub fn func() {}
}</sel>
15 changes: 2 additions & 13 deletions tests/test_data/goto/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,12 @@ mod module {
//! > Goto definition #0
modu<caret>le::bar::foo();
---
mod module <sel>{
// good
mod module {
// bad
}

mod bar { // good
fn foo() {}
}
}</sel>
mod <sel>module</sel> {

//! > Goto definition #1
module::ba<caret>r::foo();
---
mod bar <sel>{ // good
fn foo() {}
}</sel>
mod <sel>bar</sel> { // good

//! > ==========================================================================

Expand Down
9 changes: 7 additions & 2 deletions tests/test_data/hover/missing_module.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ No hover information.
// = source context
mod mis<caret>sing;
// = highlight
No highlight information.
mod <sel>missing</sel>;
// = popover
No hover information.
```cairo
hello
```
```cairo
mod missing
```

0 comments on commit 7d4527c

Please sign in to comment.