fix no space insert before and after if value is only spaces

This commit is contained in:
dfireBird 2025-01-02 16:28:25 +05:30
parent a611d8eb4e
commit 83fcdbf3f6
No known key found for this signature in database
GPG key ID: 26D522CA5FC2B93D
2 changed files with 38 additions and 2 deletions

View file

@ -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}"),

View file

@ -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]