Skip to content

Commit

Permalink
LS: Run Cairo formatter over item signatures in hover
Browse files Browse the repository at this point in the history
commit-id:3ec25feb
  • Loading branch information
mkaput committed Jun 19, 2024
1 parent c019dec commit ec681f9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
16 changes: 15 additions & 1 deletion crates/cairo-lang-language-server/src/lang/inspect/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::ids::{
LanguageElementId, LookupItemId, ModuleItemId, TopLevelLanguageElementId, TraitItemId,
};
use cairo_lang_parser::utils::SimpleParserDatabase;
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 @@ -117,7 +118,7 @@ impl ItemDef {
pub fn signature(&self, db: &RootDatabase) -> String {
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")
contexts.chain(this).map(fmt).join("\n")
}

/// Gets item documentation in a final form usable for display.
Expand Down Expand Up @@ -260,3 +261,16 @@ impl VariableDef {
format!("{prefix}{mutability}{name}: {ty}")
}
}

/// Run Cairo formatter over code with extra post-processing that is specific to signatures.
fn fmt(code: String) -> String {
let code = cairo_lang_formatter::format_string(&SimpleParserDatabase::default(), code);

code
// Trim any whitespace that formatter tends to leave.
.trim_end()
// Trim trailing semicolons, that are present in trait/impl functions, constants, etc.
// and that formatter tends to put in separate line.
.trim_end_matches("\n;")
.to_owned()
}
12 changes: 2 additions & 10 deletions crates/cairo-lang-language-server/tests/test_data/hover/basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ struct Rectangle {
/// Height of the rectangle.
height: u64,
}

```
---
Rectangle struct.
Expand Down Expand Up @@ -240,8 +239,8 @@ Type: `core::integer::u64`
hello::RectangleTrait
```
```cairo
trait RectangleTrait
fn area(self: @Rectangle) -> u64;
trait RectangleTrait
fn area(self: @Rectangle) -> u64
```
---
Calculate the area of the rectangle.
Expand Down Expand Up @@ -276,7 +275,6 @@ struct Rectangle {
/// Height of the rectangle.
height: u64,
}

```
---
Rectangle struct.
Expand Down Expand Up @@ -304,7 +302,6 @@ struct Rectangle {
/// Height of the rectangle.
height: u64,
}

```
---
Rectangle struct.
Expand Down Expand Up @@ -335,7 +332,6 @@ struct Rectangle {
/// Height of the rectangle.
height: u64,
}

```
---
Rectangle struct.
Expand Down Expand Up @@ -390,11 +386,9 @@ fn value_in_cents(coin: <sel>Coin</sel>) -> felt252 {
hello
```
```cairo

enum Coin {
Penny,
}

```

//! > hover #27
Expand All @@ -417,9 +411,7 @@ coin: hello::Coin
hello
```
```cairo

enum Coin {
Penny,
}

```
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,9 @@ No highlight information.
hello
```
```cairo


pub struct ContractState {
pub value: __member_module_value::ContractMemberState,
}

pub struct ContractState {
pub value: __member_module_value::ContractMemberState,
}
```

//! > hover #3
Expand Down Expand Up @@ -117,7 +114,7 @@ value_: core::integer::u128
hello
```
```cairo
trait IBalance<T>
trait IBalance<T>
```
---
The balance contract interface.
Expand All @@ -132,12 +129,9 @@ The balance contract interface.
hello
```
```cairo


pub struct ContractState {
pub value: __member_module_value::ContractMemberState,
}

pub struct ContractState {
pub value: __member_module_value::ContractMemberState,
}
```

//! > hover #7
Expand All @@ -151,7 +145,7 @@ core::starknet::storage::StorageMemberAccessTrait
```
```cairo
pub trait StorageMemberAccessTrait<TMemberState>
fn read(self: @TMemberState) -> Self::Value;
fn read(self: @TMemberState) -> Self::Value
```

//! > hover #8
Expand All @@ -165,7 +159,7 @@ core::starknet::storage::StorageMemberAccessTrait
```
```cairo
pub trait StorageMemberAccessTrait<TMemberState>
fn write(ref self: TMemberState, value: Self::Value);
fn write(ref self: TMemberState, value: Self::Value)
```

//! > hover #9
Expand Down

0 comments on commit ec681f9

Please sign in to comment.