From 1c7e7517231ab602dbbad4bcc7e3bd9ac0aa8fea Mon Sep 17 00:00:00 2001 From: Piotr Figiela <77412592+Draggu@users.noreply.github.com> Date: Wed, 21 Aug 2024 15:18:07 +0200 Subject: [PATCH] LS: Fix hover on definition fix #5826 commit-id:7654b189 --- crates/cairo-lang-language-server/src/lib.rs | 27 +++++++- .../tests/test_data/hover/basic.txt | 69 ++++++++++++++++--- .../tests/test_data/hover/partial.txt | 8 ++- .../tests/test_data/hover/starknet.txt | 8 ++- 4 files changed, 99 insertions(+), 13 deletions(-) diff --git a/crates/cairo-lang-language-server/src/lib.rs b/crates/cairo-lang-language-server/src/lib.rs index 9bc69f2180c..e2413b9b8dd 100644 --- a/crates/cairo-lang-language-server/src/lib.rs +++ b/crates/cairo-lang-language-server/src/lib.rs @@ -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, }; @@ -1018,7 +1019,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)] diff --git a/crates/cairo-lang-language-server/tests/test_data/hover/basic.txt b/crates/cairo-lang-language-server/tests/test_data/hover/basic.txt index 420fca4957f..6391dc1a0cd 100644 --- a/crates/cairo-lang-language-server/tests/test_data/hover/basic.txt +++ b/crates/cairo-lang-language-server/tests/test_data/hover/basic.txt @@ -264,15 +264,32 @@ Calculate the area of the rectangle. // = source context fn add_two(x: u32) -> u32 { x + 2 } // = highlight -No highlight information. +fn add_two(x: u32) -> u32 { x + 2 } // = popover +```cairo +hello +``` +```cairo +fn add_two(x: u32) -> u32 +``` +--- +`add_two` documentation. //! > hover #15 // = source context fn area(self: @Rectangle) -> u64; // = highlight -No highlight information. + fn area(self: @Rectangle) -> u64; // = popover +```cairo +hello::RectangleTrait +``` +```cairo +trait RectangleTrait +fn area(self: @Rectangle) -> u64 +``` +--- +Calculate the area of the rectangle. //! > hover #16 // = source context @@ -298,8 +315,16 @@ Rectangle struct. // = source context fn area(self: @Rectangle) -> u64 { // = highlight -No highlight information. + fn area(self: @Rectangle) -> u64 { // = popover +```cairo +hello +``` +```cairo +impl RectangleImpl of RectangleTrait +``` +--- +Implementing the `RectangleTrait` for the `Rectangle` struct. //! > hover #18 // = source context @@ -445,31 +470,57 @@ Width of the rectangle. // = source context enum Coin { // = highlight -No highlight information. +enum Coin { // = popover -No hover information. +```cairo +hello +``` +```cairo +enum Coin { + Penny, +} +``` //! > hover #27 // = source context Penny, // = highlight -No highlight information. + Penny, // = popover -No hover information. +```cairo +hello +``` +```cairo +enum Coin { + Penny, +} +``` //! > hover #28 // = source context fn value_in_cents(coin: Coin) -> felt252 { // = highlight -No highlight information. +fn value_in_cents(coin: Coin) -> felt252 { // = popover +```cairo +hello +``` +```cairo +fn value_in_cents(coin: Coin) -> felt252 +``` //! > hover #29 // = source context fn value_in_cents(coin: Coin) -> felt252 { // = highlight -No highlight information. +fn value_in_cents(coin: Coin) -> felt252 { // = popover +```cairo +hello +``` +```cairo +fn value_in_cents(coin: Coin) -> felt252 +``` //! > hover #30 // = source context diff --git a/crates/cairo-lang-language-server/tests/test_data/hover/partial.txt b/crates/cairo-lang-language-server/tests/test_data/hover/partial.txt index eab227148d2..c543e7abe63 100644 --- a/crates/cairo-lang-language-server/tests/test_data/hover/partial.txt +++ b/crates/cairo-lang-language-server/tests/test_data/hover/partial.txt @@ -52,8 +52,14 @@ let mut xyz: // = source context fn f(abc) -> felt252 { // = highlight -No highlight information. +fn f(abc) -> felt252 { // = popover +```cairo +hello +``` +```cairo +fn f(abc) -> felt252 +``` //! > hover #4 // = source context diff --git a/crates/cairo-lang-language-server/tests/test_data/hover/starknet.txt b/crates/cairo-lang-language-server/tests/test_data/hover/starknet.txt index 06d9b2cd2b9..fbcc0629610 100644 --- a/crates/cairo-lang-language-server/tests/test_data/hover/starknet.txt +++ b/crates/cairo-lang-language-server/tests/test_data/hover/starknet.txt @@ -66,8 +66,14 @@ pub fn contract_state_for_testing() -> ContractState // = source context fn constructor(ref self: ContractState, value_: u128) { // = highlight -No highlight information. + fn constructor(ref self: ContractState, value_: u128) { // = popover +```cairo +hello::Balance +``` +```cairo +fn constructor(ref self: ContractState, value_: u128) +``` //! > hover #2 // = source context