aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/assists/raw_string.rs95
-rw-r--r--crates/ra_assists/src/lib.rs1
-rw-r--r--docs/user/features.md18
3 files changed, 15 insertions, 99 deletions
diff --git a/crates/ra_assists/src/assists/raw_string.rs b/crates/ra_assists/src/assists/raw_string.rs
index fe396806b..e57f24e7c 100644
--- a/crates/ra_assists/src/assists/raw_string.rs
+++ b/crates/ra_assists/src/assists/raw_string.rs
@@ -9,23 +9,8 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
9 if literal.token().kind() != ra_syntax::SyntaxKind::STRING { 9 if literal.token().kind() != ra_syntax::SyntaxKind::STRING {
10 return None; 10 return None;
11 } 11 }
12 ctx.add_action(AssistId("make_raw_string"), "make raw string", |edit| {
13 edit.target(literal.syntax().text_range());
14 edit.insert(literal.syntax().text_range().start(), "r");
15 });
16 ctx.build()
17}
18
19pub(crate) fn make_raw_string_unescaped(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
20 let literal = ctx.node_at_offset::<Literal>()?;
21 if literal.token().kind() != ra_syntax::SyntaxKind::STRING {
22 return None;
23 }
24 let token = literal.token(); 12 let token = literal.token();
25 let text = token.text().as_str(); 13 let text = token.text().as_str();
26 if !text.contains(&['\\', '\r'][..]) {
27 return None;
28 }
29 let usual_string_range = find_usual_string_range(text)?; 14 let usual_string_range = find_usual_string_range(text)?;
30 ctx.add_action(AssistId("make_raw_string"), "make raw string", |edit| { 15 ctx.add_action(AssistId("make_raw_string"), "make raw string", |edit| {
31 edit.target(literal.syntax().text_range()); 16 edit.target(literal.syntax().text_range());
@@ -43,7 +28,7 @@ pub(crate) fn make_raw_string_unescaped(mut ctx: AssistCtx<impl HirDatabase>) ->
43 if error.is_err() { 28 if error.is_err() {
44 eprintln!("Error unescaping string"); 29 eprintln!("Error unescaping string");
45 } else { 30 } else {
46 edit.replace(literal.syntax().text_range(), format!("r\"{}\"", unescaped)); 31 edit.replace(literal.syntax().text_range(), format!("r#\"{}\"#", unescaped));
47 } 32 }
48 }); 33 });
49 ctx.build() 34 ctx.build()
@@ -126,10 +111,10 @@ mod test {
126 make_raw_string, 111 make_raw_string,
127 r#" 112 r#"
128 fn f() { 113 fn f() {
129 let s = <|>"random string"; 114 let s = <|>"random\nstring";
130 } 115 }
131 "#, 116 "#,
132 r#""random string""#, 117 r#""random\nstring""#,
133 ); 118 );
134 } 119 }
135 120
@@ -139,86 +124,32 @@ mod test {
139 make_raw_string, 124 make_raw_string,
140 r#" 125 r#"
141 fn f() { 126 fn f() {
142 let s = <|>"random string";
143 }
144 "#,
145 r#"
146 fn f() {
147 let s = <|>r"random string";
148 }
149 "#,
150 )
151 }
152
153 #[test]
154 fn make_raw_string_with_escaped_works() {
155 check_assist(
156 make_raw_string,
157 r#"
158 fn f() {
159 let s = <|>"random\nstring"; 127 let s = <|>"random\nstring";
160 } 128 }
161 "#, 129 "#,
162 r#" 130 r##"
163 fn f() { 131 fn f() {
164 let s = <|>r"random\nstring"; 132 let s = <|>r#"random
133string"#;
165 } 134 }
166 "#, 135 "##,
167 ) 136 )
168 } 137 }
169 138
170 #[test] 139 #[test]
171 fn make_raw_string_not_works() { 140 fn make_raw_string_nothing_to_unescape_works() {
172 check_assist_not_applicable(
173 make_raw_string,
174 r#"
175 fn f() {
176 let s = <|>r"random string";
177 }
178 "#,
179 );
180 }
181
182 #[test]
183 fn make_raw_string_unescaped_target() {
184 check_assist_target(
185 make_raw_string_unescaped,
186 r#"
187 fn f() {
188 let s = <|>"random\nstring";
189 }
190 "#,
191 r#""random\nstring""#,
192 );
193 }
194
195 #[test]
196 fn make_raw_string_unescaped_works() {
197 check_assist( 141 check_assist(
198 make_raw_string_unescaped, 142 make_raw_string,
199 r#"
200 fn f() {
201 let s = <|>"random\nstring";
202 }
203 "#,
204 r#" 143 r#"
205 fn f() { 144 fn f() {
206 let s = <|>r"random 145 let s = <|>"random string";
207string";
208 } 146 }
209 "#, 147 "#,
210 ) 148 r##"
211 }
212
213 #[test]
214 fn make_raw_string_unescaped_dont_works() {
215 check_assist_not_applicable(
216 make_raw_string_unescaped,
217 r#"
218 fn f() { 149 fn f() {
219 let s = <|>"random string"; 150 let s = <|>r#"random string"#;
220 } 151 }
221 "#, 152 "##,
222 ) 153 )
223 } 154 }
224 155
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index d1e2d3251..3ca3320f7 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -132,7 +132,6 @@ mod assists {
132 move_bounds::move_bounds_to_where_clause, 132 move_bounds::move_bounds_to_where_clause,
133 raw_string::add_hash, 133 raw_string::add_hash,
134 raw_string::make_raw_string, 134 raw_string::make_raw_string,
135 raw_string::make_raw_string_unescaped,
136 raw_string::make_usual_string, 135 raw_string::make_usual_string,
137 raw_string::remove_hash, 136 raw_string::remove_hash,
138 ] 137 ]
diff --git a/docs/user/features.md b/docs/user/features.md
index fadd4cdf1..23842f409 100644
--- a/docs/user/features.md
+++ b/docs/user/features.md
@@ -445,20 +445,6 @@ fn foo<T: u32, F: FnOnce(T) -> T>() {}
445fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {} 445fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}
446``` 446```
447 447
448- Make raw string
449
450```rust
451// before:
452fn f() {
453 let s = <|>"abcd";
454}
455
456// after:
457fn f() {
458 let s = <|>r"abcd";
459}
460```
461
462- Make raw string unescaped 448- Make raw string unescaped
463 449
464```rust 450```rust
@@ -469,8 +455,8 @@ fn f() {
469 455
470// after: 456// after:
471fn f() { 457fn f() {
472 let s = <|>r"ab 458 let s = <|>r#"ab
473cd"; 459cd"#;
474} 460}
475``` 461```
476 462