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: Run Cairo formatter over item signatures in hover #5822

Merged
merged 1 commit into from
Jun 19, 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
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()
}
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