aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-05-02 21:45:44 +0100
committerKirill Bulatov <[email protected]>2020-05-02 21:47:51 +0100
commit2fd054f276e6fd75237b476622d03eef2f18430a (patch)
tree9e5c592eca72244fca4d77a1a6bef4a2f231484d
parent9f0ed7ed92a978da73d19fe9f23b3953ca0a1de4 (diff)
Fix derive argument parsing
-rw-r--r--crates/ra_ide/src/completion/complete_attribute.rs66
1 files changed, 40 insertions, 26 deletions
diff --git a/crates/ra_ide/src/completion/complete_attribute.rs b/crates/ra_ide/src/completion/complete_attribute.rs
index 346ba9e7a..20e6edc17 100644
--- a/crates/ra_ide/src/completion/complete_attribute.rs
+++ b/crates/ra_ide/src/completion/complete_attribute.rs
@@ -169,15 +169,30 @@ fn parse_derive_input(derive_input: ast::TokenTree) -> Result<FxHashSet<String>,
169 if left_paren.kind() == SyntaxKind::L_PAREN 169 if left_paren.kind() == SyntaxKind::L_PAREN
170 && right_paren.kind() == SyntaxKind::R_PAREN => 170 && right_paren.kind() == SyntaxKind::R_PAREN =>
171 { 171 {
172 Ok(derive_input 172 let mut input_derives = FxHashSet::default();
173 let mut current_derive = String::new();
174 for token in derive_input
173 .syntax() 175 .syntax()
174 .children_with_tokens() 176 .children_with_tokens()
175 .filter_map(|child| child.into_token()) 177 .filter_map(|token| token.into_token())
176 .skip_while(|child| child != &left_paren) 178 .skip_while(|token| token != &left_paren)
177 .take_while(|child| child != &right_paren) 179 .skip(1)
178 .filter(|child| child.kind() == SyntaxKind::IDENT) 180 .take_while(|token| token != &right_paren)
179 .map(|child| child.to_string()) 181 {
180 .collect()) 182 if SyntaxKind::COMMA == token.kind() {
183 if !current_derive.is_empty() {
184 input_derives.insert(current_derive);
185 current_derive = String::new();
186 }
187 } else {
188 current_derive.push_str(token.to_string().trim());
189 }
190 }
191
192 if !current_derive.is_empty() {
193 input_derives.insert(current_derive);
194 }
195 Ok(input_derives)
181 } 196 }
182 _ => Err(()), 197 _ => Err(()),
183 } 198 }
@@ -188,8 +203,7 @@ fn get_derive_names_in_scope(ctx: &CompletionContext) -> FxHashSet<String> {
188 ctx.scope().process_all_names(&mut |name, scope_def| { 203 ctx.scope().process_all_names(&mut |name, scope_def| {
189 if let hir::ScopeDef::MacroDef(mac) = scope_def { 204 if let hir::ScopeDef::MacroDef(mac) = scope_def {
190 if mac.is_derive_macro() { 205 if mac.is_derive_macro() {
191 let name_string = name.to_string(); 206 result.insert(name.to_string());
192 result.insert(name_string);
193 } 207 }
194 } 208 }
195 }); 209 });
@@ -321,7 +335,7 @@ mod tests {
321 assert_debug_snapshot!( 335 assert_debug_snapshot!(
322 do_attr_completion( 336 do_attr_completion(
323 r" 337 r"
324 #[derive(Whatever, PartialEq, <|>)] 338 #[derive(serde::Serialize, PartialEq, <|>)]
325 struct Test {} 339 struct Test {}
326 ", 340 ",
327 ), 341 ),
@@ -329,57 +343,57 @@ mod tests {
329 [ 343 [
330 CompletionItem { 344 CompletionItem {
331 label: "Clone", 345 label: "Clone",
332 source_range: 51..51, 346 source_range: 59..59,
333 delete: 51..51, 347 delete: 59..59,
334 insert: "Clone", 348 insert: "Clone",
335 kind: Attribute, 349 kind: Attribute,
336 }, 350 },
337 CompletionItem { 351 CompletionItem {
338 label: "Copy, Clone", 352 label: "Copy, Clone",
339 source_range: 51..51, 353 source_range: 59..59,
340 delete: 51..51, 354 delete: 59..59,
341 insert: "Copy, Clone", 355 insert: "Copy, Clone",
342 kind: Attribute, 356 kind: Attribute,
343 }, 357 },
344 CompletionItem { 358 CompletionItem {
345 label: "Debug", 359 label: "Debug",
346 source_range: 51..51, 360 source_range: 59..59,
347 delete: 51..51, 361 delete: 59..59,
348 insert: "Debug", 362 insert: "Debug",
349 kind: Attribute, 363 kind: Attribute,
350 }, 364 },
351 CompletionItem { 365 CompletionItem {
352 label: "Default", 366 label: "Default",
353 source_range: 51..51, 367 source_range: 59..59,
354 delete: 51..51, 368 delete: 59..59,
355 insert: "Default", 369 insert: "Default",
356 kind: Attribute, 370 kind: Attribute,
357 }, 371 },
358 CompletionItem { 372 CompletionItem {
359 label: "Eq", 373 label: "Eq",
360 source_range: 51..51, 374 source_range: 59..59,
361 delete: 51..51, 375 delete: 59..59,
362 insert: "Eq", 376 insert: "Eq",
363 kind: Attribute, 377 kind: Attribute,
364 }, 378 },
365 CompletionItem { 379 CompletionItem {
366 label: "Hash", 380 label: "Hash",
367 source_range: 51..51, 381 source_range: 59..59,
368 delete: 51..51, 382 delete: 59..59,
369 insert: "Hash", 383 insert: "Hash",
370 kind: Attribute, 384 kind: Attribute,
371 }, 385 },
372 CompletionItem { 386 CompletionItem {
373 label: "Ord, PartialOrd, Eq", 387 label: "Ord, PartialOrd, Eq",
374 source_range: 51..51, 388 source_range: 59..59,
375 delete: 51..51, 389 delete: 59..59,
376 insert: "Ord, PartialOrd, Eq", 390 insert: "Ord, PartialOrd, Eq",
377 kind: Attribute, 391 kind: Attribute,
378 }, 392 },
379 CompletionItem { 393 CompletionItem {
380 label: "PartialOrd", 394 label: "PartialOrd",
381 source_range: 51..51, 395 source_range: 59..59,
382 delete: 51..51, 396 delete: 59..59,
383 insert: "PartialOrd", 397 insert: "PartialOrd",
384 kind: Attribute, 398 kind: Attribute,
385 }, 399 },