aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/assists')
-rw-r--r--crates/ra_assists/src/assists/raw_string.rs95
1 files changed, 13 insertions, 82 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