Skip to content

Commit

Permalink
LS: Fix hover on definition (#6255)
Browse files Browse the repository at this point in the history
  • Loading branch information
Draggu authored Aug 27, 2024
1 parent 2c70d5d commit 9322175
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 13 deletions.
27 changes: 25 additions & 2 deletions crates/cairo-lang-language-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ use cairo_lang_compiler::project::{setup_project, update_crate_roots_from_projec
use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::ids::{
FunctionTitleId, LanguageElementId, LookupItemId, MemberId, ModuleId, SubmoduleLongId,
TraitItemId,
};
use cairo_lang_diagnostics::Diagnostics;
use cairo_lang_diagnostics::{Diagnostics, ToOption};
use cairo_lang_filesystem::db::{
get_originating_location, AsFilesGroupMut, FilesGroup, FilesGroupEx, PrivRawFileContentQuery,
};
Expand Down Expand Up @@ -1045,7 +1046,29 @@ fn find_definition(
return Some((ResolvedItem::Concrete(item), stable_ptr));
}
}
None

// Skip variable definition, otherwise we would get parent ModuleItem for variable.
if db.first_ancestor_of_kind(identifier.as_syntax_node(), SyntaxKind::StatementLet).is_none() {
let item = match lookup_items.first().copied()? {
LookupItemId::ModuleItem(item) => {
ResolvedGenericItem::from_module_item(db, item).to_option()?
}
LookupItemId::TraitItem(trait_item) => {
if let TraitItemId::Function(trait_fn) = trait_item {
ResolvedGenericItem::TraitFunction(trait_fn)
} else {
ResolvedGenericItem::Trait(trait_item.trait_id(db))
}
}
LookupItemId::ImplItem(impl_item) => {
ResolvedGenericItem::Impl(impl_item.impl_def_id(db))
}
};

Some((ResolvedItem::Generic(item.clone()), resolved_generic_item_def(db, item)?))
} else {
None
}
}

#[tracing::instrument(level = "trace", skip_all)]
Expand Down
69 changes: 60 additions & 9 deletions crates/cairo-lang-language-server/tests/test_data/hover/basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,32 @@ Calculate the area of the rectangle.
// = source context
fn add_t<caret>wo(x: u32) -> u32 { x + 2 }
// = highlight
No highlight information.
fn <sel>add_two</sel>(x: u32) -> u32 { x + 2 }
// = popover
```cairo
hello
```
```cairo
fn add_two(x: u32) -> u32
```
---
`add_two` documentation.

//! > hover #15
// = source context
fn area(sel<caret>f: @Rectangle) -> u64;
// = highlight
No highlight information.
fn area(<sel>self</sel>: @Rectangle) -> u64;
// = popover
```cairo
hello::RectangleTrait
```
```cairo
trait RectangleTrait
fn area(self: @Rectangle) -> u64
```
---
Calculate the area of the rectangle.

//! > hover #16
// = source context
Expand All @@ -298,8 +315,16 @@ Rectangle struct.
// = source context
fn area(se<caret>lf: @Rectangle) -> u64 {
// = highlight
No highlight information.
fn area(<sel>self</sel>: @Rectangle) -> u64 {
// = popover
```cairo
hello
```
```cairo
impl RectangleImpl of RectangleTrait
```
---
Implementing the `RectangleTrait` for the `Rectangle` struct.

//! > hover #18
// = source context
Expand Down Expand Up @@ -445,31 +470,57 @@ Width of the rectangle.
// = source context
enum Co<caret>in {
// = highlight
No highlight information.
enum <sel>Coin</sel> {
// = popover
No hover information.
```cairo
hello
```
```cairo
enum Coin {
Penny,
}
```

//! > hover #27
// = source context
Pen<caret>ny,
// = highlight
No highlight information.
<sel>Penny</sel>,
// = popover
No hover information.
```cairo
hello
```
```cairo
enum Coin {
Penny,
}
```

//! > hover #28
// = source context
fn value_i<caret>n_cents(coin: Coin) -> felt252 {
// = highlight
No highlight information.
fn <sel>value_in_cents</sel>(coin: Coin) -> felt252 {
// = popover
```cairo
hello
```
```cairo
fn value_in_cents(coin: Coin) -> felt252
```

//! > hover #29
// = source context
fn value_in_cents(co<caret>in: Coin) -> felt252 {
// = highlight
No highlight information.
fn value_in_cents(<sel>coin</sel>: Coin) -> felt252 {
// = popover
```cairo
hello
```
```cairo
fn value_in_cents(coin: Coin) -> felt252
```

//! > hover #30
// = source context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ let mut xyz: <missing>
// = source context
fn f(ab<caret>c) -> felt252 {
// = highlight
No highlight information.
fn f(<sel>abc</sel>) -> felt252 {
// = popover
```cairo
hello
```
```cairo
fn f(abc) -> felt252
```

//! > hover #4
// = source context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,14 @@ pub fn contract_state_for_testing() -> ContractState
// = source context
fn constructor(ref se<caret>lf: ContractState, value_: u128) {
// = highlight
No highlight information.
fn constructor(ref <sel>self</sel>: ContractState, value_: u128) {
// = popover
```cairo
hello::Balance
```
```cairo
fn constructor(ref self: ContractState, value_: u128)
```

//! > hover #2
// = source context
Expand Down

0 comments on commit 9322175

Please sign in to comment.