diff options
Diffstat (limited to 'crates/ra_assists/src/assists/raw_string.rs')
-rw-r--r-- | crates/ra_assists/src/assists/raw_string.rs | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/crates/ra_assists/src/assists/raw_string.rs b/crates/ra_assists/src/assists/raw_string.rs index cb3a1e6e9..6f4b66c31 100644 --- a/crates/ra_assists/src/assists/raw_string.rs +++ b/crates/ra_assists/src/assists/raw_string.rs | |||
@@ -22,7 +22,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
22 | // r#"Hello, World!"#; | 22 | // r#"Hello, World!"#; |
23 | // } | 23 | // } |
24 | // ``` | 24 | // ``` |
25 | pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 25 | pub(crate) fn make_raw_string(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
26 | let token = ctx.find_token_at_offset(STRING)?; | 26 | let token = ctx.find_token_at_offset(STRING)?; |
27 | let text = token.text().as_str(); | 27 | let text = token.text().as_str(); |
28 | let usual_string_range = find_usual_string_range(text)?; | 28 | let usual_string_range = find_usual_string_range(text)?; |
@@ -41,7 +41,7 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As | |||
41 | if error.is_err() { | 41 | if error.is_err() { |
42 | return None; | 42 | return None; |
43 | } | 43 | } |
44 | ctx.add_action(AssistId("make_raw_string"), "make raw string", |edit| { | 44 | ctx.add_assist(AssistId("make_raw_string"), "make raw string", |edit| { |
45 | edit.target(token.text_range()); | 45 | edit.target(token.text_range()); |
46 | let max_hash_streak = count_hashes(&unescaped); | 46 | let max_hash_streak = count_hashes(&unescaped); |
47 | let mut hashes = String::with_capacity(max_hash_streak + 1); | 47 | let mut hashes = String::with_capacity(max_hash_streak + 1); |
@@ -49,8 +49,7 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As | |||
49 | hashes.push('#'); | 49 | hashes.push('#'); |
50 | } | 50 | } |
51 | edit.replace(token.text_range(), format!("r{}\"{}\"{}", hashes, unescaped, hashes)); | 51 | edit.replace(token.text_range(), format!("r{}\"{}\"{}", hashes, unescaped, hashes)); |
52 | }); | 52 | }) |
53 | ctx.build() | ||
54 | } | 53 | } |
55 | 54 | ||
56 | // Assist: make_usual_string | 55 | // Assist: make_usual_string |
@@ -68,11 +67,11 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As | |||
68 | // "Hello, \"World!\""; | 67 | // "Hello, \"World!\""; |
69 | // } | 68 | // } |
70 | // ``` | 69 | // ``` |
71 | pub(crate) fn make_usual_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 70 | pub(crate) fn make_usual_string(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
72 | let token = ctx.find_token_at_offset(RAW_STRING)?; | 71 | let token = ctx.find_token_at_offset(RAW_STRING)?; |
73 | let text = token.text().as_str(); | 72 | let text = token.text().as_str(); |
74 | let usual_string_range = find_usual_string_range(text)?; | 73 | let usual_string_range = find_usual_string_range(text)?; |
75 | ctx.add_action(AssistId("make_usual_string"), "make usual string", |edit| { | 74 | ctx.add_assist(AssistId("make_usual_string"), "make usual string", |edit| { |
76 | edit.target(token.text_range()); | 75 | edit.target(token.text_range()); |
77 | // parse inside string to escape `"` | 76 | // parse inside string to escape `"` |
78 | let start_of_inside = usual_string_range.start().to_usize() + 1; | 77 | let start_of_inside = usual_string_range.start().to_usize() + 1; |
@@ -80,8 +79,7 @@ pub(crate) fn make_usual_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option< | |||
80 | let inside_str = &text[start_of_inside..end_of_inside]; | 79 | let inside_str = &text[start_of_inside..end_of_inside]; |
81 | let escaped = inside_str.escape_default().to_string(); | 80 | let escaped = inside_str.escape_default().to_string(); |
82 | edit.replace(token.text_range(), format!("\"{}\"", escaped)); | 81 | edit.replace(token.text_range(), format!("\"{}\"", escaped)); |
83 | }); | 82 | }) |
84 | ctx.build() | ||
85 | } | 83 | } |
86 | 84 | ||
87 | // Assist: add_hash | 85 | // Assist: add_hash |
@@ -99,14 +97,13 @@ pub(crate) fn make_usual_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option< | |||
99 | // r##"Hello, World!"##; | 97 | // r##"Hello, World!"##; |
100 | // } | 98 | // } |
101 | // ``` | 99 | // ``` |
102 | pub(crate) fn add_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 100 | pub(crate) fn add_hash(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
103 | let token = ctx.find_token_at_offset(RAW_STRING)?; | 101 | let token = ctx.find_token_at_offset(RAW_STRING)?; |
104 | ctx.add_action(AssistId("add_hash"), "add hash to raw string", |edit| { | 102 | ctx.add_assist(AssistId("add_hash"), "add hash to raw string", |edit| { |
105 | edit.target(token.text_range()); | 103 | edit.target(token.text_range()); |
106 | edit.insert(token.text_range().start() + TextUnit::of_char('r'), "#"); | 104 | edit.insert(token.text_range().start() + TextUnit::of_char('r'), "#"); |
107 | edit.insert(token.text_range().end(), "#"); | 105 | edit.insert(token.text_range().end(), "#"); |
108 | }); | 106 | }) |
109 | ctx.build() | ||
110 | } | 107 | } |
111 | 108 | ||
112 | // Assist: remove_hash | 109 | // Assist: remove_hash |
@@ -124,14 +121,14 @@ pub(crate) fn add_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | |||
124 | // r"Hello, World!"; | 121 | // r"Hello, World!"; |
125 | // } | 122 | // } |
126 | // ``` | 123 | // ``` |
127 | pub(crate) fn remove_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 124 | pub(crate) fn remove_hash(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
128 | let token = ctx.find_token_at_offset(RAW_STRING)?; | 125 | let token = ctx.find_token_at_offset(RAW_STRING)?; |
129 | let text = token.text().as_str(); | 126 | let text = token.text().as_str(); |
130 | if text.starts_with("r\"") { | 127 | if text.starts_with("r\"") { |
131 | // no hash to remove | 128 | // no hash to remove |
132 | return None; | 129 | return None; |
133 | } | 130 | } |
134 | ctx.add_action(AssistId("remove_hash"), "remove hash from raw string", |edit| { | 131 | ctx.add_assist(AssistId("remove_hash"), "remove hash from raw string", |edit| { |
135 | edit.target(token.text_range()); | 132 | edit.target(token.text_range()); |
136 | let result = &text[2..text.len() - 1]; | 133 | let result = &text[2..text.len() - 1]; |
137 | let result = if result.starts_with("\"") { | 134 | let result = if result.starts_with("\"") { |
@@ -142,8 +139,7 @@ pub(crate) fn remove_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist | |||
142 | result.to_owned() | 139 | result.to_owned() |
143 | }; | 140 | }; |
144 | edit.replace(token.text_range(), format!("r{}", result)); | 141 | edit.replace(token.text_range(), format!("r{}", result)); |
145 | }); | 142 | }) |
146 | ctx.build() | ||
147 | } | 143 | } |
148 | 144 | ||
149 | fn count_hashes(s: &str) -> usize { | 145 | fn count_hashes(s: &str) -> usize { |