Skip to content

Commit

Permalink
Added hover for path segments
Browse files Browse the repository at this point in the history
  • Loading branch information
integraledelebesgue committed Nov 13, 2024
1 parent 4a674b5 commit 10198aa
Show file tree
Hide file tree
Showing 5 changed files with 408 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ pub fn definition(
md
}

SymbolDef::Module(module) => {
let mut md = String::new();
md += &fenced_code_block(&module.definition_path());
md += &fenced_code_block(&module.signature());
if let Some(doc) = module.documentation() {
md += RULE;
md += &doc;
}
md
}

SymbolDef::Variable(var) => fenced_code_block(&var.signature(db)),
SymbolDef::ExprInlineMacro(macro_name) => {
let mut md = fenced_code_block(macro_name);
Expand Down
47 changes: 45 additions & 2 deletions crates/cairo-lang-language-server/src/lang/inspect/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum SymbolDef {
Variable(VariableDef),
ExprInlineMacro(String),
Member(MemberDef),
Module(ModuleDef),
}

/// Information about a struct member.
Expand Down Expand Up @@ -78,7 +79,6 @@ impl SymbolDef {

match definition_item {
ResolvedItem::Generic(ResolvedGenericItem::GenericConstant(_))
| ResolvedItem::Generic(ResolvedGenericItem::Module(_))
| ResolvedItem::Generic(ResolvedGenericItem::GenericFunction(_))
| ResolvedItem::Generic(ResolvedGenericItem::TraitFunction(_))
| ResolvedItem::Generic(ResolvedGenericItem::GenericType(_))
Expand All @@ -88,7 +88,6 @@ impl SymbolDef {
| ResolvedItem::Generic(ResolvedGenericItem::Trait(_))
| ResolvedItem::Generic(ResolvedGenericItem::Impl(_))
| ResolvedItem::Concrete(ResolvedConcreteItem::Constant(_))
| ResolvedItem::Concrete(ResolvedConcreteItem::Module(_))
| ResolvedItem::Concrete(ResolvedConcreteItem::Function(_))
| ResolvedItem::Concrete(ResolvedConcreteItem::TraitFunction(_))
| ResolvedItem::Concrete(ResolvedConcreteItem::Type(_))
Expand All @@ -98,6 +97,14 @@ impl SymbolDef {
ItemDef::new(db, &definition_node).map(Self::Item)
}

ResolvedItem::Generic(ResolvedGenericItem::Module(id)) => {
Some(Self::Module(ModuleDef::new(id.name(db), id.full_path(db))))
}

ResolvedItem::Concrete(ResolvedConcreteItem::Module(id)) => {
Some(Self::Module(ModuleDef::new(id.name(db), id.full_path(db))))
}

ResolvedItem::Generic(ResolvedGenericItem::Variable(_)) => {
VariableDef::new(db, definition_node).map(Self::Variable)
}
Expand Down Expand Up @@ -290,6 +297,42 @@ impl VariableDef {
}
}

/// Information about the definition of a module.
pub struct ModuleDef {
name: SmolStr,
full_path: String,
}

impl ModuleDef {
/// Constructs new [`ModuleDef`] instance.
pub fn new(name: SmolStr, full_path: String) -> Self {
let full_path =
match full_path.strip_suffix(name.as_str()).and_then(|path| path.strip_suffix("::")) {
Some(stripped_path) => stripped_path.to_owned(),
None => full_path,
};

ModuleDef { name, full_path }
}

/// Gets module signature, which tries to resemble the way how it is defined in code.
pub fn signature(&self) -> String {
format!("mod {}", self.name)
}

/// Gets the full path of a module.
pub fn definition_path(&self) -> String {
self.full_path.clone()
}

/// Gets the module's documentation if it's available.
pub fn documentation(&self) -> Option<String> {
// Modules cannot have docstrings at the moment.
// Implement this function as soon as it becomes possible in the future.
None
}
}

// TODO(mkaput): make private.
pub fn find_definition(
db: &AnalysisDatabase,
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lang-language-server/tests/e2e/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ cairo_lang_test_utils::test_file_test!(
partial: "partial.txt",
starknet: "starknet.txt",
literals: "literals.txt",
paths: "paths.txt",
},
test_hover
);
Expand Down
14 changes: 10 additions & 4 deletions crates/cairo-lang-language-server/tests/test_data/hover/basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,26 @@ fn add_two(x: u32) -> u32
// = source context
front<caret>_of_house::hosting::add_to_waitlist();
// = highlight
No highlight information.
<sel>front_of_house</sel>::hosting::add_to_waitlist();
// = popover
```cairo
fn add_to_waitlist() -> ()
hello
```
```cairo
mod front_of_house
```

//! > hover #7
// = source context
front_of_house::ho<caret>sting::add_to_waitlist();
// = highlight
No highlight information.
front_of_house::<sel>hosting</sel>::add_to_waitlist();
// = popover
```cairo
fn add_to_waitlist() -> ()
hello::front_of_house
```
```cairo
mod hosting
```

//! > hover #8
Expand Down
Loading

0 comments on commit 10198aa

Please sign in to comment.