Skip to content

Commit

Permalink
LS: Provide trait/impl/struct/enum context for item hovers
Browse files Browse the repository at this point in the history
commit-id:8a0befe3
  • Loading branch information
mkaput committed Jun 19, 2024
1 parent 00c1c18 commit 4282bf9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
44 changes: 38 additions & 6 deletions crates/cairo-lang-language-server/src/lang/inspect/defs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::iter;

use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::ids::{LanguageElementId, LookupItemId, TopLevelLanguageElementId};
use cairo_lang_defs::ids::{
LanguageElementId, LookupItemId, ModuleItemId, TopLevelLanguageElementId, TraitItemId,
};
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_semantic::expr::pattern::QueryPatternVariablesFromDb;
use cairo_lang_semantic::items::function_with_body::SemanticExprLookup;
Expand All @@ -11,6 +15,7 @@ use cairo_lang_syntax::node::ast::{Param, PatternIdentifier, PatternPtr, Termina
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::{SyntaxNode, Terminal, TypedSyntaxNode};
use cairo_lang_utils::Upcast;
use itertools::Itertools;
use smol_str::SmolStr;
use tracing::error;

Expand Down Expand Up @@ -72,20 +77,47 @@ impl SymbolDef {
pub struct ItemDef {
/// The [`LookupItemId`] associated with the item.
lookup_item_id: LookupItemId,

/// Parent item to use as context when building signatures, etc.
///
/// Sometimes, a signature of an item, it might contain parts that are defined elsewhere.
/// For example, for trait/impl items,
/// signature may refer to generic params defined in the defining trait/impl.
/// This reference allows including simplified signatures of such contexts alongside
/// the signature of this item.
context_items: Vec<LookupItemId>,
}

impl ItemDef {
/// Constructs new [`ItemDef`] instance.
fn new(db: &RootDatabase, definition_node: &SyntaxNode) -> Option<Self> {
// Get the lookup item representing the defining item.
let lookup_item_id = db.find_lookup_item(definition_node)?;
let mut lookup_item_ids = db.collect_lookup_items_stack(definition_node)?.into_iter();

// Pull the lookup item representing the defining item.
let lookup_item_id = lookup_item_ids.next()?;

// Collect context items.
let context_items = lookup_item_ids
.take_while(|item| {
matches!(
item,
LookupItemId::ModuleItem(ModuleItemId::Struct(_))
| LookupItemId::ModuleItem(ModuleItemId::Enum(_))
| LookupItemId::ModuleItem(ModuleItemId::Trait(_))
| LookupItemId::ModuleItem(ModuleItemId::Impl(_))
| LookupItemId::TraitItem(TraitItemId::Impl(_))
)
})
.collect();

Some(Self { lookup_item_id })
Some(Self { lookup_item_id, context_items })
}

/// Get item signature without its body.
/// Get item signature without its body including signatures of its contexts.
pub fn signature(&self, db: &RootDatabase) -> String {
db.get_item_signature(self.lookup_item_id)
let contexts = self.context_items.iter().map(|item| db.get_item_signature(*item)).rev();
let this = iter::once(db.get_item_signature(self.lookup_item_id));
contexts.chain(this).join("\n")
}

/// Gets item documentation in a final form usable for display.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ Type: `core::integer::u64`
hello::RectangleTrait
```
```cairo
trait RectangleTrait
fn area(self: @Rectangle) -> u64;
```
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ hello
core::starknet::storage::StorageMemberAccessTrait
```
```cairo
pub trait StorageMemberAccessTrait<TMemberState>
fn read(self: @TMemberState) -> Self::Value;
```

Expand All @@ -163,6 +164,7 @@ fn read(self: @TMemberState) -> Self::Value;
core::starknet::storage::StorageMemberAccessTrait
```
```cairo
pub trait StorageMemberAccessTrait<TMemberState>
fn write(ref self: TMemberState, value: Self::Value);
```

Expand Down

0 comments on commit 4282bf9

Please sign in to comment.