Skip to content

Fix ide-assists raw_string suffix fail #19622

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

Merged
merged 2 commits into from
Apr 22, 2025
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
142 changes: 134 additions & 8 deletions crates/ide-assists/src/handlers/raw_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::borrow::Cow;

use syntax::{AstToken, TextRange, TextSize, ast, ast::IsString};

use crate::{AssistContext, AssistId, Assists, utils::required_hashes};
use crate::{
AssistContext, AssistId, Assists,
utils::{required_hashes, string_suffix},
};

// Assist: make_raw_string
//
Expand Down Expand Up @@ -33,12 +36,15 @@ pub(crate) fn make_raw_string(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
target,
|edit| {
let hashes = "#".repeat(required_hashes(&value).max(1));
let range = token.syntax().text_range();
let suffix = string_suffix(token.text()).unwrap_or_default();
let range = TextRange::new(range.start(), range.end() - TextSize::of(suffix));
Comment on lines +39 to +41
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add this stuff as a method on IsString? text_range_with_quotes or something like that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually instead of string_suffix we can make use of the IsString::quote_offsets function here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about char? Is there anything similar

Copy link
Member

@Veykril Veykril Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No there isn't, we should probably implement IsString for Char as well though (and then consider renaming the trait). I guess the RAW_PREFIX stuff doesn't make sense on it but that's not too much of an issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that IsString::quote_offsets is not just extracting suffixes, but extracting quotation marks, which can be difficult to handle for #

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string_suffix(r###"r##"x"##i32"###) -> "i32"
QuoteOffsets::new(r###"r##"x"##i32"###) -> range "r##\"", range "\"##i32", and range "x"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay, well 'lets merge as is then

if matches!(value, Cow::Borrowed(_)) {
// Avoid replacing the whole string to better position the cursor.
edit.insert(token.syntax().text_range().start(), format!("r{hashes}"));
edit.insert(token.syntax().text_range().end(), hashes);
edit.insert(range.start(), format!("r{hashes}"));
edit.insert(range.end(), hashes);
} else {
edit.replace(token.syntax().text_range(), format!("r{hashes}\"{value}\"{hashes}"));
edit.replace(range, format!("r{hashes}\"{value}\"{hashes}"));
}
},
)
Expand Down Expand Up @@ -73,15 +79,19 @@ pub(crate) fn make_usual_string(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
|edit| {
// parse inside string to escape `"`
let escaped = value.escape_default().to_string();
let suffix = string_suffix(token.text()).unwrap_or_default();
if let Some(offsets) = token.quote_offsets() {
if token.text()[offsets.contents - token.syntax().text_range().start()] == escaped {
let end_quote = offsets.quotes.1;
let end_quote =
TextRange::new(end_quote.start(), end_quote.end() - TextSize::of(suffix));
edit.replace(offsets.quotes.0, "\"");
edit.replace(offsets.quotes.1, "\"");
edit.replace(end_quote, "\"");
return;
}
}

edit.replace(token.syntax().text_range(), format!("\"{escaped}\""));
edit.replace(token.syntax().text_range(), format!("\"{escaped}\"{suffix}"));
},
)
}
Expand Down Expand Up @@ -109,8 +119,9 @@ pub(crate) fn add_hash(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()>
let text_range = token.syntax().text_range();
let target = text_range;
acc.add(AssistId::refactor("add_hash"), "Add #", target, |edit| {
let suffix = string_suffix(token.text()).unwrap_or_default();
edit.insert(text_range.start() + TextSize::of('r'), "#");
edit.insert(text_range.end(), "#");
edit.insert(text_range.end() - TextSize::of(suffix), "#");
})
}

Expand Down Expand Up @@ -151,8 +162,12 @@ pub(crate) fn remove_hash(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
}

acc.add(AssistId::refactor_rewrite("remove_hash"), "Remove #", text_range, |edit| {
let suffix = string_suffix(text).unwrap_or_default();
edit.delete(TextRange::at(text_range.start() + TextSize::of('r'), TextSize::of('#')));
edit.delete(TextRange::new(text_range.end() - TextSize::of('#'), text_range.end()));
edit.delete(
TextRange::new(text_range.end() - TextSize::of('#'), text_range.end())
- TextSize::of(suffix),
);
})
}

Expand Down Expand Up @@ -262,6 +277,23 @@ string"###;
)
}

#[test]
fn make_raw_string_has_suffix() {
check_assist(
make_raw_string,
r#"
fn f() {
let s = $0"random string"i32;
}
"#,
r##"
fn f() {
let s = r#"random string"#i32;
}
"##,
)
}

#[test]
fn make_raw_string_not_works_on_partial_string() {
check_assist_not_applicable(
Expand Down Expand Up @@ -316,6 +348,23 @@ string"###;
)
}

#[test]
fn add_hash_has_suffix_works() {
check_assist(
add_hash,
r#"
fn f() {
let s = $0r"random string"i32;
}
"#,
r##"
fn f() {
let s = r#"random string"#i32;
}
"##,
)
}

#[test]
fn add_more_hash_works() {
check_assist(
Expand All @@ -333,6 +382,23 @@ string"###;
)
}

#[test]
fn add_more_hash_has_suffix_works() {
check_assist(
add_hash,
r##"
fn f() {
let s = $0r#"random"string"#i32;
}
"##,
r###"
fn f() {
let s = r##"random"string"##i32;
}
"###,
)
}

#[test]
fn add_hash_not_works() {
check_assist_not_applicable(
Expand Down Expand Up @@ -367,6 +433,15 @@ string"###;
)
}

#[test]
fn remove_hash_has_suffix_works() {
check_assist(
remove_hash,
r##"fn f() { let s = $0r#"random string"#i32; }"##,
r#"fn f() { let s = r"random string"i32; }"#,
)
}

#[test]
fn cant_remove_required_hash() {
cov_mark::check!(cant_remove_required_hash);
Expand Down Expand Up @@ -397,6 +472,23 @@ string"###;
)
}

#[test]
fn remove_more_hash_has_suffix_works() {
check_assist(
remove_hash,
r###"
fn f() {
let s = $0r##"random string"##i32;
}
"###,
r##"
fn f() {
let s = r#"random string"#i32;
}
"##,
)
}

#[test]
fn remove_hash_does_not_work() {
check_assist_not_applicable(remove_hash, r#"fn f() { let s = $0"random string"; }"#);
Expand Down Expand Up @@ -437,6 +529,23 @@ string"###;
)
}

#[test]
fn make_usual_string_has_suffix_works() {
check_assist(
make_usual_string,
r##"
fn f() {
let s = $0r#"random string"#i32;
}
"##,
r#"
fn f() {
let s = "random string"i32;
}
"#,
)
}

#[test]
fn make_usual_string_with_quote_works() {
check_assist(
Expand Down Expand Up @@ -471,6 +580,23 @@ string"###;
)
}

#[test]
fn make_usual_string_more_hash_has_suffix_works() {
check_assist(
make_usual_string,
r###"
fn f() {
let s = $0r##"random string"##i32;
}
"###,
r##"
fn f() {
let s = "random string"i32;
}
"##,
)
}

#[test]
fn make_usual_string_not_works() {
check_assist_not_applicable(
Expand Down
Loading