Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LS: Show path to item definition parent item in hovers #5819

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 12 additions & 1 deletion crates/cairo-lang-language-server/src/lang/inspect/defs.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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).
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-language-server/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 27 additions & 0 deletions crates/cairo-lang-language-server/tests/test_data/hover/basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ let mut x: core::integer::u32
x = <sel>add_two</sel>(x);
// = popover
```cairo
hello
```
```cairo
fn add_two(x: u32) -> u32
```
---
Expand All @@ -119,6 +122,9 @@ fn add_two(x: u32) -> u32
x = <sel>add_two</sel>(x);
// = popover
```cairo
hello
```
```cairo
fn add_two(x: u32) -> u32
```
---
Expand All @@ -131,6 +137,9 @@ fn add_two(x: u32) -> u32
x = <sel>add_two</sel>(x);
// = popover
```cairo
hello
```
```cairo
fn add_two(x: u32) -> u32
```
---
Expand Down Expand Up @@ -163,6 +172,9 @@ fn add_to_waitlist() -> ()
front_of_house::hosting::<sel>add_to_waitlist</sel>();
// = popover
```cairo
hello::front_of_house::hosting
```
```cairo
pub fn add_to_waitlist()
```
---
Expand All @@ -182,6 +194,9 @@ No highlight information.
fn area(self: @<sel>Rectangle</sel>) -> u64;
// = popover
```cairo
hello
```
```cairo
struct Rectangle {
/// Width of the rectangle.
width: u64,
Expand All @@ -200,6 +215,9 @@ Rectangle struct.
fn area(self: @<sel>Rectangle</sel>) -> u64 {
// = popover
```cairo
hello
```
```cairo
struct Rectangle {
/// Width of the rectangle.
width: u64,
Expand Down Expand Up @@ -228,6 +246,9 @@ No highlight information.
fn area(self: @<sel>Rectangle</sel>) -> u64 {
// = popover
```cairo
hello
```
```cairo
struct Rectangle {
/// Width of the rectangle.
width: u64,
Expand Down Expand Up @@ -256,6 +277,9 @@ fn value_in_cents(coin: C<caret>oin) -> felt252 {
fn value_in_cents(coin: <sel>Coin</sel>) -> felt252 {
// = popover
```cairo
hello
```
```cairo

enum Coin {
Penny,
Expand All @@ -270,6 +294,9 @@ enum Coin {
Coin::<sel>Penny</sel> => 1,
// = popover
```cairo
hello
```
```cairo

enum Coin {
Penny,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ use Balance::contr<caret>act_state_for_testing;
use Balance::<sel>contract_state_for_testing</sel>;
// = popover
```cairo
hello
```
```cairo
pub fn contract_state_for_testing() -> ContractState
```

Expand All @@ -72,6 +75,9 @@ No highlight information.
// = highlight
fn constructor(ref self: <sel>ContractState</sel>, value_: u128) {
// = popover
```cairo
hello
```
```cairo


Expand Down Expand Up @@ -107,6 +113,9 @@ value_: core::integer::u128
// = highlight
impl Balance of super::<sel>IBalance</sel><ContractState> {
// = popover
```cairo
hello
```
```cairo
trait IBalance<T>
```
Expand All @@ -119,6 +128,9 @@ The balance contract interface.
// = highlight
impl Balance of super::IBalance<<sel>ContractState</sel>> {
// = popover
```cairo
hello
```
```cairo


Expand All @@ -135,6 +147,9 @@ The balance contract interface.
self.value.<sel>read</sel>()
// = popover
```cairo
core::starknet::storage::StorageMemberAccessTrait
```
```cairo
fn read(self: @TMemberState) -> Self::Value;
```

Expand All @@ -145,6 +160,9 @@ fn read(self: @TMemberState) -> Self::Value;
self.value.<sel>write</sel>( self.value.read() + a );
// = popover
```cairo
core::starknet::storage::StorageMemberAccessTrait
```
```cairo
fn write(ref self: TMemberState, value: Self::Value);
```

Expand Down