Skip to content

Commit

Permalink
LS: Fix hover on definition
Browse files Browse the repository at this point in the history
commit-id:7654b189
  • Loading branch information
Draggu committed Aug 23, 2024
1 parent aaad921 commit 590e171
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 12 deletions.
19 changes: 18 additions & 1 deletion crates/cairo-lang-language-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::ids::{
FunctionTitleId, LanguageElementId, LookupItemId, MemberId, ModuleId, SubmoduleLongId,
};
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 @@ -995,6 +995,23 @@ fn find_definition(
return Some((ResolvedItem::Concrete(item), stable_ptr));
}
}

for lookup_item_id in lookup_items.iter().rev().copied() {
if let LookupItemId::ModuleItem(item) = lookup_item_id {
let item = ResolvedGenericItem::from_module_item(db, item).to_option()?;

if db
.first_ancestor_of_kind(identifier.as_syntax_node(), SyntaxKind::StatementLet)
.is_none()
{
return Some((
ResolvedItem::Generic(item.clone()),
resolved_generic_item_def(db, item)?,
));
}
}
}

None
}

Expand Down
68 changes: 59 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,31 @@ 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
```
```cairo
trait RectangleTrait
```
---
Rectangle trait.

//! > hover #16
// = source context
Expand All @@ -298,8 +314,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 +469,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 590e171

Please sign in to comment.