Skip to content

Commit 1c6b838

Browse files
authored
Merge pull request #18795 from dfireBird/fix-hover-render
Fix render of literal to be rendered in codeblock
2 parents 2e13684 + 92f8fef commit 1c6b838

File tree

2 files changed

+65
-24
lines changed

2 files changed

+65
-24
lines changed

crates/ide/src/hover/render.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,10 +715,17 @@ pub(super) fn literal(
715715
let mut s = format!("```rust\n{ty}\n```\n___\n\n");
716716
match value {
717717
Ok(value) => {
718+
let backtick_len = value.chars().filter(|c| *c == '`').count();
719+
let backticks = "`".repeat(backtick_len + 1);
720+
718721
if let Some(newline) = value.find('\n') {
719-
format_to!(s, "value of literal (truncated up to newline): {}", &value[..newline])
722+
format_to!(
723+
s,
724+
"value of literal (truncated up to newline): {backticks} {} {backticks}",
725+
&value[..newline]
726+
)
720727
} else {
721-
format_to!(s, "value of literal: {value}")
728+
format_to!(s, "value of literal: {backticks} {value} {backticks}")
722729
}
723730
}
724731
Err(error) => format_to!(s, "invalid literal: {error}"),

crates/ide/src/hover/tests.rs

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8157,7 +8157,7 @@ fn main() {
81578157
```
81588158
___
81598159
8160-
value of literal: 🦀🦀\A
8160+
value of literal: ` 🦀🦀\A `
81618161
"#]],
81628162
);
81638163
check(
@@ -8173,7 +8173,7 @@ fn main() {
81738173
```
81748174
___
81758175
8176-
value of literal: 🦀\u{1f980}\\\x41
8176+
value of literal: ` 🦀\u{1f980}\\\x41 `
81778177
"#]],
81788178
);
81798179
check(
@@ -8195,7 +8195,7 @@ fsdghs";
81958195
```
81968196
___
81978197
8198-
value of literal (truncated up to newline): 🦀\u{1f980}\\\x41
8198+
value of literal (truncated up to newline): ` 🦀\u{1f980}\\\x41 `
81998199
"#]],
82008200
);
82018201
}
@@ -8215,7 +8215,41 @@ fn main() {
82158215
```
82168216
___
82178217
8218-
value of literal: 🦀🦀\A
8218+
value of literal: ` 🦀🦀\A `
8219+
"#]],
8220+
);
8221+
}
8222+
8223+
#[test]
8224+
fn rawstring_literal() {
8225+
check(
8226+
r#"
8227+
fn main() {
8228+
$0r"`[^`]*`";
8229+
}"#,
8230+
expect![[r#"
8231+
*r"`[^`]*`"*
8232+
```rust
8233+
&str
8234+
```
8235+
___
8236+
8237+
value of literal: ```` `[^`]*` ````
8238+
"#]],
8239+
);
8240+
check(
8241+
r#"
8242+
fn main() {
8243+
$0r"`";
8244+
}"#,
8245+
expect![[r#"
8246+
*r"`"*
8247+
```rust
8248+
&str
8249+
```
8250+
___
8251+
8252+
value of literal: `` ` ``
82198253
"#]],
82208254
);
82218255
}
@@ -8235,7 +8269,7 @@ fn main() {
82358269
```
82368270
___
82378271
8238-
value of literal: [240, 159, 166, 128, 92]
8272+
value of literal: ` [240, 159, 166, 128, 92] `
82398273
"#]],
82408274
);
82418275
check(
@@ -8251,7 +8285,7 @@ fn main() {
82518285
```
82528286
___
82538287
8254-
value of literal: [92, 120, 70, 48, 92, 120, 57, 70, 92, 120, 65, 54, 92, 120, 56, 48, 92, 92]
8288+
value of literal: ` [92, 120, 70, 48, 92, 120, 57, 70, 92, 120, 65, 54, 92, 120, 56, 48, 92, 92] `
82558289
"#]],
82568290
);
82578291
}
@@ -8271,7 +8305,7 @@ fn main() {
82718305
```
82728306
___
82738307
8274-
value of literal: 0xF0
8308+
value of literal: ` 0xF0 `
82758309
"#]],
82768310
);
82778311
check(
@@ -8287,7 +8321,7 @@ fn main() {
82878321
```
82888322
___
82898323
8290-
value of literal: 0x5C
8324+
value of literal: ` 0x5C `
82918325
"#]],
82928326
);
82938327
}
@@ -8307,7 +8341,7 @@ fn main() {
83078341
```
83088342
___
83098343
8310-
value of literal: A
8344+
value of literal: ` A `
83118345
"#]],
83128346
);
83138347
check(
@@ -8323,7 +8357,7 @@ fn main() {
83238357
```
83248358
___
83258359
8326-
value of literal: \
8360+
value of literal: ` \ `
83278361
"#]],
83288362
);
83298363
check(
@@ -8339,7 +8373,7 @@ fn main() {
83398373
```
83408374
___
83418375
8342-
value of literal: 🦀
8376+
value of literal: ` 🦀 `
83438377
"#]],
83448378
);
83458379
}
@@ -8359,7 +8393,7 @@ fn main() {
83598393
```
83608394
___
83618395
8362-
value of literal: 1 (bits: 0x3FF0000000000000)
8396+
value of literal: ` 1 (bits: 0x3FF0000000000000) `
83638397
"#]],
83648398
);
83658399
check(
@@ -8375,7 +8409,7 @@ fn main() {
83758409
```
83768410
___
83778411
8378-
value of literal: 1 (bits: 0x3C00)
8412+
value of literal: ` 1 (bits: 0x3C00) `
83798413
"#]],
83808414
);
83818415
check(
@@ -8391,7 +8425,7 @@ fn main() {
83918425
```
83928426
___
83938427
8394-
value of literal: 1 (bits: 0x3F800000)
8428+
value of literal: ` 1 (bits: 0x3F800000) `
83958429
"#]],
83968430
);
83978431
check(
@@ -8407,7 +8441,7 @@ fn main() {
84078441
```
84088442
___
84098443
8410-
value of literal: 1 (bits: 0x3FFF0000000000000000000000000000)
8444+
value of literal: ` 1 (bits: 0x3FFF0000000000000000000000000000) `
84118445
"#]],
84128446
);
84138447
check(
@@ -8423,7 +8457,7 @@ fn main() {
84238457
```
84248458
___
84258459
8426-
value of literal: 134000000000000 (bits: 0x42DE77D399980000)
8460+
value of literal: ` 134000000000000 (bits: 0x42DE77D399980000) `
84278461
"#]],
84288462
);
84298463
check(
@@ -8439,7 +8473,7 @@ fn main() {
84398473
```
84408474
___
84418475
8442-
value of literal: 1523527134274733600000000 (bits: 0x44F429E9249F629B)
8476+
value of literal: ` 1523527134274733600000000 (bits: 0x44F429E9249F629B) `
84438477
"#]],
84448478
);
84458479
check(
@@ -8475,7 +8509,7 @@ fn main() {
84758509
```
84768510
___
84778511
8478-
value of literal: 34325236457856836345234 (0x744C659178614489D92|0b111010001001100011001011001000101111000011000010100010010001001110110010010)
8512+
value of literal: ` 34325236457856836345234 (0x744C659178614489D92|0b111010001001100011001011001000101111000011000010100010010001001110110010010) `
84798513
"#]],
84808514
);
84818515
check(
@@ -8491,7 +8525,7 @@ fn main() {
84918525
```
84928526
___
84938527
8494-
value of literal: 13412342421 (0x31F701A95|0b1100011111011100000001101010010101)
8528+
value of literal: ` 13412342421 (0x31F701A95|0b1100011111011100000001101010010101) `
84958529
"#]],
84968530
);
84978531
check(
@@ -8507,7 +8541,7 @@ fn main() {
85078541
```
85088542
___
85098543
8510-
value of literal: 306328611 (0x12423423|0b10010010000100011010000100011)
8544+
value of literal: ` 306328611 (0x12423423|0b10010010000100011010000100011) `
85118545
"#]],
85128546
);
85138547
check(
@@ -8523,7 +8557,7 @@ fn main() {
85238557
```
85248558
___
85258559
8526-
value of literal: 255 (0xFF|0b11111111)
8560+
value of literal: ` 255 (0xFF|0b11111111) `
85278561
"#]],
85288562
);
85298563
check(
@@ -8539,7 +8573,7 @@ fn main() {
85398573
```
85408574
___
85418575
8542-
value of literal: 5349 (0x14E5|0b1010011100101)
8576+
value of literal: ` 5349 (0x14E5|0b1010011100101) `
85438577
"#]],
85448578
);
85458579
check(

0 commit comments

Comments
 (0)