diff options
Diffstat (limited to 'crates/assists/src')
-rw-r--r-- | crates/assists/src/assist_config.rs | 5 | ||||
-rw-r--r-- | crates/assists/src/ast_transform.rs | 17 | ||||
-rw-r--r-- | crates/assists/src/handlers/add_missing_impl_members.rs | 25 | ||||
-rw-r--r-- | crates/assists/src/handlers/auto_import.rs | 23 | ||||
-rw-r--r-- | crates/assists/src/handlers/convert_integer_literal.rs | 701 | ||||
-rw-r--r-- | crates/assists/src/handlers/extract_struct_from_enum_variant.rs | 8 | ||||
-rw-r--r-- | crates/assists/src/handlers/fill_match_arms.rs | 4 | ||||
-rw-r--r-- | crates/assists/src/handlers/remove_dbg.rs | 9 | ||||
-rw-r--r-- | crates/assists/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/assists/src/tests.rs | 27 | ||||
-rw-r--r-- | crates/assists/src/tests/generated.rs | 13 | ||||
-rw-r--r-- | crates/assists/src/utils.rs | 21 |
12 files changed, 821 insertions, 34 deletions
diff --git a/crates/assists/src/assist_config.rs b/crates/assists/src/assist_config.rs index adf02edab..b24527ec4 100644 --- a/crates/assists/src/assist_config.rs +++ b/crates/assists/src/assist_config.rs | |||
@@ -4,6 +4,8 @@ | |||
4 | //! module, and we use to statically check that we only produce snippet | 4 | //! module, and we use to statically check that we only produce snippet |
5 | //! assists if we are allowed to. | 5 | //! assists if we are allowed to. |
6 | 6 | ||
7 | use hir::PrefixKind; | ||
8 | |||
7 | use crate::{utils::MergeBehaviour, AssistKind}; | 9 | use crate::{utils::MergeBehaviour, AssistKind}; |
8 | 10 | ||
9 | #[derive(Clone, Debug, PartialEq, Eq)] | 11 | #[derive(Clone, Debug, PartialEq, Eq)] |
@@ -37,10 +39,11 @@ impl Default for AssistConfig { | |||
37 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | 39 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
38 | pub struct InsertUseConfig { | 40 | pub struct InsertUseConfig { |
39 | pub merge: Option<MergeBehaviour>, | 41 | pub merge: Option<MergeBehaviour>, |
42 | pub prefix_kind: PrefixKind, | ||
40 | } | 43 | } |
41 | 44 | ||
42 | impl Default for InsertUseConfig { | 45 | impl Default for InsertUseConfig { |
43 | fn default() -> Self { | 46 | fn default() -> Self { |
44 | InsertUseConfig { merge: Some(MergeBehaviour::Full) } | 47 | InsertUseConfig { merge: Some(MergeBehaviour::Full), prefix_kind: PrefixKind::Plain } |
45 | } | 48 | } |
46 | } | 49 | } |
diff --git a/crates/assists/src/ast_transform.rs b/crates/assists/src/ast_transform.rs index 4307e0191..ac72f3f02 100644 --- a/crates/assists/src/ast_transform.rs +++ b/crates/assists/src/ast_transform.rs | |||
@@ -1,13 +1,14 @@ | |||
1 | //! `AstTransformer`s are functions that replace nodes in an AST and can be easily combined. | 1 | //! `AstTransformer`s are functions that replace nodes in an AST and can be easily combined. |
2 | use rustc_hash::FxHashMap; | ||
3 | |||
4 | use hir::{HirDisplay, PathResolution, SemanticsScope}; | 2 | use hir::{HirDisplay, PathResolution, SemanticsScope}; |
3 | use rustc_hash::FxHashMap; | ||
5 | use syntax::{ | 4 | use syntax::{ |
6 | algo::SyntaxRewriter, | 5 | algo::SyntaxRewriter, |
7 | ast::{self, AstNode}, | 6 | ast::{self, AstNode}, |
8 | SyntaxNode, | 7 | SyntaxNode, |
9 | }; | 8 | }; |
10 | 9 | ||
10 | use crate::utils::mod_path_to_ast; | ||
11 | |||
11 | pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: N) -> N { | 12 | pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: N) -> N { |
12 | SyntaxRewriter::from_fn(|element| match element { | 13 | SyntaxRewriter::from_fn(|element| match element { |
13 | syntax::SyntaxElement::Node(n) => { | 14 | syntax::SyntaxElement::Node(n) => { |
@@ -189,7 +190,7 @@ impl<'a> AstTransform<'a> for QualifyPaths<'a> { | |||
189 | match resolution { | 190 | match resolution { |
190 | PathResolution::Def(def) => { | 191 | PathResolution::Def(def) => { |
191 | let found_path = from.find_use_path(self.source_scope.db.upcast(), def)?; | 192 | let found_path = from.find_use_path(self.source_scope.db.upcast(), def)?; |
192 | let mut path = path_to_ast(found_path); | 193 | let mut path = mod_path_to_ast(&found_path); |
193 | 194 | ||
194 | let type_args = p | 195 | let type_args = p |
195 | .segment() | 196 | .segment() |
@@ -210,13 +211,3 @@ impl<'a> AstTransform<'a> for QualifyPaths<'a> { | |||
210 | } | 211 | } |
211 | } | 212 | } |
212 | } | 213 | } |
213 | |||
214 | pub(crate) fn path_to_ast(path: hir::ModPath) -> ast::Path { | ||
215 | let parse = ast::SourceFile::parse(&path.to_string()); | ||
216 | parse | ||
217 | .tree() | ||
218 | .syntax() | ||
219 | .descendants() | ||
220 | .find_map(ast::Path::cast) | ||
221 | .unwrap_or_else(|| panic!("failed to parse path {:?}, `{}`", path, path)) | ||
222 | } | ||
diff --git a/crates/assists/src/handlers/add_missing_impl_members.rs b/crates/assists/src/handlers/add_missing_impl_members.rs index 51b5a2eb0..4c400f287 100644 --- a/crates/assists/src/handlers/add_missing_impl_members.rs +++ b/crates/assists/src/handlers/add_missing_impl_members.rs | |||
@@ -820,4 +820,29 @@ impl Tr for () { | |||
820 | }"#, | 820 | }"#, |
821 | ) | 821 | ) |
822 | } | 822 | } |
823 | |||
824 | #[test] | ||
825 | fn weird_path() { | ||
826 | check_assist( | ||
827 | add_missing_impl_members, | ||
828 | r#" | ||
829 | trait Test { | ||
830 | fn foo(&self, x: crate) | ||
831 | } | ||
832 | impl Test for () { | ||
833 | <|> | ||
834 | } | ||
835 | "#, | ||
836 | r#" | ||
837 | trait Test { | ||
838 | fn foo(&self, x: crate) | ||
839 | } | ||
840 | impl Test for () { | ||
841 | fn foo(&self, x: crate) { | ||
842 | ${0:todo!()} | ||
843 | } | ||
844 | } | ||
845 | "#, | ||
846 | ) | ||
847 | } | ||
823 | } | 848 | } |
diff --git a/crates/assists/src/handlers/auto_import.rs b/crates/assists/src/handlers/auto_import.rs index fa524ffd9..d3ee98e5f 100644 --- a/crates/assists/src/handlers/auto_import.rs +++ b/crates/assists/src/handlers/auto_import.rs | |||
@@ -13,7 +13,10 @@ use syntax::{ | |||
13 | SyntaxNode, | 13 | SyntaxNode, |
14 | }; | 14 | }; |
15 | 15 | ||
16 | use crate::{utils::insert_use, AssistContext, AssistId, AssistKind, Assists, GroupLabel}; | 16 | use crate::{ |
17 | utils::insert_use, utils::mod_path_to_ast, AssistContext, AssistId, AssistKind, Assists, | ||
18 | GroupLabel, | ||
19 | }; | ||
17 | 20 | ||
18 | // Assist: auto_import | 21 | // Assist: auto_import |
19 | // | 22 | // |
@@ -54,7 +57,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> | |||
54 | range, | 57 | range, |
55 | |builder| { | 58 | |builder| { |
56 | let new_syntax = | 59 | let new_syntax = |
57 | insert_use(&scope, import.to_ast_path(), ctx.config.insert_use.merge); | 60 | insert_use(&scope, mod_path_to_ast(&import), ctx.config.insert_use.merge); |
58 | builder.replace(syntax.text_range(), new_syntax.to_string()) | 61 | builder.replace(syntax.text_range(), new_syntax.to_string()) |
59 | }, | 62 | }, |
60 | ); | 63 | ); |
@@ -191,12 +194,16 @@ impl AutoImportAssets { | |||
191 | _ => Some(candidate), | 194 | _ => Some(candidate), |
192 | }) | 195 | }) |
193 | .filter_map(|candidate| match candidate { | 196 | .filter_map(|candidate| match candidate { |
194 | Either::Left(module_def) => { | 197 | Either::Left(module_def) => self.module_with_name_to_import.find_use_path_prefixed( |
195 | self.module_with_name_to_import.find_use_path_prefixed(db, module_def) | 198 | db, |
196 | } | 199 | module_def, |
197 | Either::Right(macro_def) => { | 200 | ctx.config.insert_use.prefix_kind, |
198 | self.module_with_name_to_import.find_use_path_prefixed(db, macro_def) | 201 | ), |
199 | } | 202 | Either::Right(macro_def) => self.module_with_name_to_import.find_use_path_prefixed( |
203 | db, | ||
204 | macro_def, | ||
205 | ctx.config.insert_use.prefix_kind, | ||
206 | ), | ||
200 | }) | 207 | }) |
201 | .filter(|use_path| !use_path.segments.is_empty()) | 208 | .filter(|use_path| !use_path.segments.is_empty()) |
202 | .take(20) | 209 | .take(20) |
diff --git a/crates/assists/src/handlers/convert_integer_literal.rs b/crates/assists/src/handlers/convert_integer_literal.rs new file mode 100644 index 000000000..ea35e833a --- /dev/null +++ b/crates/assists/src/handlers/convert_integer_literal.rs | |||
@@ -0,0 +1,701 @@ | |||
1 | use syntax::{ast, AstNode, SmolStr}; | ||
2 | |||
3 | use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel}; | ||
4 | |||
5 | // Assist: convert_integer_literal | ||
6 | // | ||
7 | // Converts the base of integer literals to other bases. | ||
8 | // | ||
9 | // ``` | ||
10 | // const _: i32 = 10<|>; | ||
11 | // ``` | ||
12 | // -> | ||
13 | // ``` | ||
14 | // const _: i32 = 0b1010; | ||
15 | // ``` | ||
16 | pub(crate) fn convert_integer_literal(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
17 | let literal = ctx.find_node_at_offset::<ast::Literal>()?; | ||
18 | let range = literal.syntax().text_range(); | ||
19 | let group_id = GroupLabel("Convert integer base".into()); | ||
20 | |||
21 | let suffix = match literal.kind() { | ||
22 | ast::LiteralKind::IntNumber { suffix } => suffix, | ||
23 | _ => return None, | ||
24 | }; | ||
25 | let suffix_len = suffix.as_ref().map(|s| s.len()).unwrap_or(0); | ||
26 | let raw_literal_text = literal.syntax().to_string(); | ||
27 | |||
28 | // Gets the literal's text without the type suffix and without underscores. | ||
29 | let literal_text = raw_literal_text | ||
30 | .chars() | ||
31 | .take(raw_literal_text.len() - suffix_len) | ||
32 | .filter(|c| *c != '_') | ||
33 | .collect::<SmolStr>(); | ||
34 | let literal_base = IntegerLiteralBase::identify(&literal_text)?; | ||
35 | |||
36 | for base in IntegerLiteralBase::bases() { | ||
37 | if *base == literal_base { | ||
38 | continue; | ||
39 | } | ||
40 | |||
41 | let mut converted = literal_base.convert(&literal_text, base); | ||
42 | |||
43 | let label = if let Some(suffix) = &suffix { | ||
44 | format!("Convert {} ({}) to {}", &literal_text, suffix, &converted) | ||
45 | } else { | ||
46 | format!("Convert {} to {}", &literal_text, &converted) | ||
47 | }; | ||
48 | |||
49 | // Appends the type suffix back into the new literal if it exists. | ||
50 | if let Some(suffix) = &suffix { | ||
51 | converted.push_str(&suffix); | ||
52 | } | ||
53 | |||
54 | acc.add_group( | ||
55 | &group_id, | ||
56 | AssistId("convert_integer_literal", AssistKind::RefactorInline), | ||
57 | label, | ||
58 | range, | ||
59 | |builder| builder.replace(range, converted), | ||
60 | ); | ||
61 | } | ||
62 | |||
63 | Some(()) | ||
64 | } | ||
65 | |||
66 | #[derive(Debug, PartialEq, Eq)] | ||
67 | enum IntegerLiteralBase { | ||
68 | Binary, | ||
69 | Octal, | ||
70 | Decimal, | ||
71 | Hexadecimal, | ||
72 | } | ||
73 | |||
74 | impl IntegerLiteralBase { | ||
75 | fn identify(literal_text: &str) -> Option<Self> { | ||
76 | // We cannot express a literal in anything other than decimal in under 3 characters, so we return here if possible. | ||
77 | if literal_text.len() < 3 && literal_text.chars().all(|c| c.is_digit(10)) { | ||
78 | return Some(Self::Decimal); | ||
79 | } | ||
80 | |||
81 | let base = match &literal_text[..2] { | ||
82 | "0b" => Self::Binary, | ||
83 | "0o" => Self::Octal, | ||
84 | "0x" => Self::Hexadecimal, | ||
85 | _ => Self::Decimal, | ||
86 | }; | ||
87 | |||
88 | // Checks that all characters after the base prefix are all valid digits for that base. | ||
89 | if literal_text[base.prefix_len()..].chars().all(|c| c.is_digit(base.base())) { | ||
90 | Some(base) | ||
91 | } else { | ||
92 | None | ||
93 | } | ||
94 | } | ||
95 | |||
96 | fn convert(&self, literal_text: &str, to: &IntegerLiteralBase) -> String { | ||
97 | let digits = &literal_text[self.prefix_len()..]; | ||
98 | let value = u128::from_str_radix(digits, self.base()).unwrap(); | ||
99 | |||
100 | match to { | ||
101 | Self::Binary => format!("0b{:b}", value), | ||
102 | Self::Octal => format!("0o{:o}", value), | ||
103 | Self::Decimal => value.to_string(), | ||
104 | Self::Hexadecimal => format!("0x{:X}", value), | ||
105 | } | ||
106 | } | ||
107 | |||
108 | const fn base(&self) -> u32 { | ||
109 | match self { | ||
110 | Self::Binary => 2, | ||
111 | Self::Octal => 8, | ||
112 | Self::Decimal => 10, | ||
113 | Self::Hexadecimal => 16, | ||
114 | } | ||
115 | } | ||
116 | |||
117 | const fn prefix_len(&self) -> usize { | ||
118 | match self { | ||
119 | Self::Decimal => 0, | ||
120 | _ => 2, | ||
121 | } | ||
122 | } | ||
123 | |||
124 | const fn bases() -> &'static [IntegerLiteralBase] { | ||
125 | &[ | ||
126 | IntegerLiteralBase::Binary, | ||
127 | IntegerLiteralBase::Octal, | ||
128 | IntegerLiteralBase::Decimal, | ||
129 | IntegerLiteralBase::Hexadecimal, | ||
130 | ] | ||
131 | } | ||
132 | } | ||
133 | |||
134 | #[cfg(test)] | ||
135 | mod tests { | ||
136 | |||
137 | use super::*; | ||
138 | use crate::tests::{check_assist_by_label, check_assist_target}; | ||
139 | |||
140 | #[test] | ||
141 | fn binary_target() { | ||
142 | check_assist_target(convert_integer_literal, "const _: i32 = 0b1010<|>;", "0b1010"); | ||
143 | } | ||
144 | |||
145 | #[test] | ||
146 | fn octal_target() { | ||
147 | check_assist_target(convert_integer_literal, "const _: i32 = 0o12<|>;", "0o12"); | ||
148 | } | ||
149 | |||
150 | #[test] | ||
151 | fn decimal_target() { | ||
152 | check_assist_target(convert_integer_literal, "const _: i32 = 10<|>;", "10"); | ||
153 | } | ||
154 | |||
155 | #[test] | ||
156 | fn hexadecimal_target() { | ||
157 | check_assist_target(convert_integer_literal, "const _: i32 = 0xA<|>;", "0xA"); | ||
158 | } | ||
159 | |||
160 | #[test] | ||
161 | fn binary_target_with_underscores() { | ||
162 | check_assist_target(convert_integer_literal, "const _: i32 = 0b10_10<|>;", "0b10_10"); | ||
163 | } | ||
164 | |||
165 | #[test] | ||
166 | fn octal_target_with_underscores() { | ||
167 | check_assist_target(convert_integer_literal, "const _: i32 = 0o1_2<|>;", "0o1_2"); | ||
168 | } | ||
169 | |||
170 | #[test] | ||
171 | fn decimal_target_with_underscores() { | ||
172 | check_assist_target(convert_integer_literal, "const _: i32 = 1_0<|>;", "1_0"); | ||
173 | } | ||
174 | |||
175 | #[test] | ||
176 | fn hexadecimal_target_with_underscores() { | ||
177 | check_assist_target(convert_integer_literal, "const _: i32 = 0x_A<|>;", "0x_A"); | ||
178 | } | ||
179 | |||
180 | #[test] | ||
181 | fn convert_decimal_integer() { | ||
182 | let before = "const _: i32 = 1000<|>;"; | ||
183 | |||
184 | check_assist_by_label( | ||
185 | convert_integer_literal, | ||
186 | before, | ||
187 | "const _: i32 = 0b1111101000;", | ||
188 | "Convert 1000 to 0b1111101000", | ||
189 | ); | ||
190 | |||
191 | check_assist_by_label( | ||
192 | convert_integer_literal, | ||
193 | before, | ||
194 | "const _: i32 = 0o1750;", | ||
195 | "Convert 1000 to 0o1750", | ||
196 | ); | ||
197 | |||
198 | check_assist_by_label( | ||
199 | convert_integer_literal, | ||
200 | before, | ||
201 | "const _: i32 = 0x3E8;", | ||
202 | "Convert 1000 to 0x3E8", | ||
203 | ); | ||
204 | } | ||
205 | |||
206 | // Decimal numbers under 3 digits have a special case where they return early because we can't fit a | ||
207 | // other base's prefix, so we have a separate test for that. | ||
208 | #[test] | ||
209 | fn convert_small_decimal_integer() { | ||
210 | let before = "const _: i32 = 10<|>;"; | ||
211 | |||
212 | check_assist_by_label( | ||
213 | convert_integer_literal, | ||
214 | before, | ||
215 | "const _: i32 = 0b1010;", | ||
216 | "Convert 10 to 0b1010", | ||
217 | ); | ||
218 | |||
219 | check_assist_by_label( | ||
220 | convert_integer_literal, | ||
221 | before, | ||
222 | "const _: i32 = 0o12;", | ||
223 | "Convert 10 to 0o12", | ||
224 | ); | ||
225 | |||
226 | check_assist_by_label( | ||
227 | convert_integer_literal, | ||
228 | before, | ||
229 | "const _: i32 = 0xA;", | ||
230 | "Convert 10 to 0xA", | ||
231 | ); | ||
232 | } | ||
233 | |||
234 | #[test] | ||
235 | fn convert_hexadecimal_integer() { | ||
236 | let before = "const _: i32 = 0xFF<|>;"; | ||
237 | |||
238 | check_assist_by_label( | ||
239 | convert_integer_literal, | ||
240 | before, | ||
241 | "const _: i32 = 0b11111111;", | ||
242 | "Convert 0xFF to 0b11111111", | ||
243 | ); | ||
244 | |||
245 | check_assist_by_label( | ||
246 | convert_integer_literal, | ||
247 | before, | ||
248 | "const _: i32 = 0o377;", | ||
249 | "Convert 0xFF to 0o377", | ||
250 | ); | ||
251 | |||
252 | check_assist_by_label( | ||
253 | convert_integer_literal, | ||
254 | before, | ||
255 | "const _: i32 = 255;", | ||
256 | "Convert 0xFF to 255", | ||
257 | ); | ||
258 | } | ||
259 | |||
260 | #[test] | ||
261 | fn convert_binary_integer() { | ||
262 | let before = "const _: i32 = 0b11111111<|>;"; | ||
263 | |||
264 | check_assist_by_label( | ||
265 | convert_integer_literal, | ||
266 | before, | ||
267 | "const _: i32 = 0o377;", | ||
268 | "Convert 0b11111111 to 0o377", | ||
269 | ); | ||
270 | |||
271 | check_assist_by_label( | ||
272 | convert_integer_literal, | ||
273 | before, | ||
274 | "const _: i32 = 255;", | ||
275 | "Convert 0b11111111 to 255", | ||
276 | ); | ||
277 | |||
278 | check_assist_by_label( | ||
279 | convert_integer_literal, | ||
280 | before, | ||
281 | "const _: i32 = 0xFF;", | ||
282 | "Convert 0b11111111 to 0xFF", | ||
283 | ); | ||
284 | } | ||
285 | |||
286 | #[test] | ||
287 | fn convert_octal_integer() { | ||
288 | let before = "const _: i32 = 0o377<|>;"; | ||
289 | |||
290 | check_assist_by_label( | ||
291 | convert_integer_literal, | ||
292 | before, | ||
293 | "const _: i32 = 0b11111111;", | ||
294 | "Convert 0o377 to 0b11111111", | ||
295 | ); | ||
296 | |||
297 | check_assist_by_label( | ||
298 | convert_integer_literal, | ||
299 | before, | ||
300 | "const _: i32 = 255;", | ||
301 | "Convert 0o377 to 255", | ||
302 | ); | ||
303 | |||
304 | check_assist_by_label( | ||
305 | convert_integer_literal, | ||
306 | before, | ||
307 | "const _: i32 = 0xFF;", | ||
308 | "Convert 0o377 to 0xFF", | ||
309 | ); | ||
310 | } | ||
311 | |||
312 | #[test] | ||
313 | fn convert_decimal_integer_with_underscores() { | ||
314 | let before = "const _: i32 = 1_00_0<|>;"; | ||
315 | |||
316 | check_assist_by_label( | ||
317 | convert_integer_literal, | ||
318 | before, | ||
319 | "const _: i32 = 0b1111101000;", | ||
320 | "Convert 1000 to 0b1111101000", | ||
321 | ); | ||
322 | |||
323 | check_assist_by_label( | ||
324 | convert_integer_literal, | ||
325 | before, | ||
326 | "const _: i32 = 0o1750;", | ||
327 | "Convert 1000 to 0o1750", | ||
328 | ); | ||
329 | |||
330 | check_assist_by_label( | ||
331 | convert_integer_literal, | ||
332 | before, | ||
333 | "const _: i32 = 0x3E8;", | ||
334 | "Convert 1000 to 0x3E8", | ||
335 | ); | ||
336 | } | ||
337 | |||
338 | #[test] | ||
339 | fn convert_small_decimal_integer_with_underscores() { | ||
340 | let before = "const _: i32 = 1_0<|>;"; | ||
341 | |||
342 | check_assist_by_label( | ||
343 | convert_integer_literal, | ||
344 | before, | ||
345 | "const _: i32 = 0b1010;", | ||
346 | "Convert 10 to 0b1010", | ||
347 | ); | ||
348 | |||
349 | check_assist_by_label( | ||
350 | convert_integer_literal, | ||
351 | before, | ||
352 | "const _: i32 = 0o12;", | ||
353 | "Convert 10 to 0o12", | ||
354 | ); | ||
355 | |||
356 | check_assist_by_label( | ||
357 | convert_integer_literal, | ||
358 | before, | ||
359 | "const _: i32 = 0xA;", | ||
360 | "Convert 10 to 0xA", | ||
361 | ); | ||
362 | } | ||
363 | |||
364 | #[test] | ||
365 | fn convert_hexadecimal_integer_with_underscores() { | ||
366 | let before = "const _: i32 = 0x_F_F<|>;"; | ||
367 | |||
368 | check_assist_by_label( | ||
369 | convert_integer_literal, | ||
370 | before, | ||
371 | "const _: i32 = 0b11111111;", | ||
372 | "Convert 0xFF to 0b11111111", | ||
373 | ); | ||
374 | |||
375 | check_assist_by_label( | ||
376 | convert_integer_literal, | ||
377 | before, | ||
378 | "const _: i32 = 0o377;", | ||
379 | "Convert 0xFF to 0o377", | ||
380 | ); | ||
381 | |||
382 | check_assist_by_label( | ||
383 | convert_integer_literal, | ||
384 | before, | ||
385 | "const _: i32 = 255;", | ||
386 | "Convert 0xFF to 255", | ||
387 | ); | ||
388 | } | ||
389 | |||
390 | #[test] | ||
391 | fn convert_binary_integer_with_underscores() { | ||
392 | let before = "const _: i32 = 0b1111_1111<|>;"; | ||
393 | |||
394 | check_assist_by_label( | ||
395 | convert_integer_literal, | ||
396 | before, | ||
397 | "const _: i32 = 0o377;", | ||
398 | "Convert 0b11111111 to 0o377", | ||
399 | ); | ||
400 | |||
401 | check_assist_by_label( | ||
402 | convert_integer_literal, | ||
403 | before, | ||
404 | "const _: i32 = 255;", | ||
405 | "Convert 0b11111111 to 255", | ||
406 | ); | ||
407 | |||
408 | check_assist_by_label( | ||
409 | convert_integer_literal, | ||
410 | before, | ||
411 | "const _: i32 = 0xFF;", | ||
412 | "Convert 0b11111111 to 0xFF", | ||
413 | ); | ||
414 | } | ||
415 | |||
416 | #[test] | ||
417 | fn convert_octal_integer_with_underscores() { | ||
418 | let before = "const _: i32 = 0o3_77<|>;"; | ||
419 | |||
420 | check_assist_by_label( | ||
421 | convert_integer_literal, | ||
422 | before, | ||
423 | "const _: i32 = 0b11111111;", | ||
424 | "Convert 0o377 to 0b11111111", | ||
425 | ); | ||
426 | |||
427 | check_assist_by_label( | ||
428 | convert_integer_literal, | ||
429 | before, | ||
430 | "const _: i32 = 255;", | ||
431 | "Convert 0o377 to 255", | ||
432 | ); | ||
433 | |||
434 | check_assist_by_label( | ||
435 | convert_integer_literal, | ||
436 | before, | ||
437 | "const _: i32 = 0xFF;", | ||
438 | "Convert 0o377 to 0xFF", | ||
439 | ); | ||
440 | } | ||
441 | |||
442 | #[test] | ||
443 | fn convert_decimal_integer_with_suffix() { | ||
444 | let before = "const _: i32 = 1000i32<|>;"; | ||
445 | |||
446 | check_assist_by_label( | ||
447 | convert_integer_literal, | ||
448 | before, | ||
449 | "const _: i32 = 0b1111101000i32;", | ||
450 | "Convert 1000 (i32) to 0b1111101000", | ||
451 | ); | ||
452 | |||
453 | check_assist_by_label( | ||
454 | convert_integer_literal, | ||
455 | before, | ||
456 | "const _: i32 = 0o1750i32;", | ||
457 | "Convert 1000 (i32) to 0o1750", | ||
458 | ); | ||
459 | |||
460 | check_assist_by_label( | ||
461 | convert_integer_literal, | ||
462 | before, | ||
463 | "const _: i32 = 0x3E8i32;", | ||
464 | "Convert 1000 (i32) to 0x3E8", | ||
465 | ); | ||
466 | } | ||
467 | |||
468 | #[test] | ||
469 | fn convert_small_decimal_integer_with_suffix() { | ||
470 | let before = "const _: i32 = 10i32<|>;"; | ||
471 | |||
472 | check_assist_by_label( | ||
473 | convert_integer_literal, | ||
474 | before, | ||
475 | "const _: i32 = 0b1010i32;", | ||
476 | "Convert 10 (i32) to 0b1010", | ||
477 | ); | ||
478 | |||
479 | check_assist_by_label( | ||
480 | convert_integer_literal, | ||
481 | before, | ||
482 | "const _: i32 = 0o12i32;", | ||
483 | "Convert 10 (i32) to 0o12", | ||
484 | ); | ||
485 | |||
486 | check_assist_by_label( | ||
487 | convert_integer_literal, | ||
488 | before, | ||
489 | "const _: i32 = 0xAi32;", | ||
490 | "Convert 10 (i32) to 0xA", | ||
491 | ); | ||
492 | } | ||
493 | |||
494 | #[test] | ||
495 | fn convert_hexadecimal_integer_with_suffix() { | ||
496 | let before = "const _: i32 = 0xFFi32<|>;"; | ||
497 | |||
498 | check_assist_by_label( | ||
499 | convert_integer_literal, | ||
500 | before, | ||
501 | "const _: i32 = 0b11111111i32;", | ||
502 | "Convert 0xFF (i32) to 0b11111111", | ||
503 | ); | ||
504 | |||
505 | check_assist_by_label( | ||
506 | convert_integer_literal, | ||
507 | before, | ||
508 | "const _: i32 = 0o377i32;", | ||
509 | "Convert 0xFF (i32) to 0o377", | ||
510 | ); | ||
511 | |||
512 | check_assist_by_label( | ||
513 | convert_integer_literal, | ||
514 | before, | ||
515 | "const _: i32 = 255i32;", | ||
516 | "Convert 0xFF (i32) to 255", | ||
517 | ); | ||
518 | } | ||
519 | |||
520 | #[test] | ||
521 | fn convert_binary_integer_with_suffix() { | ||
522 | let before = "const _: i32 = 0b11111111i32<|>;"; | ||
523 | |||
524 | check_assist_by_label( | ||
525 | convert_integer_literal, | ||
526 | before, | ||
527 | "const _: i32 = 0o377i32;", | ||
528 | "Convert 0b11111111 (i32) to 0o377", | ||
529 | ); | ||
530 | |||
531 | check_assist_by_label( | ||
532 | convert_integer_literal, | ||
533 | before, | ||
534 | "const _: i32 = 255i32;", | ||
535 | "Convert 0b11111111 (i32) to 255", | ||
536 | ); | ||
537 | |||
538 | check_assist_by_label( | ||
539 | convert_integer_literal, | ||
540 | before, | ||
541 | "const _: i32 = 0xFFi32;", | ||
542 | "Convert 0b11111111 (i32) to 0xFF", | ||
543 | ); | ||
544 | } | ||
545 | |||
546 | #[test] | ||
547 | fn convert_octal_integer_with_suffix() { | ||
548 | let before = "const _: i32 = 0o377i32<|>;"; | ||
549 | |||
550 | check_assist_by_label( | ||
551 | convert_integer_literal, | ||
552 | before, | ||
553 | "const _: i32 = 0b11111111i32;", | ||
554 | "Convert 0o377 (i32) to 0b11111111", | ||
555 | ); | ||
556 | |||
557 | check_assist_by_label( | ||
558 | convert_integer_literal, | ||
559 | before, | ||
560 | "const _: i32 = 255i32;", | ||
561 | "Convert 0o377 (i32) to 255", | ||
562 | ); | ||
563 | |||
564 | check_assist_by_label( | ||
565 | convert_integer_literal, | ||
566 | before, | ||
567 | "const _: i32 = 0xFFi32;", | ||
568 | "Convert 0o377 (i32) to 0xFF", | ||
569 | ); | ||
570 | } | ||
571 | |||
572 | #[test] | ||
573 | fn convert_decimal_integer_with_underscores_and_suffix() { | ||
574 | let before = "const _: i32 = 1_00_0i32<|>;"; | ||
575 | |||
576 | check_assist_by_label( | ||
577 | convert_integer_literal, | ||
578 | before, | ||
579 | "const _: i32 = 0b1111101000i32;", | ||
580 | "Convert 1000 (i32) to 0b1111101000", | ||
581 | ); | ||
582 | |||
583 | check_assist_by_label( | ||
584 | convert_integer_literal, | ||
585 | before, | ||
586 | "const _: i32 = 0o1750i32;", | ||
587 | "Convert 1000 (i32) to 0o1750", | ||
588 | ); | ||
589 | |||
590 | check_assist_by_label( | ||
591 | convert_integer_literal, | ||
592 | before, | ||
593 | "const _: i32 = 0x3E8i32;", | ||
594 | "Convert 1000 (i32) to 0x3E8", | ||
595 | ); | ||
596 | } | ||
597 | |||
598 | #[test] | ||
599 | fn convert_small_decimal_integer_with_underscores_and_suffix() { | ||
600 | let before = "const _: i32 = 1_0i32<|>;"; | ||
601 | |||
602 | check_assist_by_label( | ||
603 | convert_integer_literal, | ||
604 | before, | ||
605 | "const _: i32 = 0b1010i32;", | ||
606 | "Convert 10 (i32) to 0b1010", | ||
607 | ); | ||
608 | |||
609 | check_assist_by_label( | ||
610 | convert_integer_literal, | ||
611 | before, | ||
612 | "const _: i32 = 0o12i32;", | ||
613 | "Convert 10 (i32) to 0o12", | ||
614 | ); | ||
615 | |||
616 | check_assist_by_label( | ||
617 | convert_integer_literal, | ||
618 | before, | ||
619 | "const _: i32 = 0xAi32;", | ||
620 | "Convert 10 (i32) to 0xA", | ||
621 | ); | ||
622 | } | ||
623 | |||
624 | #[test] | ||
625 | fn convert_hexadecimal_integer_with_underscores_and_suffix() { | ||
626 | let before = "const _: i32 = 0x_F_Fi32<|>;"; | ||
627 | |||
628 | check_assist_by_label( | ||
629 | convert_integer_literal, | ||
630 | before, | ||
631 | "const _: i32 = 0b11111111i32;", | ||
632 | "Convert 0xFF (i32) to 0b11111111", | ||
633 | ); | ||
634 | |||
635 | check_assist_by_label( | ||
636 | convert_integer_literal, | ||
637 | before, | ||
638 | "const _: i32 = 0o377i32;", | ||
639 | "Convert 0xFF (i32) to 0o377", | ||
640 | ); | ||
641 | |||
642 | check_assist_by_label( | ||
643 | convert_integer_literal, | ||
644 | before, | ||
645 | "const _: i32 = 255i32;", | ||
646 | "Convert 0xFF (i32) to 255", | ||
647 | ); | ||
648 | } | ||
649 | |||
650 | #[test] | ||
651 | fn convert_binary_integer_with_underscores_and_suffix() { | ||
652 | let before = "const _: i32 = 0b1111_1111i32<|>;"; | ||
653 | |||
654 | check_assist_by_label( | ||
655 | convert_integer_literal, | ||
656 | before, | ||
657 | "const _: i32 = 0o377i32;", | ||
658 | "Convert 0b11111111 (i32) to 0o377", | ||
659 | ); | ||
660 | |||
661 | check_assist_by_label( | ||
662 | convert_integer_literal, | ||
663 | before, | ||
664 | "const _: i32 = 255i32;", | ||
665 | "Convert 0b11111111 (i32) to 255", | ||
666 | ); | ||
667 | |||
668 | check_assist_by_label( | ||
669 | convert_integer_literal, | ||
670 | before, | ||
671 | "const _: i32 = 0xFFi32;", | ||
672 | "Convert 0b11111111 (i32) to 0xFF", | ||
673 | ); | ||
674 | } | ||
675 | |||
676 | #[test] | ||
677 | fn convert_octal_integer_with_underscores_and_suffix() { | ||
678 | let before = "const _: i32 = 0o3_77i32<|>;"; | ||
679 | |||
680 | check_assist_by_label( | ||
681 | convert_integer_literal, | ||
682 | before, | ||
683 | "const _: i32 = 0b11111111i32;", | ||
684 | "Convert 0o377 (i32) to 0b11111111", | ||
685 | ); | ||
686 | |||
687 | check_assist_by_label( | ||
688 | convert_integer_literal, | ||
689 | before, | ||
690 | "const _: i32 = 255i32;", | ||
691 | "Convert 0o377 (i32) to 255", | ||
692 | ); | ||
693 | |||
694 | check_assist_by_label( | ||
695 | convert_integer_literal, | ||
696 | before, | ||
697 | "const _: i32 = 0xFFi32;", | ||
698 | "Convert 0o377 (i32) to 0xFF", | ||
699 | ); | ||
700 | } | ||
701 | } | ||
diff --git a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs index f5f03ef36..7f4f80b23 100644 --- a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs | |||
@@ -10,9 +10,10 @@ use syntax::{ | |||
10 | }; | 10 | }; |
11 | 11 | ||
12 | use crate::{ | 12 | use crate::{ |
13 | assist_context::AssistBuilder, utils::insert_use, AssistContext, AssistId, AssistKind, Assists, | 13 | assist_context::AssistBuilder, |
14 | utils::{insert_use, mod_path_to_ast, ImportScope}, | ||
15 | AssistContext, AssistId, AssistKind, Assists, | ||
14 | }; | 16 | }; |
15 | use insert_use::ImportScope; | ||
16 | 17 | ||
17 | // Assist: extract_struct_from_enum_variant | 18 | // Assist: extract_struct_from_enum_variant |
18 | // | 19 | // |
@@ -111,7 +112,8 @@ fn insert_import( | |||
111 | let scope = ImportScope::find_insert_use_container(path.syntax(), ctx)?; | 112 | let scope = ImportScope::find_insert_use_container(path.syntax(), ctx)?; |
112 | let syntax = scope.as_syntax_node(); | 113 | let syntax = scope.as_syntax_node(); |
113 | 114 | ||
114 | let new_syntax = insert_use(&scope, mod_path.to_ast_path(), ctx.config.insert_use.merge); | 115 | let new_syntax = |
116 | insert_use(&scope, mod_path_to_ast(&mod_path), ctx.config.insert_use.merge); | ||
115 | // FIXME: this will currently panic as multiple imports will have overlapping text ranges | 117 | // FIXME: this will currently panic as multiple imports will have overlapping text ranges |
116 | builder.replace(syntax.text_range(), new_syntax.to_string()) | 118 | builder.replace(syntax.text_range(), new_syntax.to_string()) |
117 | } | 119 | } |
diff --git a/crates/assists/src/handlers/fill_match_arms.rs b/crates/assists/src/handlers/fill_match_arms.rs index 3d9bdb2bf..676f5ad92 100644 --- a/crates/assists/src/handlers/fill_match_arms.rs +++ b/crates/assists/src/handlers/fill_match_arms.rs | |||
@@ -7,7 +7,7 @@ use syntax::ast::{self, make, AstNode, MatchArm, NameOwner, Pat}; | |||
7 | use test_utils::mark; | 7 | use test_utils::mark; |
8 | 8 | ||
9 | use crate::{ | 9 | use crate::{ |
10 | utils::{render_snippet, Cursor, FamousDefs}, | 10 | utils::{mod_path_to_ast, render_snippet, Cursor, FamousDefs}, |
11 | AssistContext, AssistId, AssistKind, Assists, | 11 | AssistContext, AssistId, AssistKind, Assists, |
12 | }; | 12 | }; |
13 | 13 | ||
@@ -192,7 +192,7 @@ fn resolve_tuple_of_enum_def( | |||
192 | } | 192 | } |
193 | 193 | ||
194 | fn build_pat(db: &RootDatabase, module: hir::Module, var: hir::EnumVariant) -> Option<ast::Pat> { | 194 | fn build_pat(db: &RootDatabase, module: hir::Module, var: hir::EnumVariant) -> Option<ast::Pat> { |
195 | let path = crate::ast_transform::path_to_ast(module.find_use_path(db, ModuleDef::from(var))?); | 195 | let path = mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var))?); |
196 | 196 | ||
197 | // FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though | 197 | // FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though |
198 | let pat: ast::Pat = match var.source(db).value.kind() { | 198 | let pat: ast::Pat = match var.source(db).value.kind() { |
diff --git a/crates/assists/src/handlers/remove_dbg.rs b/crates/assists/src/handlers/remove_dbg.rs index a8ab2aecc..e10616779 100644 --- a/crates/assists/src/handlers/remove_dbg.rs +++ b/crates/assists/src/handlers/remove_dbg.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use syntax::{ | 1 | use syntax::{ |
2 | ast::{self, AstNode}, | 2 | ast::{self, AstNode}, |
3 | SyntaxElement, TextRange, TextSize, T, | 3 | SyntaxElement, SyntaxKind, TextRange, TextSize, T, |
4 | }; | 4 | }; |
5 | 5 | ||
6 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 6 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
@@ -117,7 +117,10 @@ fn needs_parentheses_around_macro_contents(macro_contents: Vec<SyntaxElement>) - | |||
117 | } | 117 | } |
118 | symbol_kind => { | 118 | symbol_kind => { |
119 | let symbol_not_in_bracket = unpaired_brackets_in_contents.is_empty(); | 119 | let symbol_not_in_bracket = unpaired_brackets_in_contents.is_empty(); |
120 | if symbol_not_in_bracket && symbol_kind.is_punct() { | 120 | if symbol_not_in_bracket |
121 | && symbol_kind != SyntaxKind::COLON | ||
122 | && symbol_kind.is_punct() | ||
123 | { | ||
121 | return true; | 124 | return true; |
122 | } | 125 | } |
123 | } | 126 | } |
@@ -159,6 +162,8 @@ fn foo(n: usize) { | |||
159 | } | 162 | } |
160 | ", | 163 | ", |
161 | ); | 164 | ); |
165 | |||
166 | check_assist(remove_dbg, "<|>dbg!(Foo::foo_test()).bar()", "Foo::foo_test().bar()"); | ||
162 | } | 167 | } |
163 | 168 | ||
164 | #[test] | 169 | #[test] |
diff --git a/crates/assists/src/lib.rs b/crates/assists/src/lib.rs index cbac53e71..a2bec818c 100644 --- a/crates/assists/src/lib.rs +++ b/crates/assists/src/lib.rs | |||
@@ -128,6 +128,7 @@ mod handlers { | |||
128 | mod auto_import; | 128 | mod auto_import; |
129 | mod change_return_type_to_result; | 129 | mod change_return_type_to_result; |
130 | mod change_visibility; | 130 | mod change_visibility; |
131 | mod convert_integer_literal; | ||
131 | mod early_return; | 132 | mod early_return; |
132 | mod expand_glob_import; | 133 | mod expand_glob_import; |
133 | mod extract_struct_from_enum_variant; | 134 | mod extract_struct_from_enum_variant; |
@@ -172,6 +173,7 @@ mod handlers { | |||
172 | auto_import::auto_import, | 173 | auto_import::auto_import, |
173 | change_return_type_to_result::change_return_type_to_result, | 174 | change_return_type_to_result::change_return_type_to_result, |
174 | change_visibility::change_visibility, | 175 | change_visibility::change_visibility, |
176 | convert_integer_literal::convert_integer_literal, | ||
175 | early_return::convert_to_guarded_return, | 177 | early_return::convert_to_guarded_return, |
176 | expand_glob_import::expand_glob_import, | 178 | expand_glob_import::expand_glob_import, |
177 | extract_struct_from_enum_variant::extract_struct_from_enum_variant, | 179 | extract_struct_from_enum_variant::extract_struct_from_enum_variant, |
diff --git a/crates/assists/src/tests.rs b/crates/assists/src/tests.rs index ba1fb543b..2b687decf 100644 --- a/crates/assists/src/tests.rs +++ b/crates/assists/src/tests.rs | |||
@@ -15,18 +15,30 @@ pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { | |||
15 | 15 | ||
16 | pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { | 16 | pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { |
17 | let ra_fixture_after = trim_indent(ra_fixture_after); | 17 | let ra_fixture_after = trim_indent(ra_fixture_after); |
18 | check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after)); | 18 | check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), None); |
19 | } | ||
20 | |||
21 | // There is no way to choose what assist within a group you want to test against, | ||
22 | // so this is here to allow you choose. | ||
23 | pub(crate) fn check_assist_by_label( | ||
24 | assist: Handler, | ||
25 | ra_fixture_before: &str, | ||
26 | ra_fixture_after: &str, | ||
27 | label: &str, | ||
28 | ) { | ||
29 | let ra_fixture_after = trim_indent(ra_fixture_after); | ||
30 | check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), Some(label)); | ||
19 | } | 31 | } |
20 | 32 | ||
21 | // FIXME: instead of having a separate function here, maybe use | 33 | // FIXME: instead of having a separate function here, maybe use |
22 | // `extract_ranges` and mark the target as `<target> </target>` in the | 34 | // `extract_ranges` and mark the target as `<target> </target>` in the |
23 | // fixture? | 35 | // fixture? |
24 | pub(crate) fn check_assist_target(assist: Handler, ra_fixture: &str, target: &str) { | 36 | pub(crate) fn check_assist_target(assist: Handler, ra_fixture: &str, target: &str) { |
25 | check(assist, ra_fixture, ExpectedResult::Target(target)); | 37 | check(assist, ra_fixture, ExpectedResult::Target(target), None); |
26 | } | 38 | } |
27 | 39 | ||
28 | pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) { | 40 | pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) { |
29 | check(assist, ra_fixture, ExpectedResult::NotApplicable); | 41 | check(assist, ra_fixture, ExpectedResult::NotApplicable, None); |
30 | } | 42 | } |
31 | 43 | ||
32 | fn check_doc_test(assist_id: &str, before: &str, after: &str) { | 44 | fn check_doc_test(assist_id: &str, before: &str, after: &str) { |
@@ -65,7 +77,7 @@ enum ExpectedResult<'a> { | |||
65 | Target(&'a str), | 77 | Target(&'a str), |
66 | } | 78 | } |
67 | 79 | ||
68 | fn check(handler: Handler, before: &str, expected: ExpectedResult) { | 80 | fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label: Option<&str>) { |
69 | let (db, file_with_caret_id, range_or_offset) = RootDatabase::with_range_or_offset(before); | 81 | let (db, file_with_caret_id, range_or_offset) = RootDatabase::with_range_or_offset(before); |
70 | let text_without_caret = db.file_text(file_with_caret_id).to_string(); | 82 | let text_without_caret = db.file_text(file_with_caret_id).to_string(); |
71 | 83 | ||
@@ -77,7 +89,12 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult) { | |||
77 | let mut acc = Assists::new_resolved(&ctx); | 89 | let mut acc = Assists::new_resolved(&ctx); |
78 | handler(&mut acc, &ctx); | 90 | handler(&mut acc, &ctx); |
79 | let mut res = acc.finish_resolved(); | 91 | let mut res = acc.finish_resolved(); |
80 | let assist = res.pop(); | 92 | |
93 | let assist = match assist_label { | ||
94 | Some(label) => res.into_iter().find(|resolved| resolved.assist.label == label), | ||
95 | None => res.pop(), | ||
96 | }; | ||
97 | |||
81 | match (assist, expected) { | 98 | match (assist, expected) { |
82 | (Some(assist), ExpectedResult::After(after)) => { | 99 | (Some(assist), ExpectedResult::After(after)) => { |
83 | let mut source_change = assist.source_change; | 100 | let mut source_change = assist.source_change; |
diff --git a/crates/assists/src/tests/generated.rs b/crates/assists/src/tests/generated.rs index 27d15adb0..7f6e98a54 100644 --- a/crates/assists/src/tests/generated.rs +++ b/crates/assists/src/tests/generated.rs | |||
@@ -204,6 +204,19 @@ pub(crate) fn frobnicate() {} | |||
204 | } | 204 | } |
205 | 205 | ||
206 | #[test] | 206 | #[test] |
207 | fn doctest_convert_integer_literal() { | ||
208 | check_doc_test( | ||
209 | "convert_integer_literal", | ||
210 | r#####" | ||
211 | const _: i32 = 10<|>; | ||
212 | "#####, | ||
213 | r#####" | ||
214 | const _: i32 = 0b1010; | ||
215 | "#####, | ||
216 | ) | ||
217 | } | ||
218 | |||
219 | #[test] | ||
207 | fn doctest_convert_to_guarded_return() { | 220 | fn doctest_convert_to_guarded_return() { |
208 | check_doc_test( | 221 | check_doc_test( |
209 | "convert_to_guarded_return", | 222 | "convert_to_guarded_return", |
diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index b0511ceb6..eb69c49a4 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs | |||
@@ -19,6 +19,27 @@ use crate::assist_config::SnippetCap; | |||
19 | pub use insert_use::MergeBehaviour; | 19 | pub use insert_use::MergeBehaviour; |
20 | pub(crate) use insert_use::{insert_use, ImportScope}; | 20 | pub(crate) use insert_use::{insert_use, ImportScope}; |
21 | 21 | ||
22 | pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path { | ||
23 | let mut segments = Vec::new(); | ||
24 | let mut is_abs = false; | ||
25 | match path.kind { | ||
26 | hir::PathKind::Plain => {} | ||
27 | hir::PathKind::Super(0) => segments.push(make::path_segment_self()), | ||
28 | hir::PathKind::Super(n) => segments.extend((0..n).map(|_| make::path_segment_super())), | ||
29 | hir::PathKind::DollarCrate(_) | hir::PathKind::Crate => { | ||
30 | segments.push(make::path_segment_crate()) | ||
31 | } | ||
32 | hir::PathKind::Abs => is_abs = true, | ||
33 | } | ||
34 | |||
35 | segments.extend( | ||
36 | path.segments | ||
37 | .iter() | ||
38 | .map(|segment| make::path_segment(make::name_ref(&segment.to_string()))), | ||
39 | ); | ||
40 | make::path_from_segments(segments, is_abs) | ||
41 | } | ||
42 | |||
22 | pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr { | 43 | pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr { |
23 | extract_trivial_expression(&block) | 44 | extract_trivial_expression(&block) |
24 | .filter(|expr| !expr.syntax().text().contains_char('\n')) | 45 | .filter(|expr| !expr.syntax().text().contains_char('\n')) |