3 use syntax::{ast, ast::IsString, AstToken, TextRange, TextSize};
5 use crate::{AssistContext, AssistId, AssistKind, Assists};
7 // Assist: make_raw_string
9 // Adds `r#` to a plain string literal.
19 // r#"Hello, World!"#;
22 pub(crate) fn make_raw_string(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
23 let token = ctx.find_token_at_offset::<ast::String>()?;
27 let value = token.value()?;
28 let target = token.syntax().text_range();
30 AssistId("make_raw_string", AssistKind::RefactorRewrite),
31 "Rewrite as raw string",
34 let hashes = "#".repeat(required_hashes(&value).max(1));
35 if matches!(value, Cow::Borrowed(_)) {
36 // Avoid replacing the whole string to better position the cursor.
37 edit.insert(token.syntax().text_range().start(), format!("r{}", hashes));
38 edit.insert(token.syntax().text_range().end(), hashes);
41 token.syntax().text_range(),
42 format!("r{}\"{}\"{}", hashes, value, hashes),
49 // Assist: make_usual_string
51 // Turns a raw string into a plain string.
55 // r#"Hello,$0 "World!""#;
61 // "Hello, \"World!\"";
64 pub(crate) fn make_usual_string(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
65 let token = ctx.find_token_at_offset::<ast::String>()?;
69 let value = token.value()?;
70 let target = token.syntax().text_range();
72 AssistId("make_usual_string", AssistKind::RefactorRewrite),
73 "Rewrite as regular string",
76 // parse inside string to escape `"`
77 let escaped = value.escape_default().to_string();
78 if let Some(offsets) = token.quote_offsets() {
79 if token.text()[offsets.contents - token.syntax().text_range().start()] == escaped {
80 edit.replace(offsets.quotes.0, "\"");
81 edit.replace(offsets.quotes.1, "\"");
86 edit.replace(token.syntax().text_range(), format!("\"{}\"", escaped));
93 // Adds a hash to a raw string literal.
97 // r#"Hello,$0 World!"#;
103 // r##"Hello, World!"##;
106 pub(crate) fn add_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
107 let token = ctx.find_token_at_offset::<ast::String>()?;
111 let text_range = token.syntax().text_range();
112 let target = text_range;
113 acc.add(AssistId("add_hash", AssistKind::Refactor), "Add #", target, |edit| {
114 edit.insert(text_range.start() + TextSize::of('r'), "#");
115 edit.insert(text_range.end(), "#");
119 // Assist: remove_hash
121 // Removes a hash from a raw string literal.
125 // r#"Hello,$0 World!"#;
134 pub(crate) fn remove_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
135 let token = ctx.find_token_at_offset::<ast::String>()?;
140 let text = token.text();
141 if !text.starts_with("r#") && text.ends_with('#') {
145 let existing_hashes = text.chars().skip(1).take_while(|&it| it == '#').count();
147 let text_range = token.syntax().text_range();
148 let internal_text = &text[token.text_range_between_quotes()? - text_range.start()];
150 if existing_hashes == required_hashes(internal_text) {
151 cov_mark::hit!(cant_remove_required_hash);
155 acc.add(AssistId("remove_hash", AssistKind::RefactorRewrite), "Remove #", text_range, |edit| {
156 edit.delete(TextRange::at(text_range.start() + TextSize::of('r'), TextSize::of('#')));
157 edit.delete(TextRange::new(text_range.end() - TextSize::of('#'), text_range.end()));
161 fn required_hashes(s: &str) -> usize {
162 let mut res = 0usize;
163 for idx in s.match_indices('"').map(|(i, _)| i) {
164 let (_, sub) = s.split_at(idx + 1);
165 let n_hashes = sub.chars().take_while(|c| *c == '#').count();
166 res = res.max(n_hashes + 1)
172 fn test_required_hashes() {
173 assert_eq!(0, required_hashes("abc"));
174 assert_eq!(0, required_hashes("###"));
175 assert_eq!(1, required_hashes("\""));
176 assert_eq!(2, required_hashes("\"#abc"));
177 assert_eq!(0, required_hashes("#abc"));
178 assert_eq!(3, required_hashes("#ab\"##c"));
179 assert_eq!(5, required_hashes("#ab\"##\"####c"));
184 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
189 fn make_raw_string_target() {
194 let s = $0"random\nstring";
197 r#""random\nstring""#,
202 fn make_raw_string_works() {
207 let s = $0"random\nstring";
220 fn make_raw_string_works_inside_macros() {
225 format!($0"x = {}", 92)
230 format!(r#"x = {}"#, 92)
237 fn make_raw_string_hashes_inside_works() {
242 let s = $0"#random##\nstring";
255 fn make_raw_string_closing_hashes_inside_works() {
260 let s = $0"#random\"##\nstring";
265 let s = r###"#random"##
273 fn make_raw_string_nothing_to_unescape_works() {
278 let s = $0"random string";
283 let s = r#"random string"#;
290 fn make_raw_string_not_works_on_partial_string() {
291 check_assist_not_applicable(
302 fn make_usual_string_not_works_on_partial_string() {
303 check_assist_not_applicable(
314 fn add_hash_target() {
319 let s = $0r"random string";
322 r#"r"random string""#,
327 fn add_hash_works() {
332 let s = $0r"random string";
337 let s = r#"random string"#;
344 fn add_more_hash_works() {
349 let s = $0r#"random"string"#;
354 let s = r##"random"string"##;
361 fn add_hash_not_works() {
362 check_assist_not_applicable(
366 let s = $0"random string";
373 fn remove_hash_target() {
378 let s = $0r#"random string"#;
381 r##"r#"random string"#"##,
386 fn remove_hash_works() {
389 r##"fn f() { let s = $0r#"random string"#; }"##,
390 r#"fn f() { let s = r"random string"; }"#,
395 fn cant_remove_required_hash() {
396 cov_mark::check!(cant_remove_required_hash);
397 check_assist_not_applicable(
401 let s = $0r#"random"str"ing"#;
408 fn remove_more_hash_works() {
413 let s = $0r##"random string"##;
418 let s = r#"random string"#;
425 fn remove_hash_doesnt_work() {
426 check_assist_not_applicable(remove_hash, r#"fn f() { let s = $0"random string"; }"#);
430 fn remove_hash_no_hash_doesnt_work() {
431 check_assist_not_applicable(remove_hash, r#"fn f() { let s = $0r"random string"; }"#);
435 fn make_usual_string_target() {
440 let s = $0r#"random string"#;
443 r##"r#"random string"#"##,
448 fn make_usual_string_works() {
453 let s = $0r#"random string"#;
458 let s = "random string";
465 fn make_usual_string_with_quote_works() {
470 let s = $0r#"random"str"ing"#;
475 let s = "random\"str\"ing";
482 fn make_usual_string_more_hash_works() {
487 let s = $0r##"random string"##;
492 let s = "random string";
499 fn make_usual_string_not_works() {
500 check_assist_not_applicable(
504 let s = $0"random string";