From 23eaea5b792e2882498cc1446b0c586b933c27c4 Mon Sep 17 00:00:00 2001 From: Marek Kaput Date: Tue, 18 Jun 2024 10:51:15 +0200 Subject: [PATCH] LS: Show path to item definition parent item in hovers commit-id:eaa21ef2 --- .../src/ide/hover/render/definition.rs | 4 ++- .../src/lang/inspect/defs.rs | 13 ++++++++- .../src/markdown.rs | 4 +-- .../tests/test_data/hover/basic.txt | 27 +++++++++++++++++++ .../tests/test_data/hover/starknet.txt | 18 +++++++++++++ 5 files changed, 62 insertions(+), 4 deletions(-) diff --git a/crates/cairo-lang-language-server/src/ide/hover/render/definition.rs b/crates/cairo-lang-language-server/src/ide/hover/render/definition.rs index fce1b9f9a99..a2e6dd3f768 100644 --- a/crates/cairo-lang-language-server/src/ide/hover/render/definition.rs +++ b/crates/cairo-lang-language-server/src/ide/hover/render/definition.rs @@ -22,7 +22,9 @@ pub fn definition( let md = match &symbol { SymbolDef::Item(item) => { // TODO(mkaput): Format this with Cairo formatter. - let mut md = Markdown::fenced_code_block(&item.signature(db)); + let mut md = Markdown::empty(); + md += Markdown::fenced_code_block(&item.definition_path(db)); + md += Markdown::fenced_code_block(&item.signature(db)); if let Some(doc) = item.documentation(db) { md += Markdown::rule(); md += doc; diff --git a/crates/cairo-lang-language-server/src/lang/inspect/defs.rs b/crates/cairo-lang-language-server/src/lang/inspect/defs.rs index 80f0342239a..d89affe823d 100644 --- a/crates/cairo-lang-language-server/src/lang/inspect/defs.rs +++ b/crates/cairo-lang-language-server/src/lang/inspect/defs.rs @@ -1,6 +1,6 @@ use cairo_lang_compiler::db::RootDatabase; use cairo_lang_defs::db::DefsGroup; -use cairo_lang_defs::ids::LookupItemId; +use cairo_lang_defs::ids::{LanguageElementId, LookupItemId, TopLevelLanguageElementId}; use cairo_lang_semantic::db::SemanticGroup; use cairo_lang_semantic::expr::pattern::QueryPatternVariablesFromDb; use cairo_lang_semantic::items::function_with_body::SemanticExprLookup; @@ -101,6 +101,17 @@ impl ItemDef { md }) } + + /// Gets full path (including crate name and defining trait/impl if applicable) + /// to the module containing the item. + pub fn definition_path(&self, db: &RootDatabase) -> String { + let defs_db = db.upcast(); + match self.lookup_item_id { + LookupItemId::ModuleItem(item) => item.parent_module(defs_db).full_path(defs_db), + LookupItemId::TraitItem(item) => item.trait_id(defs_db).full_path(defs_db), + LookupItemId::ImplItem(item) => item.impl_def_id(defs_db).full_path(defs_db), + } + } } /// Information about the definition of a variable (local, function parameter). diff --git a/crates/cairo-lang-language-server/src/markdown.rs b/crates/cairo-lang-language-server/src/markdown.rs index 0fc3ae8f896..15495d7725b 100644 --- a/crates/cairo-lang-language-server/src/markdown.rs +++ b/crates/cairo-lang-language-server/src/markdown.rs @@ -25,13 +25,13 @@ impl Markdown { /// Horizontal rule. pub fn rule() -> Self { - "\n---\n".into() + "---\n".into() } /// Creates a new [`Markdown`] instance with the given code surrounded with `cairo` fenced code /// block. pub fn fenced_code_block(contents: &str) -> Self { - format!("```cairo\n{contents}\n```").into() + format!("```cairo\n{contents}\n```\n").into() } /// Appends the given Markdown text to the current text. 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 cb1f29f3ef7..f83d8d2d45a 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 @@ -107,6 +107,9 @@ let mut x: core::integer::u32 x = add_two(x); // = popover ```cairo +hello +``` +```cairo fn add_two(x: u32) -> u32 ``` --- @@ -119,6 +122,9 @@ fn add_two(x: u32) -> u32 x = add_two(x); // = popover ```cairo +hello +``` +```cairo fn add_two(x: u32) -> u32 ``` --- @@ -131,6 +137,9 @@ fn add_two(x: u32) -> u32 x = add_two(x); // = popover ```cairo +hello +``` +```cairo fn add_two(x: u32) -> u32 ``` --- @@ -163,6 +172,9 @@ fn add_to_waitlist() -> () front_of_house::hosting::add_to_waitlist(); // = popover ```cairo +hello::front_of_house::hosting +``` +```cairo pub fn add_to_waitlist() ``` --- @@ -182,6 +194,9 @@ No highlight information. fn area(self: @Rectangle) -> u64; // = popover ```cairo +hello +``` +```cairo struct Rectangle { /// Width of the rectangle. width: u64, @@ -200,6 +215,9 @@ Rectangle struct. fn area(self: @Rectangle) -> u64 { // = popover ```cairo +hello +``` +```cairo struct Rectangle { /// Width of the rectangle. width: u64, @@ -228,6 +246,9 @@ No highlight information. fn area(self: @Rectangle) -> u64 { // = popover ```cairo +hello +``` +```cairo struct Rectangle { /// Width of the rectangle. width: u64, @@ -256,6 +277,9 @@ fn value_in_cents(coin: Coin) -> felt252 { fn value_in_cents(coin: Coin) -> felt252 { // = popover ```cairo +hello +``` +```cairo enum Coin { Penny, @@ -270,6 +294,9 @@ enum Coin { Coin::Penny => 1, // = popover ```cairo +hello +``` +```cairo enum Coin { Penny, 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 589bcefbcae..f6df61cc979 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 @@ -56,6 +56,9 @@ use Balance::contract_state_for_testing; use Balance::contract_state_for_testing; // = popover ```cairo +hello +``` +```cairo pub fn contract_state_for_testing() -> ContractState ``` @@ -72,6 +75,9 @@ No highlight information. // = highlight fn constructor(ref self: ContractState, value_: u128) { // = popover +```cairo +hello +``` ```cairo @@ -107,6 +113,9 @@ value_: core::integer::u128 // = highlight impl Balance of super::IBalance { // = popover +```cairo +hello +``` ```cairo trait IBalance ``` @@ -119,6 +128,9 @@ The balance contract interface. // = highlight impl Balance of super::IBalance<ContractState> { // = popover +```cairo +hello +``` ```cairo @@ -135,6 +147,9 @@ The balance contract interface. self.value.read() // = popover ```cairo +core::starknet::storage::StorageMemberAccessTrait +``` +```cairo fn read(self: @TMemberState) -> Self::Value; ``` @@ -145,6 +160,9 @@ fn read(self: @TMemberState) -> Self::Value; self.value.write( self.value.read() + a ); // = popover ```cairo +core::starknet::storage::StorageMemberAccessTrait +``` +```cairo fn write(ref self: TMemberState, value: Self::Value); ```