Skip to content

Commit

Permalink
Merge pull request #18820 from dfireBird/fix-space-char-on-literal-re…
Browse files Browse the repository at this point in the history
…nder

Fix no space insert before and after if value is only spaces
  • Loading branch information
Veykril authored Jan 2, 2025
2 parents a611d8e + 83fcdbf commit d3dd40f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
9 changes: 7 additions & 2 deletions crates/ide/src/hover/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,16 +716,21 @@ pub(super) fn literal(
match value {
Ok(value) => {
let backtick_len = value.chars().filter(|c| *c == '`').count();
let spaces_len = value.chars().filter(|c| *c == ' ').count();
let backticks = "`".repeat(backtick_len + 1);
let space_char = if spaces_len == value.len() { "" } else { " " };

if let Some(newline) = value.find('\n') {
format_to!(
s,
"value of literal (truncated up to newline): {backticks} {} {backticks}",
"value of literal (truncated up to newline): {backticks}{space_char}{}{space_char}{backticks}",
&value[..newline]
)
} else {
format_to!(s, "value of literal: {backticks} {value} {backticks}")
format_to!(
s,
"value of literal: {backticks}{space_char}{value}{space_char}{backticks}"
)
}
}
Err(error) => format_to!(s, "invalid literal: {error}"),
Expand Down
31 changes: 31 additions & 0 deletions crates/ide/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8303,6 +8303,37 @@ fn main() {
value of literal: `` ` ``
"#]],
);
check(
r#"
fn main() {
$0r" ";
}"#,
expect![[r#"
*r" "*
```rust
&str
```
___
value of literal: ` `
"#]],
);
check(
r#"
fn main() {
$0r" Hello World ";
}"#,
expect![[r#"
*r" Hello World "*
```rust
&str
```
___
value of literal: ` Hello World `
"#]],
)
}

#[test]
Expand Down

0 comments on commit d3dd40f

Please sign in to comment.