aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/handlers/raw_string.rs31
1 files changed, 22 insertions, 9 deletions
diff --git a/crates/ra_assists/src/handlers/raw_string.rs b/crates/ra_assists/src/handlers/raw_string.rs
index 96679e160..6d77dff13 100644
--- a/crates/ra_assists/src/handlers/raw_string.rs
+++ b/crates/ra_assists/src/handlers/raw_string.rs
@@ -1,5 +1,7 @@
1use std::borrow::Cow;
2
1use ra_syntax::{ 3use ra_syntax::{
2 ast::{self, HasStringValue}, 4 ast::{self, HasQuotes, HasStringValue},
3 AstToken, 5 AstToken,
4 SyntaxKind::{RAW_STRING, STRING}, 6 SyntaxKind::{RAW_STRING, STRING},
5 TextSize, 7 TextSize,
@@ -32,14 +34,17 @@ pub(crate) fn make_raw_string(acc: &mut Assists, ctx: &AssistContext) -> Option<
32 target, 34 target,
33 |edit| { 35 |edit| {
34 let max_hash_streak = count_hashes(&value); 36 let max_hash_streak = count_hashes(&value);
35 let mut hashes = String::with_capacity(max_hash_streak + 1); 37 let hashes = "#".repeat(max_hash_streak + 1);
36 for _ in 0..hashes.capacity() { 38 if matches!(value, Cow::Borrowed(_)) {
37 hashes.push('#'); 39 // Avoid replacing the whole string to better position the cursor.
38 } 40 edit.insert(token.syntax().text_range().start(), format!("r{}", hashes));
39 edit.replace( 41 edit.insert(token.syntax().text_range().end(), format!("{}", hashes));
40 token.syntax().text_range(), 42 } else {
41 format!("r{}\"{}\"{}", hashes, value, hashes), 43 edit.replace(
42 ); 44 token.syntax().text_range(),
45 format!("r{}\"{}\"{}", hashes, value, hashes),
46 );
47 }
43 }, 48 },
44 ) 49 )
45} 50}
@@ -70,6 +75,14 @@ pub(crate) fn make_usual_string(acc: &mut Assists, ctx: &AssistContext) -> Optio
70 |edit| { 75 |edit| {
71 // parse inside string to escape `"` 76 // parse inside string to escape `"`
72 let escaped = value.escape_default().to_string(); 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, "\"");
82 return;
83 }
84 }
85
73 edit.replace(token.syntax().text_range(), format!("\"{}\"", escaped)); 86 edit.replace(token.syntax().text_range(), format!("\"{}\"", escaped));
74 }, 87 },
75 ) 88 )