diff options
Diffstat (limited to 'crates/ide')
48 files changed, 2992 insertions, 2362 deletions
diff --git a/crates/ide/Cargo.toml b/crates/ide/Cargo.toml index 4d483580d..bb28cca4d 100644 --- a/crates/ide/Cargo.toml +++ b/crates/ide/Cargo.toml | |||
@@ -12,7 +12,7 @@ doctest = false | |||
12 | [dependencies] | 12 | [dependencies] |
13 | either = "1.5.3" | 13 | either = "1.5.3" |
14 | indexmap = "1.4.0" | 14 | indexmap = "1.4.0" |
15 | itertools = "0.9.0" | 15 | itertools = "0.10.0" |
16 | log = "0.4.8" | 16 | log = "0.4.8" |
17 | rustc-hash = "1.1.0" | 17 | rustc-hash = "1.1.0" |
18 | oorandom = "11.1.2" | 18 | oorandom = "11.1.2" |
@@ -36,4 +36,4 @@ completion = { path = "../completion", version = "0.0.0" } | |||
36 | hir = { path = "../hir", version = "0.0.0" } | 36 | hir = { path = "../hir", version = "0.0.0" } |
37 | 37 | ||
38 | [dev-dependencies] | 38 | [dev-dependencies] |
39 | expect-test = "1.0" | 39 | expect-test = "1.1" |
diff --git a/crates/ide/src/call_hierarchy.rs b/crates/ide/src/call_hierarchy.rs index 60e0cd4ad..e8999a7f3 100644 --- a/crates/ide/src/call_hierarchy.rs +++ b/crates/ide/src/call_hierarchy.rs | |||
@@ -5,10 +5,10 @@ use indexmap::IndexMap; | |||
5 | use hir::Semantics; | 5 | use hir::Semantics; |
6 | use ide_db::call_info::FnCallNode; | 6 | use ide_db::call_info::FnCallNode; |
7 | use ide_db::RootDatabase; | 7 | use ide_db::RootDatabase; |
8 | use syntax::{ast, match_ast, AstNode, TextRange}; | 8 | use syntax::{ast, AstNode, TextRange}; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | display::ToNav, goto_definition, references, FilePosition, NavigationTarget, RangeInfo, | 11 | display::TryToNav, goto_definition, references, FilePosition, NavigationTarget, RangeInfo, |
12 | }; | 12 | }; |
13 | 13 | ||
14 | #[derive(Debug, Clone)] | 14 | #[derive(Debug, Clone)] |
@@ -47,28 +47,23 @@ pub(crate) fn incoming_calls(db: &RootDatabase, position: FilePosition) -> Optio | |||
47 | 47 | ||
48 | let mut calls = CallLocations::default(); | 48 | let mut calls = CallLocations::default(); |
49 | 49 | ||
50 | for reference in refs.info.references() { | 50 | for (&file_id, references) in refs.info.references().iter() { |
51 | let file_id = reference.file_range.file_id; | ||
52 | let file = sema.parse(file_id); | 51 | let file = sema.parse(file_id); |
53 | let file = file.syntax(); | 52 | let file = file.syntax(); |
54 | let token = file.token_at_offset(reference.file_range.range.start()).next()?; | 53 | for reference in references { |
55 | let token = sema.descend_into_macros(token); | 54 | let token = file.token_at_offset(reference.range.start()).next()?; |
56 | let syntax = token.parent(); | 55 | let token = sema.descend_into_macros(token); |
57 | 56 | let syntax = token.parent(); | |
58 | // This target is the containing function | 57 | |
59 | if let Some(nav) = syntax.ancestors().find_map(|node| { | 58 | // This target is the containing function |
60 | match_ast! { | 59 | if let Some(nav) = syntax.ancestors().find_map(|node| { |
61 | match node { | 60 | let fn_ = ast::Fn::cast(node)?; |
62 | ast::Fn(it) => { | 61 | let def = sema.to_def(&fn_)?; |
63 | let def = sema.to_def(&it)?; | 62 | def.try_to_nav(sema.db) |
64 | Some(def.to_nav(sema.db)) | 63 | }) { |
65 | }, | 64 | let relative_range = reference.range; |
66 | _ => None, | 65 | calls.add(&nav, relative_range); |
67 | } | ||
68 | } | 66 | } |
69 | }) { | ||
70 | let relative_range = reference.file_range.range; | ||
71 | calls.add(&nav, relative_range); | ||
72 | } | 67 | } |
73 | } | 68 | } |
74 | 69 | ||
@@ -91,29 +86,21 @@ pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Optio | |||
91 | .filter_map(|node| FnCallNode::with_node_exact(&node)) | 86 | .filter_map(|node| FnCallNode::with_node_exact(&node)) |
92 | .filter_map(|call_node| { | 87 | .filter_map(|call_node| { |
93 | let name_ref = call_node.name_ref()?; | 88 | let name_ref = call_node.name_ref()?; |
94 | 89 | let func_target = match call_node { | |
95 | if let Some(func_target) = match &call_node { | ||
96 | FnCallNode::CallExpr(expr) => { | 90 | FnCallNode::CallExpr(expr) => { |
97 | //FIXME: Type::as_callable is broken | 91 | //FIXME: Type::as_callable is broken |
98 | let callable = sema.type_of_expr(&expr.expr()?)?.as_callable(db)?; | 92 | let callable = sema.type_of_expr(&expr.expr()?)?.as_callable(db)?; |
99 | match callable.kind() { | 93 | match callable.kind() { |
100 | hir::CallableKind::Function(it) => { | 94 | hir::CallableKind::Function(it) => it.try_to_nav(db), |
101 | let fn_def: hir::Function = it.into(); | ||
102 | let nav = fn_def.to_nav(db); | ||
103 | Some(nav) | ||
104 | } | ||
105 | _ => None, | 95 | _ => None, |
106 | } | 96 | } |
107 | } | 97 | } |
108 | FnCallNode::MethodCallExpr(expr) => { | 98 | FnCallNode::MethodCallExpr(expr) => { |
109 | let function = sema.resolve_method_call(&expr)?; | 99 | let function = sema.resolve_method_call(&expr)?; |
110 | Some(function.to_nav(db)) | 100 | function.try_to_nav(db) |
111 | } | 101 | } |
112 | } { | 102 | }?; |
113 | Some((func_target, name_ref.syntax().text_range())) | 103 | Some((func_target, name_ref.syntax().text_range())) |
114 | } else { | ||
115 | None | ||
116 | } | ||
117 | }) | 104 | }) |
118 | .for_each(|(nav, range)| calls.add(&nav, range)); | 105 | .for_each(|(nav, range)| calls.add(&nav, range)); |
119 | 106 | ||
@@ -178,7 +165,7 @@ mod tests { | |||
178 | //- /lib.rs | 165 | //- /lib.rs |
179 | fn callee() {} | 166 | fn callee() {} |
180 | fn caller() { | 167 | fn caller() { |
181 | call<|>ee(); | 168 | call$0ee(); |
182 | } | 169 | } |
183 | "#, | 170 | "#, |
184 | "callee Function FileId(0) 0..14 3..9", | 171 | "callee Function FileId(0) 0..14 3..9", |
@@ -192,7 +179,7 @@ fn caller() { | |||
192 | check_hierarchy( | 179 | check_hierarchy( |
193 | r#" | 180 | r#" |
194 | //- /lib.rs | 181 | //- /lib.rs |
195 | fn call<|>ee() {} | 182 | fn call$0ee() {} |
196 | fn caller() { | 183 | fn caller() { |
197 | callee(); | 184 | callee(); |
198 | } | 185 | } |
@@ -210,7 +197,7 @@ fn caller() { | |||
210 | //- /lib.rs | 197 | //- /lib.rs |
211 | fn callee() {} | 198 | fn callee() {} |
212 | fn caller() { | 199 | fn caller() { |
213 | call<|>ee(); | 200 | call$0ee(); |
214 | callee(); | 201 | callee(); |
215 | } | 202 | } |
216 | "#, | 203 | "#, |
@@ -227,7 +214,7 @@ fn caller() { | |||
227 | //- /lib.rs | 214 | //- /lib.rs |
228 | fn callee() {} | 215 | fn callee() {} |
229 | fn caller1() { | 216 | fn caller1() { |
230 | call<|>ee(); | 217 | call$0ee(); |
231 | } | 218 | } |
232 | 219 | ||
233 | fn caller2() { | 220 | fn caller2() { |
@@ -250,7 +237,7 @@ fn caller2() { | |||
250 | //- /lib.rs cfg:test | 237 | //- /lib.rs cfg:test |
251 | fn callee() {} | 238 | fn callee() {} |
252 | fn caller1() { | 239 | fn caller1() { |
253 | call<|>ee(); | 240 | call$0ee(); |
254 | } | 241 | } |
255 | 242 | ||
256 | #[cfg(test)] | 243 | #[cfg(test)] |
@@ -281,7 +268,7 @@ mod foo; | |||
281 | use foo::callee; | 268 | use foo::callee; |
282 | 269 | ||
283 | fn caller() { | 270 | fn caller() { |
284 | call<|>ee(); | 271 | call$0ee(); |
285 | } | 272 | } |
286 | 273 | ||
287 | //- /foo/mod.rs | 274 | //- /foo/mod.rs |
@@ -299,7 +286,7 @@ pub fn callee() {} | |||
299 | r#" | 286 | r#" |
300 | //- /lib.rs | 287 | //- /lib.rs |
301 | fn callee() {} | 288 | fn callee() {} |
302 | fn call<|>er() { | 289 | fn call$0er() { |
303 | callee(); | 290 | callee(); |
304 | callee(); | 291 | callee(); |
305 | } | 292 | } |
@@ -318,7 +305,7 @@ fn call<|>er() { | |||
318 | mod foo; | 305 | mod foo; |
319 | use foo::callee; | 306 | use foo::callee; |
320 | 307 | ||
321 | fn call<|>er() { | 308 | fn call$0er() { |
322 | callee(); | 309 | callee(); |
323 | } | 310 | } |
324 | 311 | ||
@@ -337,7 +324,7 @@ pub fn callee() {} | |||
337 | r#" | 324 | r#" |
338 | //- /lib.rs | 325 | //- /lib.rs |
339 | fn caller1() { | 326 | fn caller1() { |
340 | call<|>er2(); | 327 | call$0er2(); |
341 | } | 328 | } |
342 | 329 | ||
343 | fn caller2() { | 330 | fn caller2() { |
@@ -365,7 +352,7 @@ fn a() { | |||
365 | fn b() {} | 352 | fn b() {} |
366 | 353 | ||
367 | fn main() { | 354 | fn main() { |
368 | a<|>() | 355 | a$0() |
369 | } | 356 | } |
370 | "#, | 357 | "#, |
371 | "a Function FileId(0) 0..18 3..4", | 358 | "a Function FileId(0) 0..18 3..4", |
@@ -376,7 +363,7 @@ fn main() { | |||
376 | check_hierarchy( | 363 | check_hierarchy( |
377 | r#" | 364 | r#" |
378 | fn a() { | 365 | fn a() { |
379 | b<|>() | 366 | b$0() |
380 | } | 367 | } |
381 | 368 | ||
382 | fn b() {} | 369 | fn b() {} |
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index 79d126ff2..055c0a79c 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs | |||
@@ -125,7 +125,7 @@ pub(crate) fn diagnostics( | |||
125 | .on::<hir::diagnostics::MissingFields, _>(|d| { | 125 | .on::<hir::diagnostics::MissingFields, _>(|d| { |
126 | res.borrow_mut().push(diagnostic_with_fix(d, &sema)); | 126 | res.borrow_mut().push(diagnostic_with_fix(d, &sema)); |
127 | }) | 127 | }) |
128 | .on::<hir::diagnostics::MissingOkInTailExpr, _>(|d| { | 128 | .on::<hir::diagnostics::MissingOkOrSomeInTailExpr, _>(|d| { |
129 | res.borrow_mut().push(diagnostic_with_fix(d, &sema)); | 129 | res.borrow_mut().push(diagnostic_with_fix(d, &sema)); |
130 | }) | 130 | }) |
131 | .on::<hir::diagnostics::NoSuchField, _>(|d| { | 131 | .on::<hir::diagnostics::NoSuchField, _>(|d| { |
@@ -305,6 +305,40 @@ mod tests { | |||
305 | } | 305 | } |
306 | 306 | ||
307 | #[test] | 307 | #[test] |
308 | fn test_wrap_return_type_option() { | ||
309 | check_fix( | ||
310 | r#" | ||
311 | //- /main.rs crate:main deps:core | ||
312 | use core::option::Option::{self, Some, None}; | ||
313 | |||
314 | fn div(x: i32, y: i32) -> Option<i32> { | ||
315 | if y == 0 { | ||
316 | return None; | ||
317 | } | ||
318 | x / y$0 | ||
319 | } | ||
320 | //- /core/lib.rs crate:core | ||
321 | pub mod result { | ||
322 | pub enum Result<T, E> { Ok(T), Err(E) } | ||
323 | } | ||
324 | pub mod option { | ||
325 | pub enum Option<T> { Some(T), None } | ||
326 | } | ||
327 | "#, | ||
328 | r#" | ||
329 | use core::option::Option::{self, Some, None}; | ||
330 | |||
331 | fn div(x: i32, y: i32) -> Option<i32> { | ||
332 | if y == 0 { | ||
333 | return None; | ||
334 | } | ||
335 | Some(x / y) | ||
336 | } | ||
337 | "#, | ||
338 | ); | ||
339 | } | ||
340 | |||
341 | #[test] | ||
308 | fn test_wrap_return_type() { | 342 | fn test_wrap_return_type() { |
309 | check_fix( | 343 | check_fix( |
310 | r#" | 344 | r#" |
@@ -315,12 +349,15 @@ fn div(x: i32, y: i32) -> Result<i32, ()> { | |||
315 | if y == 0 { | 349 | if y == 0 { |
316 | return Err(()); | 350 | return Err(()); |
317 | } | 351 | } |
318 | x / y<|> | 352 | x / y$0 |
319 | } | 353 | } |
320 | //- /core/lib.rs crate:core | 354 | //- /core/lib.rs crate:core |
321 | pub mod result { | 355 | pub mod result { |
322 | pub enum Result<T, E> { Ok(T), Err(E) } | 356 | pub enum Result<T, E> { Ok(T), Err(E) } |
323 | } | 357 | } |
358 | pub mod option { | ||
359 | pub enum Option<T> { Some(T), None } | ||
360 | } | ||
324 | "#, | 361 | "#, |
325 | r#" | 362 | r#" |
326 | use core::result::Result::{self, Ok, Err}; | 363 | use core::result::Result::{self, Ok, Err}; |
@@ -346,12 +383,15 @@ fn div<T>(x: T) -> Result<T, i32> { | |||
346 | if x == 0 { | 383 | if x == 0 { |
347 | return Err(7); | 384 | return Err(7); |
348 | } | 385 | } |
349 | <|>x | 386 | $0x |
350 | } | 387 | } |
351 | //- /core/lib.rs crate:core | 388 | //- /core/lib.rs crate:core |
352 | pub mod result { | 389 | pub mod result { |
353 | pub enum Result<T, E> { Ok(T), Err(E) } | 390 | pub enum Result<T, E> { Ok(T), Err(E) } |
354 | } | 391 | } |
392 | pub mod option { | ||
393 | pub enum Option<T> { Some(T), None } | ||
394 | } | ||
355 | "#, | 395 | "#, |
356 | r#" | 396 | r#" |
357 | use core::result::Result::{self, Ok, Err}; | 397 | use core::result::Result::{self, Ok, Err}; |
@@ -379,12 +419,15 @@ fn div(x: i32, y: i32) -> MyResult<i32> { | |||
379 | if y == 0 { | 419 | if y == 0 { |
380 | return Err(()); | 420 | return Err(()); |
381 | } | 421 | } |
382 | x <|>/ y | 422 | x $0/ y |
383 | } | 423 | } |
384 | //- /core/lib.rs crate:core | 424 | //- /core/lib.rs crate:core |
385 | pub mod result { | 425 | pub mod result { |
386 | pub enum Result<T, E> { Ok(T), Err(E) } | 426 | pub enum Result<T, E> { Ok(T), Err(E) } |
387 | } | 427 | } |
428 | pub mod option { | ||
429 | pub enum Option<T> { Some(T), None } | ||
430 | } | ||
388 | "#, | 431 | "#, |
389 | r#" | 432 | r#" |
390 | use core::result::Result::{self, Ok, Err}; | 433 | use core::result::Result::{self, Ok, Err}; |
@@ -414,12 +457,15 @@ fn foo() -> Result<(), i32> { 0 } | |||
414 | pub mod result { | 457 | pub mod result { |
415 | pub enum Result<T, E> { Ok(T), Err(E) } | 458 | pub enum Result<T, E> { Ok(T), Err(E) } |
416 | } | 459 | } |
460 | pub mod option { | ||
461 | pub enum Option<T> { Some(T), None } | ||
462 | } | ||
417 | "#, | 463 | "#, |
418 | ); | 464 | ); |
419 | } | 465 | } |
420 | 466 | ||
421 | #[test] | 467 | #[test] |
422 | fn test_wrap_return_type_not_applicable_when_return_type_is_not_result() { | 468 | fn test_wrap_return_type_not_applicable_when_return_type_is_not_result_or_option() { |
423 | check_no_diagnostics( | 469 | check_no_diagnostics( |
424 | r#" | 470 | r#" |
425 | //- /main.rs crate:main deps:core | 471 | //- /main.rs crate:main deps:core |
@@ -433,6 +479,9 @@ fn foo() -> SomeOtherEnum { 0 } | |||
433 | pub mod result { | 479 | pub mod result { |
434 | pub enum Result<T, E> { Ok(T), Err(E) } | 480 | pub enum Result<T, E> { Ok(T), Err(E) } |
435 | } | 481 | } |
482 | pub mod option { | ||
483 | pub enum Option<T> { Some(T), None } | ||
484 | } | ||
436 | "#, | 485 | "#, |
437 | ); | 486 | ); |
438 | } | 487 | } |
@@ -444,7 +493,7 @@ pub mod result { | |||
444 | struct TestStruct { one: i32, two: i64 } | 493 | struct TestStruct { one: i32, two: i64 } |
445 | 494 | ||
446 | fn test_fn() { | 495 | fn test_fn() { |
447 | let s = TestStruct {<|>}; | 496 | let s = TestStruct {$0}; |
448 | } | 497 | } |
449 | "#, | 498 | "#, |
450 | r#" | 499 | r#" |
@@ -464,7 +513,7 @@ fn test_fn() { | |||
464 | struct TestStruct { one: i32 } | 513 | struct TestStruct { one: i32 } |
465 | 514 | ||
466 | impl TestStruct { | 515 | impl TestStruct { |
467 | fn test_fn() { let s = Self {<|>}; } | 516 | fn test_fn() { let s = Self {$0}; } |
468 | } | 517 | } |
469 | "#, | 518 | "#, |
470 | r#" | 519 | r#" |
@@ -487,7 +536,7 @@ enum Expr { | |||
487 | 536 | ||
488 | impl Expr { | 537 | impl Expr { |
489 | fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr { | 538 | fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr { |
490 | Expr::Bin {<|> } | 539 | Expr::Bin {$0 } |
491 | } | 540 | } |
492 | } | 541 | } |
493 | "#, | 542 | "#, |
@@ -512,7 +561,7 @@ impl Expr { | |||
512 | struct TestStruct { one: i32, two: i64 } | 561 | struct TestStruct { one: i32, two: i64 } |
513 | 562 | ||
514 | fn test_fn() { | 563 | fn test_fn() { |
515 | let s = TestStruct{ two: 2<|> }; | 564 | let s = TestStruct{ two: 2$0 }; |
516 | } | 565 | } |
517 | "#, | 566 | "#, |
518 | r" | 567 | r" |
@@ -608,7 +657,7 @@ fn here() {} | |||
608 | macro_rules! id { ($($tt:tt)*) => { $($tt)*}; } | 657 | macro_rules! id { ($($tt:tt)*) => { $($tt)*}; } |
609 | 658 | ||
610 | fn main() { | 659 | fn main() { |
611 | let _x = id![Foo { a: <|>42 }]; | 660 | let _x = id![Foo { a: $042 }]; |
612 | } | 661 | } |
613 | 662 | ||
614 | pub struct Foo { pub a: i32, pub b: i32 } | 663 | pub struct Foo { pub a: i32, pub b: i32 } |
@@ -663,7 +712,7 @@ mod a { | |||
663 | check_fix( | 712 | check_fix( |
664 | r" | 713 | r" |
665 | mod b {} | 714 | mod b {} |
666 | use {<|>b}; | 715 | use {$0b}; |
667 | ", | 716 | ", |
668 | r" | 717 | r" |
669 | mod b {} | 718 | mod b {} |
@@ -673,7 +722,7 @@ mod a { | |||
673 | check_fix( | 722 | check_fix( |
674 | r" | 723 | r" |
675 | mod b {} | 724 | mod b {} |
676 | use {b<|>}; | 725 | use {b$0}; |
677 | ", | 726 | ", |
678 | r" | 727 | r" |
679 | mod b {} | 728 | mod b {} |
@@ -683,7 +732,7 @@ mod a { | |||
683 | check_fix( | 732 | check_fix( |
684 | r" | 733 | r" |
685 | mod a { mod c {} } | 734 | mod a { mod c {} } |
686 | use a::{c<|>}; | 735 | use a::{c$0}; |
687 | ", | 736 | ", |
688 | r" | 737 | r" |
689 | mod a { mod c {} } | 738 | mod a { mod c {} } |
@@ -693,7 +742,7 @@ mod a { | |||
693 | check_fix( | 742 | check_fix( |
694 | r" | 743 | r" |
695 | mod a {} | 744 | mod a {} |
696 | use a::{self<|>}; | 745 | use a::{self$0}; |
697 | ", | 746 | ", |
698 | r" | 747 | r" |
699 | mod a {} | 748 | mod a {} |
@@ -703,7 +752,7 @@ mod a { | |||
703 | check_fix( | 752 | check_fix( |
704 | r" | 753 | r" |
705 | mod a { mod c {} mod d { mod e {} } } | 754 | mod a { mod c {} mod d { mod e {} } } |
706 | use a::{c, d::{e<|>}}; | 755 | use a::{c, d::{e$0}}; |
707 | ", | 756 | ", |
708 | r" | 757 | r" |
709 | mod a { mod c {} mod d { mod e {} } } | 758 | mod a { mod c {} mod d { mod e {} } } |
@@ -717,7 +766,7 @@ mod a { | |||
717 | check_fix( | 766 | check_fix( |
718 | r" | 767 | r" |
719 | fn main() { | 768 | fn main() { |
720 | Foo { bar: 3, baz<|>: false}; | 769 | Foo { bar: 3, baz$0: false}; |
721 | } | 770 | } |
722 | struct Foo { | 771 | struct Foo { |
723 | bar: i32 | 772 | bar: i32 |
@@ -743,7 +792,7 @@ struct Foo { | |||
743 | mod foo; | 792 | mod foo; |
744 | 793 | ||
745 | fn main() { | 794 | fn main() { |
746 | foo::Foo { bar: 3, <|>baz: false}; | 795 | foo::Foo { bar: 3, $0baz: false}; |
747 | } | 796 | } |
748 | //- /foo.rs | 797 | //- /foo.rs |
749 | struct Foo { | 798 | struct Foo { |
@@ -777,7 +826,7 @@ struct Foo { | |||
777 | fn test_rename_incorrect_case() { | 826 | fn test_rename_incorrect_case() { |
778 | check_fix( | 827 | check_fix( |
779 | r#" | 828 | r#" |
780 | pub struct test_struct<|> { one: i32 } | 829 | pub struct test_struct$0 { one: i32 } |
781 | 830 | ||
782 | pub fn some_fn(val: test_struct) -> test_struct { | 831 | pub fn some_fn(val: test_struct) -> test_struct { |
783 | test_struct { one: val.one + 1 } | 832 | test_struct { one: val.one + 1 } |
@@ -794,7 +843,7 @@ pub fn some_fn(val: TestStruct) -> TestStruct { | |||
794 | 843 | ||
795 | check_fix( | 844 | check_fix( |
796 | r#" | 845 | r#" |
797 | pub fn some_fn(NonSnakeCase<|>: u8) -> u8 { | 846 | pub fn some_fn(NonSnakeCase$0: u8) -> u8 { |
798 | NonSnakeCase | 847 | NonSnakeCase |
799 | } | 848 | } |
800 | "#, | 849 | "#, |
@@ -807,7 +856,7 @@ pub fn some_fn(non_snake_case: u8) -> u8 { | |||
807 | 856 | ||
808 | check_fix( | 857 | check_fix( |
809 | r#" | 858 | r#" |
810 | pub fn SomeFn<|>(val: u8) -> u8 { | 859 | pub fn SomeFn$0(val: u8) -> u8 { |
811 | if val != 0 { SomeFn(val - 1) } else { val } | 860 | if val != 0 { SomeFn(val - 1) } else { val } |
812 | } | 861 | } |
813 | "#, | 862 | "#, |
@@ -821,7 +870,7 @@ pub fn some_fn(val: u8) -> u8 { | |||
821 | check_fix( | 870 | check_fix( |
822 | r#" | 871 | r#" |
823 | fn some_fn() { | 872 | fn some_fn() { |
824 | let whatAWeird_Formatting<|> = 10; | 873 | let whatAWeird_Formatting$0 = 10; |
825 | another_func(whatAWeird_Formatting); | 874 | another_func(whatAWeird_Formatting); |
826 | } | 875 | } |
827 | "#, | 876 | "#, |
@@ -839,7 +888,7 @@ fn some_fn() { | |||
839 | check_no_diagnostics( | 888 | check_no_diagnostics( |
840 | r#" | 889 | r#" |
841 | fn foo() { | 890 | fn foo() { |
842 | const ANOTHER_ITEM<|>: &str = "some_item"; | 891 | const ANOTHER_ITEM$0: &str = "some_item"; |
843 | } | 892 | } |
844 | "#, | 893 | "#, |
845 | ); | 894 | ); |
@@ -852,7 +901,7 @@ fn foo() { | |||
852 | pub struct TestStruct; | 901 | pub struct TestStruct; |
853 | 902 | ||
854 | impl TestStruct { | 903 | impl TestStruct { |
855 | pub fn SomeFn<|>() -> TestStruct { | 904 | pub fn SomeFn$0() -> TestStruct { |
856 | TestStruct | 905 | TestStruct |
857 | } | 906 | } |
858 | } | 907 | } |
@@ -871,7 +920,7 @@ impl TestStruct { | |||
871 | 920 | ||
872 | #[test] | 921 | #[test] |
873 | fn test_single_incorrect_case_diagnostic_in_function_name_issue_6970() { | 922 | fn test_single_incorrect_case_diagnostic_in_function_name_issue_6970() { |
874 | let input = r#"fn FOO<|>() {}"#; | 923 | let input = r#"fn FOO$0() {}"#; |
875 | let expected = r#"fn foo() {}"#; | 924 | let expected = r#"fn foo() {}"#; |
876 | 925 | ||
877 | let (analysis, file_position) = fixture::position(input); | 926 | let (analysis, file_position) = fixture::position(input); |
diff --git a/crates/ide/src/diagnostics/field_shorthand.rs b/crates/ide/src/diagnostics/field_shorthand.rs index f41bcd619..16c6ea827 100644 --- a/crates/ide/src/diagnostics/field_shorthand.rs +++ b/crates/ide/src/diagnostics/field_shorthand.rs | |||
@@ -120,7 +120,7 @@ fn main() { A { 0: 0 } } | |||
120 | struct A { a: &'static str } | 120 | struct A { a: &'static str } |
121 | fn main() { | 121 | fn main() { |
122 | let a = "haha"; | 122 | let a = "haha"; |
123 | A { a<|>: a } | 123 | A { a$0: a } |
124 | } | 124 | } |
125 | "#, | 125 | "#, |
126 | r#" | 126 | r#" |
@@ -138,7 +138,7 @@ struct A { a: &'static str, b: &'static str } | |||
138 | fn main() { | 138 | fn main() { |
139 | let a = "haha"; | 139 | let a = "haha"; |
140 | let b = "bb"; | 140 | let b = "bb"; |
141 | A { a<|>: a, b } | 141 | A { a$0: a, b } |
142 | } | 142 | } |
143 | "#, | 143 | "#, |
144 | r#" | 144 | r#" |
@@ -171,7 +171,7 @@ fn f(a: A) { let A { 0: 0 } = a; } | |||
171 | r#" | 171 | r#" |
172 | struct A { a: &'static str } | 172 | struct A { a: &'static str } |
173 | fn f(a: A) { | 173 | fn f(a: A) { |
174 | let A { a<|>: a } = a; | 174 | let A { a$0: a } = a; |
175 | } | 175 | } |
176 | "#, | 176 | "#, |
177 | r#" | 177 | r#" |
@@ -186,7 +186,7 @@ fn f(a: A) { | |||
186 | r#" | 186 | r#" |
187 | struct A { a: &'static str, b: &'static str } | 187 | struct A { a: &'static str, b: &'static str } |
188 | fn f(a: A) { | 188 | fn f(a: A) { |
189 | let A { a<|>: a, b } = a; | 189 | let A { a$0: a, b } = a; |
190 | } | 190 | } |
191 | "#, | 191 | "#, |
192 | r#" | 192 | r#" |
diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index d79f5c170..d7ad88ed5 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs | |||
@@ -3,7 +3,7 @@ | |||
3 | use hir::{ | 3 | use hir::{ |
4 | db::AstDatabase, | 4 | db::AstDatabase, |
5 | diagnostics::{ | 5 | diagnostics::{ |
6 | Diagnostic, IncorrectCase, MissingFields, MissingOkInTailExpr, NoSuchField, | 6 | Diagnostic, IncorrectCase, MissingFields, MissingOkOrSomeInTailExpr, NoSuchField, |
7 | RemoveThisSemicolon, UnresolvedModule, | 7 | RemoveThisSemicolon, UnresolvedModule, |
8 | }, | 8 | }, |
9 | HasSource, HirDisplay, InFile, Semantics, VariantDef, | 9 | HasSource, HirDisplay, InFile, Semantics, VariantDef, |
@@ -94,15 +94,17 @@ impl DiagnosticWithFix for MissingFields { | |||
94 | } | 94 | } |
95 | } | 95 | } |
96 | 96 | ||
97 | impl DiagnosticWithFix for MissingOkInTailExpr { | 97 | impl DiagnosticWithFix for MissingOkOrSomeInTailExpr { |
98 | fn fix(&self, sema: &Semantics<RootDatabase>) -> Option<Fix> { | 98 | fn fix(&self, sema: &Semantics<RootDatabase>) -> Option<Fix> { |
99 | let root = sema.db.parse_or_expand(self.file)?; | 99 | let root = sema.db.parse_or_expand(self.file)?; |
100 | let tail_expr = self.expr.to_node(&root); | 100 | let tail_expr = self.expr.to_node(&root); |
101 | let tail_expr_range = tail_expr.syntax().text_range(); | 101 | let tail_expr_range = tail_expr.syntax().text_range(); |
102 | let edit = TextEdit::replace(tail_expr_range, format!("Ok({})", tail_expr.syntax())); | 102 | let replacement = format!("{}({})", self.required, tail_expr.syntax()); |
103 | let edit = TextEdit::replace(tail_expr_range, replacement); | ||
103 | let source_change = | 104 | let source_change = |
104 | SourceFileEdit { file_id: self.file.original_file(sema.db), edit }.into(); | 105 | SourceFileEdit { file_id: self.file.original_file(sema.db), edit }.into(); |
105 | Some(Fix::new("Wrap with ok", source_change, tail_expr_range)) | 106 | let name = if self.required == "Ok" { "Wrap with Ok" } else { "Wrap with Some" }; |
107 | Some(Fix::new(name, source_change, tail_expr_range)) | ||
106 | } | 108 | } |
107 | } | 109 | } |
108 | 110 | ||
@@ -156,20 +158,20 @@ fn missing_record_expr_field_fix( | |||
156 | let record_fields = match VariantDef::from(def_id) { | 158 | let record_fields = match VariantDef::from(def_id) { |
157 | VariantDef::Struct(s) => { | 159 | VariantDef::Struct(s) => { |
158 | module = s.module(sema.db); | 160 | module = s.module(sema.db); |
159 | let source = s.source(sema.db); | 161 | let source = s.source(sema.db)?; |
160 | def_file_id = source.file_id; | 162 | def_file_id = source.file_id; |
161 | let fields = source.value.field_list()?; | 163 | let fields = source.value.field_list()?; |
162 | record_field_list(fields)? | 164 | record_field_list(fields)? |
163 | } | 165 | } |
164 | VariantDef::Union(u) => { | 166 | VariantDef::Union(u) => { |
165 | module = u.module(sema.db); | 167 | module = u.module(sema.db); |
166 | let source = u.source(sema.db); | 168 | let source = u.source(sema.db)?; |
167 | def_file_id = source.file_id; | 169 | def_file_id = source.file_id; |
168 | source.value.record_field_list()? | 170 | source.value.record_field_list()? |
169 | } | 171 | } |
170 | VariantDef::Variant(e) => { | 172 | VariantDef::Variant(e) => { |
171 | module = e.module(sema.db); | 173 | module = e.module(sema.db); |
172 | let source = e.source(sema.db); | 174 | let source = e.source(sema.db)?; |
173 | def_file_id = source.file_id; | 175 | def_file_id = source.file_id; |
174 | let fields = source.value.field_list()?; | 176 | let fields = source.value.field_list()?; |
175 | record_field_list(fields)? | 177 | record_field_list(fields)? |
diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index 6431e7d6d..4eecae697 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs | |||
@@ -24,6 +24,7 @@ pub enum SymbolKind { | |||
24 | Impl, | 24 | Impl, |
25 | Field, | 25 | Field, |
26 | TypeParam, | 26 | TypeParam, |
27 | ConstParam, | ||
27 | LifetimeParam, | 28 | LifetimeParam, |
28 | ValueParam, | 29 | ValueParam, |
29 | SelfParam, | 30 | SelfParam, |
@@ -209,21 +210,12 @@ impl ToNav for FileSymbol { | |||
209 | impl TryToNav for Definition { | 210 | impl TryToNav for Definition { |
210 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { | 211 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
211 | match self { | 212 | match self { |
212 | Definition::Macro(it) => { | 213 | Definition::Macro(it) => it.try_to_nav(db), |
213 | // FIXME: Currently proc-macro do not have ast-node, | 214 | Definition::Field(it) => it.try_to_nav(db), |
214 | // such that it does not have source | ||
215 | // more discussion: https://github.com/rust-analyzer/rust-analyzer/issues/6913 | ||
216 | if it.is_proc_macro() { | ||
217 | return None; | ||
218 | } | ||
219 | Some(it.to_nav(db)) | ||
220 | } | ||
221 | Definition::Field(it) => Some(it.to_nav(db)), | ||
222 | Definition::ModuleDef(it) => it.try_to_nav(db), | 215 | Definition::ModuleDef(it) => it.try_to_nav(db), |
223 | Definition::SelfType(it) => Some(it.to_nav(db)), | 216 | Definition::SelfType(it) => it.try_to_nav(db), |
224 | Definition::Local(it) => Some(it.to_nav(db)), | 217 | Definition::Local(it) => Some(it.to_nav(db)), |
225 | Definition::TypeParam(it) => Some(it.to_nav(db)), | 218 | Definition::GenericParam(it) => it.try_to_nav(db), |
226 | Definition::LifetimeParam(it) => Some(it.to_nav(db)), | ||
227 | Definition::Label(it) => Some(it.to_nav(db)), | 219 | Definition::Label(it) => Some(it.to_nav(db)), |
228 | } | 220 | } |
229 | } | 221 | } |
@@ -231,18 +223,17 @@ impl TryToNav for Definition { | |||
231 | 223 | ||
232 | impl TryToNav for hir::ModuleDef { | 224 | impl TryToNav for hir::ModuleDef { |
233 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { | 225 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
234 | let res = match self { | 226 | match self { |
235 | hir::ModuleDef::Module(it) => it.to_nav(db), | 227 | hir::ModuleDef::Module(it) => Some(it.to_nav(db)), |
236 | hir::ModuleDef::Function(it) => it.to_nav(db), | 228 | hir::ModuleDef::Function(it) => it.try_to_nav(db), |
237 | hir::ModuleDef::Adt(it) => it.to_nav(db), | 229 | hir::ModuleDef::Adt(it) => it.try_to_nav(db), |
238 | hir::ModuleDef::Variant(it) => it.to_nav(db), | 230 | hir::ModuleDef::Variant(it) => it.try_to_nav(db), |
239 | hir::ModuleDef::Const(it) => it.to_nav(db), | 231 | hir::ModuleDef::Const(it) => it.try_to_nav(db), |
240 | hir::ModuleDef::Static(it) => it.to_nav(db), | 232 | hir::ModuleDef::Static(it) => it.try_to_nav(db), |
241 | hir::ModuleDef::Trait(it) => it.to_nav(db), | 233 | hir::ModuleDef::Trait(it) => it.try_to_nav(db), |
242 | hir::ModuleDef::TypeAlias(it) => it.to_nav(db), | 234 | hir::ModuleDef::TypeAlias(it) => it.try_to_nav(db), |
243 | hir::ModuleDef::BuiltinType(_) => return None, | 235 | hir::ModuleDef::BuiltinType(_) => None, |
244 | }; | 236 | } |
245 | Some(res) | ||
246 | } | 237 | } |
247 | } | 238 | } |
248 | 239 | ||
@@ -277,13 +268,13 @@ impl ToNavFromAst for hir::Trait { | |||
277 | const KIND: SymbolKind = SymbolKind::Trait; | 268 | const KIND: SymbolKind = SymbolKind::Trait; |
278 | } | 269 | } |
279 | 270 | ||
280 | impl<D> ToNav for D | 271 | impl<D> TryToNav for D |
281 | where | 272 | where |
282 | D: HasSource + ToNavFromAst + Copy + HasAttrs, | 273 | D: HasSource + ToNavFromAst + Copy + HasAttrs, |
283 | D::Ast: ast::NameOwner + ShortLabel, | 274 | D::Ast: ast::NameOwner + ShortLabel, |
284 | { | 275 | { |
285 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 276 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
286 | let src = self.source(db); | 277 | let src = self.source(db)?; |
287 | let mut res = NavigationTarget::from_named( | 278 | let mut res = NavigationTarget::from_named( |
288 | db, | 279 | db, |
289 | src.as_ref().map(|it| it as &dyn ast::NameOwner), | 280 | src.as_ref().map(|it| it as &dyn ast::NameOwner), |
@@ -291,7 +282,7 @@ where | |||
291 | ); | 282 | ); |
292 | res.docs = self.docs(db); | 283 | res.docs = self.docs(db); |
293 | res.description = src.value.short_label(); | 284 | res.description = src.value.short_label(); |
294 | res | 285 | Some(res) |
295 | } | 286 | } |
296 | } | 287 | } |
297 | 288 | ||
@@ -310,9 +301,9 @@ impl ToNav for hir::Module { | |||
310 | } | 301 | } |
311 | } | 302 | } |
312 | 303 | ||
313 | impl ToNav for hir::Impl { | 304 | impl TryToNav for hir::Impl { |
314 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 305 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
315 | let src = self.source(db); | 306 | let src = self.source(db)?; |
316 | let derive_attr = self.is_builtin_derive(db); | 307 | let derive_attr = self.is_builtin_derive(db); |
317 | let frange = if let Some(item) = &derive_attr { | 308 | let frange = if let Some(item) = &derive_attr { |
318 | item.syntax().original_file_range(db) | 309 | item.syntax().original_file_range(db) |
@@ -325,21 +316,21 @@ impl ToNav for hir::Impl { | |||
325 | src.value.self_ty().map(|ty| src.with_value(ty.syntax()).original_file_range(db).range) | 316 | src.value.self_ty().map(|ty| src.with_value(ty.syntax()).original_file_range(db).range) |
326 | }; | 317 | }; |
327 | 318 | ||
328 | NavigationTarget::from_syntax( | 319 | Some(NavigationTarget::from_syntax( |
329 | frange.file_id, | 320 | frange.file_id, |
330 | "impl".into(), | 321 | "impl".into(), |
331 | focus_range, | 322 | focus_range, |
332 | frange.range, | 323 | frange.range, |
333 | SymbolKind::Impl, | 324 | SymbolKind::Impl, |
334 | ) | 325 | )) |
335 | } | 326 | } |
336 | } | 327 | } |
337 | 328 | ||
338 | impl ToNav for hir::Field { | 329 | impl TryToNav for hir::Field { |
339 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 330 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
340 | let src = self.source(db); | 331 | let src = self.source(db)?; |
341 | 332 | ||
342 | match &src.value { | 333 | let field_source = match &src.value { |
343 | FieldSource::Named(it) => { | 334 | FieldSource::Named(it) => { |
344 | let mut res = | 335 | let mut res = |
345 | NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field); | 336 | NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field); |
@@ -357,13 +348,14 @@ impl ToNav for hir::Field { | |||
357 | SymbolKind::Field, | 348 | SymbolKind::Field, |
358 | ) | 349 | ) |
359 | } | 350 | } |
360 | } | 351 | }; |
352 | Some(field_source) | ||
361 | } | 353 | } |
362 | } | 354 | } |
363 | 355 | ||
364 | impl ToNav for hir::MacroDef { | 356 | impl TryToNav for hir::MacroDef { |
365 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 357 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
366 | let src = self.source(db); | 358 | let src = self.source(db)?; |
367 | log::debug!("nav target {:#?}", src.value.syntax()); | 359 | log::debug!("nav target {:#?}", src.value.syntax()); |
368 | let mut res = NavigationTarget::from_named( | 360 | let mut res = NavigationTarget::from_named( |
369 | db, | 361 | db, |
@@ -371,26 +363,36 @@ impl ToNav for hir::MacroDef { | |||
371 | SymbolKind::Macro, | 363 | SymbolKind::Macro, |
372 | ); | 364 | ); |
373 | res.docs = self.docs(db); | 365 | res.docs = self.docs(db); |
374 | res | 366 | Some(res) |
375 | } | 367 | } |
376 | } | 368 | } |
377 | 369 | ||
378 | impl ToNav for hir::Adt { | 370 | impl TryToNav for hir::Adt { |
379 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 371 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
380 | match self { | 372 | match self { |
381 | hir::Adt::Struct(it) => it.to_nav(db), | 373 | hir::Adt::Struct(it) => it.try_to_nav(db), |
382 | hir::Adt::Union(it) => it.to_nav(db), | 374 | hir::Adt::Union(it) => it.try_to_nav(db), |
383 | hir::Adt::Enum(it) => it.to_nav(db), | 375 | hir::Adt::Enum(it) => it.try_to_nav(db), |
384 | } | 376 | } |
385 | } | 377 | } |
386 | } | 378 | } |
387 | 379 | ||
388 | impl ToNav for hir::AssocItem { | 380 | impl TryToNav for hir::AssocItem { |
389 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 381 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
390 | match self { | 382 | match self { |
391 | AssocItem::Function(it) => it.to_nav(db), | 383 | AssocItem::Function(it) => it.try_to_nav(db), |
392 | AssocItem::Const(it) => it.to_nav(db), | 384 | AssocItem::Const(it) => it.try_to_nav(db), |
393 | AssocItem::TypeAlias(it) => it.to_nav(db), | 385 | AssocItem::TypeAlias(it) => it.try_to_nav(db), |
386 | } | ||
387 | } | ||
388 | } | ||
389 | |||
390 | impl TryToNav for hir::GenericParam { | ||
391 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { | ||
392 | match self { | ||
393 | hir::GenericParam::TypeParam(it) => it.try_to_nav(db), | ||
394 | hir::GenericParam::ConstParam(it) => it.try_to_nav(db), | ||
395 | hir::GenericParam::LifetimeParam(it) => it.try_to_nav(db), | ||
394 | } | 396 | } |
395 | } | 397 | } |
396 | } | 398 | } |
@@ -444,9 +446,9 @@ impl ToNav for hir::Label { | |||
444 | } | 446 | } |
445 | } | 447 | } |
446 | 448 | ||
447 | impl ToNav for hir::TypeParam { | 449 | impl TryToNav for hir::TypeParam { |
448 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 450 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
449 | let src = self.source(db); | 451 | let src = self.source(db)?; |
450 | let full_range = match &src.value { | 452 | let full_range = match &src.value { |
451 | Either::Left(it) => it.syntax().text_range(), | 453 | Either::Left(it) => it.syntax().text_range(), |
452 | Either::Right(it) => it.syntax().text_range(), | 454 | Either::Right(it) => it.syntax().text_range(), |
@@ -455,7 +457,7 @@ impl ToNav for hir::TypeParam { | |||
455 | Either::Left(_) => None, | 457 | Either::Left(_) => None, |
456 | Either::Right(it) => it.name().map(|it| it.syntax().text_range()), | 458 | Either::Right(it) => it.name().map(|it| it.syntax().text_range()), |
457 | }; | 459 | }; |
458 | NavigationTarget { | 460 | Some(NavigationTarget { |
459 | file_id: src.file_id.original_file(db), | 461 | file_id: src.file_id.original_file(db), |
460 | name: self.name(db).to_string().into(), | 462 | name: self.name(db).to_string().into(), |
461 | kind: Some(SymbolKind::TypeParam), | 463 | kind: Some(SymbolKind::TypeParam), |
@@ -464,15 +466,15 @@ impl ToNav for hir::TypeParam { | |||
464 | container_name: None, | 466 | container_name: None, |
465 | description: None, | 467 | description: None, |
466 | docs: None, | 468 | docs: None, |
467 | } | 469 | }) |
468 | } | 470 | } |
469 | } | 471 | } |
470 | 472 | ||
471 | impl ToNav for hir::LifetimeParam { | 473 | impl TryToNav for hir::LifetimeParam { |
472 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 474 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
473 | let src = self.source(db); | 475 | let src = self.source(db)?; |
474 | let full_range = src.value.syntax().text_range(); | 476 | let full_range = src.value.syntax().text_range(); |
475 | NavigationTarget { | 477 | Some(NavigationTarget { |
476 | file_id: src.file_id.original_file(db), | 478 | file_id: src.file_id.original_file(db), |
477 | name: self.name(db).to_string().into(), | 479 | name: self.name(db).to_string().into(), |
478 | kind: Some(SymbolKind::LifetimeParam), | 480 | kind: Some(SymbolKind::LifetimeParam), |
@@ -481,7 +483,24 @@ impl ToNav for hir::LifetimeParam { | |||
481 | container_name: None, | 483 | container_name: None, |
482 | description: None, | 484 | description: None, |
483 | docs: None, | 485 | docs: None, |
484 | } | 486 | }) |
487 | } | ||
488 | } | ||
489 | |||
490 | impl TryToNav for hir::ConstParam { | ||
491 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { | ||
492 | let src = self.source(db)?; | ||
493 | let full_range = src.value.syntax().text_range(); | ||
494 | Some(NavigationTarget { | ||
495 | file_id: src.file_id.original_file(db), | ||
496 | name: self.name(db).to_string().into(), | ||
497 | kind: Some(SymbolKind::ConstParam), | ||
498 | full_range, | ||
499 | focus_range: src.value.name().map(|n| n.syntax().text_range()), | ||
500 | container_name: None, | ||
501 | description: None, | ||
502 | docs: None, | ||
503 | }) | ||
485 | } | 504 | } |
486 | } | 505 | } |
487 | 506 | ||
diff --git a/crates/ide/src/display/short_label.rs b/crates/ide/src/display/short_label.rs index ea49d9f97..990f740b8 100644 --- a/crates/ide/src/display/short_label.rs +++ b/crates/ide/src/display/short_label.rs | |||
@@ -87,6 +87,17 @@ impl ShortLabel for ast::Variant { | |||
87 | } | 87 | } |
88 | } | 88 | } |
89 | 89 | ||
90 | impl ShortLabel for ast::ConstParam { | ||
91 | fn short_label(&self) -> Option<String> { | ||
92 | let mut buf = "const ".to_owned(); | ||
93 | buf.push_str(self.name()?.text().as_str()); | ||
94 | if let Some(type_ref) = self.ty() { | ||
95 | format_to!(buf, ": {}", type_ref.syntax()); | ||
96 | } | ||
97 | Some(buf) | ||
98 | } | ||
99 | } | ||
100 | |||
90 | fn short_label_from_ty<T>(node: &T, ty: Option<ast::Type>, prefix: &str) -> Option<String> | 101 | fn short_label_from_ty<T>(node: &T, ty: Option<ast::Type>, prefix: &str) -> Option<String> |
91 | where | 102 | where |
92 | T: NameOwner + VisibilityOwner, | 103 | T: NameOwner + VisibilityOwner, |
diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs index e10516f43..de10406bc 100644 --- a/crates/ide/src/doc_links.rs +++ b/crates/ide/src/doc_links.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | //! Resolves and rewrites links in markdown documentation. | 1 | //! Resolves and rewrites links in markdown documentation. |
2 | 2 | ||
3 | use std::{convert::TryFrom, iter::once}; | 3 | use std::{convert::TryFrom, iter::once, ops::Range}; |
4 | 4 | ||
5 | use itertools::Itertools; | 5 | use itertools::Itertools; |
6 | use pulldown_cmark::{BrokenLink, CowStr, Event, InlineStr, LinkType, Options, Parser, Tag}; | 6 | use pulldown_cmark::{BrokenLink, CowStr, Event, InlineStr, LinkType, Options, Parser, Tag}; |
@@ -39,7 +39,7 @@ pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Defi | |||
39 | if target.contains("://") { | 39 | if target.contains("://") { |
40 | (target.to_string(), title.to_string()) | 40 | (target.to_string(), title.to_string()) |
41 | } else { | 41 | } else { |
42 | // Two posibilities: | 42 | // Two possibilities: |
43 | // * path-based links: `../../module/struct.MyStruct.html` | 43 | // * path-based links: `../../module/struct.MyStruct.html` |
44 | // * module-based links (AKA intra-doc links): `super::super::module::MyStruct` | 44 | // * module-based links (AKA intra-doc links): `super::super::module::MyStruct` |
45 | if let Some(rewritten) = rewrite_intra_doc_link(db, *definition, target, title) { | 45 | if let Some(rewritten) = rewrite_intra_doc_link(db, *definition, target, title) { |
@@ -61,6 +61,30 @@ pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Defi | |||
61 | out | 61 | out |
62 | } | 62 | } |
63 | 63 | ||
64 | pub(crate) fn extract_definitions_from_markdown( | ||
65 | markdown: &str, | ||
66 | ) -> Vec<(String, Option<hir::Namespace>, Range<usize>)> { | ||
67 | let mut res = vec![]; | ||
68 | let mut cb = |link: BrokenLink| { | ||
69 | Some(( | ||
70 | /*url*/ link.reference.to_owned().into(), | ||
71 | /*title*/ link.reference.to_owned().into(), | ||
72 | )) | ||
73 | }; | ||
74 | let doc = Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(&mut cb)); | ||
75 | for (event, range) in doc.into_offset_iter() { | ||
76 | match event { | ||
77 | Event::Start(Tag::Link(_link_type, ref target, ref title)) => { | ||
78 | let link = if target.is_empty() { title } else { target }; | ||
79 | let (link, ns) = parse_link(link); | ||
80 | res.push((link.to_string(), ns, range)); | ||
81 | } | ||
82 | _ => {} | ||
83 | } | ||
84 | } | ||
85 | res | ||
86 | } | ||
87 | |||
64 | /// Remove all links in markdown documentation. | 88 | /// Remove all links in markdown documentation. |
65 | pub(crate) fn remove_links(markdown: &str) -> String { | 89 | pub(crate) fn remove_links(markdown: &str) -> String { |
66 | let mut drop_link = false; | 90 | let mut drop_link = false; |
@@ -192,8 +216,7 @@ fn rewrite_intra_doc_link( | |||
192 | Definition::Field(it) => it.resolve_doc_path(db, link, ns), | 216 | Definition::Field(it) => it.resolve_doc_path(db, link, ns), |
193 | Definition::SelfType(_) | 217 | Definition::SelfType(_) |
194 | | Definition::Local(_) | 218 | | Definition::Local(_) |
195 | | Definition::TypeParam(_) | 219 | | Definition::GenericParam(_) |
196 | | Definition::LifetimeParam(_) | ||
197 | | Definition::Label(_) => return None, | 220 | | Definition::Label(_) => return None, |
198 | }?; | 221 | }?; |
199 | let krate = resolved.module(db)?.krate(); | 222 | let krate = resolved.module(db)?.krate(); |
@@ -419,7 +442,7 @@ fn get_symbol_fragment(db: &dyn HirDatabase, field_or_assoc: &FieldOrAssocItem) | |||
419 | function.as_assoc_item(db).map(|assoc| assoc.container(db)), | 442 | function.as_assoc_item(db).map(|assoc| assoc.container(db)), |
420 | Some(AssocItemContainer::Trait(..)) | 443 | Some(AssocItemContainer::Trait(..)) |
421 | ); | 444 | ); |
422 | // This distinction may get more complicated when specialisation is available. | 445 | // This distinction may get more complicated when specialization is available. |
423 | // Rustdoc makes this decision based on whether a method 'has defaultness'. | 446 | // Rustdoc makes this decision based on whether a method 'has defaultness'. |
424 | // Currently this is only the case for provided trait methods. | 447 | // Currently this is only the case for provided trait methods. |
425 | if is_trait_method && !function.has_body(db) { | 448 | if is_trait_method && !function.has_body(db) { |
@@ -463,7 +486,7 @@ mod tests { | |||
463 | fn test_doc_url_struct() { | 486 | fn test_doc_url_struct() { |
464 | check( | 487 | check( |
465 | r#" | 488 | r#" |
466 | pub struct Fo<|>o; | 489 | pub struct Fo$0o; |
467 | "#, | 490 | "#, |
468 | expect![[r#"https://docs.rs/test/*/test/struct.Foo.html"#]], | 491 | expect![[r#"https://docs.rs/test/*/test/struct.Foo.html"#]], |
469 | ); | 492 | ); |
@@ -473,7 +496,7 @@ pub struct Fo<|>o; | |||
473 | fn test_doc_url_fn() { | 496 | fn test_doc_url_fn() { |
474 | check( | 497 | check( |
475 | r#" | 498 | r#" |
476 | pub fn fo<|>o() {} | 499 | pub fn fo$0o() {} |
477 | "#, | 500 | "#, |
478 | expect![[r##"https://docs.rs/test/*/test/fn.foo.html#method.foo"##]], | 501 | expect![[r##"https://docs.rs/test/*/test/fn.foo.html#method.foo"##]], |
479 | ); | 502 | ); |
@@ -486,7 +509,7 @@ pub fn fo<|>o() {} | |||
486 | pub struct Foo; | 509 | pub struct Foo; |
487 | 510 | ||
488 | impl Foo { | 511 | impl Foo { |
489 | pub fn met<|>hod() {} | 512 | pub fn met$0hod() {} |
490 | } | 513 | } |
491 | 514 | ||
492 | "#, | 515 | "#, |
@@ -499,7 +522,7 @@ impl Foo { | |||
499 | check( | 522 | check( |
500 | r#" | 523 | r#" |
501 | pub trait Bar { | 524 | pub trait Bar { |
502 | fn met<|>hod() {} | 525 | fn met$0hod() {} |
503 | } | 526 | } |
504 | 527 | ||
505 | "#, | 528 | "#, |
@@ -512,7 +535,7 @@ pub trait Bar { | |||
512 | check( | 535 | check( |
513 | r#" | 536 | r#" |
514 | pub trait Foo { | 537 | pub trait Foo { |
515 | fn met<|>hod(); | 538 | fn met$0hod(); |
516 | } | 539 | } |
517 | 540 | ||
518 | "#, | 541 | "#, |
@@ -525,7 +548,7 @@ pub trait Foo { | |||
525 | check( | 548 | check( |
526 | r#" | 549 | r#" |
527 | pub struct Foo { | 550 | pub struct Foo { |
528 | pub fie<|>ld: () | 551 | pub fie$0ld: () |
529 | } | 552 | } |
530 | 553 | ||
531 | "#, | 554 | "#, |
@@ -538,7 +561,7 @@ pub struct Foo { | |||
538 | check( | 561 | check( |
539 | r#" | 562 | r#" |
540 | pub mod foo { | 563 | pub mod foo { |
541 | pub mod ba<|>r {} | 564 | pub mod ba$0r {} |
542 | } | 565 | } |
543 | "#, | 566 | "#, |
544 | expect![[r#"https://docs.rs/test/*/test/foo/bar/index.html"#]], | 567 | expect![[r#"https://docs.rs/test/*/test/foo/bar/index.html"#]], |
@@ -563,7 +586,7 @@ pub mod wrapper { | |||
563 | } | 586 | } |
564 | 587 | ||
565 | fn foo() { | 588 | fn foo() { |
566 | let bar: wrapper::It<|>em; | 589 | let bar: wrapper::It$0em; |
567 | } | 590 | } |
568 | "#, | 591 | "#, |
569 | expect![[r#"https://docs.rs/test/*/test/wrapper/module/struct.Item.html"#]], | 592 | expect![[r#"https://docs.rs/test/*/test/wrapper/module/struct.Item.html"#]], |
diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs index 8d75e0f05..ffb3a6f7d 100644 --- a/crates/ide/src/expand_macro.rs +++ b/crates/ide/src/expand_macro.rs | |||
@@ -144,7 +144,7 @@ macro_rules! foo { | |||
144 | macro_rules! baz { | 144 | macro_rules! baz { |
145 | () => { foo!(); } | 145 | () => { foo!(); } |
146 | } | 146 | } |
147 | f<|>oo!(); | 147 | f$0oo!(); |
148 | "#, | 148 | "#, |
149 | expect![[r#" | 149 | expect![[r#" |
150 | foo | 150 | foo |
@@ -165,7 +165,7 @@ macro_rules! foo { | |||
165 | } | 165 | } |
166 | } | 166 | } |
167 | } | 167 | } |
168 | f<|>oo!(); | 168 | f$0oo!(); |
169 | "#, | 169 | "#, |
170 | expect![[r#" | 170 | expect![[r#" |
171 | foo | 171 | foo |
@@ -192,7 +192,7 @@ macro_rules! match_ast { | |||
192 | } | 192 | } |
193 | 193 | ||
194 | fn main() { | 194 | fn main() { |
195 | mat<|>ch_ast! { | 195 | mat$0ch_ast! { |
196 | match container { | 196 | match container { |
197 | ast::TraitDef(it) => {}, | 197 | ast::TraitDef(it) => {}, |
198 | ast::ImplDef(it) => {}, | 198 | ast::ImplDef(it) => {}, |
@@ -226,7 +226,7 @@ macro_rules! match_ast { | |||
226 | 226 | ||
227 | fn main() { | 227 | fn main() { |
228 | let p = f(|it| { | 228 | let p = f(|it| { |
229 | let res = mat<|>ch_ast! { match c {}}; | 229 | let res = mat$0ch_ast! { match c {}}; |
230 | Some(res) | 230 | Some(res) |
231 | })?; | 231 | })?; |
232 | } | 232 | } |
@@ -250,7 +250,7 @@ macro_rules! foo { | |||
250 | } | 250 | } |
251 | 251 | ||
252 | fn main() { | 252 | fn main() { |
253 | let res = fo<|>o!(); | 253 | let res = fo$0o!(); |
254 | } | 254 | } |
255 | "#, | 255 | "#, |
256 | expect![[r#" | 256 | expect![[r#" |
@@ -272,7 +272,7 @@ macro_rules! foo { | |||
272 | } | 272 | } |
273 | 273 | ||
274 | fn main() { | 274 | fn main() { |
275 | let res = fo<|>o!(); | 275 | let res = fo$0o!(); |
276 | } | 276 | } |
277 | "#, | 277 | "#, |
278 | expect![[r#" | 278 | expect![[r#" |
diff --git a/crates/ide/src/extend_selection.rs b/crates/ide/src/extend_selection.rs index 6f3022dfd..56418c960 100644 --- a/crates/ide/src/extend_selection.rs +++ b/crates/ide/src/extend_selection.rs | |||
@@ -334,29 +334,29 @@ mod tests { | |||
334 | 334 | ||
335 | #[test] | 335 | #[test] |
336 | fn test_extend_selection_arith() { | 336 | fn test_extend_selection_arith() { |
337 | do_check(r#"fn foo() { <|>1 + 1 }"#, &["1", "1 + 1", "{ 1 + 1 }"]); | 337 | do_check(r#"fn foo() { $01 + 1 }"#, &["1", "1 + 1", "{ 1 + 1 }"]); |
338 | } | 338 | } |
339 | 339 | ||
340 | #[test] | 340 | #[test] |
341 | fn test_extend_selection_list() { | 341 | fn test_extend_selection_list() { |
342 | do_check(r#"fn foo(<|>x: i32) {}"#, &["x", "x: i32"]); | 342 | do_check(r#"fn foo($0x: i32) {}"#, &["x", "x: i32"]); |
343 | do_check(r#"fn foo(<|>x: i32, y: i32) {}"#, &["x", "x: i32", "x: i32, "]); | 343 | do_check(r#"fn foo($0x: i32, y: i32) {}"#, &["x", "x: i32", "x: i32, "]); |
344 | do_check(r#"fn foo(<|>x: i32,y: i32) {}"#, &["x", "x: i32", "x: i32,", "(x: i32,y: i32)"]); | 344 | do_check(r#"fn foo($0x: i32,y: i32) {}"#, &["x", "x: i32", "x: i32,", "(x: i32,y: i32)"]); |
345 | do_check(r#"fn foo(x: i32, <|>y: i32) {}"#, &["y", "y: i32", ", y: i32"]); | 345 | do_check(r#"fn foo(x: i32, $0y: i32) {}"#, &["y", "y: i32", ", y: i32"]); |
346 | do_check(r#"fn foo(x: i32, <|>y: i32, ) {}"#, &["y", "y: i32", "y: i32, "]); | 346 | do_check(r#"fn foo(x: i32, $0y: i32, ) {}"#, &["y", "y: i32", "y: i32, "]); |
347 | do_check(r#"fn foo(x: i32,<|>y: i32) {}"#, &["y", "y: i32", ",y: i32"]); | 347 | do_check(r#"fn foo(x: i32,$0y: i32) {}"#, &["y", "y: i32", ",y: i32"]); |
348 | 348 | ||
349 | do_check(r#"const FOO: [usize; 2] = [ 22<|> , 33];"#, &["22", "22 , "]); | 349 | do_check(r#"const FOO: [usize; 2] = [ 22$0 , 33];"#, &["22", "22 , "]); |
350 | do_check(r#"const FOO: [usize; 2] = [ 22 , 33<|>];"#, &["33", ", 33"]); | 350 | do_check(r#"const FOO: [usize; 2] = [ 22 , 33$0];"#, &["33", ", 33"]); |
351 | do_check(r#"const FOO: [usize; 2] = [ 22 , 33<|> ,];"#, &["33", "33 ,", "[ 22 , 33 ,]"]); | 351 | do_check(r#"const FOO: [usize; 2] = [ 22 , 33$0 ,];"#, &["33", "33 ,", "[ 22 , 33 ,]"]); |
352 | 352 | ||
353 | do_check(r#"fn main() { (1, 2<|>) }"#, &["2", ", 2", "(1, 2)"]); | 353 | do_check(r#"fn main() { (1, 2$0) }"#, &["2", ", 2", "(1, 2)"]); |
354 | 354 | ||
355 | do_check( | 355 | do_check( |
356 | r#" | 356 | r#" |
357 | const FOO: [usize; 2] = [ | 357 | const FOO: [usize; 2] = [ |
358 | 22, | 358 | 22, |
359 | <|>33, | 359 | $033, |
360 | ]"#, | 360 | ]"#, |
361 | &["33", "33,"], | 361 | &["33", "33,"], |
362 | ); | 362 | ); |
@@ -365,7 +365,7 @@ const FOO: [usize; 2] = [ | |||
365 | r#" | 365 | r#" |
366 | const FOO: [usize; 2] = [ | 366 | const FOO: [usize; 2] = [ |
367 | 22 | 367 | 22 |
368 | , 33<|>, | 368 | , 33$0, |
369 | ]"#, | 369 | ]"#, |
370 | &["33", "33,"], | 370 | &["33", "33,"], |
371 | ); | 371 | ); |
@@ -376,7 +376,7 @@ const FOO: [usize; 2] = [ | |||
376 | do_check( | 376 | do_check( |
377 | r#" | 377 | r#" |
378 | impl S { | 378 | impl S { |
379 | <|> fn foo() { | 379 | $0 fn foo() { |
380 | 380 | ||
381 | } | 381 | } |
382 | }"#, | 382 | }"#, |
@@ -393,7 +393,7 @@ struct A; | |||
393 | /// bla | 393 | /// bla |
394 | /// bla | 394 | /// bla |
395 | struct B { | 395 | struct B { |
396 | <|> | 396 | $0 |
397 | } | 397 | } |
398 | "#, | 398 | "#, |
399 | &["\n \n", "{\n \n}", "/// bla\n/// bla\nstruct B {\n \n}"], | 399 | &["\n \n", "{\n \n}", "/// bla\n/// bla\nstruct B {\n \n}"], |
@@ -407,7 +407,7 @@ struct B { | |||
407 | fn bar(){} | 407 | fn bar(){} |
408 | 408 | ||
409 | // fn foo() { | 409 | // fn foo() { |
410 | // 1 + <|>1 | 410 | // 1 + $01 |
411 | // } | 411 | // } |
412 | 412 | ||
413 | // fn foo(){} | 413 | // fn foo(){} |
@@ -419,7 +419,7 @@ fn bar(){} | |||
419 | r#" | 419 | r#" |
420 | // #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 420 | // #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
421 | // pub enum Direction { | 421 | // pub enum Direction { |
422 | // <|> Next, | 422 | // $0 Next, |
423 | // Prev | 423 | // Prev |
424 | // } | 424 | // } |
425 | "#, | 425 | "#, |
@@ -433,27 +433,27 @@ fn bar(){} | |||
433 | r#" | 433 | r#" |
434 | /* | 434 | /* |
435 | foo | 435 | foo |
436 | _bar1<|>*/ | 436 | _bar1$0*/ |
437 | "#, | 437 | "#, |
438 | &["_bar1", "/*\nfoo\n_bar1*/"], | 438 | &["_bar1", "/*\nfoo\n_bar1*/"], |
439 | ); | 439 | ); |
440 | 440 | ||
441 | do_check(r#"//!<|>foo_2 bar"#, &["foo_2", "//!foo_2 bar"]); | 441 | do_check(r#"//!$0foo_2 bar"#, &["foo_2", "//!foo_2 bar"]); |
442 | 442 | ||
443 | do_check(r#"/<|>/foo bar"#, &["//foo bar"]); | 443 | do_check(r#"/$0/foo bar"#, &["//foo bar"]); |
444 | } | 444 | } |
445 | 445 | ||
446 | #[test] | 446 | #[test] |
447 | fn test_extend_selection_prefer_idents() { | 447 | fn test_extend_selection_prefer_idents() { |
448 | do_check( | 448 | do_check( |
449 | r#" | 449 | r#" |
450 | fn main() { foo<|>+bar;} | 450 | fn main() { foo$0+bar;} |
451 | "#, | 451 | "#, |
452 | &["foo", "foo+bar"], | 452 | &["foo", "foo+bar"], |
453 | ); | 453 | ); |
454 | do_check( | 454 | do_check( |
455 | r#" | 455 | r#" |
456 | fn main() { foo+<|>bar;} | 456 | fn main() { foo+$0bar;} |
457 | "#, | 457 | "#, |
458 | &["bar", "foo+bar"], | 458 | &["bar", "foo+bar"], |
459 | ); | 459 | ); |
@@ -461,18 +461,18 @@ fn main() { foo+<|>bar;} | |||
461 | 461 | ||
462 | #[test] | 462 | #[test] |
463 | fn test_extend_selection_prefer_lifetimes() { | 463 | fn test_extend_selection_prefer_lifetimes() { |
464 | do_check(r#"fn foo<<|>'a>() {}"#, &["'a", "<'a>"]); | 464 | do_check(r#"fn foo<$0'a>() {}"#, &["'a", "<'a>"]); |
465 | do_check(r#"fn foo<'a<|>>() {}"#, &["'a", "<'a>"]); | 465 | do_check(r#"fn foo<'a$0>() {}"#, &["'a", "<'a>"]); |
466 | } | 466 | } |
467 | 467 | ||
468 | #[test] | 468 | #[test] |
469 | fn test_extend_selection_select_first_word() { | 469 | fn test_extend_selection_select_first_word() { |
470 | do_check(r#"// foo bar b<|>az quxx"#, &["baz", "// foo bar baz quxx"]); | 470 | do_check(r#"// foo bar b$0az quxx"#, &["baz", "// foo bar baz quxx"]); |
471 | do_check( | 471 | do_check( |
472 | r#" | 472 | r#" |
473 | impl S { | 473 | impl S { |
474 | fn foo() { | 474 | fn foo() { |
475 | // hel<|>lo world | 475 | // hel$0lo world |
476 | } | 476 | } |
477 | } | 477 | } |
478 | "#, | 478 | "#, |
@@ -486,7 +486,7 @@ fn foo() { | |||
486 | r#" | 486 | r#" |
487 | fn bar(){} | 487 | fn bar(){} |
488 | 488 | ||
489 | " fn f<|>oo() {" | 489 | " fn f$0oo() {" |
490 | "#, | 490 | "#, |
491 | &["foo", "\" fn foo() {\""], | 491 | &["foo", "\" fn foo() {\""], |
492 | ); | 492 | ); |
@@ -499,7 +499,7 @@ fn bar(){} | |||
499 | fn foo<R>() | 499 | fn foo<R>() |
500 | where | 500 | where |
501 | R: req::Request + 'static, | 501 | R: req::Request + 'static, |
502 | R::Params: DeserializeOwned<|> + panic::UnwindSafe + 'static, | 502 | R::Params: DeserializeOwned$0 + panic::UnwindSafe + 'static, |
503 | R::Result: Serialize + 'static, | 503 | R::Result: Serialize + 'static, |
504 | "#, | 504 | "#, |
505 | &[ | 505 | &[ |
@@ -510,26 +510,26 @@ fn foo<R>() | |||
510 | "R::Params: DeserializeOwned + panic::UnwindSafe + 'static,", | 510 | "R::Params: DeserializeOwned + panic::UnwindSafe + 'static,", |
511 | ], | 511 | ], |
512 | ); | 512 | ); |
513 | do_check(r#"fn foo<T>() where T: <|>Copy"#, &["Copy"]); | 513 | do_check(r#"fn foo<T>() where T: $0Copy"#, &["Copy"]); |
514 | do_check(r#"fn foo<T>() where T: <|>Copy + Display"#, &["Copy", "Copy + "]); | 514 | do_check(r#"fn foo<T>() where T: $0Copy + Display"#, &["Copy", "Copy + "]); |
515 | do_check(r#"fn foo<T>() where T: <|>Copy +Display"#, &["Copy", "Copy +"]); | 515 | do_check(r#"fn foo<T>() where T: $0Copy +Display"#, &["Copy", "Copy +"]); |
516 | do_check(r#"fn foo<T>() where T: <|>Copy+Display"#, &["Copy", "Copy+"]); | 516 | do_check(r#"fn foo<T>() where T: $0Copy+Display"#, &["Copy", "Copy+"]); |
517 | do_check(r#"fn foo<T>() where T: Copy + <|>Display"#, &["Display", "+ Display"]); | 517 | do_check(r#"fn foo<T>() where T: Copy + $0Display"#, &["Display", "+ Display"]); |
518 | do_check(r#"fn foo<T>() where T: Copy + <|>Display + Sync"#, &["Display", "Display + "]); | 518 | do_check(r#"fn foo<T>() where T: Copy + $0Display + Sync"#, &["Display", "Display + "]); |
519 | do_check(r#"fn foo<T>() where T: Copy +<|>Display"#, &["Display", "+Display"]); | 519 | do_check(r#"fn foo<T>() where T: Copy +$0Display"#, &["Display", "+Display"]); |
520 | } | 520 | } |
521 | 521 | ||
522 | #[test] | 522 | #[test] |
523 | fn test_extend_trait_bounds_list_inline() { | 523 | fn test_extend_trait_bounds_list_inline() { |
524 | do_check(r#"fn foo<T: <|>Copy>() {}"#, &["Copy"]); | 524 | do_check(r#"fn foo<T: $0Copy>() {}"#, &["Copy"]); |
525 | do_check(r#"fn foo<T: <|>Copy + Display>() {}"#, &["Copy", "Copy + "]); | 525 | do_check(r#"fn foo<T: $0Copy + Display>() {}"#, &["Copy", "Copy + "]); |
526 | do_check(r#"fn foo<T: <|>Copy +Display>() {}"#, &["Copy", "Copy +"]); | 526 | do_check(r#"fn foo<T: $0Copy +Display>() {}"#, &["Copy", "Copy +"]); |
527 | do_check(r#"fn foo<T: <|>Copy+Display>() {}"#, &["Copy", "Copy+"]); | 527 | do_check(r#"fn foo<T: $0Copy+Display>() {}"#, &["Copy", "Copy+"]); |
528 | do_check(r#"fn foo<T: Copy + <|>Display>() {}"#, &["Display", "+ Display"]); | 528 | do_check(r#"fn foo<T: Copy + $0Display>() {}"#, &["Display", "+ Display"]); |
529 | do_check(r#"fn foo<T: Copy + <|>Display + Sync>() {}"#, &["Display", "Display + "]); | 529 | do_check(r#"fn foo<T: Copy + $0Display + Sync>() {}"#, &["Display", "Display + "]); |
530 | do_check(r#"fn foo<T: Copy +<|>Display>() {}"#, &["Display", "+Display"]); | 530 | do_check(r#"fn foo<T: Copy +$0Display>() {}"#, &["Display", "+Display"]); |
531 | do_check( | 531 | do_check( |
532 | r#"fn foo<T: Copy<|> + Display, U: Copy>() {}"#, | 532 | r#"fn foo<T: Copy$0 + Display, U: Copy>() {}"#, |
533 | &[ | 533 | &[ |
534 | "Copy", | 534 | "Copy", |
535 | "Copy + ", | 535 | "Copy + ", |
@@ -544,19 +544,19 @@ fn foo<R>() | |||
544 | #[test] | 544 | #[test] |
545 | fn test_extend_selection_on_tuple_in_type() { | 545 | fn test_extend_selection_on_tuple_in_type() { |
546 | do_check( | 546 | do_check( |
547 | r#"fn main() { let _: (krate, <|>_crate_def_map, module_id) = (); }"#, | 547 | r#"fn main() { let _: (krate, $0_crate_def_map, module_id) = (); }"#, |
548 | &["_crate_def_map", "_crate_def_map, ", "(krate, _crate_def_map, module_id)"], | 548 | &["_crate_def_map", "_crate_def_map, ", "(krate, _crate_def_map, module_id)"], |
549 | ); | 549 | ); |
550 | // white space variations | 550 | // white space variations |
551 | do_check( | 551 | do_check( |
552 | r#"fn main() { let _: (krate,<|>_crate_def_map,module_id) = (); }"#, | 552 | r#"fn main() { let _: (krate,$0_crate_def_map,module_id) = (); }"#, |
553 | &["_crate_def_map", "_crate_def_map,", "(krate,_crate_def_map,module_id)"], | 553 | &["_crate_def_map", "_crate_def_map,", "(krate,_crate_def_map,module_id)"], |
554 | ); | 554 | ); |
555 | do_check( | 555 | do_check( |
556 | r#" | 556 | r#" |
557 | fn main() { let _: ( | 557 | fn main() { let _: ( |
558 | krate, | 558 | krate, |
559 | _crate<|>_def_map, | 559 | _crate$0_def_map, |
560 | module_id | 560 | module_id |
561 | ) = (); }"#, | 561 | ) = (); }"#, |
562 | &[ | 562 | &[ |
@@ -570,19 +570,19 @@ fn main() { let _: ( | |||
570 | #[test] | 570 | #[test] |
571 | fn test_extend_selection_on_tuple_in_rvalue() { | 571 | fn test_extend_selection_on_tuple_in_rvalue() { |
572 | do_check( | 572 | do_check( |
573 | r#"fn main() { let var = (krate, _crate_def_map<|>, module_id); }"#, | 573 | r#"fn main() { let var = (krate, _crate_def_map$0, module_id); }"#, |
574 | &["_crate_def_map", "_crate_def_map, ", "(krate, _crate_def_map, module_id)"], | 574 | &["_crate_def_map", "_crate_def_map, ", "(krate, _crate_def_map, module_id)"], |
575 | ); | 575 | ); |
576 | // white space variations | 576 | // white space variations |
577 | do_check( | 577 | do_check( |
578 | r#"fn main() { let var = (krate,_crate<|>_def_map,module_id); }"#, | 578 | r#"fn main() { let var = (krate,_crate$0_def_map,module_id); }"#, |
579 | &["_crate_def_map", "_crate_def_map,", "(krate,_crate_def_map,module_id)"], | 579 | &["_crate_def_map", "_crate_def_map,", "(krate,_crate_def_map,module_id)"], |
580 | ); | 580 | ); |
581 | do_check( | 581 | do_check( |
582 | r#" | 582 | r#" |
583 | fn main() { let var = ( | 583 | fn main() { let var = ( |
584 | krate, | 584 | krate, |
585 | _crate_def_map<|>, | 585 | _crate_def_map$0, |
586 | module_id | 586 | module_id |
587 | ); }"#, | 587 | ); }"#, |
588 | &[ | 588 | &[ |
@@ -596,19 +596,19 @@ fn main() { let var = ( | |||
596 | #[test] | 596 | #[test] |
597 | fn test_extend_selection_on_tuple_pat() { | 597 | fn test_extend_selection_on_tuple_pat() { |
598 | do_check( | 598 | do_check( |
599 | r#"fn main() { let (krate, _crate_def_map<|>, module_id) = var; }"#, | 599 | r#"fn main() { let (krate, _crate_def_map$0, module_id) = var; }"#, |
600 | &["_crate_def_map", "_crate_def_map, ", "(krate, _crate_def_map, module_id)"], | 600 | &["_crate_def_map", "_crate_def_map, ", "(krate, _crate_def_map, module_id)"], |
601 | ); | 601 | ); |
602 | // white space variations | 602 | // white space variations |
603 | do_check( | 603 | do_check( |
604 | r#"fn main() { let (krate,_crate<|>_def_map,module_id) = var; }"#, | 604 | r#"fn main() { let (krate,_crate$0_def_map,module_id) = var; }"#, |
605 | &["_crate_def_map", "_crate_def_map,", "(krate,_crate_def_map,module_id)"], | 605 | &["_crate_def_map", "_crate_def_map,", "(krate,_crate_def_map,module_id)"], |
606 | ); | 606 | ); |
607 | do_check( | 607 | do_check( |
608 | r#" | 608 | r#" |
609 | fn main() { let ( | 609 | fn main() { let ( |
610 | krate, | 610 | krate, |
611 | _crate_def_map<|>, | 611 | _crate_def_map$0, |
612 | module_id | 612 | module_id |
613 | ) = var; }"#, | 613 | ) = var; }"#, |
614 | &[ | 614 | &[ |
@@ -623,7 +623,7 @@ fn main() { let ( | |||
623 | fn extend_selection_inside_macros() { | 623 | fn extend_selection_inside_macros() { |
624 | do_check( | 624 | do_check( |
625 | r#"macro_rules! foo { ($item:item) => {$item} } | 625 | r#"macro_rules! foo { ($item:item) => {$item} } |
626 | foo!{fn hello(na<|>me:usize){}}"#, | 626 | foo!{fn hello(na$0me:usize){}}"#, |
627 | &[ | 627 | &[ |
628 | "name", | 628 | "name", |
629 | "name:usize", | 629 | "name:usize", |
@@ -640,7 +640,7 @@ fn main() { let ( | |||
640 | do_check( | 640 | do_check( |
641 | r#" macro_rules! foo2 { ($item:item) => {$item} } | 641 | r#" macro_rules! foo2 { ($item:item) => {$item} } |
642 | macro_rules! foo { ($item:item) => {foo2!($item);} } | 642 | macro_rules! foo { ($item:item) => {foo2!($item);} } |
643 | foo!{fn hello(na<|>me:usize){}}"#, | 643 | foo!{fn hello(na$0me:usize){}}"#, |
644 | &[ | 644 | &[ |
645 | "name", | 645 | "name", |
646 | "name:usize", | 646 | "name:usize", |
diff --git a/crates/ide/src/fixture.rs b/crates/ide/src/fixture.rs index eb57f9224..cc8218885 100644 --- a/crates/ide/src/fixture.rs +++ b/crates/ide/src/fixture.rs | |||
@@ -20,12 +20,12 @@ pub(crate) fn files(ra_fixture: &str) -> (Analysis, Vec<FileId>) { | |||
20 | (host.analysis(), change_fixture.files) | 20 | (host.analysis(), change_fixture.files) |
21 | } | 21 | } |
22 | 22 | ||
23 | /// Creates analysis from a multi-file fixture, returns positions marked with <|>. | 23 | /// Creates analysis from a multi-file fixture, returns positions marked with $0. |
24 | pub(crate) fn position(ra_fixture: &str) -> (Analysis, FilePosition) { | 24 | pub(crate) fn position(ra_fixture: &str) -> (Analysis, FilePosition) { |
25 | let mut host = AnalysisHost::default(); | 25 | let mut host = AnalysisHost::default(); |
26 | let change_fixture = ChangeFixture::parse(ra_fixture); | 26 | let change_fixture = ChangeFixture::parse(ra_fixture); |
27 | host.db.apply_change(change_fixture.change); | 27 | host.db.apply_change(change_fixture.change); |
28 | let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker (<|>)"); | 28 | let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)"); |
29 | let offset = match range_or_offset { | 29 | let offset = match range_or_offset { |
30 | RangeOrOffset::Range(_) => panic!(), | 30 | RangeOrOffset::Range(_) => panic!(), |
31 | RangeOrOffset::Offset(it) => it, | 31 | RangeOrOffset::Offset(it) => it, |
@@ -33,12 +33,12 @@ pub(crate) fn position(ra_fixture: &str) -> (Analysis, FilePosition) { | |||
33 | (host.analysis(), FilePosition { file_id, offset }) | 33 | (host.analysis(), FilePosition { file_id, offset }) |
34 | } | 34 | } |
35 | 35 | ||
36 | /// Creates analysis for a single file, returns range marked with a pair of <|>. | 36 | /// Creates analysis for a single file, returns range marked with a pair of $0. |
37 | pub(crate) fn range(ra_fixture: &str) -> (Analysis, FileRange) { | 37 | pub(crate) fn range(ra_fixture: &str) -> (Analysis, FileRange) { |
38 | let mut host = AnalysisHost::default(); | 38 | let mut host = AnalysisHost::default(); |
39 | let change_fixture = ChangeFixture::parse(ra_fixture); | 39 | let change_fixture = ChangeFixture::parse(ra_fixture); |
40 | host.db.apply_change(change_fixture.change); | 40 | host.db.apply_change(change_fixture.change); |
41 | let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker (<|>)"); | 41 | let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)"); |
42 | let range = match range_or_offset { | 42 | let range = match range_or_offset { |
43 | RangeOrOffset::Range(it) => it, | 43 | RangeOrOffset::Range(it) => it, |
44 | RangeOrOffset::Offset(_) => panic!(), | 44 | RangeOrOffset::Offset(_) => panic!(), |
@@ -46,12 +46,12 @@ pub(crate) fn range(ra_fixture: &str) -> (Analysis, FileRange) { | |||
46 | (host.analysis(), FileRange { file_id, range }) | 46 | (host.analysis(), FileRange { file_id, range }) |
47 | } | 47 | } |
48 | 48 | ||
49 | /// Creates analysis from a multi-file fixture, returns positions marked with <|>. | 49 | /// Creates analysis from a multi-file fixture, returns positions marked with $0. |
50 | pub(crate) fn annotations(ra_fixture: &str) -> (Analysis, FilePosition, Vec<(FileRange, String)>) { | 50 | pub(crate) fn annotations(ra_fixture: &str) -> (Analysis, FilePosition, Vec<(FileRange, String)>) { |
51 | let mut host = AnalysisHost::default(); | 51 | let mut host = AnalysisHost::default(); |
52 | let change_fixture = ChangeFixture::parse(ra_fixture); | 52 | let change_fixture = ChangeFixture::parse(ra_fixture); |
53 | host.db.apply_change(change_fixture.change); | 53 | host.db.apply_change(change_fixture.change); |
54 | let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker (<|>)"); | 54 | let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)"); |
55 | let offset = match range_or_offset { | 55 | let offset = match range_or_offset { |
56 | RangeOrOffset::Range(_) => panic!(), | 56 | RangeOrOffset::Range(_) => panic!(), |
57 | RangeOrOffset::Offset(it) => it, | 57 | RangeOrOffset::Offset(it) => it, |
diff --git a/crates/ide/src/fn_references.rs b/crates/ide/src/fn_references.rs index 5cbbe306e..f6e5a522b 100644 --- a/crates/ide/src/fn_references.rs +++ b/crates/ide/src/fn_references.rs | |||
@@ -34,7 +34,7 @@ mod tests { | |||
34 | fn test_find_all_methods() { | 34 | fn test_find_all_methods() { |
35 | let (analysis, pos) = fixture::position( | 35 | let (analysis, pos) = fixture::position( |
36 | r#" | 36 | r#" |
37 | fn private_fn() {<|>} | 37 | fn private_fn() {$0} |
38 | 38 | ||
39 | pub fn pub_fn() {} | 39 | pub fn pub_fn() {} |
40 | 40 | ||
@@ -51,7 +51,7 @@ mod tests { | |||
51 | let (analysis, pos) = fixture::position( | 51 | let (analysis, pos) = fixture::position( |
52 | r#" | 52 | r#" |
53 | trait Foo { | 53 | trait Foo { |
54 | fn bar() {<|>} | 54 | fn bar() {$0} |
55 | fn baz() {} | 55 | fn baz() {} |
56 | } | 56 | } |
57 | "#, | 57 | "#, |
@@ -67,7 +67,7 @@ mod tests { | |||
67 | r#" | 67 | r#" |
68 | //- /lib.rs | 68 | //- /lib.rs |
69 | #[test] | 69 | #[test] |
70 | fn foo() {<|>} | 70 | fn foo() {$0} |
71 | 71 | ||
72 | pub fn pub_fn() {} | 72 | pub fn pub_fn() {} |
73 | 73 | ||
diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs index 912144f8b..cd4afc804 100644 --- a/crates/ide/src/goto_definition.rs +++ b/crates/ide/src/goto_definition.rs | |||
@@ -1,14 +1,18 @@ | |||
1 | use either::Either; | 1 | use either::Either; |
2 | use hir::Semantics; | 2 | use hir::{HasAttrs, ModuleDef, Semantics}; |
3 | use ide_db::{ | 3 | use ide_db::{ |
4 | base_db::FileId, | 4 | base_db::FileId, |
5 | defs::{NameClass, NameRefClass}, | 5 | defs::{Definition, NameClass, NameRefClass}, |
6 | symbol_index, RootDatabase, | 6 | symbol_index, RootDatabase, |
7 | }; | 7 | }; |
8 | use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; | 8 | use syntax::{ |
9 | ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, TextSize, TokenAtOffset, T, | ||
10 | }; | ||
9 | 11 | ||
10 | use crate::{ | 12 | use crate::{ |
11 | display::{ToNav, TryToNav}, | 13 | display::{ToNav, TryToNav}, |
14 | doc_links::extract_definitions_from_markdown, | ||
15 | runnables::doc_owner_to_def, | ||
12 | FilePosition, NavigationTarget, RangeInfo, SymbolKind, | 16 | FilePosition, NavigationTarget, RangeInfo, SymbolKind, |
13 | }; | 17 | }; |
14 | 18 | ||
@@ -30,6 +34,10 @@ pub(crate) fn goto_definition( | |||
30 | let original_token = pick_best(file.token_at_offset(position.offset))?; | 34 | let original_token = pick_best(file.token_at_offset(position.offset))?; |
31 | let token = sema.descend_into_macros(original_token.clone()); | 35 | let token = sema.descend_into_macros(original_token.clone()); |
32 | let parent = token.parent(); | 36 | let parent = token.parent(); |
37 | if let Some(comment) = ast::Comment::cast(token.clone()) { | ||
38 | let nav = def_for_doc_comment(&sema, position, &comment)?.try_to_nav(db)?; | ||
39 | return Some(RangeInfo::new(original_token.text_range(), vec![nav])); | ||
40 | } | ||
33 | 41 | ||
34 | let nav_targets = match_ast! { | 42 | let nav_targets = match_ast! { |
35 | match parent { | 43 | match parent { |
@@ -68,11 +76,58 @@ pub(crate) fn goto_definition( | |||
68 | Some(RangeInfo::new(original_token.text_range(), nav_targets)) | 76 | Some(RangeInfo::new(original_token.text_range(), nav_targets)) |
69 | } | 77 | } |
70 | 78 | ||
79 | fn def_for_doc_comment( | ||
80 | sema: &Semantics<RootDatabase>, | ||
81 | position: FilePosition, | ||
82 | doc_comment: &ast::Comment, | ||
83 | ) -> Option<hir::ModuleDef> { | ||
84 | let parent = doc_comment.syntax().parent(); | ||
85 | let (link, ns) = extract_positioned_link_from_comment(position, doc_comment)?; | ||
86 | |||
87 | let def = doc_owner_to_def(sema, parent)?; | ||
88 | match def { | ||
89 | Definition::ModuleDef(def) => match def { | ||
90 | ModuleDef::Module(it) => it.resolve_doc_path(sema.db, &link, ns), | ||
91 | ModuleDef::Function(it) => it.resolve_doc_path(sema.db, &link, ns), | ||
92 | ModuleDef::Adt(it) => it.resolve_doc_path(sema.db, &link, ns), | ||
93 | ModuleDef::Variant(it) => it.resolve_doc_path(sema.db, &link, ns), | ||
94 | ModuleDef::Const(it) => it.resolve_doc_path(sema.db, &link, ns), | ||
95 | ModuleDef::Static(it) => it.resolve_doc_path(sema.db, &link, ns), | ||
96 | ModuleDef::Trait(it) => it.resolve_doc_path(sema.db, &link, ns), | ||
97 | ModuleDef::TypeAlias(it) => it.resolve_doc_path(sema.db, &link, ns), | ||
98 | ModuleDef::BuiltinType(_) => return None, | ||
99 | }, | ||
100 | Definition::Macro(it) => it.resolve_doc_path(sema.db, &link, ns), | ||
101 | Definition::Field(it) => it.resolve_doc_path(sema.db, &link, ns), | ||
102 | Definition::SelfType(_) | ||
103 | | Definition::Local(_) | ||
104 | | Definition::GenericParam(_) | ||
105 | | Definition::Label(_) => return None, | ||
106 | } | ||
107 | } | ||
108 | |||
109 | fn extract_positioned_link_from_comment( | ||
110 | position: FilePosition, | ||
111 | comment: &ast::Comment, | ||
112 | ) -> Option<(String, Option<hir::Namespace>)> { | ||
113 | let comment_range = comment.syntax().text_range(); | ||
114 | let doc_comment = comment.doc_comment()?; | ||
115 | let def_links = extract_definitions_from_markdown(doc_comment); | ||
116 | let (def_link, ns, _) = def_links.iter().min_by_key(|(_, _, def_link_range)| { | ||
117 | let matched_position = comment_range.start() + TextSize::from(def_link_range.start as u32); | ||
118 | match position.offset.checked_sub(matched_position) { | ||
119 | Some(distance) => distance, | ||
120 | None => comment_range.end(), | ||
121 | } | ||
122 | })?; | ||
123 | Some((def_link.to_string(), ns.clone())) | ||
124 | } | ||
125 | |||
71 | fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> { | 126 | fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> { |
72 | return tokens.max_by_key(priority); | 127 | return tokens.max_by_key(priority); |
73 | fn priority(n: &SyntaxToken) -> usize { | 128 | fn priority(n: &SyntaxToken) -> usize { |
74 | match n.kind() { | 129 | match n.kind() { |
75 | IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] => 2, | 130 | IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | COMMENT => 2, |
76 | kind if kind.is_trivia() => 0, | 131 | kind if kind.is_trivia() => 0, |
77 | _ => 1, | 132 | _ => 1, |
78 | } | 133 | } |
@@ -166,7 +221,7 @@ mod tests { | |||
166 | check( | 221 | check( |
167 | r#" | 222 | r#" |
168 | //- /main.rs crate:main deps:std | 223 | //- /main.rs crate:main deps:std |
169 | extern crate std<|>; | 224 | extern crate std$0; |
170 | //- /std/lib.rs crate:std | 225 | //- /std/lib.rs crate:std |
171 | // empty | 226 | // empty |
172 | //^ file | 227 | //^ file |
@@ -179,7 +234,7 @@ mod tests { | |||
179 | check( | 234 | check( |
180 | r#" | 235 | r#" |
181 | //- /main.rs crate:main deps:std | 236 | //- /main.rs crate:main deps:std |
182 | extern crate std as abc<|>; | 237 | extern crate std as abc$0; |
183 | //- /std/lib.rs crate:std | 238 | //- /std/lib.rs crate:std |
184 | // empty | 239 | // empty |
185 | //^ file | 240 | //^ file |
@@ -193,7 +248,7 @@ mod tests { | |||
193 | r#" | 248 | r#" |
194 | struct Foo; | 249 | struct Foo; |
195 | //^^^ | 250 | //^^^ |
196 | enum E { X(Foo<|>) } | 251 | enum E { X(Foo$0) } |
197 | "#, | 252 | "#, |
198 | ); | 253 | ); |
199 | } | 254 | } |
@@ -204,7 +259,7 @@ enum E { X(Foo<|>) } | |||
204 | r#" | 259 | r#" |
205 | struct Foo; | 260 | struct Foo; |
206 | //^^^ | 261 | //^^^ |
207 | enum E { X(<|>Foo) } | 262 | enum E { X($0Foo) } |
208 | "#, | 263 | "#, |
209 | ); | 264 | ); |
210 | } | 265 | } |
@@ -217,7 +272,7 @@ enum E { X(<|>Foo) } | |||
217 | use a::Foo; | 272 | use a::Foo; |
218 | mod a; | 273 | mod a; |
219 | mod b; | 274 | mod b; |
220 | enum E { X(Foo<|>) } | 275 | enum E { X(Foo$0) } |
221 | 276 | ||
222 | //- /a.rs | 277 | //- /a.rs |
223 | struct Foo; | 278 | struct Foo; |
@@ -233,7 +288,7 @@ struct Foo; | |||
233 | check( | 288 | check( |
234 | r#" | 289 | r#" |
235 | //- /lib.rs | 290 | //- /lib.rs |
236 | mod <|>foo; | 291 | mod $0foo; |
237 | 292 | ||
238 | //- /foo.rs | 293 | //- /foo.rs |
239 | // empty | 294 | // empty |
@@ -244,7 +299,7 @@ mod <|>foo; | |||
244 | check( | 299 | check( |
245 | r#" | 300 | r#" |
246 | //- /lib.rs | 301 | //- /lib.rs |
247 | mod <|>foo; | 302 | mod $0foo; |
248 | 303 | ||
249 | //- /foo/mod.rs | 304 | //- /foo/mod.rs |
250 | // empty | 305 | // empty |
@@ -260,7 +315,7 @@ mod <|>foo; | |||
260 | macro_rules! foo { () => { () } } | 315 | macro_rules! foo { () => { () } } |
261 | //^^^ | 316 | //^^^ |
262 | fn bar() { | 317 | fn bar() { |
263 | <|>foo!(); | 318 | $0foo!(); |
264 | } | 319 | } |
265 | "#, | 320 | "#, |
266 | ); | 321 | ); |
@@ -273,7 +328,7 @@ fn bar() { | |||
273 | //- /lib.rs | 328 | //- /lib.rs |
274 | use foo::foo; | 329 | use foo::foo; |
275 | fn bar() { | 330 | fn bar() { |
276 | <|>foo!(); | 331 | $0foo!(); |
277 | } | 332 | } |
278 | 333 | ||
279 | //- /foo/lib.rs | 334 | //- /foo/lib.rs |
@@ -289,7 +344,7 @@ macro_rules! foo { () => { () } } | |||
289 | check( | 344 | check( |
290 | r#" | 345 | r#" |
291 | //- /lib.rs | 346 | //- /lib.rs |
292 | use foo::foo<|>; | 347 | use foo::foo$0; |
293 | 348 | ||
294 | //- /foo/lib.rs | 349 | //- /foo/lib.rs |
295 | #[macro_export] | 350 | #[macro_export] |
@@ -312,7 +367,7 @@ define_fn!(foo); | |||
312 | //^^^ | 367 | //^^^ |
313 | 368 | ||
314 | fn bar() { | 369 | fn bar() { |
315 | <|>foo(); | 370 | $0foo(); |
316 | } | 371 | } |
317 | "#, | 372 | "#, |
318 | ); | 373 | ); |
@@ -331,7 +386,7 @@ macro_rules! define_fn { | |||
331 | //^^^^^^^^^^^^^ | 386 | //^^^^^^^^^^^^^ |
332 | 387 | ||
333 | fn bar() { | 388 | fn bar() { |
334 | <|>foo(); | 389 | $0foo(); |
335 | } | 390 | } |
336 | "#, | 391 | "#, |
337 | ); | 392 | ); |
@@ -347,7 +402,7 @@ macro_rules! foo {() => {0}} | |||
347 | 402 | ||
348 | fn bar() { | 403 | fn bar() { |
349 | match (0,1) { | 404 | match (0,1) { |
350 | (<|>foo!(), _) => {} | 405 | ($0foo!(), _) => {} |
351 | } | 406 | } |
352 | } | 407 | } |
353 | "#, | 408 | "#, |
@@ -363,7 +418,7 @@ macro_rules! foo {() => {0}} | |||
363 | //^^^ | 418 | //^^^ |
364 | fn bar() { | 419 | fn bar() { |
365 | match 0 { | 420 | match 0 { |
366 | <|>foo!() => {} | 421 | $0foo!() => {} |
367 | } | 422 | } |
368 | } | 423 | } |
369 | "#, | 424 | "#, |
@@ -375,7 +430,7 @@ fn bar() { | |||
375 | check( | 430 | check( |
376 | r#" | 431 | r#" |
377 | //- /lib.rs crate:main deps:foo | 432 | //- /lib.rs crate:main deps:foo |
378 | use foo as bar<|>; | 433 | use foo as bar$0; |
379 | 434 | ||
380 | //- /foo/lib.rs crate:foo | 435 | //- /foo/lib.rs crate:foo |
381 | // empty | 436 | // empty |
@@ -389,7 +444,7 @@ use foo as bar<|>; | |||
389 | check( | 444 | check( |
390 | r#" | 445 | r#" |
391 | //- /lib.rs crate:main deps:foo | 446 | //- /lib.rs crate:main deps:foo |
392 | use foo::foo as bar<|>; | 447 | use foo::foo as bar$0; |
393 | 448 | ||
394 | //- /foo/lib.rs crate:foo | 449 | //- /foo/lib.rs crate:foo |
395 | #[macro_export] | 450 | #[macro_export] |
@@ -410,7 +465,7 @@ impl Foo { | |||
410 | } | 465 | } |
411 | 466 | ||
412 | fn bar(foo: &Foo) { | 467 | fn bar(foo: &Foo) { |
413 | foo.frobnicate<|>(); | 468 | foo.frobnicate$0(); |
414 | } | 469 | } |
415 | "#, | 470 | "#, |
416 | ); | 471 | ); |
@@ -425,7 +480,7 @@ struct Foo { | |||
425 | } //^^^^ | 480 | } //^^^^ |
426 | 481 | ||
427 | fn bar(foo: &Foo) { | 482 | fn bar(foo: &Foo) { |
428 | foo.spam<|>; | 483 | foo.spam$0; |
429 | } | 484 | } |
430 | "#, | 485 | "#, |
431 | ); | 486 | ); |
@@ -442,7 +497,7 @@ struct Foo { | |||
442 | 497 | ||
443 | fn bar() -> Foo { | 498 | fn bar() -> Foo { |
444 | Foo { | 499 | Foo { |
445 | spam<|>: 0, | 500 | spam$0: 0, |
446 | } | 501 | } |
447 | } | 502 | } |
448 | "#, | 503 | "#, |
@@ -459,7 +514,7 @@ struct Foo { | |||
459 | } //^^^^ | 514 | } //^^^^ |
460 | 515 | ||
461 | fn bar(foo: Foo) -> Foo { | 516 | fn bar(foo: Foo) -> Foo { |
462 | let Foo { spam<|>: _, } = foo | 517 | let Foo { spam$0: _, } = foo |
463 | } | 518 | } |
464 | "#, | 519 | "#, |
465 | ); | 520 | ); |
@@ -474,7 +529,7 @@ struct Foo { spam: u32 } | |||
474 | //^^^^ | 529 | //^^^^ |
475 | 530 | ||
476 | fn bar() -> Foo { | 531 | fn bar() -> Foo { |
477 | Foo { spam<|>: m!() } | 532 | Foo { spam$0: m!() } |
478 | } | 533 | } |
479 | ", | 534 | ", |
480 | ); | 535 | ); |
@@ -489,7 +544,7 @@ struct Foo(u32); | |||
489 | 544 | ||
490 | fn bar() { | 545 | fn bar() { |
491 | let foo = Foo(0); | 546 | let foo = Foo(0); |
492 | foo.<|>0; | 547 | foo.$00; |
493 | } | 548 | } |
494 | "#, | 549 | "#, |
495 | ); | 550 | ); |
@@ -505,7 +560,7 @@ impl Foo { | |||
505 | } //^^^^^^^^^^ | 560 | } //^^^^^^^^^^ |
506 | 561 | ||
507 | fn bar(foo: &Foo) { | 562 | fn bar(foo: &Foo) { |
508 | Foo::frobnicate<|>(); | 563 | Foo::frobnicate$0(); |
509 | } | 564 | } |
510 | "#, | 565 | "#, |
511 | ); | 566 | ); |
@@ -520,7 +575,7 @@ trait Foo { | |||
520 | } //^^^^^^^^^^ | 575 | } //^^^^^^^^^^ |
521 | 576 | ||
522 | fn bar() { | 577 | fn bar() { |
523 | Foo::frobnicate<|>(); | 578 | Foo::frobnicate$0(); |
524 | } | 579 | } |
525 | "#, | 580 | "#, |
526 | ); | 581 | ); |
@@ -537,7 +592,7 @@ trait Trait { | |||
537 | impl Trait for Foo {} | 592 | impl Trait for Foo {} |
538 | 593 | ||
539 | fn bar() { | 594 | fn bar() { |
540 | Foo::frobnicate<|>(); | 595 | Foo::frobnicate$0(); |
541 | } | 596 | } |
542 | "#, | 597 | "#, |
543 | ); | 598 | ); |
@@ -551,7 +606,7 @@ struct Foo; | |||
551 | impl Foo { | 606 | impl Foo { |
552 | //^^^ | 607 | //^^^ |
553 | pub fn new() -> Self { | 608 | pub fn new() -> Self { |
554 | Self<|> {} | 609 | Self$0 {} |
555 | } | 610 | } |
556 | } | 611 | } |
557 | "#, | 612 | "#, |
@@ -561,7 +616,7 @@ impl Foo { | |||
561 | struct Foo; | 616 | struct Foo; |
562 | impl Foo { | 617 | impl Foo { |
563 | //^^^ | 618 | //^^^ |
564 | pub fn new() -> Self<|> { | 619 | pub fn new() -> Self$0 { |
565 | Self {} | 620 | Self {} |
566 | } | 621 | } |
567 | } | 622 | } |
@@ -573,7 +628,7 @@ impl Foo { | |||
573 | enum Foo { A } | 628 | enum Foo { A } |
574 | impl Foo { | 629 | impl Foo { |
575 | //^^^ | 630 | //^^^ |
576 | pub fn new() -> Self<|> { | 631 | pub fn new() -> Self$0 { |
577 | Foo::A | 632 | Foo::A |
578 | } | 633 | } |
579 | } | 634 | } |
@@ -585,7 +640,7 @@ impl Foo { | |||
585 | enum Foo { A } | 640 | enum Foo { A } |
586 | impl Foo { | 641 | impl Foo { |
587 | //^^^ | 642 | //^^^ |
588 | pub fn thing(a: &Self<|>) { | 643 | pub fn thing(a: &Self$0) { |
589 | } | 644 | } |
590 | } | 645 | } |
591 | "#, | 646 | "#, |
@@ -603,7 +658,7 @@ trait Make { | |||
603 | impl Make for Foo { | 658 | impl Make for Foo { |
604 | //^^^ | 659 | //^^^ |
605 | fn new() -> Self { | 660 | fn new() -> Self { |
606 | Self<|> {} | 661 | Self$0 {} |
607 | } | 662 | } |
608 | } | 663 | } |
609 | "#, | 664 | "#, |
@@ -617,7 +672,7 @@ trait Make { | |||
617 | } | 672 | } |
618 | impl Make for Foo { | 673 | impl Make for Foo { |
619 | //^^^ | 674 | //^^^ |
620 | fn new() -> Self<|> { | 675 | fn new() -> Self$0 { |
621 | Self {} | 676 | Self {} |
622 | } | 677 | } |
623 | } | 678 | } |
@@ -629,7 +684,7 @@ impl Make for Foo { | |||
629 | fn goto_def_when_used_on_definition_name_itself() { | 684 | fn goto_def_when_used_on_definition_name_itself() { |
630 | check( | 685 | check( |
631 | r#" | 686 | r#" |
632 | struct Foo<|> { value: u32 } | 687 | struct Foo$0 { value: u32 } |
633 | //^^^ | 688 | //^^^ |
634 | "#, | 689 | "#, |
635 | ); | 690 | ); |
@@ -637,21 +692,21 @@ struct Foo<|> { value: u32 } | |||
637 | check( | 692 | check( |
638 | r#" | 693 | r#" |
639 | struct Foo { | 694 | struct Foo { |
640 | field<|>: string, | 695 | field$0: string, |
641 | } //^^^^^ | 696 | } //^^^^^ |
642 | "#, | 697 | "#, |
643 | ); | 698 | ); |
644 | 699 | ||
645 | check( | 700 | check( |
646 | r#" | 701 | r#" |
647 | fn foo_test<|>() { } | 702 | fn foo_test$0() { } |
648 | //^^^^^^^^ | 703 | //^^^^^^^^ |
649 | "#, | 704 | "#, |
650 | ); | 705 | ); |
651 | 706 | ||
652 | check( | 707 | check( |
653 | r#" | 708 | r#" |
654 | enum Foo<|> { Variant } | 709 | enum Foo$0 { Variant } |
655 | //^^^ | 710 | //^^^ |
656 | "#, | 711 | "#, |
657 | ); | 712 | ); |
@@ -660,7 +715,7 @@ enum Foo<|> { Variant } | |||
660 | r#" | 715 | r#" |
661 | enum Foo { | 716 | enum Foo { |
662 | Variant1, | 717 | Variant1, |
663 | Variant2<|>, | 718 | Variant2$0, |
664 | //^^^^^^^^ | 719 | //^^^^^^^^ |
665 | Variant3, | 720 | Variant3, |
666 | } | 721 | } |
@@ -669,35 +724,35 @@ enum Foo { | |||
669 | 724 | ||
670 | check( | 725 | check( |
671 | r#" | 726 | r#" |
672 | static INNER<|>: &str = ""; | 727 | static INNER$0: &str = ""; |
673 | //^^^^^ | 728 | //^^^^^ |
674 | "#, | 729 | "#, |
675 | ); | 730 | ); |
676 | 731 | ||
677 | check( | 732 | check( |
678 | r#" | 733 | r#" |
679 | const INNER<|>: &str = ""; | 734 | const INNER$0: &str = ""; |
680 | //^^^^^ | 735 | //^^^^^ |
681 | "#, | 736 | "#, |
682 | ); | 737 | ); |
683 | 738 | ||
684 | check( | 739 | check( |
685 | r#" | 740 | r#" |
686 | type Thing<|> = Option<()>; | 741 | type Thing$0 = Option<()>; |
687 | //^^^^^ | 742 | //^^^^^ |
688 | "#, | 743 | "#, |
689 | ); | 744 | ); |
690 | 745 | ||
691 | check( | 746 | check( |
692 | r#" | 747 | r#" |
693 | trait Foo<|> { } | 748 | trait Foo$0 { } |
694 | //^^^ | 749 | //^^^ |
695 | "#, | 750 | "#, |
696 | ); | 751 | ); |
697 | 752 | ||
698 | check( | 753 | check( |
699 | r#" | 754 | r#" |
700 | mod bar<|> { } | 755 | mod bar$0 { } |
701 | //^^^ | 756 | //^^^ |
702 | "#, | 757 | "#, |
703 | ); | 758 | ); |
@@ -714,7 +769,7 @@ fn foo() {} | |||
714 | //^^^ | 769 | //^^^ |
715 | id! { | 770 | id! { |
716 | fn bar() { | 771 | fn bar() { |
717 | fo<|>o(); | 772 | fo$0o(); |
718 | } | 773 | } |
719 | } | 774 | } |
720 | mod confuse_index { fn foo(); } | 775 | mod confuse_index { fn foo(); } |
@@ -743,7 +798,7 @@ pub mod __export { | |||
743 | fn foo() -> i8 {} | 798 | fn foo() -> i8 {} |
744 | //^^^ | 799 | //^^^ |
745 | fn test() { | 800 | fn test() { |
746 | format!("{}", fo<|>o()) | 801 | format!("{}", fo$0o()) |
747 | } | 802 | } |
748 | "#, | 803 | "#, |
749 | ); | 804 | ); |
@@ -761,7 +816,7 @@ macro_rules! include {} | |||
761 | //^^^^^^^^^^^^^^^^^^^ | 816 | //^^^^^^^^^^^^^^^^^^^ |
762 | 817 | ||
763 | fn f() { | 818 | fn f() { |
764 | foo<|>(); | 819 | foo$0(); |
765 | } | 820 | } |
766 | 821 | ||
767 | mod confuse_index { | 822 | mod confuse_index { |
@@ -778,7 +833,7 @@ fn foo() {} | |||
778 | fn goto_for_type_param() { | 833 | fn goto_for_type_param() { |
779 | check( | 834 | check( |
780 | r#" | 835 | r#" |
781 | struct Foo<T: Clone> { t: <|>T } | 836 | struct Foo<T: Clone> { t: $0T } |
782 | //^ | 837 | //^ |
783 | "#, | 838 | "#, |
784 | ); | 839 | ); |
@@ -796,7 +851,7 @@ fn foo() { | |||
796 | let x = 1; | 851 | let x = 1; |
797 | //^ | 852 | //^ |
798 | id!({ | 853 | id!({ |
799 | let y = <|>x; | 854 | let y = $0x; |
800 | let z = y; | 855 | let z = y; |
801 | }); | 856 | }); |
802 | } | 857 | } |
@@ -814,7 +869,7 @@ fn foo() { | |||
814 | id!({ | 869 | id!({ |
815 | let y = x; | 870 | let y = x; |
816 | //^ | 871 | //^ |
817 | let z = <|>y; | 872 | let z = $0y; |
818 | }); | 873 | }); |
819 | } | 874 | } |
820 | "#, | 875 | "#, |
@@ -829,7 +884,7 @@ fn main() { | |||
829 | fn foo() { | 884 | fn foo() { |
830 | let x = 92; | 885 | let x = 92; |
831 | //^ | 886 | //^ |
832 | <|>x; | 887 | $0x; |
833 | } | 888 | } |
834 | } | 889 | } |
835 | "#, | 890 | "#, |
@@ -843,7 +898,7 @@ fn main() { | |||
843 | fn bar() { | 898 | fn bar() { |
844 | macro_rules! foo { () => { () } } | 899 | macro_rules! foo { () => { () } } |
845 | //^^^ | 900 | //^^^ |
846 | <|>foo!(); | 901 | $0foo!(); |
847 | } | 902 | } |
848 | "#, | 903 | "#, |
849 | ); | 904 | ); |
@@ -857,7 +912,7 @@ struct Foo { x: i32 } | |||
857 | fn main() { | 912 | fn main() { |
858 | let x = 92; | 913 | let x = 92; |
859 | //^ | 914 | //^ |
860 | Foo { x<|> }; | 915 | Foo { x$0 }; |
861 | } | 916 | } |
862 | "#, | 917 | "#, |
863 | ) | 918 | ) |
@@ -872,7 +927,7 @@ enum Foo { | |||
872 | } //^ | 927 | } //^ |
873 | fn baz(foo: Foo) { | 928 | fn baz(foo: Foo) { |
874 | match foo { | 929 | match foo { |
875 | Foo::Bar { x<|> } => x | 930 | Foo::Bar { x$0 } => x |
876 | }; | 931 | }; |
877 | } | 932 | } |
878 | "#, | 933 | "#, |
@@ -887,7 +942,7 @@ enum Foo { Bar } | |||
887 | //^^^ | 942 | //^^^ |
888 | impl Foo { | 943 | impl Foo { |
889 | fn baz(self) { | 944 | fn baz(self) { |
890 | match self { Self::Bar<|> => {} } | 945 | match self { Self::Bar$0 => {} } |
891 | } | 946 | } |
892 | } | 947 | } |
893 | "#, | 948 | "#, |
@@ -902,7 +957,7 @@ enum Foo { Bar { val: i32 } } | |||
902 | //^^^ | 957 | //^^^ |
903 | impl Foo { | 958 | impl Foo { |
904 | fn baz(self) -> i32 { | 959 | fn baz(self) -> i32 { |
905 | match self { Self::Bar<|> { val } => {} } | 960 | match self { Self::Bar$0 { val } => {} } |
906 | } | 961 | } |
907 | } | 962 | } |
908 | "#, | 963 | "#, |
@@ -916,7 +971,7 @@ impl Foo { | |||
916 | enum Foo { Bar } | 971 | enum Foo { Bar } |
917 | //^^^ | 972 | //^^^ |
918 | impl Foo { | 973 | impl Foo { |
919 | fn baz(self) { Self::Bar<|>; } | 974 | fn baz(self) { Self::Bar$0; } |
920 | } | 975 | } |
921 | "#, | 976 | "#, |
922 | ); | 977 | ); |
@@ -929,7 +984,7 @@ impl Foo { | |||
929 | enum Foo { Bar { val: i32 } } | 984 | enum Foo { Bar { val: i32 } } |
930 | //^^^ | 985 | //^^^ |
931 | impl Foo { | 986 | impl Foo { |
932 | fn baz(self) { Self::Bar<|> {val: 4}; } | 987 | fn baz(self) { Self::Bar$0 {val: 4}; } |
933 | } | 988 | } |
934 | "#, | 989 | "#, |
935 | ); | 990 | ); |
@@ -939,7 +994,7 @@ impl Foo { | |||
939 | fn goto_def_for_type_alias_generic_parameter() { | 994 | fn goto_def_for_type_alias_generic_parameter() { |
940 | check( | 995 | check( |
941 | r#" | 996 | r#" |
942 | type Alias<T> = T<|>; | 997 | type Alias<T> = T$0; |
943 | //^ | 998 | //^ |
944 | "#, | 999 | "#, |
945 | ) | 1000 | ) |
@@ -950,7 +1005,7 @@ type Alias<T> = T<|>; | |||
950 | check( | 1005 | check( |
951 | r#" | 1006 | r#" |
952 | //- /lib.rs | 1007 | //- /lib.rs |
953 | foo::module<|>::mac!(); | 1008 | foo::module$0::mac!(); |
954 | 1009 | ||
955 | //- /foo/lib.rs | 1010 | //- /foo/lib.rs |
956 | pub mod module { | 1011 | pub mod module { |
@@ -972,7 +1027,7 @@ trait Iterator { | |||
972 | //^^^^ | 1027 | //^^^^ |
973 | } | 1028 | } |
974 | 1029 | ||
975 | fn f() -> impl Iterator<Item<|> = u8> {} | 1030 | fn f() -> impl Iterator<Item$0 = u8> {} |
976 | "#, | 1031 | "#, |
977 | ); | 1032 | ); |
978 | } | 1033 | } |
@@ -987,7 +1042,7 @@ trait Iterator { | |||
987 | type B; | 1042 | type B; |
988 | } | 1043 | } |
989 | 1044 | ||
990 | fn f() -> impl Iterator<A<|> = u8, B = ()> {} | 1045 | fn f() -> impl Iterator<A$0 = u8, B = ()> {} |
991 | "#, | 1046 | "#, |
992 | ); | 1047 | ); |
993 | check( | 1048 | check( |
@@ -998,7 +1053,7 @@ trait Iterator { | |||
998 | //^ | 1053 | //^ |
999 | } | 1054 | } |
1000 | 1055 | ||
1001 | fn f() -> impl Iterator<A = u8, B<|> = ()> {} | 1056 | fn f() -> impl Iterator<A = u8, B$0 = ()> {} |
1002 | "#, | 1057 | "#, |
1003 | ); | 1058 | ); |
1004 | } | 1059 | } |
@@ -1012,7 +1067,7 @@ trait Iterator { | |||
1012 | //^^^^ | 1067 | //^^^^ |
1013 | } | 1068 | } |
1014 | 1069 | ||
1015 | fn g() -> <() as Iterator<Item<|> = ()>>::Item {} | 1070 | fn g() -> <() as Iterator<Item$0 = ()>>::Item {} |
1016 | "#, | 1071 | "#, |
1017 | ); | 1072 | ); |
1018 | } | 1073 | } |
@@ -1027,7 +1082,7 @@ trait Iterator { | |||
1027 | type B; | 1082 | type B; |
1028 | } | 1083 | } |
1029 | 1084 | ||
1030 | fn g() -> <() as Iterator<A<|> = (), B = u8>>::B {} | 1085 | fn g() -> <() as Iterator<A$0 = (), B = u8>>::B {} |
1031 | "#, | 1086 | "#, |
1032 | ); | 1087 | ); |
1033 | check( | 1088 | check( |
@@ -1038,7 +1093,7 @@ trait Iterator { | |||
1038 | //^ | 1093 | //^ |
1039 | } | 1094 | } |
1040 | 1095 | ||
1041 | fn g() -> <() as Iterator<A = (), B<|> = u8>>::A {} | 1096 | fn g() -> <() as Iterator<A = (), B$0 = u8>>::A {} |
1042 | "#, | 1097 | "#, |
1043 | ); | 1098 | ); |
1044 | } | 1099 | } |
@@ -1052,7 +1107,7 @@ struct Foo {} | |||
1052 | impl Foo { | 1107 | impl Foo { |
1053 | fn bar(self: &Foo) { | 1108 | fn bar(self: &Foo) { |
1054 | //^^^^ | 1109 | //^^^^ |
1055 | let foo = sel<|>f; | 1110 | let foo = sel$0f; |
1056 | } | 1111 | } |
1057 | }"#, | 1112 | }"#, |
1058 | ) | 1113 | ) |
@@ -1065,7 +1120,7 @@ impl Foo { | |||
1065 | struct Foo {} | 1120 | struct Foo {} |
1066 | 1121 | ||
1067 | impl Foo { | 1122 | impl Foo { |
1068 | fn bar(&self<|>) { | 1123 | fn bar(&self$0) { |
1069 | //^^^^ | 1124 | //^^^^ |
1070 | } | 1125 | } |
1071 | }"#, | 1126 | }"#, |
@@ -1076,7 +1131,7 @@ impl Foo { | |||
1076 | fn goto_lifetime_param_on_decl() { | 1131 | fn goto_lifetime_param_on_decl() { |
1077 | check( | 1132 | check( |
1078 | r#" | 1133 | r#" |
1079 | fn foo<'foobar<|>>(_: &'foobar ()) { | 1134 | fn foo<'foobar$0>(_: &'foobar ()) { |
1080 | //^^^^^^^ | 1135 | //^^^^^^^ |
1081 | }"#, | 1136 | }"#, |
1082 | ) | 1137 | ) |
@@ -1086,7 +1141,7 @@ fn foo<'foobar<|>>(_: &'foobar ()) { | |||
1086 | fn goto_lifetime_param_decl() { | 1141 | fn goto_lifetime_param_decl() { |
1087 | check( | 1142 | check( |
1088 | r#" | 1143 | r#" |
1089 | fn foo<'foobar>(_: &'foobar<|> ()) { | 1144 | fn foo<'foobar>(_: &'foobar$0 ()) { |
1090 | //^^^^^^^ | 1145 | //^^^^^^^ |
1091 | }"#, | 1146 | }"#, |
1092 | ) | 1147 | ) |
@@ -1097,7 +1152,7 @@ fn foo<'foobar>(_: &'foobar<|> ()) { | |||
1097 | check( | 1152 | check( |
1098 | r#" | 1153 | r#" |
1099 | fn foo<'foobar>(_: &'foobar ()) { | 1154 | fn foo<'foobar>(_: &'foobar ()) { |
1100 | fn foo<'foobar>(_: &'foobar<|> ()) {} | 1155 | fn foo<'foobar>(_: &'foobar$0 ()) {} |
1101 | //^^^^^^^ | 1156 | //^^^^^^^ |
1102 | }"#, | 1157 | }"#, |
1103 | ) | 1158 | ) |
@@ -1108,13 +1163,13 @@ fn foo<'foobar>(_: &'foobar ()) { | |||
1108 | fn goto_lifetime_hrtb() { | 1163 | fn goto_lifetime_hrtb() { |
1109 | check( | 1164 | check( |
1110 | r#"trait Foo<T> {} | 1165 | r#"trait Foo<T> {} |
1111 | fn foo<T>() where for<'a> T: Foo<&'a<|> (u8, u16)>, {} | 1166 | fn foo<T>() where for<'a> T: Foo<&'a$0 (u8, u16)>, {} |
1112 | //^^ | 1167 | //^^ |
1113 | "#, | 1168 | "#, |
1114 | ); | 1169 | ); |
1115 | check( | 1170 | check( |
1116 | r#"trait Foo<T> {} | 1171 | r#"trait Foo<T> {} |
1117 | fn foo<T>() where for<'a<|>> T: Foo<&'a (u8, u16)>, {} | 1172 | fn foo<T>() where for<'a$0> T: Foo<&'a (u8, u16)>, {} |
1118 | //^^ | 1173 | //^^ |
1119 | "#, | 1174 | "#, |
1120 | ); | 1175 | ); |
@@ -1125,7 +1180,7 @@ fn foo<T>() where for<'a<|>> T: Foo<&'a (u8, u16)>, {} | |||
1125 | fn goto_lifetime_hrtb_for_type() { | 1180 | fn goto_lifetime_hrtb_for_type() { |
1126 | check( | 1181 | check( |
1127 | r#"trait Foo<T> {} | 1182 | r#"trait Foo<T> {} |
1128 | fn foo<T>() where T: for<'a> Foo<&'a<|> (u8, u16)>, {} | 1183 | fn foo<T>() where T: for<'a> Foo<&'a$0 (u8, u16)>, {} |
1129 | //^^ | 1184 | //^^ |
1130 | "#, | 1185 | "#, |
1131 | ); | 1186 | ); |
@@ -1139,10 +1194,40 @@ fn foo<'foo>(_: &'foo ()) { | |||
1139 | 'foo: { | 1194 | 'foo: { |
1140 | //^^^^ | 1195 | //^^^^ |
1141 | 'bar: loop { | 1196 | 'bar: loop { |
1142 | break 'foo<|>; | 1197 | break 'foo$0; |
1143 | } | 1198 | } |
1144 | } | 1199 | } |
1145 | }"#, | 1200 | }"#, |
1146 | ) | 1201 | ) |
1147 | } | 1202 | } |
1203 | |||
1204 | #[test] | ||
1205 | fn goto_def_for_intra_doc_link_same_file() { | ||
1206 | check( | ||
1207 | r#" | ||
1208 | /// Blah, [`bar`](bar) .. [`foo`](foo)$0 has [`bar`](bar) | ||
1209 | pub fn bar() { } | ||
1210 | |||
1211 | /// You might want to see [`std::fs::read()`] too. | ||
1212 | pub fn foo() { } | ||
1213 | //^^^ | ||
1214 | |||
1215 | }"#, | ||
1216 | ) | ||
1217 | } | ||
1218 | |||
1219 | #[test] | ||
1220 | fn goto_def_for_intra_doc_link_inner() { | ||
1221 | check( | ||
1222 | r#" | ||
1223 | //- /main.rs | ||
1224 | mod m; | ||
1225 | struct S; | ||
1226 | //^ | ||
1227 | |||
1228 | //- /m.rs | ||
1229 | //! [`super::S$0`] | ||
1230 | "#, | ||
1231 | ) | ||
1232 | } | ||
1148 | } | 1233 | } |
diff --git a/crates/ide/src/goto_implementation.rs b/crates/ide/src/goto_implementation.rs index 6eac39639..761a98b2c 100644 --- a/crates/ide/src/goto_implementation.rs +++ b/crates/ide/src/goto_implementation.rs | |||
@@ -2,7 +2,7 @@ use hir::{Crate, Impl, Semantics}; | |||
2 | use ide_db::RootDatabase; | 2 | use ide_db::RootDatabase; |
3 | use syntax::{algo::find_node_at_offset, ast, AstNode}; | 3 | use syntax::{algo::find_node_at_offset, ast, AstNode}; |
4 | 4 | ||
5 | use crate::{display::ToNav, FilePosition, NavigationTarget, RangeInfo}; | 5 | use crate::{display::TryToNav, FilePosition, NavigationTarget, RangeInfo}; |
6 | 6 | ||
7 | // Feature: Go to Implementation | 7 | // Feature: Go to Implementation |
8 | // | 8 | // |
@@ -55,7 +55,7 @@ fn impls_for_def( | |||
55 | impls | 55 | impls |
56 | .into_iter() | 56 | .into_iter() |
57 | .filter(|impl_def| ty.is_equal_for_find_impls(&impl_def.target_ty(sema.db))) | 57 | .filter(|impl_def| ty.is_equal_for_find_impls(&impl_def.target_ty(sema.db))) |
58 | .map(|imp| imp.to_nav(sema.db)) | 58 | .filter_map(|imp| imp.try_to_nav(sema.db)) |
59 | .collect(), | 59 | .collect(), |
60 | ) | 60 | ) |
61 | } | 61 | } |
@@ -69,7 +69,7 @@ fn impls_for_trait( | |||
69 | 69 | ||
70 | let impls = Impl::for_trait(sema.db, krate, tr); | 70 | let impls = Impl::for_trait(sema.db, krate, tr); |
71 | 71 | ||
72 | Some(impls.into_iter().map(|imp| imp.to_nav(sema.db)).collect()) | 72 | Some(impls.into_iter().filter_map(|imp| imp.try_to_nav(sema.db)).collect()) |
73 | } | 73 | } |
74 | 74 | ||
75 | #[cfg(test)] | 75 | #[cfg(test)] |
@@ -107,7 +107,7 @@ mod tests { | |||
107 | fn goto_implementation_works() { | 107 | fn goto_implementation_works() { |
108 | check( | 108 | check( |
109 | r#" | 109 | r#" |
110 | struct Foo<|>; | 110 | struct Foo$0; |
111 | impl Foo {} | 111 | impl Foo {} |
112 | //^^^ | 112 | //^^^ |
113 | "#, | 113 | "#, |
@@ -118,7 +118,7 @@ impl Foo {} | |||
118 | fn goto_implementation_works_multiple_blocks() { | 118 | fn goto_implementation_works_multiple_blocks() { |
119 | check( | 119 | check( |
120 | r#" | 120 | r#" |
121 | struct Foo<|>; | 121 | struct Foo$0; |
122 | impl Foo {} | 122 | impl Foo {} |
123 | //^^^ | 123 | //^^^ |
124 | impl Foo {} | 124 | impl Foo {} |
@@ -131,7 +131,7 @@ impl Foo {} | |||
131 | fn goto_implementation_works_multiple_mods() { | 131 | fn goto_implementation_works_multiple_mods() { |
132 | check( | 132 | check( |
133 | r#" | 133 | r#" |
134 | struct Foo<|>; | 134 | struct Foo$0; |
135 | mod a { | 135 | mod a { |
136 | impl super::Foo {} | 136 | impl super::Foo {} |
137 | //^^^^^^^^^^ | 137 | //^^^^^^^^^^ |
@@ -149,7 +149,7 @@ mod b { | |||
149 | check( | 149 | check( |
150 | r#" | 150 | r#" |
151 | //- /lib.rs | 151 | //- /lib.rs |
152 | struct Foo<|>; | 152 | struct Foo$0; |
153 | mod a; | 153 | mod a; |
154 | mod b; | 154 | mod b; |
155 | //- /a.rs | 155 | //- /a.rs |
@@ -166,7 +166,7 @@ impl crate::Foo {} | |||
166 | fn goto_implementation_for_trait() { | 166 | fn goto_implementation_for_trait() { |
167 | check( | 167 | check( |
168 | r#" | 168 | r#" |
169 | trait T<|> {} | 169 | trait T$0 {} |
170 | struct Foo; | 170 | struct Foo; |
171 | impl T for Foo {} | 171 | impl T for Foo {} |
172 | //^^^ | 172 | //^^^ |
@@ -179,7 +179,7 @@ impl T for Foo {} | |||
179 | check( | 179 | check( |
180 | r#" | 180 | r#" |
181 | //- /lib.rs | 181 | //- /lib.rs |
182 | trait T<|> {}; | 182 | trait T$0 {}; |
183 | struct Foo; | 183 | struct Foo; |
184 | mod a; | 184 | mod a; |
185 | mod b; | 185 | mod b; |
@@ -199,7 +199,7 @@ impl crate::T for crate::Foo {} | |||
199 | r#" | 199 | r#" |
200 | //- /lib.rs | 200 | //- /lib.rs |
201 | trait T {} | 201 | trait T {} |
202 | struct Foo<|>; | 202 | struct Foo$0; |
203 | impl Foo {} | 203 | impl Foo {} |
204 | //^^^ | 204 | //^^^ |
205 | impl T for Foo {} | 205 | impl T for Foo {} |
@@ -216,7 +216,7 @@ impl T for &Foo {} | |||
216 | r#" | 216 | r#" |
217 | #[derive(Copy)] | 217 | #[derive(Copy)] |
218 | //^^^^^^^^^^^^^^^ | 218 | //^^^^^^^^^^^^^^^ |
219 | struct Foo<|>; | 219 | struct Foo$0; |
220 | 220 | ||
221 | mod marker { | 221 | mod marker { |
222 | trait Copy {} | 222 | trait Copy {} |
diff --git a/crates/ide/src/goto_type_definition.rs b/crates/ide/src/goto_type_definition.rs index aba6bf5dc..369a59820 100644 --- a/crates/ide/src/goto_type_definition.rs +++ b/crates/ide/src/goto_type_definition.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use ide_db::RootDatabase; | 1 | use ide_db::RootDatabase; |
2 | use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; | 2 | use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; |
3 | 3 | ||
4 | use crate::{display::ToNav, FilePosition, NavigationTarget, RangeInfo}; | 4 | use crate::{display::TryToNav, FilePosition, NavigationTarget, RangeInfo}; |
5 | 5 | ||
6 | // Feature: Go to Type Definition | 6 | // Feature: Go to Type Definition |
7 | // | 7 | // |
@@ -37,7 +37,7 @@ pub(crate) fn goto_type_definition( | |||
37 | 37 | ||
38 | let adt_def = ty.autoderef(db).filter_map(|ty| ty.as_adt()).last()?; | 38 | let adt_def = ty.autoderef(db).filter_map(|ty| ty.as_adt()).last()?; |
39 | 39 | ||
40 | let nav = adt_def.to_nav(db); | 40 | let nav = adt_def.try_to_nav(db)?; |
41 | Some(RangeInfo::new(node.text_range(), vec![nav])) | 41 | Some(RangeInfo::new(node.text_range(), vec![nav])) |
42 | } | 42 | } |
43 | 43 | ||
@@ -76,7 +76,7 @@ mod tests { | |||
76 | struct Foo; | 76 | struct Foo; |
77 | //^^^ | 77 | //^^^ |
78 | fn foo() { | 78 | fn foo() { |
79 | let f: Foo; f<|> | 79 | let f: Foo; f$0 |
80 | } | 80 | } |
81 | "#, | 81 | "#, |
82 | ); | 82 | ); |
@@ -89,7 +89,7 @@ fn foo() { | |||
89 | struct Foo; | 89 | struct Foo; |
90 | //^^^ | 90 | //^^^ |
91 | fn foo() { | 91 | fn foo() { |
92 | let f: &Foo; f<|> | 92 | let f: &Foo; f$0 |
93 | } | 93 | } |
94 | "#, | 94 | "#, |
95 | ); | 95 | ); |
@@ -103,7 +103,7 @@ macro_rules! id { ($($tt:tt)*) => { $($tt)* } } | |||
103 | struct Foo {} | 103 | struct Foo {} |
104 | //^^^ | 104 | //^^^ |
105 | id! { | 105 | id! { |
106 | fn bar() { let f<|> = Foo {}; } | 106 | fn bar() { let f$0 = Foo {}; } |
107 | } | 107 | } |
108 | "#, | 108 | "#, |
109 | ); | 109 | ); |
@@ -115,7 +115,7 @@ id! { | |||
115 | r#" | 115 | r#" |
116 | struct Foo; | 116 | struct Foo; |
117 | //^^^ | 117 | //^^^ |
118 | fn foo(<|>f: Foo) {} | 118 | fn foo($0f: Foo) {} |
119 | "#, | 119 | "#, |
120 | ); | 120 | ); |
121 | } | 121 | } |
@@ -129,7 +129,7 @@ struct Foo; | |||
129 | struct Bar(Foo); | 129 | struct Bar(Foo); |
130 | fn foo() { | 130 | fn foo() { |
131 | let bar = Bar(Foo); | 131 | let bar = Bar(Foo); |
132 | bar.<|>0; | 132 | bar.$00; |
133 | } | 133 | } |
134 | "#, | 134 | "#, |
135 | ); | 135 | ); |
@@ -142,7 +142,7 @@ fn foo() { | |||
142 | struct Foo; | 142 | struct Foo; |
143 | //^^^ | 143 | //^^^ |
144 | impl Foo { | 144 | impl Foo { |
145 | fn f(&self<|>) {} | 145 | fn f(&self$0) {} |
146 | } | 146 | } |
147 | "#, | 147 | "#, |
148 | ) | 148 | ) |
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 73245fbe7..317b6f011 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use hir::{ | 1 | use hir::{ |
2 | Adt, AsAssocItem, AssocItemContainer, FieldSource, HasAttrs, HasSource, HirDisplay, Module, | 2 | Adt, AsAssocItem, AssocItemContainer, FieldSource, GenericParam, HasAttrs, HasSource, |
3 | ModuleDef, ModuleSource, Semantics, | 3 | HirDisplay, Module, ModuleDef, ModuleSource, Semantics, |
4 | }; | 4 | }; |
5 | use ide_db::base_db::SourceDatabase; | 5 | use ide_db::base_db::SourceDatabase; |
6 | use ide_db::{ | 6 | use ide_db::{ |
@@ -13,11 +13,11 @@ use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, | |||
13 | use test_utils::mark; | 13 | use test_utils::mark; |
14 | 14 | ||
15 | use crate::{ | 15 | use crate::{ |
16 | display::{macro_label, ShortLabel, ToNav, TryToNav}, | 16 | display::{macro_label, ShortLabel, TryToNav}, |
17 | doc_links::{remove_links, rewrite_links}, | 17 | doc_links::{remove_links, rewrite_links}, |
18 | markdown_remove::remove_markdown, | 18 | markdown_remove::remove_markdown, |
19 | markup::Markup, | 19 | markup::Markup, |
20 | runnables::runnable, | 20 | runnables::{runnable_fn, runnable_mod}, |
21 | FileId, FilePosition, NavigationTarget, RangeInfo, Runnable, | 21 | FileId, FilePosition, NavigationTarget, RangeInfo, Runnable, |
22 | }; | 22 | }; |
23 | 23 | ||
@@ -31,19 +31,6 @@ pub struct HoverConfig { | |||
31 | pub markdown: bool, | 31 | pub markdown: bool, |
32 | } | 32 | } |
33 | 33 | ||
34 | impl Default for HoverConfig { | ||
35 | fn default() -> Self { | ||
36 | Self { | ||
37 | implementations: true, | ||
38 | run: true, | ||
39 | debug: true, | ||
40 | goto_type_def: true, | ||
41 | links_in_hover: true, | ||
42 | markdown: true, | ||
43 | } | ||
44 | } | ||
45 | } | ||
46 | |||
47 | impl HoverConfig { | 34 | impl HoverConfig { |
48 | pub const NO_ACTIONS: Self = Self { | 35 | pub const NO_ACTIONS: Self = Self { |
49 | implementations: false, | 36 | implementations: false, |
@@ -70,7 +57,7 @@ impl HoverConfig { | |||
70 | #[derive(Debug, Clone)] | 57 | #[derive(Debug, Clone)] |
71 | pub enum HoverAction { | 58 | pub enum HoverAction { |
72 | Runnable(Runnable), | 59 | Runnable(Runnable), |
73 | Implementaion(FilePosition), | 60 | Implementation(FilePosition), |
74 | GoToType(Vec<HoverGotoTypeData>), | 61 | GoToType(Vec<HoverGotoTypeData>), |
75 | } | 62 | } |
76 | 63 | ||
@@ -109,17 +96,20 @@ pub(crate) fn hover( | |||
109 | match node { | 96 | match node { |
110 | ast::Name(name) => NameClass::classify(&sema, &name).and_then(|d| d.defined(sema.db)), | 97 | ast::Name(name) => NameClass::classify(&sema, &name).and_then(|d| d.defined(sema.db)), |
111 | ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)), | 98 | ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)), |
99 | ast::Lifetime(lifetime) => NameClass::classify_lifetime(&sema, &lifetime) | ||
100 | .map_or_else(|| NameRefClass::classify_lifetime(&sema, &lifetime).map(|d| d.referenced(sema.db)), |d| d.defined(sema.db)), | ||
112 | _ => None, | 101 | _ => None, |
113 | } | 102 | } |
114 | }; | 103 | }; |
115 | if let Some(definition) = definition { | 104 | if let Some(definition) = definition { |
116 | if let Some(markup) = hover_for_definition(db, definition) { | 105 | if let Some(markup) = hover_for_definition(db, definition) { |
106 | let markup = markup.as_str(); | ||
117 | let markup = if !markdown { | 107 | let markup = if !markdown { |
118 | remove_markdown(&markup.as_str()) | 108 | remove_markdown(markup) |
119 | } else if links_in_hover { | 109 | } else if links_in_hover { |
120 | rewrite_links(db, &markup.as_str(), &definition) | 110 | rewrite_links(db, markup, &definition) |
121 | } else { | 111 | } else { |
122 | remove_links(&markup.as_str()) | 112 | remove_links(markup) |
123 | }; | 113 | }; |
124 | res.markup = Markup::from(markup); | 114 | res.markup = Markup::from(markup); |
125 | if let Some(action) = show_implementations_action(db, definition) { | 115 | if let Some(action) = show_implementations_action(db, definition) { |
@@ -173,22 +163,19 @@ pub(crate) fn hover( | |||
173 | 163 | ||
174 | fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> { | 164 | fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> { |
175 | fn to_action(nav_target: NavigationTarget) -> HoverAction { | 165 | fn to_action(nav_target: NavigationTarget) -> HoverAction { |
176 | HoverAction::Implementaion(FilePosition { | 166 | HoverAction::Implementation(FilePosition { |
177 | file_id: nav_target.file_id, | 167 | file_id: nav_target.file_id, |
178 | offset: nav_target.focus_or_full_range().start(), | 168 | offset: nav_target.focus_or_full_range().start(), |
179 | }) | 169 | }) |
180 | } | 170 | } |
181 | 171 | ||
182 | match def { | 172 | let adt = match def { |
183 | Definition::ModuleDef(it) => match it { | 173 | Definition::ModuleDef(ModuleDef::Trait(it)) => return it.try_to_nav(db).map(to_action), |
184 | ModuleDef::Adt(Adt::Struct(it)) => Some(to_action(it.to_nav(db))), | 174 | Definition::ModuleDef(ModuleDef::Adt(it)) => Some(it), |
185 | ModuleDef::Adt(Adt::Union(it)) => Some(to_action(it.to_nav(db))), | 175 | Definition::SelfType(it) => it.target_ty(db).as_adt(), |
186 | ModuleDef::Adt(Adt::Enum(it)) => Some(to_action(it.to_nav(db))), | ||
187 | ModuleDef::Trait(it) => Some(to_action(it.to_nav(db))), | ||
188 | _ => None, | ||
189 | }, | ||
190 | _ => None, | 176 | _ => None, |
191 | } | 177 | }?; |
178 | adt.try_to_nav(db).map(to_action) | ||
192 | } | 179 | } |
193 | 180 | ||
194 | fn runnable_action( | 181 | fn runnable_action( |
@@ -199,21 +186,20 @@ fn runnable_action( | |||
199 | match def { | 186 | match def { |
200 | Definition::ModuleDef(it) => match it { | 187 | Definition::ModuleDef(it) => match it { |
201 | ModuleDef::Module(it) => match it.definition_source(sema.db).value { | 188 | ModuleDef::Module(it) => match it.definition_source(sema.db).value { |
202 | ModuleSource::Module(it) => runnable(&sema, it.syntax().clone(), file_id) | 189 | ModuleSource::Module(it) => { |
203 | .map(|it| HoverAction::Runnable(it)), | 190 | runnable_mod(&sema, it).map(|it| HoverAction::Runnable(it)) |
191 | } | ||
204 | _ => None, | 192 | _ => None, |
205 | }, | 193 | }, |
206 | ModuleDef::Function(it) => { | 194 | ModuleDef::Function(func) => { |
207 | let src = it.source(sema.db); | 195 | let src = func.source(sema.db)?; |
208 | if src.file_id != file_id.into() { | 196 | if src.file_id != file_id.into() { |
209 | mark::hit!(hover_macro_generated_struct_fn_doc_comment); | 197 | mark::hit!(hover_macro_generated_struct_fn_doc_comment); |
210 | mark::hit!(hover_macro_generated_struct_fn_doc_attr); | 198 | mark::hit!(hover_macro_generated_struct_fn_doc_attr); |
211 | |||
212 | return None; | 199 | return None; |
213 | } | 200 | } |
214 | 201 | ||
215 | runnable(&sema, src.value.syntax().clone(), file_id) | 202 | runnable_fn(&sema, func).map(HoverAction::Runnable) |
216 | .map(|it| HoverAction::Runnable(it)) | ||
217 | } | 203 | } |
218 | _ => None, | 204 | _ => None, |
219 | }, | 205 | }, |
@@ -222,45 +208,46 @@ fn runnable_action( | |||
222 | } | 208 | } |
223 | 209 | ||
224 | fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> { | 210 | fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> { |
225 | match def { | 211 | let mut targets: Vec<ModuleDef> = Vec::new(); |
226 | Definition::Local(it) => { | 212 | let mut push_new_def = |item: ModuleDef| { |
227 | let mut targets: Vec<ModuleDef> = Vec::new(); | 213 | if !targets.contains(&item) { |
228 | let mut push_new_def = |item: ModuleDef| { | 214 | targets.push(item); |
229 | if !targets.contains(&item) { | ||
230 | targets.push(item); | ||
231 | } | ||
232 | }; | ||
233 | |||
234 | it.ty(db).walk(db, |t| { | ||
235 | if let Some(adt) = t.as_adt() { | ||
236 | push_new_def(adt.into()); | ||
237 | } else if let Some(trait_) = t.as_dyn_trait() { | ||
238 | push_new_def(trait_.into()); | ||
239 | } else if let Some(traits) = t.as_impl_traits(db) { | ||
240 | traits.into_iter().for_each(|it| push_new_def(it.into())); | ||
241 | } else if let Some(trait_) = t.as_associated_type_parent_trait(db) { | ||
242 | push_new_def(trait_.into()); | ||
243 | } | ||
244 | }); | ||
245 | |||
246 | let targets = targets | ||
247 | .into_iter() | ||
248 | .filter_map(|it| { | ||
249 | Some(HoverGotoTypeData { | ||
250 | mod_path: render_path( | ||
251 | db, | ||
252 | it.module(db)?, | ||
253 | it.name(db).map(|name| name.to_string()), | ||
254 | ), | ||
255 | nav: it.try_to_nav(db)?, | ||
256 | }) | ||
257 | }) | ||
258 | .collect(); | ||
259 | |||
260 | Some(HoverAction::GoToType(targets)) | ||
261 | } | 215 | } |
262 | _ => None, | 216 | }; |
217 | |||
218 | if let Definition::GenericParam(GenericParam::TypeParam(it)) = def { | ||
219 | it.trait_bounds(db).into_iter().for_each(|it| push_new_def(it.into())); | ||
220 | } else { | ||
221 | let ty = match def { | ||
222 | Definition::Local(it) => it.ty(db), | ||
223 | Definition::GenericParam(GenericParam::ConstParam(it)) => it.ty(db), | ||
224 | _ => return None, | ||
225 | }; | ||
226 | |||
227 | ty.walk(db, |t| { | ||
228 | if let Some(adt) = t.as_adt() { | ||
229 | push_new_def(adt.into()); | ||
230 | } else if let Some(trait_) = t.as_dyn_trait() { | ||
231 | push_new_def(trait_.into()); | ||
232 | } else if let Some(traits) = t.as_impl_traits(db) { | ||
233 | traits.into_iter().for_each(|it| push_new_def(it.into())); | ||
234 | } else if let Some(trait_) = t.as_associated_type_parent_trait(db) { | ||
235 | push_new_def(trait_.into()); | ||
236 | } | ||
237 | }); | ||
263 | } | 238 | } |
239 | |||
240 | let targets = targets | ||
241 | .into_iter() | ||
242 | .filter_map(|it| { | ||
243 | Some(HoverGotoTypeData { | ||
244 | mod_path: render_path(db, it.module(db)?, it.name(db).map(|name| name.to_string())), | ||
245 | nav: it.try_to_nav(db)?, | ||
246 | }) | ||
247 | }) | ||
248 | .collect(); | ||
249 | |||
250 | Some(HoverAction::GoToType(targets)) | ||
264 | } | 251 | } |
265 | 252 | ||
266 | fn hover_markup( | 253 | fn hover_markup( |
@@ -324,17 +311,11 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> { | |||
324 | let mod_path = definition_mod_path(db, &def); | 311 | let mod_path = definition_mod_path(db, &def); |
325 | return match def { | 312 | return match def { |
326 | Definition::Macro(it) => { | 313 | Definition::Macro(it) => { |
327 | // FIXME: Currently proc-macro do not have ast-node, | 314 | let label = macro_label(&it.source(db)?.value); |
328 | // such that it does not have source | ||
329 | // more discussion: https://github.com/rust-analyzer/rust-analyzer/issues/6913 | ||
330 | if it.is_proc_macro() { | ||
331 | return None; | ||
332 | } | ||
333 | let label = macro_label(&it.source(db).value); | ||
334 | from_def_source_labeled(db, it, Some(label), mod_path) | 315 | from_def_source_labeled(db, it, Some(label), mod_path) |
335 | } | 316 | } |
336 | Definition::Field(def) => { | 317 | Definition::Field(def) => { |
337 | let src = def.source(db).value; | 318 | let src = def.source(db)?.value; |
338 | if let FieldSource::Named(it) = src { | 319 | if let FieldSource::Named(it) = src { |
339 | from_def_source_labeled(db, def, it.short_label(), mod_path) | 320 | from_def_source_labeled(db, def, it.short_label(), mod_path) |
340 | } else { | 321 | } else { |
@@ -360,9 +341,9 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> { | |||
360 | ModuleDef::Static(it) => from_def_source(db, it, mod_path), | 341 | ModuleDef::Static(it) => from_def_source(db, it, mod_path), |
361 | ModuleDef::Trait(it) => from_def_source(db, it, mod_path), | 342 | ModuleDef::Trait(it) => from_def_source(db, it, mod_path), |
362 | ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path), | 343 | ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path), |
363 | ModuleDef::BuiltinType(it) => return Some(it.to_string().into()), | 344 | ModuleDef::BuiltinType(it) => Some(Markup::fenced_block(&it)), |
364 | }, | 345 | }, |
365 | Definition::Local(it) => return Some(Markup::fenced_block(&it.ty(db).display(db))), | 346 | Definition::Local(it) => Some(Markup::fenced_block(&it.ty(db).display(db))), |
366 | Definition::SelfType(impl_def) => { | 347 | Definition::SelfType(impl_def) => { |
367 | impl_def.target_ty(db).as_adt().and_then(|adt| match adt { | 348 | impl_def.target_ty(db).as_adt().and_then(|adt| match adt { |
368 | Adt::Struct(it) => from_def_source(db, it, mod_path), | 349 | Adt::Struct(it) => from_def_source(db, it, mod_path), |
@@ -370,10 +351,12 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> { | |||
370 | Adt::Enum(it) => from_def_source(db, it, mod_path), | 351 | Adt::Enum(it) => from_def_source(db, it, mod_path), |
371 | }) | 352 | }) |
372 | } | 353 | } |
373 | Definition::TypeParam(_) | Definition::LifetimeParam(_) | Definition::Label(_) => { | 354 | Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))), |
374 | // FIXME: Hover for generic param | 355 | Definition::GenericParam(it) => match it { |
375 | None | 356 | GenericParam::TypeParam(it) => Some(Markup::fenced_block(&it.display(db))), |
376 | } | 357 | GenericParam::LifetimeParam(it) => Some(Markup::fenced_block(&it.name(db))), |
358 | GenericParam::ConstParam(it) => from_def_source(db, it, None), | ||
359 | }, | ||
377 | }; | 360 | }; |
378 | 361 | ||
379 | fn from_def_source<A, D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<Markup> | 362 | fn from_def_source<A, D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<Markup> |
@@ -381,7 +364,7 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> { | |||
381 | D: HasSource<Ast = A> + HasAttrs + Copy, | 364 | D: HasSource<Ast = A> + HasAttrs + Copy, |
382 | A: ShortLabel, | 365 | A: ShortLabel, |
383 | { | 366 | { |
384 | let short_label = def.source(db).value.short_label(); | 367 | let short_label = def.source(db)?.value.short_label(); |
385 | from_def_source_labeled(db, def, short_label, mod_path) | 368 | from_def_source_labeled(db, def, short_label, mod_path) |
386 | } | 369 | } |
387 | 370 | ||
@@ -403,7 +386,7 @@ fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> { | |||
403 | return tokens.max_by_key(priority); | 386 | return tokens.max_by_key(priority); |
404 | fn priority(n: &SyntaxToken) -> usize { | 387 | fn priority(n: &SyntaxToken) -> usize { |
405 | match n.kind() { | 388 | match n.kind() { |
406 | IDENT | INT_NUMBER => 3, | 389 | IDENT | INT_NUMBER | LIFETIME_IDENT => 3, |
407 | T!['('] | T![')'] => 2, | 390 | T!['('] | T![')'] => 2, |
408 | kind if kind.is_trivia() => 0, | 391 | kind if kind.is_trivia() => 0, |
409 | _ => 1, | 392 | _ => 1, |
@@ -471,7 +454,7 @@ mod tests { | |||
471 | pub fn foo() -> u32 { 1 } | 454 | pub fn foo() -> u32 { 1 } |
472 | 455 | ||
473 | fn main() { | 456 | fn main() { |
474 | let foo_test = foo()<|>; | 457 | let foo_test = foo()$0; |
475 | } | 458 | } |
476 | "#, | 459 | "#, |
477 | expect![[r#" | 460 | expect![[r#" |
@@ -490,7 +473,7 @@ fn main() { | |||
490 | pub fn foo() -> u32 { 1 } | 473 | pub fn foo() -> u32 { 1 } |
491 | 474 | ||
492 | fn main() { | 475 | fn main() { |
493 | let foo_test = foo()<|>; | 476 | let foo_test = foo()$0; |
494 | } | 477 | } |
495 | "#, | 478 | "#, |
496 | expect![[r#" | 479 | expect![[r#" |
@@ -520,7 +503,7 @@ fn main() { | |||
520 | Option::Some(*memo + value) | 503 | Option::Some(*memo + value) |
521 | }; | 504 | }; |
522 | let number = 5u32; | 505 | let number = 5u32; |
523 | let mut iter<|> = scan(OtherStruct { i: num }, closure, number); | 506 | let mut iter$0 = scan(OtherStruct { i: num }, closure, number); |
524 | } | 507 | } |
525 | "#, | 508 | "#, |
526 | expect![[r#" | 509 | expect![[r#" |
@@ -540,7 +523,7 @@ fn main() { | |||
540 | r#" | 523 | r#" |
541 | pub fn foo() -> u32 { 1 } | 524 | pub fn foo() -> u32 { 1 } |
542 | 525 | ||
543 | fn main() { let foo_test = fo<|>o(); } | 526 | fn main() { let foo_test = fo$0o(); } |
544 | "#, | 527 | "#, |
545 | expect![[r#" | 528 | expect![[r#" |
546 | *foo* | 529 | *foo* |
@@ -572,7 +555,7 @@ mod a; | |||
572 | mod b; | 555 | mod b; |
573 | mod c; | 556 | mod c; |
574 | 557 | ||
575 | fn main() { let foo_test = fo<|>o(); } | 558 | fn main() { let foo_test = fo$0o(); } |
576 | "#, | 559 | "#, |
577 | expect![[r#" | 560 | expect![[r#" |
578 | *foo* | 561 | *foo* |
@@ -589,7 +572,7 @@ fn main() { let foo_test = fo<|>o(); } | |||
589 | r#" | 572 | r#" |
590 | pub fn foo<'a, T: AsRef<str>>(b: &'a T) -> &'a str { } | 573 | pub fn foo<'a, T: AsRef<str>>(b: &'a T) -> &'a str { } |
591 | 574 | ||
592 | fn main() { let foo_test = fo<|>o(); } | 575 | fn main() { let foo_test = fo$0o(); } |
593 | "#, | 576 | "#, |
594 | expect![[r#" | 577 | expect![[r#" |
595 | *foo* | 578 | *foo* |
@@ -609,7 +592,7 @@ fn main() { let foo_test = fo<|>o(); } | |||
609 | fn hover_shows_fn_signature_on_fn_name() { | 592 | fn hover_shows_fn_signature_on_fn_name() { |
610 | check( | 593 | check( |
611 | r#" | 594 | r#" |
612 | pub fn foo<|>(a: u32, b: u32) -> u32 {} | 595 | pub fn foo$0(a: u32, b: u32) -> u32 {} |
613 | 596 | ||
614 | fn main() { } | 597 | fn main() { } |
615 | "#, | 598 | "#, |
@@ -637,7 +620,7 @@ fn main() { } | |||
637 | /// # | 620 | /// # |
638 | /// foo(Path::new("hello, world!")) | 621 | /// foo(Path::new("hello, world!")) |
639 | /// ``` | 622 | /// ``` |
640 | pub fn foo<|>(_: &Path) {} | 623 | pub fn foo$0(_: &Path) {} |
641 | 624 | ||
642 | fn main() { } | 625 | fn main() { } |
643 | "#, | 626 | "#, |
@@ -670,7 +653,7 @@ fn main() { } | |||
670 | check( | 653 | check( |
671 | r##" | 654 | r##" |
672 | #[doc = r#"Raw string doc attr"#] | 655 | #[doc = r#"Raw string doc attr"#] |
673 | pub fn foo<|>(_: &Path) {} | 656 | pub fn foo$0(_: &Path) {} |
674 | 657 | ||
675 | fn main() { } | 658 | fn main() { } |
676 | "##, | 659 | "##, |
@@ -700,7 +683,7 @@ fn main() { } | |||
700 | struct Foo { field_a: u32 } | 683 | struct Foo { field_a: u32 } |
701 | 684 | ||
702 | fn main() { | 685 | fn main() { |
703 | let foo = Foo { field_a<|>: 0, }; | 686 | let foo = Foo { field_a$0: 0, }; |
704 | } | 687 | } |
705 | "#, | 688 | "#, |
706 | expect![[r#" | 689 | expect![[r#" |
@@ -719,7 +702,7 @@ fn main() { | |||
719 | // Hovering over the field in the definition | 702 | // Hovering over the field in the definition |
720 | check( | 703 | check( |
721 | r#" | 704 | r#" |
722 | struct Foo { field_a<|>: u32 } | 705 | struct Foo { field_a$0: u32 } |
723 | 706 | ||
724 | fn main() { | 707 | fn main() { |
725 | let foo = Foo { field_a: 0 }; | 708 | let foo = Foo { field_a: 0 }; |
@@ -742,7 +725,7 @@ fn main() { | |||
742 | #[test] | 725 | #[test] |
743 | fn hover_const_static() { | 726 | fn hover_const_static() { |
744 | check( | 727 | check( |
745 | r#"const foo<|>: u32 = 123;"#, | 728 | r#"const foo$0: u32 = 123;"#, |
746 | expect![[r#" | 729 | expect![[r#" |
747 | *foo* | 730 | *foo* |
748 | 731 | ||
@@ -756,7 +739,7 @@ fn main() { | |||
756 | "#]], | 739 | "#]], |
757 | ); | 740 | ); |
758 | check( | 741 | check( |
759 | r#"static foo<|>: u32 = 456;"#, | 742 | r#"static foo$0: u32 = 456;"#, |
760 | expect![[r#" | 743 | expect![[r#" |
761 | *foo* | 744 | *foo* |
762 | 745 | ||
@@ -778,7 +761,7 @@ fn main() { | |||
778 | struct Test<K, T = u8> { k: K, t: T } | 761 | struct Test<K, T = u8> { k: K, t: T } |
779 | 762 | ||
780 | fn main() { | 763 | fn main() { |
781 | let zz<|> = Test { t: 23u8, k: 33 }; | 764 | let zz$0 = Test { t: 23u8, k: 33 }; |
782 | }"#, | 765 | }"#, |
783 | expect![[r#" | 766 | expect![[r#" |
784 | *zz* | 767 | *zz* |
@@ -797,7 +780,7 @@ fn main() { | |||
797 | enum Option<T> { Some(T) } | 780 | enum Option<T> { Some(T) } |
798 | use Option::Some; | 781 | use Option::Some; |
799 | 782 | ||
800 | fn main() { So<|>me(12); } | 783 | fn main() { So$0me(12); } |
801 | "#, | 784 | "#, |
802 | expect![[r#" | 785 | expect![[r#" |
803 | *Some* | 786 | *Some* |
@@ -817,7 +800,7 @@ fn main() { So<|>me(12); } | |||
817 | enum Option<T> { Some(T) } | 800 | enum Option<T> { Some(T) } |
818 | use Option::Some; | 801 | use Option::Some; |
819 | 802 | ||
820 | fn main() { let b<|>ar = Some(12); } | 803 | fn main() { let b$0ar = Some(12); } |
821 | "#, | 804 | "#, |
822 | expect![[r#" | 805 | expect![[r#" |
823 | *bar* | 806 | *bar* |
@@ -835,7 +818,7 @@ fn main() { let b<|>ar = Some(12); } | |||
835 | r#" | 818 | r#" |
836 | enum Option<T> { | 819 | enum Option<T> { |
837 | /// The None variant | 820 | /// The None variant |
838 | Non<|>e | 821 | Non$0e |
839 | } | 822 | } |
840 | "#, | 823 | "#, |
841 | expect![[r#" | 824 | expect![[r#" |
@@ -862,7 +845,7 @@ enum Option<T> { | |||
862 | Some(T) | 845 | Some(T) |
863 | } | 846 | } |
864 | fn main() { | 847 | fn main() { |
865 | let s = Option::Som<|>e(12); | 848 | let s = Option::Som$0e(12); |
866 | } | 849 | } |
867 | "#, | 850 | "#, |
868 | expect![[r#" | 851 | expect![[r#" |
@@ -886,7 +869,7 @@ fn main() { | |||
886 | #[test] | 869 | #[test] |
887 | fn hover_for_local_variable() { | 870 | fn hover_for_local_variable() { |
888 | check( | 871 | check( |
889 | r#"fn func(foo: i32) { fo<|>o; }"#, | 872 | r#"fn func(foo: i32) { fo$0o; }"#, |
890 | expect![[r#" | 873 | expect![[r#" |
891 | *foo* | 874 | *foo* |
892 | 875 | ||
@@ -900,7 +883,7 @@ fn main() { | |||
900 | #[test] | 883 | #[test] |
901 | fn hover_for_local_variable_pat() { | 884 | fn hover_for_local_variable_pat() { |
902 | check( | 885 | check( |
903 | r#"fn func(fo<|>o: i32) {}"#, | 886 | r#"fn func(fo$0o: i32) {}"#, |
904 | expect![[r#" | 887 | expect![[r#" |
905 | *foo* | 888 | *foo* |
906 | 889 | ||
@@ -914,7 +897,7 @@ fn main() { | |||
914 | #[test] | 897 | #[test] |
915 | fn hover_local_var_edge() { | 898 | fn hover_local_var_edge() { |
916 | check( | 899 | check( |
917 | r#"fn func(foo: i32) { if true { <|>foo; }; }"#, | 900 | r#"fn func(foo: i32) { if true { $0foo; }; }"#, |
918 | expect![[r#" | 901 | expect![[r#" |
919 | *foo* | 902 | *foo* |
920 | 903 | ||
@@ -928,7 +911,7 @@ fn main() { | |||
928 | #[test] | 911 | #[test] |
929 | fn hover_for_param_edge() { | 912 | fn hover_for_param_edge() { |
930 | check( | 913 | check( |
931 | r#"fn func(<|>foo: i32) {}"#, | 914 | r#"fn func($0foo: i32) {}"#, |
932 | expect![[r#" | 915 | expect![[r#" |
933 | *foo* | 916 | *foo* |
934 | 917 | ||
@@ -948,7 +931,7 @@ fn main() { | |||
948 | trait DerefMut { | 931 | trait DerefMut { |
949 | type Target: ?Sized; | 932 | type Target: ?Sized; |
950 | } | 933 | } |
951 | fn f(_x<|>: impl Deref<Target=u8> + DerefMut<Target=u8>) {}"#, | 934 | fn f(_x$0: impl Deref<Target=u8> + DerefMut<Target=u8>) {}"#, |
952 | expect![[r#" | 935 | expect![[r#" |
953 | *_x* | 936 | *_x* |
954 | 937 | ||
@@ -969,7 +952,7 @@ impl Thing { | |||
969 | fn new() -> Thing { Thing { x: 0 } } | 952 | fn new() -> Thing { Thing { x: 0 } } |
970 | } | 953 | } |
971 | 954 | ||
972 | fn main() { let foo_<|>test = Thing::new(); } | 955 | fn main() { let foo_$0test = Thing::new(); } |
973 | "#, | 956 | "#, |
974 | expect![[r#" | 957 | expect![[r#" |
975 | *foo_test* | 958 | *foo_test* |
@@ -993,7 +976,7 @@ mod wrapper { | |||
993 | } | 976 | } |
994 | } | 977 | } |
995 | 978 | ||
996 | fn main() { let foo_test = wrapper::Thing::new<|>(); } | 979 | fn main() { let foo_test = wrapper::Thing::new$0(); } |
997 | "#, | 980 | "#, |
998 | expect![[r#" | 981 | expect![[r#" |
999 | *new* | 982 | *new* |
@@ -1020,7 +1003,7 @@ impl X { | |||
1020 | 1003 | ||
1021 | fn main() { | 1004 | fn main() { |
1022 | match 1 { | 1005 | match 1 { |
1023 | X::C<|> => {}, | 1006 | X::C$0 => {}, |
1024 | 2 => {}, | 1007 | 2 => {}, |
1025 | _ => {} | 1008 | _ => {} |
1026 | }; | 1009 | }; |
@@ -1046,7 +1029,7 @@ fn main() { | |||
1046 | r#" | 1029 | r#" |
1047 | struct Thing { x: u32 } | 1030 | struct Thing { x: u32 } |
1048 | impl Thing { | 1031 | impl Thing { |
1049 | fn new() -> Self { Self<|> { x: 0 } } | 1032 | fn new() -> Self { Self$0 { x: 0 } } |
1050 | } | 1033 | } |
1051 | "#, | 1034 | "#, |
1052 | expect![[r#" | 1035 | expect![[r#" |
@@ -1065,7 +1048,7 @@ impl Thing { | |||
1065 | r#" | 1048 | r#" |
1066 | struct Thing { x: u32 } | 1049 | struct Thing { x: u32 } |
1067 | impl Thing { | 1050 | impl Thing { |
1068 | fn new() -> Self<|> { Self { x: 0 } } | 1051 | fn new() -> Self$0 { Self { x: 0 } } |
1069 | } | 1052 | } |
1070 | "#, | 1053 | "#, |
1071 | expect![[r#" | 1054 | expect![[r#" |
@@ -1084,7 +1067,7 @@ impl Thing { | |||
1084 | r#" | 1067 | r#" |
1085 | enum Thing { A } | 1068 | enum Thing { A } |
1086 | impl Thing { | 1069 | impl Thing { |
1087 | pub fn new() -> Self<|> { Thing::A } | 1070 | pub fn new() -> Self$0 { Thing::A } |
1088 | } | 1071 | } |
1089 | "#, | 1072 | "#, |
1090 | expect![[r#" | 1073 | expect![[r#" |
@@ -1103,7 +1086,7 @@ impl Thing { | |||
1103 | r#" | 1086 | r#" |
1104 | enum Thing { A } | 1087 | enum Thing { A } |
1105 | impl Thing { | 1088 | impl Thing { |
1106 | pub fn thing(a: Self<|>) {} | 1089 | pub fn thing(a: Self$0) {} |
1107 | } | 1090 | } |
1108 | "#, | 1091 | "#, |
1109 | expect![[r#" | 1092 | expect![[r#" |
@@ -1128,7 +1111,7 @@ fn x() {} | |||
1128 | 1111 | ||
1129 | fn y() { | 1112 | fn y() { |
1130 | let x = 0i32; | 1113 | let x = 0i32; |
1131 | x<|>; | 1114 | x$0; |
1132 | } | 1115 | } |
1133 | "#, | 1116 | "#, |
1134 | expect![[r#" | 1117 | expect![[r#" |
@@ -1147,7 +1130,7 @@ fn y() { | |||
1147 | r#" | 1130 | r#" |
1148 | macro_rules! foo { () => {} } | 1131 | macro_rules! foo { () => {} } |
1149 | 1132 | ||
1150 | fn f() { fo<|>o!(); } | 1133 | fn f() { fo$0o!(); } |
1151 | "#, | 1134 | "#, |
1152 | expect![[r#" | 1135 | expect![[r#" |
1153 | *foo* | 1136 | *foo* |
@@ -1166,10 +1149,13 @@ fn f() { fo<|>o!(); } | |||
1166 | #[test] | 1149 | #[test] |
1167 | fn test_hover_tuple_field() { | 1150 | fn test_hover_tuple_field() { |
1168 | check( | 1151 | check( |
1169 | r#"struct TS(String, i32<|>);"#, | 1152 | r#"struct TS(String, i32$0);"#, |
1170 | expect![[r#" | 1153 | expect![[r#" |
1171 | *i32* | 1154 | *i32* |
1155 | |||
1156 | ```rust | ||
1172 | i32 | 1157 | i32 |
1158 | ``` | ||
1173 | "#]], | 1159 | "#]], |
1174 | ) | 1160 | ) |
1175 | } | 1161 | } |
@@ -1181,7 +1167,7 @@ fn f() { fo<|>o!(); } | |||
1181 | macro_rules! id { ($($tt:tt)*) => { $($tt)* } } | 1167 | macro_rules! id { ($($tt:tt)*) => { $($tt)* } } |
1182 | fn foo() {} | 1168 | fn foo() {} |
1183 | id! { | 1169 | id! { |
1184 | fn bar() { fo<|>o(); } | 1170 | fn bar() { fo$0o(); } |
1185 | } | 1171 | } |
1186 | "#, | 1172 | "#, |
1187 | expect![[r#" | 1173 | expect![[r#" |
@@ -1203,7 +1189,7 @@ id! { | |||
1203 | check( | 1189 | check( |
1204 | r#" | 1190 | r#" |
1205 | macro_rules! id { ($($tt:tt)*) => { $($tt)* } } | 1191 | macro_rules! id { ($($tt:tt)*) => { $($tt)* } } |
1206 | fn foo(bar:u32) { let a = id!(ba<|>r); } | 1192 | fn foo(bar:u32) { let a = id!(ba$0r); } |
1207 | "#, | 1193 | "#, |
1208 | expect![[r#" | 1194 | expect![[r#" |
1209 | *bar* | 1195 | *bar* |
@@ -1221,7 +1207,7 @@ fn foo(bar:u32) { let a = id!(ba<|>r); } | |||
1221 | r#" | 1207 | r#" |
1222 | macro_rules! id_deep { ($($tt:tt)*) => { $($tt)* } } | 1208 | macro_rules! id_deep { ($($tt:tt)*) => { $($tt)* } } |
1223 | macro_rules! id { ($($tt:tt)*) => { id_deep!($($tt)*) } } | 1209 | macro_rules! id { ($($tt:tt)*) => { id_deep!($($tt)*) } } |
1224 | fn foo(bar:u32) { let a = id!(ba<|>r); } | 1210 | fn foo(bar:u32) { let a = id!(ba$0r); } |
1225 | "#, | 1211 | "#, |
1226 | expect![[r#" | 1212 | expect![[r#" |
1227 | *bar* | 1213 | *bar* |
@@ -1240,7 +1226,7 @@ fn foo(bar:u32) { let a = id!(ba<|>r); } | |||
1240 | macro_rules! id_deep { ($($tt:tt)*) => { $($tt)* } } | 1226 | macro_rules! id_deep { ($($tt:tt)*) => { $($tt)* } } |
1241 | macro_rules! id { ($($tt:tt)*) => { id_deep!($($tt)*) } } | 1227 | macro_rules! id { ($($tt:tt)*) => { id_deep!($($tt)*) } } |
1242 | fn bar() -> u32 { 0 } | 1228 | fn bar() -> u32 { 0 } |
1243 | fn foo() { let a = id!([0u32, bar(<|>)] ); } | 1229 | fn foo() { let a = id!([0u32, bar($0)] ); } |
1244 | "#, | 1230 | "#, |
1245 | expect![[r#" | 1231 | expect![[r#" |
1246 | *bar()* | 1232 | *bar()* |
@@ -1258,7 +1244,7 @@ fn foo() { let a = id!([0u32, bar(<|>)] ); } | |||
1258 | macro_rules! arr { ($($tt:tt)*) => { [$($tt)*)] } } | 1244 | macro_rules! arr { ($($tt:tt)*) => { [$($tt)*)] } } |
1259 | fn foo() { | 1245 | fn foo() { |
1260 | let mastered_for_itunes = ""; | 1246 | let mastered_for_itunes = ""; |
1261 | let _ = arr!("Tr<|>acks", &mastered_for_itunes); | 1247 | let _ = arr!("Tr$0acks", &mastered_for_itunes); |
1262 | } | 1248 | } |
1263 | "#, | 1249 | "#, |
1264 | expect![[r#" | 1250 | expect![[r#" |
@@ -1279,7 +1265,7 @@ macro_rules! assert {} | |||
1279 | 1265 | ||
1280 | fn bar() -> bool { true } | 1266 | fn bar() -> bool { true } |
1281 | fn foo() { | 1267 | fn foo() { |
1282 | assert!(ba<|>r()); | 1268 | assert!(ba$0r()); |
1283 | } | 1269 | } |
1284 | "#, | 1270 | "#, |
1285 | expect![[r#" | 1271 | expect![[r#" |
@@ -1304,7 +1290,7 @@ fn foo() { | |||
1304 | macro_rules! format {} | 1290 | macro_rules! format {} |
1305 | 1291 | ||
1306 | fn foo() { | 1292 | fn foo() { |
1307 | format!("hel<|>lo {}", 0); | 1293 | format!("hel$0lo {}", 0); |
1308 | } | 1294 | } |
1309 | "#, | 1295 | "#, |
1310 | ); | 1296 | ); |
@@ -1317,7 +1303,7 @@ fn foo() { | |||
1317 | /// <- `\u{3000}` here | 1303 | /// <- `\u{3000}` here |
1318 | fn foo() { } | 1304 | fn foo() { } |
1319 | 1305 | ||
1320 | fn bar() { fo<|>o(); } | 1306 | fn bar() { fo$0o(); } |
1321 | ", | 1307 | ", |
1322 | expect![[r#" | 1308 | expect![[r#" |
1323 | *foo* | 1309 | *foo* |
@@ -1340,7 +1326,7 @@ fn bar() { fo<|>o(); } | |||
1340 | #[test] | 1326 | #[test] |
1341 | fn test_hover_function_show_qualifiers() { | 1327 | fn test_hover_function_show_qualifiers() { |
1342 | check( | 1328 | check( |
1343 | r#"async fn foo<|>() {}"#, | 1329 | r#"async fn foo$0() {}"#, |
1344 | expect![[r#" | 1330 | expect![[r#" |
1345 | *foo* | 1331 | *foo* |
1346 | 1332 | ||
@@ -1354,7 +1340,7 @@ fn bar() { fo<|>o(); } | |||
1354 | "#]], | 1340 | "#]], |
1355 | ); | 1341 | ); |
1356 | check( | 1342 | check( |
1357 | r#"pub const unsafe fn foo<|>() {}"#, | 1343 | r#"pub const unsafe fn foo$0() {}"#, |
1358 | expect![[r#" | 1344 | expect![[r#" |
1359 | *foo* | 1345 | *foo* |
1360 | 1346 | ||
@@ -1368,7 +1354,7 @@ fn bar() { fo<|>o(); } | |||
1368 | "#]], | 1354 | "#]], |
1369 | ); | 1355 | ); |
1370 | check( | 1356 | check( |
1371 | r#"pub(crate) async unsafe extern "C" fn foo<|>() {}"#, | 1357 | r#"pub(crate) async unsafe extern "C" fn foo$0() {}"#, |
1372 | expect![[r#" | 1358 | expect![[r#" |
1373 | *foo* | 1359 | *foo* |
1374 | 1360 | ||
@@ -1386,10 +1372,10 @@ fn bar() { fo<|>o(); } | |||
1386 | #[test] | 1372 | #[test] |
1387 | fn test_hover_trait_show_qualifiers() { | 1373 | fn test_hover_trait_show_qualifiers() { |
1388 | check_actions( | 1374 | check_actions( |
1389 | r"unsafe trait foo<|>() {}", | 1375 | r"unsafe trait foo$0() {}", |
1390 | expect![[r#" | 1376 | expect![[r#" |
1391 | [ | 1377 | [ |
1392 | Implementaion( | 1378 | Implementation( |
1393 | FilePosition { | 1379 | FilePosition { |
1394 | file_id: FileId( | 1380 | file_id: FileId( |
1395 | 0, | 1381 | 0, |
@@ -1407,7 +1393,7 @@ fn bar() { fo<|>o(); } | |||
1407 | check( | 1393 | check( |
1408 | r#" | 1394 | r#" |
1409 | //- /main.rs crate:main deps:std | 1395 | //- /main.rs crate:main deps:std |
1410 | extern crate st<|>d; | 1396 | extern crate st$0d; |
1411 | //- /std/lib.rs crate:std | 1397 | //- /std/lib.rs crate:std |
1412 | //! Standard library for this test | 1398 | //! Standard library for this test |
1413 | //! | 1399 | //! |
@@ -1425,7 +1411,7 @@ extern crate st<|>d; | |||
1425 | check( | 1411 | check( |
1426 | r#" | 1412 | r#" |
1427 | //- /main.rs crate:main deps:std | 1413 | //- /main.rs crate:main deps:std |
1428 | extern crate std as ab<|>c; | 1414 | extern crate std as ab$0c; |
1429 | //- /std/lib.rs crate:std | 1415 | //- /std/lib.rs crate:std |
1430 | //! Standard library for this test | 1416 | //! Standard library for this test |
1431 | //! | 1417 | //! |
@@ -1446,7 +1432,7 @@ extern crate std as ab<|>c; | |||
1446 | fn test_hover_mod_with_same_name_as_function() { | 1432 | fn test_hover_mod_with_same_name_as_function() { |
1447 | check( | 1433 | check( |
1448 | r#" | 1434 | r#" |
1449 | use self::m<|>y::Bar; | 1435 | use self::m$0y::Bar; |
1450 | mod my { pub struct Bar; } | 1436 | mod my { pub struct Bar; } |
1451 | 1437 | ||
1452 | fn my() {} | 1438 | fn my() {} |
@@ -1472,7 +1458,7 @@ fn my() {} | |||
1472 | /// bar docs | 1458 | /// bar docs |
1473 | struct Bar; | 1459 | struct Bar; |
1474 | 1460 | ||
1475 | fn foo() { let bar = Ba<|>r; } | 1461 | fn foo() { let bar = Ba$0r; } |
1476 | "#, | 1462 | "#, |
1477 | expect![[r#" | 1463 | expect![[r#" |
1478 | *Bar* | 1464 | *Bar* |
@@ -1499,7 +1485,7 @@ fn foo() { let bar = Ba<|>r; } | |||
1499 | #[doc = "bar docs"] | 1485 | #[doc = "bar docs"] |
1500 | struct Bar; | 1486 | struct Bar; |
1501 | 1487 | ||
1502 | fn foo() { let bar = Ba<|>r; } | 1488 | fn foo() { let bar = Ba$0r; } |
1503 | "#, | 1489 | "#, |
1504 | expect![[r#" | 1490 | expect![[r#" |
1505 | *Bar* | 1491 | *Bar* |
@@ -1528,7 +1514,7 @@ fn foo() { let bar = Ba<|>r; } | |||
1528 | #[doc = "bar docs 2"] | 1514 | #[doc = "bar docs 2"] |
1529 | struct Bar; | 1515 | struct Bar; |
1530 | 1516 | ||
1531 | fn foo() { let bar = Ba<|>r; } | 1517 | fn foo() { let bar = Ba$0r; } |
1532 | "#, | 1518 | "#, |
1533 | expect | 1544 | /// [Foo](struct.Foo.html) |
1559 | pub struct B<|>ar | 1545 | pub struct B$0ar |
1560 | "#, | 1546 | "#, |
1561 | expect | 1570 | /// [struct Foo](struct.Foo.html) |
1585 | pub struct B<|>ar | 1571 | pub struct B$0ar |
1586 | "#, | 1572 | "#, |
1587 | expect | 1598 | /// [Foo](struct.Foo.html) |
1613 | fie<|>ld: () | 1599 | fie$0ld: () |
1614 | } | 1600 | } |
1615 | "#, | 1601 | "#, |
1616 | expect | 1627 | /// [Foo](foo::Foo) |
1642 | pub struct B<|>ar | 1628 | pub struct B$0ar |
1643 | "#, | 1629 | "#, |
1644 | expect | 1657 | /// [Foo](foo::Foo) |
1672 | pub struct B<|>ar | 1658 | pub struct B$0ar |
1673 | "#, | 1659 | "#, |
1674 | expect![[r#" | 1660 | expect![[r#" |
1675 | *Bar* | 1661 | *Bar* |
@@ -1695,7 +1681,7 @@ pub struct B<|>ar | |||
1695 | r#" | 1681 | r#" |
1696 | pub struct Foo; | 1682 | pub struct Foo; |
1697 | /// [Foo] | 1683 | /// [Foo] |
1698 | pub struct B<|>ar | 1684 | pub struct B$0ar |
1699 | "#, | 1685 | "#, |
1700 | expect![[r#" | 1686 | expect![[r#" |
1701 | *Bar* | 1687 | *Bar* |
@@ -1721,7 +1707,7 @@ pub struct B<|>ar | |||
1721 | r#" | 1707 | r#" |
1722 | pub struct Foo; | 1708 | pub struct Foo; |
1723 | /// [`Foo`] | 1709 | /// [`Foo`] |
1724 | pub struct B<|>ar | 1710 | pub struct B$0ar |
1725 | "#, | 1711 | "#, |
1726 | expect![[r#" | 1712 | expect![[r#" |
1727 | *Bar* | 1713 | *Bar* |
@@ -1748,7 +1734,7 @@ pub struct B<|>ar | |||
1748 | pub struct Foo; | 1734 | pub struct Foo; |
1749 | fn Foo() {} | 1735 | fn Foo() {} |
1750 | /// [Foo()] | 1736 | /// [Foo()] |
1751 | pub struct B<|>ar | 1737 | pub struct B$0ar |
1752 | "#, | 1738 | "#, |
1753 | expect![[r#" | 1739 | expect![[r#" |
1754 | *Bar* | 1740 | *Bar* |
@@ -1774,7 +1760,7 @@ pub struct B<|>ar | |||
1774 | r#" | 1760 | r#" |
1775 | pub struct Foo; | 1761 | pub struct Foo; |
1776 | /// [`struct Foo`] | 1762 | /// [`struct Foo`] |
1777 | pub struct B<|>ar | 1763 | pub struct B$0ar |
1778 | "#, | 1764 | "#, |
1779 | expect![[r#" | 1765 | expect![[r#" |
1780 | *Bar* | 1766 | *Bar* |
@@ -1800,7 +1786,7 @@ pub struct B<|>ar | |||
1800 | r#" | 1786 | r#" |
1801 | pub struct Foo; | 1787 | pub struct Foo; |
1802 | /// [`struct@Foo`] | 1788 | /// [`struct@Foo`] |
1803 | pub struct B<|>ar | 1789 | pub struct B$0ar |
1804 | "#, | 1790 | "#, |
1805 | expect![[r#" | 1791 | expect![[r#" |
1806 | *Bar* | 1792 | *Bar* |
@@ -1828,7 +1814,7 @@ pub struct Foo; | |||
1828 | /// [my Foo][foo] | 1814 | /// [my Foo][foo] |
1829 | /// | 1815 | /// |
1830 | /// [foo]: Foo | 1816 | /// [foo]: Foo |
1831 | pub struct B<|>ar | 1817 | pub struct B$0ar |
1832 | "#, | 1818 | "#, |
1833 | expect | 1842 | /// [external](https://www.google.com) |
1857 | pub struct B<|>ar | 1843 | pub struct B$0ar |
1858 | "#, | 1844 | "#, |
1859 | expect | 1869 | /// [baz](Baz) |
1884 | pub struct B<|>ar | 1870 | pub struct B$0ar |
1885 | "#, | 1871 | "#, |
1886 | expect![[r#" | 1872 | expect![[r#" |
1887 | *Bar* | 1873 | *Bar* |
@@ -1907,7 +1893,7 @@ pub struct B<|>ar | |||
1907 | r#" | 1893 | r#" |
1908 | enum E { | 1894 | enum E { |
1909 | /// [E] | 1895 | /// [E] |
1910 | V<|> { field: i32 } | 1896 | V$0 { field: i32 } |
1911 | } | 1897 | } |
1912 | "#, | 1898 | "#, |
1913 | expect![[r#" | 1899 | expect![[r#" |
@@ -1934,7 +1920,7 @@ enum E { | |||
1934 | r#" | 1920 | r#" |
1935 | struct S { | 1921 | struct S { |
1936 | /// [`S`] | 1922 | /// [`S`] |
1937 | field<|>: i32 | 1923 | field$0: i32 |
1938 | } | 1924 | } |
1939 | "#, | 1925 | "#, |
1940 | expect | 1950 | /// case 2. inline URL with title: [example](https://www.example.com/) |
1965 | /// case 3. code refrence: [`Result`] | 1951 | /// case 3. code reference: [`Result`] |
1966 | /// case 4. code refrence but miss footnote: [`String`] | 1952 | /// case 4. code reference but miss footnote: [`String`] |
1967 | /// case 5. autolink: <http://www.example.com/> | 1953 | /// case 5. autolink: <http://www.example.com/> |
1968 | /// case 6. email address: <[email protected]> | 1954 | /// case 6. email address: <[email protected]> |
1969 | /// case 7. refrence: [example][example] | 1955 | /// case 7. reference: [example][example] |
1970 | /// case 8. collapsed link: [example][] | 1956 | /// case 8. collapsed link: [example][] |
1971 | /// case 9. shortcut link: [example] | 1957 | /// case 9. shortcut link: [example] |
1972 | /// case 10. inline without URL: [example]() | 1958 | /// case 10. inline without URL: [example]() |
1973 | /// case 11. refrence: [foo][foo] | 1959 | /// case 11. reference: [foo][foo] |
1974 | /// case 12. refrence: [foo][bar] | 1960 | /// case 12. reference: [foo][bar] |
1975 | /// case 13. collapsed link: [foo][] | 1961 | /// case 13. collapsed link: [foo][] |
1976 | /// case 14. shortcut link: [foo] | 1962 | /// case 14. shortcut link: [foo] |
1977 | /// case 15. inline without URL: [foo]() | 1963 | /// case 15. inline without URL: [foo]() |
@@ -1980,7 +1966,7 @@ struct S { | |||
1980 | /// | 1966 | /// |
1981 | /// [`Result`]: ../../std/result/enum.Result.html | 1967 | /// [`Result`]: ../../std/result/enum.Result.html |
1982 | /// [^example]: https://www.example.com/ | 1968 | /// [^example]: https://www.example.com/ |
1983 | pub fn fo<|>o() {} | 1969 | pub fn fo$0o() {} |
1984 | "#, | 1970 | "#, |
1985 | expect | 1986 | case 2. inline URL with title: [example](https://www.example.com/) |
2001 | case 3. code refrence: `Result` | 1987 | case 3. code reference: `Result` |
2002 | case 4. code refrence but miss footnote: `String` | 1988 | case 4. code reference but miss footnote: `String` |
2003 | case 5. autolink: http://www.example.com/ | 1989 | case 5. autolink: http://www.example.com/ |
2004 | case 6. email address: [email protected] | 1990 | case 6. email address: [email protected] |
2005 | case 7. refrence: example | 1991 | case 7. reference: example |
2006 | case 8. collapsed link: example | 1992 | case 8. collapsed link: example |
2007 | case 9. shortcut link: example | 1993 | case 9. shortcut link: example |
2008 | case 10. inline without URL: example | 1994 | case 10. inline without URL: example |
2009 | case 11. refrence: foo | 1995 | case 11. reference: foo |
2010 | case 12. refrence: foo | 1996 | case 12. reference: foo |
2011 | case 13. collapsed link: foo | 1997 | case 13. collapsed link: foo |
2012 | case 14. shortcut link: foo | 1998 | case 14. shortcut link: foo |
2013 | case 15. inline without URL: foo | 1999 | case 15. inline without URL: foo |
@@ -2037,7 +2023,7 @@ macro_rules! bar { | |||
2037 | 2023 | ||
2038 | bar!(); | 2024 | bar!(); |
2039 | 2025 | ||
2040 | fn foo() { let bar = Bar; bar.fo<|>o(); } | 2026 | fn foo() { let bar = Bar; bar.fo$0o(); } |
2041 | "#, | 2027 | "#, |
2042 | expect![[r#" | 2028 | expect![[r#" |
2043 | *foo* | 2029 | *foo* |
@@ -2075,7 +2061,7 @@ macro_rules! bar { | |||
2075 | 2061 | ||
2076 | bar!(); | 2062 | bar!(); |
2077 | 2063 | ||
2078 | fn foo() { let bar = Bar; bar.fo<|>o(); } | 2064 | fn foo() { let bar = Bar; bar.fo$0o(); } |
2079 | "#, | 2065 | "#, |
2080 | expect![[r#" | 2066 | expect![[r#" |
2081 | *foo* | 2067 | *foo* |
@@ -2098,10 +2084,10 @@ fn foo() { let bar = Bar; bar.fo<|>o(); } | |||
2098 | #[test] | 2084 | #[test] |
2099 | fn test_hover_trait_has_impl_action() { | 2085 | fn test_hover_trait_has_impl_action() { |
2100 | check_actions( | 2086 | check_actions( |
2101 | r#"trait foo<|>() {}"#, | 2087 | r#"trait foo$0() {}"#, |
2102 | expect![[r#" | 2088 | expect![[r#" |
2103 | [ | 2089 | [ |
2104 | Implementaion( | 2090 | Implementation( |
2105 | FilePosition { | 2091 | FilePosition { |
2106 | file_id: FileId( | 2092 | file_id: FileId( |
2107 | 0, | 2093 | 0, |
@@ -2117,10 +2103,10 @@ fn foo() { let bar = Bar; bar.fo<|>o(); } | |||
2117 | #[test] | 2103 | #[test] |
2118 | fn test_hover_struct_has_impl_action() { | 2104 | fn test_hover_struct_has_impl_action() { |
2119 | check_actions( | 2105 | check_actions( |
2120 | r"struct foo<|>() {}", | 2106 | r"struct foo$0() {}", |
2121 | expect![[r#" | 2107 | expect![[r#" |
2122 | [ | 2108 | [ |
2123 | Implementaion( | 2109 | Implementation( |
2124 | FilePosition { | 2110 | FilePosition { |
2125 | file_id: FileId( | 2111 | file_id: FileId( |
2126 | 0, | 2112 | 0, |
@@ -2136,10 +2122,10 @@ fn foo() { let bar = Bar; bar.fo<|>o(); } | |||
2136 | #[test] | 2122 | #[test] |
2137 | fn test_hover_union_has_impl_action() { | 2123 | fn test_hover_union_has_impl_action() { |
2138 | check_actions( | 2124 | check_actions( |
2139 | r#"union foo<|>() {}"#, | 2125 | r#"union foo$0() {}"#, |
2140 | expect![[r#" | 2126 | expect![[r#" |
2141 | [ | 2127 | [ |
2142 | Implementaion( | 2128 | Implementation( |
2143 | FilePosition { | 2129 | FilePosition { |
2144 | file_id: FileId( | 2130 | file_id: FileId( |
2145 | 0, | 2131 | 0, |
@@ -2155,10 +2141,10 @@ fn foo() { let bar = Bar; bar.fo<|>o(); } | |||
2155 | #[test] | 2141 | #[test] |
2156 | fn test_hover_enum_has_impl_action() { | 2142 | fn test_hover_enum_has_impl_action() { |
2157 | check_actions( | 2143 | check_actions( |
2158 | r"enum foo<|>() { A, B }", | 2144 | r"enum foo$0() { A, B }", |
2159 | expect![[r#" | 2145 | expect![[r#" |
2160 | [ | 2146 | [ |
2161 | Implementaion( | 2147 | Implementation( |
2162 | FilePosition { | 2148 | FilePosition { |
2163 | file_id: FileId( | 2149 | file_id: FileId( |
2164 | 0, | 2150 | 0, |
@@ -2172,11 +2158,30 @@ fn foo() { let bar = Bar; bar.fo<|>o(); } | |||
2172 | } | 2158 | } |
2173 | 2159 | ||
2174 | #[test] | 2160 | #[test] |
2161 | fn test_hover_self_has_impl_action() { | ||
2162 | check_actions( | ||
2163 | r#"struct foo where Self$0:;"#, | ||
2164 | expect![[r#" | ||
2165 | [ | ||
2166 | Implementation( | ||
2167 | FilePosition { | ||
2168 | file_id: FileId( | ||
2169 | 0, | ||
2170 | ), | ||
2171 | offset: 7, | ||
2172 | }, | ||
2173 | ), | ||
2174 | ] | ||
2175 | "#]], | ||
2176 | ); | ||
2177 | } | ||
2178 | |||
2179 | #[test] | ||
2175 | fn test_hover_test_has_action() { | 2180 | fn test_hover_test_has_action() { |
2176 | check_actions( | 2181 | check_actions( |
2177 | r#" | 2182 | r#" |
2178 | #[test] | 2183 | #[test] |
2179 | fn foo_<|>test() {} | 2184 | fn foo_$0test() {} |
2180 | "#, | 2185 | "#, |
2181 | expect![[r#" | 2186 | expect![[r#" |
2182 | [ | 2187 | [ |
@@ -2211,7 +2216,7 @@ fn foo_<|>test() {} | |||
2211 | fn test_hover_test_mod_has_action() { | 2216 | fn test_hover_test_mod_has_action() { |
2212 | check_actions( | 2217 | check_actions( |
2213 | r#" | 2218 | r#" |
2214 | mod tests<|> { | 2219 | mod tests$0 { |
2215 | #[test] | 2220 | #[test] |
2216 | fn foo_test() {} | 2221 | fn foo_test() {} |
2217 | } | 2222 | } |
@@ -2246,7 +2251,7 @@ mod tests<|> { | |||
2246 | r#" | 2251 | r#" |
2247 | struct S{ f1: u32 } | 2252 | struct S{ f1: u32 } |
2248 | 2253 | ||
2249 | fn main() { let s<|>t = S{ f1:0 }; } | 2254 | fn main() { let s$0t = S{ f1:0 }; } |
2250 | "#, | 2255 | "#, |
2251 | expect![[r#" | 2256 | expect![[r#" |
2252 | [ | 2257 | [ |
@@ -2279,7 +2284,7 @@ fn main() { let s<|>t = S{ f1:0 }; } | |||
2279 | struct Arg(u32); | 2284 | struct Arg(u32); |
2280 | struct S<T>{ f1: T } | 2285 | struct S<T>{ f1: T } |
2281 | 2286 | ||
2282 | fn main() { let s<|>t = S{ f1:Arg(0) }; } | 2287 | fn main() { let s$0t = S{ f1:Arg(0) }; } |
2283 | "#, | 2288 | "#, |
2284 | expect![[r#" | 2289 | expect![[r#" |
2285 | [ | 2290 | [ |
@@ -2325,7 +2330,7 @@ fn main() { let s<|>t = S{ f1:Arg(0) }; } | |||
2325 | struct Arg(u32); | 2330 | struct Arg(u32); |
2326 | struct S<T>{ f1: T } | 2331 | struct S<T>{ f1: T } |
2327 | 2332 | ||
2328 | fn main() { let s<|>t = S{ f1: S{ f1: Arg(0) } }; } | 2333 | fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; } |
2329 | "#, | 2334 | "#, |
2330 | expect![[r#" | 2335 | expect![[r#" |
2331 | [ | 2336 | [ |
@@ -2374,7 +2379,7 @@ mod M { | |||
2374 | pub struct C(u32); | 2379 | pub struct C(u32); |
2375 | } | 2380 | } |
2376 | 2381 | ||
2377 | fn main() { let s<|>t = (A(1), B(2), M::C(3) ); } | 2382 | fn main() { let s$0t = (A(1), B(2), M::C(3) ); } |
2378 | "#, | 2383 | "#, |
2379 | expect![[r#" | 2384 | expect![[r#" |
2380 | [ | 2385 | [ |
@@ -2433,7 +2438,7 @@ fn main() { let s<|>t = (A(1), B(2), M::C(3) ); } | |||
2433 | trait Foo {} | 2438 | trait Foo {} |
2434 | fn foo() -> impl Foo {} | 2439 | fn foo() -> impl Foo {} |
2435 | 2440 | ||
2436 | fn main() { let s<|>t = foo(); } | 2441 | fn main() { let s$0t = foo(); } |
2437 | "#, | 2442 | "#, |
2438 | expect![[r#" | 2443 | expect![[r#" |
2439 | [ | 2444 | [ |
@@ -2467,7 +2472,7 @@ trait Foo<T> {} | |||
2467 | struct S; | 2472 | struct S; |
2468 | fn foo() -> impl Foo<S> {} | 2473 | fn foo() -> impl Foo<S> {} |
2469 | 2474 | ||
2470 | fn main() { let s<|>t = foo(); } | 2475 | fn main() { let s$0t = foo(); } |
2471 | "#, | 2476 | "#, |
2472 | expect![[r#" | 2477 | expect![[r#" |
2473 | [ | 2478 | [ |
@@ -2514,7 +2519,7 @@ trait Foo {} | |||
2514 | trait Bar {} | 2519 | trait Bar {} |
2515 | fn foo() -> impl Foo + Bar {} | 2520 | fn foo() -> impl Foo + Bar {} |
2516 | 2521 | ||
2517 | fn main() { let s<|>t = foo(); } | 2522 | fn main() { let s$0t = foo(); } |
2518 | "#, | 2523 | "#, |
2519 | expect![[r#" | 2524 | expect![[r#" |
2520 | [ | 2525 | [ |
@@ -2564,7 +2569,7 @@ struct S2 {} | |||
2564 | 2569 | ||
2565 | fn foo() -> impl Foo<S1> + Bar<S2> {} | 2570 | fn foo() -> impl Foo<S1> + Bar<S2> {} |
2566 | 2571 | ||
2567 | fn main() { let s<|>t = foo(); } | 2572 | fn main() { let s$0t = foo(); } |
2568 | "#, | 2573 | "#, |
2569 | expect![[r#" | 2574 | expect![[r#" |
2570 | [ | 2575 | [ |
@@ -2634,7 +2639,7 @@ fn main() { let s<|>t = foo(); } | |||
2634 | check_actions( | 2639 | check_actions( |
2635 | r#" | 2640 | r#" |
2636 | trait Foo {} | 2641 | trait Foo {} |
2637 | fn foo(ar<|>g: &impl Foo) {} | 2642 | fn foo(ar$0g: &impl Foo) {} |
2638 | "#, | 2643 | "#, |
2639 | expect![[r#" | 2644 | expect![[r#" |
2640 | [ | 2645 | [ |
@@ -2668,7 +2673,7 @@ trait Foo {} | |||
2668 | trait Bar<T> {} | 2673 | trait Bar<T> {} |
2669 | struct S{} | 2674 | struct S{} |
2670 | 2675 | ||
2671 | fn foo(ar<|>g: &impl Foo + Bar<S>) {} | 2676 | fn foo(ar$0g: &impl Foo + Bar<S>) {} |
2672 | "#, | 2677 | "#, |
2673 | expect![[r#" | 2678 | expect![[r#" |
2674 | [ | 2679 | [ |
@@ -2726,7 +2731,7 @@ fn foo(ar<|>g: &impl Foo + Bar<S>) {} | |||
2726 | r#" | 2731 | r#" |
2727 | struct S; | 2732 | struct S; |
2728 | fn foo() { | 2733 | fn foo() { |
2729 | let fo<|>o = async { S }; | 2734 | let fo$0o = async { S }; |
2730 | } | 2735 | } |
2731 | 2736 | ||
2732 | #[prelude_import] use future::*; | 2737 | #[prelude_import] use future::*; |
@@ -2778,7 +2783,7 @@ mod future { | |||
2778 | r#" | 2783 | r#" |
2779 | trait Foo<T> {} | 2784 | trait Foo<T> {} |
2780 | struct S {} | 2785 | struct S {} |
2781 | fn foo(ar<|>g: &impl Foo<S>) {} | 2786 | fn foo(ar$0g: &impl Foo<S>) {} |
2782 | "#, | 2787 | "#, |
2783 | expect![[r#" | 2788 | expect![[r#" |
2784 | [ | 2789 | [ |
@@ -2828,7 +2833,7 @@ impl Foo for S {} | |||
2828 | struct B<T>{} | 2833 | struct B<T>{} |
2829 | fn foo() -> B<dyn Foo> {} | 2834 | fn foo() -> B<dyn Foo> {} |
2830 | 2835 | ||
2831 | fn main() { let s<|>t = foo(); } | 2836 | fn main() { let s$0t = foo(); } |
2832 | "#, | 2837 | "#, |
2833 | expect![[r#" | 2838 | expect![[r#" |
2834 | [ | 2839 | [ |
@@ -2872,7 +2877,7 @@ fn main() { let s<|>t = foo(); } | |||
2872 | check_actions( | 2877 | check_actions( |
2873 | r#" | 2878 | r#" |
2874 | trait Foo {} | 2879 | trait Foo {} |
2875 | fn foo(ar<|>g: &dyn Foo) {} | 2880 | fn foo(ar$0g: &dyn Foo) {} |
2876 | "#, | 2881 | "#, |
2877 | expect![[r#" | 2882 | expect![[r#" |
2878 | [ | 2883 | [ |
@@ -2904,7 +2909,7 @@ fn foo(ar<|>g: &dyn Foo) {} | |||
2904 | r#" | 2909 | r#" |
2905 | trait Foo<T> {} | 2910 | trait Foo<T> {} |
2906 | struct S {} | 2911 | struct S {} |
2907 | fn foo(ar<|>g: &dyn Foo<S>) {} | 2912 | fn foo(ar$0g: &dyn Foo<S>) {} |
2908 | "#, | 2913 | "#, |
2909 | expect![[r#" | 2914 | expect![[r#" |
2910 | [ | 2915 | [ |
@@ -2952,7 +2957,7 @@ trait DynTrait<T> {} | |||
2952 | struct B<T> {} | 2957 | struct B<T> {} |
2953 | struct S {} | 2958 | struct S {} |
2954 | 2959 | ||
2955 | fn foo(a<|>rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {} | 2960 | fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {} |
2956 | "#, | 2961 | "#, |
2957 | expect![[r#" | 2962 | expect![[r#" |
2958 | [ | 2963 | [ |
@@ -3033,7 +3038,7 @@ impl Foo for S { type Item = Bar; } | |||
3033 | 3038 | ||
3034 | fn test() -> impl Foo { S {} } | 3039 | fn test() -> impl Foo { S {} } |
3035 | 3040 | ||
3036 | fn main() { let s<|>t = test().get(); } | 3041 | fn main() { let s$0t = test().get(); } |
3037 | "#, | 3042 | "#, |
3038 | expect![[r#" | 3043 | expect![[r#" |
3039 | [ | 3044 | [ |
@@ -3060,6 +3065,71 @@ fn main() { let s<|>t = test().get(); } | |||
3060 | } | 3065 | } |
3061 | 3066 | ||
3062 | #[test] | 3067 | #[test] |
3068 | fn test_hover_const_param_has_goto_type_action() { | ||
3069 | check_actions( | ||
3070 | r#" | ||
3071 | struct Bar; | ||
3072 | struct Foo<const BAR: Bar>; | ||
3073 | |||
3074 | impl<const BAR: Bar> Foo<BAR$0> {} | ||
3075 | "#, | ||
3076 | expect![[r#" | ||
3077 | [ | ||
3078 | GoToType( | ||
3079 | [ | ||
3080 | HoverGotoTypeData { | ||
3081 | mod_path: "test::Bar", | ||
3082 | nav: NavigationTarget { | ||
3083 | file_id: FileId( | ||
3084 | 0, | ||
3085 | ), | ||
3086 | full_range: 0..11, | ||
3087 | focus_range: 7..10, | ||
3088 | name: "Bar", | ||
3089 | kind: Struct, | ||
3090 | description: "struct Bar", | ||
3091 | }, | ||
3092 | }, | ||
3093 | ], | ||
3094 | ), | ||
3095 | ] | ||
3096 | "#]], | ||
3097 | ); | ||
3098 | } | ||
3099 | |||
3100 | #[test] | ||
3101 | fn test_hover_type_param_has_goto_type_action() { | ||
3102 | check_actions( | ||
3103 | r#" | ||
3104 | trait Foo {} | ||
3105 | |||
3106 | fn foo<T: Foo>(t: T$0){} | ||
3107 | "#, | ||
3108 | expect![[r#" | ||
3109 | [ | ||
3110 | GoToType( | ||
3111 | [ | ||
3112 | HoverGotoTypeData { | ||
3113 | mod_path: "test::Foo", | ||
3114 | nav: NavigationTarget { | ||
3115 | file_id: FileId( | ||
3116 | 0, | ||
3117 | ), | ||
3118 | full_range: 0..12, | ||
3119 | focus_range: 6..9, | ||
3120 | name: "Foo", | ||
3121 | kind: Trait, | ||
3122 | description: "trait Foo", | ||
3123 | }, | ||
3124 | }, | ||
3125 | ], | ||
3126 | ), | ||
3127 | ] | ||
3128 | "#]], | ||
3129 | ); | ||
3130 | } | ||
3131 | |||
3132 | #[test] | ||
3063 | fn hover_displays_normalized_crate_names() { | 3133 | fn hover_displays_normalized_crate_names() { |
3064 | check( | 3134 | check( |
3065 | r#" | 3135 | r#" |
@@ -3073,7 +3143,7 @@ pub mod wrapper { | |||
3073 | } | 3143 | } |
3074 | 3144 | ||
3075 | //- /main.rs crate:main deps:name-with-dashes | 3145 | //- /main.rs crate:main deps:name-with-dashes |
3076 | fn main() { let foo_test = name_with_dashes::wrapper::Thing::new<|>(); } | 3146 | fn main() { let foo_test = name_with_dashes::wrapper::Thing::new$0(); } |
3077 | "#, | 3147 | "#, |
3078 | expect![[r#" | 3148 | expect![[r#" |
3079 | *new* | 3149 | *new* |
@@ -3099,7 +3169,7 @@ struct S { | |||
3099 | 3169 | ||
3100 | fn main() { | 3170 | fn main() { |
3101 | let s = S { f: 0 }; | 3171 | let s = S { f: 0 }; |
3102 | let S { f<|> } = &s; | 3172 | let S { f$0 } = &s; |
3103 | } | 3173 | } |
3104 | "#, | 3174 | "#, |
3105 | expect![[r#" | 3175 | expect![[r#" |
@@ -3118,7 +3188,7 @@ fn main() { | |||
3118 | r#" | 3188 | r#" |
3119 | struct Foo {} | 3189 | struct Foo {} |
3120 | impl Foo { | 3190 | impl Foo { |
3121 | fn bar(&sel<|>f) {} | 3191 | fn bar(&sel$0f) {} |
3122 | } | 3192 | } |
3123 | "#, | 3193 | "#, |
3124 | expect![[r#" | 3194 | expect![[r#" |
@@ -3137,7 +3207,7 @@ impl Foo { | |||
3137 | struct Arc<T>(T); | 3207 | struct Arc<T>(T); |
3138 | struct Foo {} | 3208 | struct Foo {} |
3139 | impl Foo { | 3209 | impl Foo { |
3140 | fn bar(sel<|>f: Arc<Foo>) {} | 3210 | fn bar(sel$0f: Arc<Foo>) {} |
3141 | } | 3211 | } |
3142 | "#, | 3212 | "#, |
3143 | expect![[r#" | 3213 | expect![[r#" |
@@ -3154,7 +3224,7 @@ impl Foo { | |||
3154 | check( | 3224 | check( |
3155 | r#" | 3225 | r#" |
3156 | /// Be quick; | 3226 | /// Be quick; |
3157 | mod Foo<|> { | 3227 | mod Foo$0 { |
3158 | //! time is mana | 3228 | //! time is mana |
3159 | 3229 | ||
3160 | /// This comment belongs to the function | 3230 | /// This comment belongs to the function |
@@ -3185,7 +3255,7 @@ mod Foo<|> { | |||
3185 | check( | 3255 | check( |
3186 | r#" | 3256 | r#" |
3187 | #[doc = "Be quick;"] | 3257 | #[doc = "Be quick;"] |
3188 | mod Foo<|> { | 3258 | mod Foo$0 { |
3189 | #![doc = "time is mana"] | 3259 | #![doc = "time is mana"] |
3190 | 3260 | ||
3191 | #[doc = "This comment belongs to the function"] | 3261 | #[doc = "This comment belongs to the function"] |
@@ -3216,9 +3286,105 @@ mod Foo<|> { | |||
3216 | check_hover_no_result( | 3286 | check_hover_no_result( |
3217 | r#" | 3287 | r#" |
3218 | fn no_hover() { | 3288 | fn no_hover() { |
3219 | // no<|>hover | 3289 | // no$0hover |
3220 | } | 3290 | } |
3221 | "#, | 3291 | "#, |
3222 | ); | 3292 | ); |
3223 | } | 3293 | } |
3294 | |||
3295 | #[test] | ||
3296 | fn hover_label() { | ||
3297 | check( | ||
3298 | r#" | ||
3299 | fn foo() { | ||
3300 | 'label$0: loop {} | ||
3301 | } | ||
3302 | "#, | ||
3303 | expect![[r#" | ||
3304 | *'label* | ||
3305 | |||
3306 | ```rust | ||
3307 | 'label | ||
3308 | ``` | ||
3309 | "#]], | ||
3310 | ); | ||
3311 | } | ||
3312 | |||
3313 | #[test] | ||
3314 | fn hover_lifetime() { | ||
3315 | check( | ||
3316 | r#"fn foo<'lifetime>(_: &'lifetime$0 ()) {}"#, | ||
3317 | expect![[r#" | ||
3318 | *'lifetime* | ||
3319 | |||
3320 | ```rust | ||
3321 | 'lifetime | ||
3322 | ``` | ||
3323 | "#]], | ||
3324 | ); | ||
3325 | } | ||
3326 | |||
3327 | #[test] | ||
3328 | fn hover_type_param() { | ||
3329 | check( | ||
3330 | r#" | ||
3331 | struct Foo<T>(T); | ||
3332 | trait Copy {} | ||
3333 | trait Clone {} | ||
3334 | trait Sized {} | ||
3335 | impl<T: Copy + Clone> Foo<T$0> where T: Sized {} | ||
3336 | "#, | ||
3337 | expect![[r#" | ||
3338 | *T* | ||
3339 | |||
3340 | ```rust | ||
3341 | T: Copy + Clone + Sized | ||
3342 | ``` | ||
3343 | "#]], | ||
3344 | ); | ||
3345 | check( | ||
3346 | r#" | ||
3347 | struct Foo<T>(T); | ||
3348 | impl<T> Foo<T$0> {} | ||
3349 | "#, | ||
3350 | expect![[r#" | ||
3351 | *T* | ||
3352 | |||
3353 | ```rust | ||
3354 | T | ||
3355 | ``` | ||
3356 | "#]], | ||
3357 | ); | ||
3358 | // lifetimes aren't being substituted yet | ||
3359 | check( | ||
3360 | r#" | ||
3361 | struct Foo<T>(T); | ||
3362 | impl<T: 'static> Foo<T$0> {} | ||
3363 | "#, | ||
3364 | expect![[r#" | ||
3365 | *T* | ||
3366 | |||
3367 | ```rust | ||
3368 | T: {error} | ||
3369 | ``` | ||
3370 | "#]], | ||
3371 | ); | ||
3372 | } | ||
3373 | |||
3374 | #[test] | ||
3375 | fn hover_const_param() { | ||
3376 | check( | ||
3377 | r#" | ||
3378 | struct Foo<const LEN: usize>; | ||
3379 | impl<const LEN: usize> Foo<LEN$0> {} | ||
3380 | "#, | ||
3381 | expect![[r#" | ||
3382 | *LEN* | ||
3383 | |||
3384 | ```rust | ||
3385 | const LEN: usize | ||
3386 | ``` | ||
3387 | "#]], | ||
3388 | ); | ||
3389 | } | ||
3224 | } | 3390 | } |
diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 65df7979c..a2039fcc7 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs | |||
@@ -18,12 +18,6 @@ pub struct InlayHintsConfig { | |||
18 | pub max_length: Option<usize>, | 18 | pub max_length: Option<usize>, |
19 | } | 19 | } |
20 | 20 | ||
21 | impl Default for InlayHintsConfig { | ||
22 | fn default() -> Self { | ||
23 | Self { type_hints: true, parameter_hints: true, chaining_hints: true, max_length: None } | ||
24 | } | ||
25 | } | ||
26 | |||
27 | #[derive(Clone, Debug, PartialEq, Eq)] | 21 | #[derive(Clone, Debug, PartialEq, Eq)] |
28 | pub enum InlayKind { | 22 | pub enum InlayKind { |
29 | TypeHint, | 23 | TypeHint, |
@@ -359,9 +353,25 @@ fn is_argument_similar_to_param_name( | |||
359 | } | 353 | } |
360 | match get_string_representation(argument) { | 354 | match get_string_representation(argument) { |
361 | None => false, | 355 | None => false, |
362 | Some(repr) => { | 356 | Some(argument_string) => { |
363 | let argument_string = repr.trim_start_matches('_'); | 357 | let num_leading_underscores = |
364 | argument_string.starts_with(param_name) || argument_string.ends_with(param_name) | 358 | argument_string.bytes().take_while(|&c| c == b'_').count(); |
359 | |||
360 | // Does the argument name begin with the parameter name? Ignore leading underscores. | ||
361 | let mut arg_bytes = argument_string.bytes().skip(num_leading_underscores); | ||
362 | let starts_with_pattern = param_name.bytes().all( | ||
363 | |expected| matches!(arg_bytes.next(), Some(actual) if expected.eq_ignore_ascii_case(&actual)), | ||
364 | ); | ||
365 | |||
366 | if starts_with_pattern { | ||
367 | return true; | ||
368 | } | ||
369 | |||
370 | // Does the argument name end with the parameter name? | ||
371 | let mut arg_bytes = argument_string.bytes().skip(num_leading_underscores); | ||
372 | param_name.bytes().rev().all( | ||
373 | |expected| matches!(arg_bytes.next_back(), Some(actual) if expected.eq_ignore_ascii_case(&actual)), | ||
374 | ) | ||
365 | } | 375 | } |
366 | } | 376 | } |
367 | } | 377 | } |
@@ -433,8 +443,15 @@ mod tests { | |||
433 | 443 | ||
434 | use crate::{fixture, inlay_hints::InlayHintsConfig}; | 444 | use crate::{fixture, inlay_hints::InlayHintsConfig}; |
435 | 445 | ||
446 | const TEST_CONFIG: InlayHintsConfig = InlayHintsConfig { | ||
447 | type_hints: true, | ||
448 | parameter_hints: true, | ||
449 | chaining_hints: true, | ||
450 | max_length: None, | ||
451 | }; | ||
452 | |||
436 | fn check(ra_fixture: &str) { | 453 | fn check(ra_fixture: &str) { |
437 | check_with_config(InlayHintsConfig::default(), ra_fixture); | 454 | check_with_config(TEST_CONFIG, ra_fixture); |
438 | } | 455 | } |
439 | 456 | ||
440 | fn check_with_config(config: InlayHintsConfig, ra_fixture: &str) { | 457 | fn check_with_config(config: InlayHintsConfig, ra_fixture: &str) { |
@@ -748,7 +765,7 @@ fn main() { | |||
748 | #[test] | 765 | #[test] |
749 | fn hint_truncation() { | 766 | fn hint_truncation() { |
750 | check_with_config( | 767 | check_with_config( |
751 | InlayHintsConfig { max_length: Some(8), ..Default::default() }, | 768 | InlayHintsConfig { max_length: Some(8), ..TEST_CONFIG }, |
752 | r#" | 769 | r#" |
753 | struct Smol<T>(T); | 770 | struct Smol<T>(T); |
754 | 771 | ||
@@ -831,7 +848,7 @@ fn main() { | |||
831 | #[test] | 848 | #[test] |
832 | fn omitted_parameters_hints_heuristics() { | 849 | fn omitted_parameters_hints_heuristics() { |
833 | check_with_config( | 850 | check_with_config( |
834 | InlayHintsConfig { max_length: Some(8), ..Default::default() }, | 851 | InlayHintsConfig { max_length: Some(8), ..TEST_CONFIG }, |
835 | r#" | 852 | r#" |
836 | fn map(f: i32) {} | 853 | fn map(f: i32) {} |
837 | fn filter(predicate: i32) {} | 854 | fn filter(predicate: i32) {} |
@@ -900,6 +917,9 @@ fn main() { | |||
900 | twiddle(true); | 917 | twiddle(true); |
901 | doo(true); | 918 | doo(true); |
902 | 919 | ||
920 | const TWIDDLE_UPPERCASE: bool = true; | ||
921 | twiddle(TWIDDLE_UPPERCASE); | ||
922 | |||
903 | let mut param_begin: Param = Param {}; | 923 | let mut param_begin: Param = Param {}; |
904 | different_order(¶m_begin); | 924 | different_order(¶m_begin); |
905 | different_order(&mut param_begin); | 925 | different_order(&mut param_begin); |
@@ -924,7 +944,7 @@ fn main() { | |||
924 | #[test] | 944 | #[test] |
925 | fn unit_structs_have_no_type_hints() { | 945 | fn unit_structs_have_no_type_hints() { |
926 | check_with_config( | 946 | check_with_config( |
927 | InlayHintsConfig { max_length: Some(8), ..Default::default() }, | 947 | InlayHintsConfig { max_length: Some(8), ..TEST_CONFIG }, |
928 | r#" | 948 | r#" |
929 | enum Result<T, E> { Ok(T), Err(E) } | 949 | enum Result<T, E> { Ok(T), Err(E) } |
930 | use Result::*; | 950 | use Result::*; |
@@ -1381,4 +1401,41 @@ fn main() { | |||
1381 | "#, | 1401 | "#, |
1382 | ) | 1402 | ) |
1383 | } | 1403 | } |
1404 | |||
1405 | #[test] | ||
1406 | fn fn_hints() { | ||
1407 | check( | ||
1408 | r#" | ||
1409 | trait Sized {} | ||
1410 | |||
1411 | fn foo() -> impl Fn() { loop {} } | ||
1412 | fn foo1() -> impl Fn(f64) { loop {} } | ||
1413 | fn foo2() -> impl Fn(f64, f64) { loop {} } | ||
1414 | fn foo3() -> impl Fn(f64, f64) -> u32 { loop {} } | ||
1415 | fn foo4() -> &'static dyn Fn(f64, f64) -> u32 { loop {} } | ||
1416 | fn foo5() -> &'static dyn Fn(&'static dyn Fn(f64, f64) -> u32, f64) -> u32 { loop {} } | ||
1417 | fn foo6() -> impl Fn(f64, f64) -> u32 + Sized { loop {} } | ||
1418 | fn foo7() -> *const (impl Fn(f64, f64) -> u32 + Sized) { loop {} } | ||
1419 | |||
1420 | fn main() { | ||
1421 | let foo = foo(); | ||
1422 | // ^^^ impl Fn() | ||
1423 | let foo = foo1(); | ||
1424 | // ^^^ impl Fn(f64) | ||
1425 | let foo = foo2(); | ||
1426 | // ^^^ impl Fn(f64, f64) | ||
1427 | let foo = foo3(); | ||
1428 | // ^^^ impl Fn(f64, f64) -> u32 | ||
1429 | let foo = foo4(); | ||
1430 | // ^^^ &dyn Fn(f64, f64) -> u32 | ||
1431 | let foo = foo5(); | ||
1432 | // ^^^ &dyn Fn(&dyn Fn(f64, f64) -> u32, f64) -> u32 | ||
1433 | let foo = foo6(); | ||
1434 | // ^^^ impl Fn(f64, f64) -> u32 + Sized | ||
1435 | let foo = foo7(); | ||
1436 | // ^^^ *const (impl Fn(f64, f64) -> u32 + Sized) | ||
1437 | } | ||
1438 | "#, | ||
1439 | ) | ||
1440 | } | ||
1384 | } | 1441 | } |
diff --git a/crates/ide/src/join_lines.rs b/crates/ide/src/join_lines.rs index b5a6f66fd..05380f2a1 100644 --- a/crates/ide/src/join_lines.rs +++ b/crates/ide/src/join_lines.rs | |||
@@ -104,7 +104,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextS | |||
104 | // Special case that turns something like: | 104 | // Special case that turns something like: |
105 | // | 105 | // |
106 | // ``` | 106 | // ``` |
107 | // my_function({<|> | 107 | // my_function({$0 |
108 | // <some-expr> | 108 | // <some-expr> |
109 | // }) | 109 | // }) |
110 | // ``` | 110 | // ``` |
@@ -116,7 +116,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextS | |||
116 | // ditto for | 116 | // ditto for |
117 | // | 117 | // |
118 | // ``` | 118 | // ``` |
119 | // use foo::{<|> | 119 | // use foo::{$0 |
120 | // bar | 120 | // bar |
121 | // }; | 121 | // }; |
122 | // ``` | 122 | // ``` |
@@ -198,8 +198,8 @@ mod tests { | |||
198 | 198 | ||
199 | use super::*; | 199 | use super::*; |
200 | 200 | ||
201 | fn check_join_lines(before: &str, after: &str) { | 201 | fn check_join_lines(ra_fixture_before: &str, ra_fixture_after: &str) { |
202 | let (before_cursor_pos, before) = extract_offset(before); | 202 | let (before_cursor_pos, before) = extract_offset(ra_fixture_before); |
203 | let file = SourceFile::parse(&before).ok().unwrap(); | 203 | let file = SourceFile::parse(&before).ok().unwrap(); |
204 | 204 | ||
205 | let range = TextRange::empty(before_cursor_pos); | 205 | let range = TextRange::empty(before_cursor_pos); |
@@ -214,7 +214,7 @@ mod tests { | |||
214 | .apply_to_offset(before_cursor_pos) | 214 | .apply_to_offset(before_cursor_pos) |
215 | .expect("cursor position is affected by the edit"); | 215 | .expect("cursor position is affected by the edit"); |
216 | let actual = add_cursor(&actual, actual_cursor_pos); | 216 | let actual = add_cursor(&actual, actual_cursor_pos); |
217 | assert_eq_text!(after, &actual); | 217 | assert_eq_text!(ra_fixture_after, &actual); |
218 | } | 218 | } |
219 | 219 | ||
220 | #[test] | 220 | #[test] |
@@ -222,13 +222,13 @@ mod tests { | |||
222 | check_join_lines( | 222 | check_join_lines( |
223 | r" | 223 | r" |
224 | fn foo() { | 224 | fn foo() { |
225 | <|>foo(1, | 225 | $0foo(1, |
226 | ) | 226 | ) |
227 | } | 227 | } |
228 | ", | 228 | ", |
229 | r" | 229 | r" |
230 | fn foo() { | 230 | fn foo() { |
231 | <|>foo(1) | 231 | $0foo(1) |
232 | } | 232 | } |
233 | ", | 233 | ", |
234 | ); | 234 | ); |
@@ -239,14 +239,14 @@ fn foo() { | |||
239 | check_join_lines( | 239 | check_join_lines( |
240 | r" | 240 | r" |
241 | pub fn reparse(&self, edit: &AtomTextEdit) -> File { | 241 | pub fn reparse(&self, edit: &AtomTextEdit) -> File { |
242 | <|>self.incremental_reparse(edit).unwrap_or_else(|| { | 242 | $0self.incremental_reparse(edit).unwrap_or_else(|| { |
243 | self.full_reparse(edit) | 243 | self.full_reparse(edit) |
244 | }) | 244 | }) |
245 | } | 245 | } |
246 | ", | 246 | ", |
247 | r" | 247 | r" |
248 | pub fn reparse(&self, edit: &AtomTextEdit) -> File { | 248 | pub fn reparse(&self, edit: &AtomTextEdit) -> File { |
249 | <|>self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit)) | 249 | $0self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit)) |
250 | } | 250 | } |
251 | ", | 251 | ", |
252 | ); | 252 | ); |
@@ -257,13 +257,13 @@ pub fn reparse(&self, edit: &AtomTextEdit) -> File { | |||
257 | check_join_lines( | 257 | check_join_lines( |
258 | r" | 258 | r" |
259 | fn foo() { | 259 | fn foo() { |
260 | foo(<|>{ | 260 | foo($0{ |
261 | 92 | 261 | 92 |
262 | }) | 262 | }) |
263 | }", | 263 | }", |
264 | r" | 264 | r" |
265 | fn foo() { | 265 | fn foo() { |
266 | foo(<|>92) | 266 | foo($092) |
267 | }", | 267 | }", |
268 | ); | 268 | ); |
269 | } | 269 | } |
@@ -274,7 +274,7 @@ fn foo() { | |||
274 | fn foo() { | 274 | fn foo() { |
275 | loop { | 275 | loop { |
276 | match x { | 276 | match x { |
277 | 92 => <|>{ | 277 | 92 => $0{ |
278 | continue; | 278 | continue; |
279 | } | 279 | } |
280 | } | 280 | } |
@@ -285,7 +285,7 @@ fn foo() { | |||
285 | fn foo() { | 285 | fn foo() { |
286 | loop { | 286 | loop { |
287 | match x { | 287 | match x { |
288 | 92 => <|>continue, | 288 | 92 => $0continue, |
289 | } | 289 | } |
290 | } | 290 | } |
291 | } | 291 | } |
@@ -299,7 +299,7 @@ fn foo() { | |||
299 | r" | 299 | r" |
300 | fn foo(e: Result<U, V>) { | 300 | fn foo(e: Result<U, V>) { |
301 | match e { | 301 | match e { |
302 | Ok(u) => <|>{ | 302 | Ok(u) => $0{ |
303 | u.foo() | 303 | u.foo() |
304 | } | 304 | } |
305 | Err(v) => v, | 305 | Err(v) => v, |
@@ -308,7 +308,7 @@ fn foo(e: Result<U, V>) { | |||
308 | r" | 308 | r" |
309 | fn foo(e: Result<U, V>) { | 309 | fn foo(e: Result<U, V>) { |
310 | match e { | 310 | match e { |
311 | Ok(u) => <|>u.foo(), | 311 | Ok(u) => $0u.foo(), |
312 | Err(v) => v, | 312 | Err(v) => v, |
313 | } | 313 | } |
314 | }", | 314 | }", |
@@ -321,7 +321,7 @@ fn foo(e: Result<U, V>) { | |||
321 | r" | 321 | r" |
322 | fn foo() { | 322 | fn foo() { |
323 | match ty { | 323 | match ty { |
324 | <|> Some(ty) => { | 324 | $0 Some(ty) => { |
325 | match ty { | 325 | match ty { |
326 | _ => false, | 326 | _ => false, |
327 | } | 327 | } |
@@ -333,7 +333,7 @@ fn foo() { | |||
333 | r" | 333 | r" |
334 | fn foo() { | 334 | fn foo() { |
335 | match ty { | 335 | match ty { |
336 | <|> Some(ty) => match ty { | 336 | $0 Some(ty) => match ty { |
337 | _ => false, | 337 | _ => false, |
338 | }, | 338 | }, |
339 | _ => true, | 339 | _ => true, |
@@ -350,7 +350,7 @@ fn foo() { | |||
350 | r" | 350 | r" |
351 | fn foo(e: Result<U, V>) { | 351 | fn foo(e: Result<U, V>) { |
352 | match e { | 352 | match e { |
353 | Ok(u) => <|>{ | 353 | Ok(u) => $0{ |
354 | u.foo() | 354 | u.foo() |
355 | }, | 355 | }, |
356 | Err(v) => v, | 356 | Err(v) => v, |
@@ -359,7 +359,7 @@ fn foo(e: Result<U, V>) { | |||
359 | r" | 359 | r" |
360 | fn foo(e: Result<U, V>) { | 360 | fn foo(e: Result<U, V>) { |
361 | match e { | 361 | match e { |
362 | Ok(u) => <|>u.foo(), | 362 | Ok(u) => $0u.foo(), |
363 | Err(v) => v, | 363 | Err(v) => v, |
364 | } | 364 | } |
365 | }", | 365 | }", |
@@ -370,7 +370,7 @@ fn foo(e: Result<U, V>) { | |||
370 | r" | 370 | r" |
371 | fn foo(e: Result<U, V>) { | 371 | fn foo(e: Result<U, V>) { |
372 | match e { | 372 | match e { |
373 | Ok(u) => <|>{ | 373 | Ok(u) => $0{ |
374 | u.foo() | 374 | u.foo() |
375 | } , | 375 | } , |
376 | Err(v) => v, | 376 | Err(v) => v, |
@@ -379,7 +379,7 @@ fn foo(e: Result<U, V>) { | |||
379 | r" | 379 | r" |
380 | fn foo(e: Result<U, V>) { | 380 | fn foo(e: Result<U, V>) { |
381 | match e { | 381 | match e { |
382 | Ok(u) => <|>u.foo() , | 382 | Ok(u) => $0u.foo() , |
383 | Err(v) => v, | 383 | Err(v) => v, |
384 | } | 384 | } |
385 | }", | 385 | }", |
@@ -390,7 +390,7 @@ fn foo(e: Result<U, V>) { | |||
390 | r" | 390 | r" |
391 | fn foo(e: Result<U, V>) { | 391 | fn foo(e: Result<U, V>) { |
392 | match e { | 392 | match e { |
393 | Ok(u) => <|>{ | 393 | Ok(u) => $0{ |
394 | u.foo() | 394 | u.foo() |
395 | } | 395 | } |
396 | , | 396 | , |
@@ -400,7 +400,7 @@ fn foo(e: Result<U, V>) { | |||
400 | r" | 400 | r" |
401 | fn foo(e: Result<U, V>) { | 401 | fn foo(e: Result<U, V>) { |
402 | match e { | 402 | match e { |
403 | Ok(u) => <|>u.foo() | 403 | Ok(u) => $0u.foo() |
404 | , | 404 | , |
405 | Err(v) => v, | 405 | Err(v) => v, |
406 | } | 406 | } |
@@ -414,13 +414,13 @@ fn foo(e: Result<U, V>) { | |||
414 | check_join_lines( | 414 | check_join_lines( |
415 | r" | 415 | r" |
416 | fn foo() { | 416 | fn foo() { |
417 | let x = (<|>{ | 417 | let x = ($0{ |
418 | 4 | 418 | 4 |
419 | },); | 419 | },); |
420 | }", | 420 | }", |
421 | r" | 421 | r" |
422 | fn foo() { | 422 | fn foo() { |
423 | let x = (<|>4,); | 423 | let x = ($04,); |
424 | }", | 424 | }", |
425 | ); | 425 | ); |
426 | 426 | ||
@@ -428,13 +428,13 @@ fn foo() { | |||
428 | check_join_lines( | 428 | check_join_lines( |
429 | r" | 429 | r" |
430 | fn foo() { | 430 | fn foo() { |
431 | let x = (<|>{ | 431 | let x = ($0{ |
432 | 4 | 432 | 4 |
433 | } ,); | 433 | } ,); |
434 | }", | 434 | }", |
435 | r" | 435 | r" |
436 | fn foo() { | 436 | fn foo() { |
437 | let x = (<|>4 ,); | 437 | let x = ($04 ,); |
438 | }", | 438 | }", |
439 | ); | 439 | ); |
440 | 440 | ||
@@ -442,14 +442,14 @@ fn foo() { | |||
442 | check_join_lines( | 442 | check_join_lines( |
443 | r" | 443 | r" |
444 | fn foo() { | 444 | fn foo() { |
445 | let x = (<|>{ | 445 | let x = ($0{ |
446 | 4 | 446 | 4 |
447 | } | 447 | } |
448 | ,); | 448 | ,); |
449 | }", | 449 | }", |
450 | r" | 450 | r" |
451 | fn foo() { | 451 | fn foo() { |
452 | let x = (<|>4 | 452 | let x = ($04 |
453 | ,); | 453 | ,); |
454 | }", | 454 | }", |
455 | ); | 455 | ); |
@@ -460,11 +460,11 @@ fn foo() { | |||
460 | // No space after the '{' | 460 | // No space after the '{' |
461 | check_join_lines( | 461 | check_join_lines( |
462 | r" | 462 | r" |
463 | <|>use syntax::{ | 463 | $0use syntax::{ |
464 | TextSize, TextRange, | 464 | TextSize, TextRange, |
465 | };", | 465 | };", |
466 | r" | 466 | r" |
467 | <|>use syntax::{TextSize, TextRange, | 467 | $0use syntax::{TextSize, TextRange, |
468 | };", | 468 | };", |
469 | ); | 469 | ); |
470 | } | 470 | } |
@@ -475,11 +475,11 @@ fn foo() { | |||
475 | check_join_lines( | 475 | check_join_lines( |
476 | r" | 476 | r" |
477 | use syntax::{ | 477 | use syntax::{ |
478 | <|> TextSize, TextRange | 478 | $0 TextSize, TextRange |
479 | };", | 479 | };", |
480 | r" | 480 | r" |
481 | use syntax::{ | 481 | use syntax::{ |
482 | <|> TextSize, TextRange};", | 482 | $0 TextSize, TextRange};", |
483 | ); | 483 | ); |
484 | } | 484 | } |
485 | 485 | ||
@@ -489,11 +489,11 @@ use syntax::{ | |||
489 | check_join_lines( | 489 | check_join_lines( |
490 | r" | 490 | r" |
491 | use syntax::{ | 491 | use syntax::{ |
492 | <|> TextSize, TextRange, | 492 | $0 TextSize, TextRange, |
493 | };", | 493 | };", |
494 | r" | 494 | r" |
495 | use syntax::{ | 495 | use syntax::{ |
496 | <|> TextSize, TextRange};", | 496 | $0 TextSize, TextRange};", |
497 | ); | 497 | ); |
498 | } | 498 | } |
499 | 499 | ||
@@ -502,14 +502,14 @@ use syntax::{ | |||
502 | check_join_lines( | 502 | check_join_lines( |
503 | r" | 503 | r" |
504 | use syntax::{ | 504 | use syntax::{ |
505 | algo::<|>{ | 505 | algo::$0{ |
506 | find_token_at_offset, | 506 | find_token_at_offset, |
507 | }, | 507 | }, |
508 | ast, | 508 | ast, |
509 | };", | 509 | };", |
510 | r" | 510 | r" |
511 | use syntax::{ | 511 | use syntax::{ |
512 | algo::<|>find_token_at_offset, | 512 | algo::$0find_token_at_offset, |
513 | ast, | 513 | ast, |
514 | };", | 514 | };", |
515 | ); | 515 | ); |
@@ -520,13 +520,13 @@ use syntax::{ | |||
520 | check_join_lines( | 520 | check_join_lines( |
521 | r" | 521 | r" |
522 | fn foo() { | 522 | fn foo() { |
523 | // Hello<|> | 523 | // Hello$0 |
524 | // world! | 524 | // world! |
525 | } | 525 | } |
526 | ", | 526 | ", |
527 | r" | 527 | r" |
528 | fn foo() { | 528 | fn foo() { |
529 | // Hello<|> world! | 529 | // Hello$0 world! |
530 | } | 530 | } |
531 | ", | 531 | ", |
532 | ); | 532 | ); |
@@ -537,13 +537,13 @@ fn foo() { | |||
537 | check_join_lines( | 537 | check_join_lines( |
538 | r" | 538 | r" |
539 | fn foo() { | 539 | fn foo() { |
540 | /// Hello<|> | 540 | /// Hello$0 |
541 | /// world! | 541 | /// world! |
542 | } | 542 | } |
543 | ", | 543 | ", |
544 | r" | 544 | r" |
545 | fn foo() { | 545 | fn foo() { |
546 | /// Hello<|> world! | 546 | /// Hello$0 world! |
547 | } | 547 | } |
548 | ", | 548 | ", |
549 | ); | 549 | ); |
@@ -554,13 +554,13 @@ fn foo() { | |||
554 | check_join_lines( | 554 | check_join_lines( |
555 | r" | 555 | r" |
556 | fn foo() { | 556 | fn foo() { |
557 | //! Hello<|> | 557 | //! Hello$0 |
558 | //! world! | 558 | //! world! |
559 | } | 559 | } |
560 | ", | 560 | ", |
561 | r" | 561 | r" |
562 | fn foo() { | 562 | fn foo() { |
563 | //! Hello<|> world! | 563 | //! Hello$0 world! |
564 | } | 564 | } |
565 | ", | 565 | ", |
566 | ); | 566 | ); |
@@ -571,13 +571,13 @@ fn foo() { | |||
571 | check_join_lines( | 571 | check_join_lines( |
572 | r" | 572 | r" |
573 | fn foo() { | 573 | fn foo() { |
574 | // Hello<|> | 574 | // Hello$0 |
575 | /* world! */ | 575 | /* world! */ |
576 | } | 576 | } |
577 | ", | 577 | ", |
578 | r" | 578 | r" |
579 | fn foo() { | 579 | fn foo() { |
580 | // Hello<|> world! */ | 580 | // Hello$0 world! */ |
581 | } | 581 | } |
582 | ", | 582 | ", |
583 | ); | 583 | ); |
@@ -588,7 +588,7 @@ fn foo() { | |||
588 | check_join_lines( | 588 | check_join_lines( |
589 | r" | 589 | r" |
590 | fn foo() { | 590 | fn foo() { |
591 | // The<|> | 591 | // The$0 |
592 | /* quick | 592 | /* quick |
593 | brown | 593 | brown |
594 | fox! */ | 594 | fox! */ |
@@ -596,7 +596,7 @@ fn foo() { | |||
596 | ", | 596 | ", |
597 | r" | 597 | r" |
598 | fn foo() { | 598 | fn foo() { |
599 | // The<|> quick | 599 | // The$0 quick |
600 | brown | 600 | brown |
601 | fox! */ | 601 | fox! */ |
602 | } | 602 | } |
@@ -604,8 +604,8 @@ fn foo() { | |||
604 | ); | 604 | ); |
605 | } | 605 | } |
606 | 606 | ||
607 | fn check_join_lines_sel(before: &str, after: &str) { | 607 | fn check_join_lines_sel(ra_fixture_before: &str, ra_fixture_after: &str) { |
608 | let (sel, before) = extract_range(before); | 608 | let (sel, before) = extract_range(ra_fixture_before); |
609 | let parse = SourceFile::parse(&before); | 609 | let parse = SourceFile::parse(&before); |
610 | let result = join_lines(&parse.tree(), sel); | 610 | let result = join_lines(&parse.tree(), sel); |
611 | let actual = { | 611 | let actual = { |
@@ -613,7 +613,7 @@ fn foo() { | |||
613 | result.apply(&mut actual); | 613 | result.apply(&mut actual); |
614 | actual | 614 | actual |
615 | }; | 615 | }; |
616 | assert_eq_text!(after, &actual); | 616 | assert_eq_text!(ra_fixture_after, &actual); |
617 | } | 617 | } |
618 | 618 | ||
619 | #[test] | 619 | #[test] |
@@ -621,10 +621,10 @@ fn foo() { | |||
621 | check_join_lines_sel( | 621 | check_join_lines_sel( |
622 | r" | 622 | r" |
623 | fn foo() { | 623 | fn foo() { |
624 | <|>foo(1, | 624 | $0foo(1, |
625 | 2, | 625 | 2, |
626 | 3, | 626 | 3, |
627 | <|>) | 627 | $0) |
628 | } | 628 | } |
629 | ", | 629 | ", |
630 | r" | 630 | r" |
@@ -639,9 +639,9 @@ fn foo() { | |||
639 | fn test_join_lines_selection_struct() { | 639 | fn test_join_lines_selection_struct() { |
640 | check_join_lines_sel( | 640 | check_join_lines_sel( |
641 | r" | 641 | r" |
642 | struct Foo <|>{ | 642 | struct Foo $0{ |
643 | f: u32, | 643 | f: u32, |
644 | }<|> | 644 | }$0 |
645 | ", | 645 | ", |
646 | r" | 646 | r" |
647 | struct Foo { f: u32 } | 647 | struct Foo { f: u32 } |
@@ -654,9 +654,9 @@ struct Foo { f: u32 } | |||
654 | check_join_lines_sel( | 654 | check_join_lines_sel( |
655 | r" | 655 | r" |
656 | fn foo() { | 656 | fn foo() { |
657 | join(<|>type_params.type_params() | 657 | join($0type_params.type_params() |
658 | .filter_map(|it| it.name()) | 658 | .filter_map(|it| it.name()) |
659 | .map(|it| it.text())<|>) | 659 | .map(|it| it.text())$0) |
660 | }", | 660 | }", |
661 | r" | 661 | r" |
662 | fn foo() { | 662 | fn foo() { |
@@ -671,9 +671,9 @@ fn foo() { | |||
671 | r" | 671 | r" |
672 | pub fn handle_find_matching_brace() { | 672 | pub fn handle_find_matching_brace() { |
673 | params.offsets | 673 | params.offsets |
674 | .map(|offset| <|>{ | 674 | .map(|offset| $0{ |
675 | world.analysis().matching_brace(&file, offset).unwrap_or(offset) | 675 | world.analysis().matching_brace(&file, offset).unwrap_or(offset) |
676 | }<|>) | 676 | }$0) |
677 | .collect(); | 677 | .collect(); |
678 | }", | 678 | }", |
679 | r" | 679 | r" |
@@ -691,7 +691,7 @@ pub fn handle_find_matching_brace() { | |||
691 | r" | 691 | r" |
692 | fn main() { | 692 | fn main() { |
693 | let _ = { | 693 | let _ = { |
694 | // <|>foo | 694 | // $0foo |
695 | // bar | 695 | // bar |
696 | 92 | 696 | 92 |
697 | }; | 697 | }; |
@@ -700,7 +700,7 @@ fn main() { | |||
700 | r" | 700 | r" |
701 | fn main() { | 701 | fn main() { |
702 | let _ = { | 702 | let _ = { |
703 | // <|>foo bar | 703 | // $0foo bar |
704 | 92 | 704 | 92 |
705 | }; | 705 | }; |
706 | } | 706 | } |
@@ -712,12 +712,12 @@ fn main() { | |||
712 | fn join_lines_mandatory_blocks_block() { | 712 | fn join_lines_mandatory_blocks_block() { |
713 | check_join_lines( | 713 | check_join_lines( |
714 | r" | 714 | r" |
715 | <|>fn foo() { | 715 | $0fn foo() { |
716 | 92 | 716 | 92 |
717 | } | 717 | } |
718 | ", | 718 | ", |
719 | r" | 719 | r" |
720 | <|>fn foo() { 92 | 720 | $0fn foo() { 92 |
721 | } | 721 | } |
722 | ", | 722 | ", |
723 | ); | 723 | ); |
@@ -725,14 +725,14 @@ fn main() { | |||
725 | check_join_lines( | 725 | check_join_lines( |
726 | r" | 726 | r" |
727 | fn foo() { | 727 | fn foo() { |
728 | <|>if true { | 728 | $0if true { |
729 | 92 | 729 | 92 |
730 | } | 730 | } |
731 | } | 731 | } |
732 | ", | 732 | ", |
733 | r" | 733 | r" |
734 | fn foo() { | 734 | fn foo() { |
735 | <|>if true { 92 | 735 | $0if true { 92 |
736 | } | 736 | } |
737 | } | 737 | } |
738 | ", | 738 | ", |
@@ -741,14 +741,14 @@ fn foo() { | |||
741 | check_join_lines( | 741 | check_join_lines( |
742 | r" | 742 | r" |
743 | fn foo() { | 743 | fn foo() { |
744 | <|>loop { | 744 | $0loop { |
745 | 92 | 745 | 92 |
746 | } | 746 | } |
747 | } | 747 | } |
748 | ", | 748 | ", |
749 | r" | 749 | r" |
750 | fn foo() { | 750 | fn foo() { |
751 | <|>loop { 92 | 751 | $0loop { 92 |
752 | } | 752 | } |
753 | } | 753 | } |
754 | ", | 754 | ", |
@@ -757,14 +757,14 @@ fn foo() { | |||
757 | check_join_lines( | 757 | check_join_lines( |
758 | r" | 758 | r" |
759 | fn foo() { | 759 | fn foo() { |
760 | <|>unsafe { | 760 | $0unsafe { |
761 | 92 | 761 | 92 |
762 | } | 762 | } |
763 | } | 763 | } |
764 | ", | 764 | ", |
765 | r" | 765 | r" |
766 | fn foo() { | 766 | fn foo() { |
767 | <|>unsafe { 92 | 767 | $0unsafe { 92 |
768 | } | 768 | } |
769 | } | 769 | } |
770 | ", | 770 | ", |
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index b3331f03f..1e03832ec 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs | |||
@@ -31,6 +31,7 @@ mod folding_ranges; | |||
31 | mod goto_definition; | 31 | mod goto_definition; |
32 | mod goto_implementation; | 32 | mod goto_implementation; |
33 | mod goto_type_definition; | 33 | mod goto_type_definition; |
34 | mod view_hir; | ||
34 | mod hover; | 35 | mod hover; |
35 | mod inlay_hints; | 36 | mod inlay_hints; |
36 | mod join_lines; | 37 | mod join_lines; |
@@ -75,14 +76,14 @@ pub use crate::{ | |||
75 | references::{rename::RenameError, Declaration, ReferenceSearchResult}, | 76 | references::{rename::RenameError, Declaration, ReferenceSearchResult}, |
76 | runnables::{Runnable, RunnableKind, TestId}, | 77 | runnables::{Runnable, RunnableKind, TestId}, |
77 | syntax_highlighting::{ | 78 | syntax_highlighting::{ |
78 | tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag}, | 79 | tags::{Highlight, HlMod, HlMods, HlPunct, HlTag}, |
79 | HighlightedRange, | 80 | HlRange, |
80 | }, | 81 | }, |
81 | }; | 82 | }; |
82 | pub use assists::{Assist, AssistConfig, AssistId, AssistKind}; | 83 | pub use assists::{Assist, AssistConfig, AssistId, AssistKind, InsertUseConfig}; |
83 | pub use completion::{ | 84 | pub use completion::{ |
84 | CompletionConfig, CompletionItem, CompletionItemKind, CompletionResolveCapability, | 85 | CompletionConfig, CompletionItem, CompletionItemKind, CompletionScore, ImportEdit, |
85 | CompletionScore, ImportEdit, InsertTextFormat, | 86 | InsertTextFormat, |
86 | }; | 87 | }; |
87 | pub use hir::{Documentation, Semantics}; | 88 | pub use hir::{Documentation, Semantics}; |
88 | pub use ide_db::base_db::{ | 89 | pub use ide_db::base_db::{ |
@@ -91,7 +92,7 @@ pub use ide_db::base_db::{ | |||
91 | }; | 92 | }; |
92 | pub use ide_db::{ | 93 | pub use ide_db::{ |
93 | call_info::CallInfo, | 94 | call_info::CallInfo, |
94 | search::{Reference, ReferenceAccess, ReferenceKind}, | 95 | search::{FileReference, ReferenceAccess, ReferenceKind}, |
95 | }; | 96 | }; |
96 | pub use ide_db::{ | 97 | pub use ide_db::{ |
97 | label::Label, | 98 | label::Label, |
@@ -271,6 +272,10 @@ impl Analysis { | |||
271 | self.with_db(|db| syntax_tree::syntax_tree(&db, file_id, text_range)) | 272 | self.with_db(|db| syntax_tree::syntax_tree(&db, file_id, text_range)) |
272 | } | 273 | } |
273 | 274 | ||
275 | pub fn view_hir(&self, position: FilePosition) -> Cancelable<String> { | ||
276 | self.with_db(|db| view_hir::view_hir(&db, position)) | ||
277 | } | ||
278 | |||
274 | pub fn expand_macro(&self, position: FilePosition) -> Cancelable<Option<ExpandedMacro>> { | 279 | pub fn expand_macro(&self, position: FilePosition) -> Cancelable<Option<ExpandedMacro>> { |
275 | self.with_db(|db| expand_macro::expand_macro(db, position)) | 280 | self.with_db(|db| expand_macro::expand_macro(db, position)) |
276 | } | 281 | } |
@@ -444,12 +449,12 @@ impl Analysis { | |||
444 | } | 449 | } |
445 | 450 | ||
446 | /// Computes syntax highlighting for the given file | 451 | /// Computes syntax highlighting for the given file |
447 | pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> { | 452 | pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HlRange>> { |
448 | self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false)) | 453 | self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false)) |
449 | } | 454 | } |
450 | 455 | ||
451 | /// Computes syntax highlighting for the given file range. | 456 | /// Computes syntax highlighting for the given file range. |
452 | pub fn highlight_range(&self, frange: FileRange) -> Cancelable<Vec<HighlightedRange>> { | 457 | pub fn highlight_range(&self, frange: FileRange) -> Cancelable<Vec<HlRange>> { |
453 | self.with_db(|db| { | 458 | self.with_db(|db| { |
454 | syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false) | 459 | syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false) |
455 | }) | 460 | }) |
diff --git a/crates/ide/src/matching_brace.rs b/crates/ide/src/matching_brace.rs index d70248afe..1bfa1439d 100644 --- a/crates/ide/src/matching_brace.rs +++ b/crates/ide/src/matching_brace.rs | |||
@@ -58,15 +58,15 @@ mod tests { | |||
58 | assert_eq_text!(after, &actual); | 58 | assert_eq_text!(after, &actual); |
59 | } | 59 | } |
60 | 60 | ||
61 | do_check("struct Foo { a: i32, }<|>", "struct Foo <|>{ a: i32, }"); | 61 | do_check("struct Foo { a: i32, }$0", "struct Foo $0{ a: i32, }"); |
62 | do_check("fn main() { |x: i32|<|> x * 2;}", "fn main() { <|>|x: i32| x * 2;}"); | 62 | do_check("fn main() { |x: i32|$0 x * 2;}", "fn main() { $0|x: i32| x * 2;}"); |
63 | do_check("fn main() { <|>|x: i32| x * 2;}", "fn main() { |x: i32<|>| x * 2;}"); | 63 | do_check("fn main() { $0|x: i32| x * 2;}", "fn main() { |x: i32$0| x * 2;}"); |
64 | 64 | ||
65 | { | 65 | { |
66 | mark::check!(pipes_not_braces); | 66 | mark::check!(pipes_not_braces); |
67 | do_check( | 67 | do_check( |
68 | "fn main() { match 92 { 1 | 2 |<|> 3 => 92 } }", | 68 | "fn main() { match 92 { 1 | 2 |$0 3 => 92 } }", |
69 | "fn main() { match 92 { 1 | 2 |<|> 3 => 92 } }", | 69 | "fn main() { match 92 { 1 | 2 |$0 3 => 92 } }", |
70 | ); | 70 | ); |
71 | } | 71 | } |
72 | } | 72 | } |
diff --git a/crates/ide/src/parent_module.rs b/crates/ide/src/parent_module.rs index be344a09b..d343638fb 100644 --- a/crates/ide/src/parent_module.rs +++ b/crates/ide/src/parent_module.rs | |||
@@ -74,7 +74,7 @@ mod tests { | |||
74 | //- /lib.rs | 74 | //- /lib.rs |
75 | mod foo; | 75 | mod foo; |
76 | //- /foo.rs | 76 | //- /foo.rs |
77 | <|>// empty | 77 | $0// empty |
78 | ", | 78 | ", |
79 | ); | 79 | ); |
80 | let nav = analysis.parent_module(pos).unwrap().pop().unwrap(); | 80 | let nav = analysis.parent_module(pos).unwrap().pop().unwrap(); |
@@ -90,7 +90,7 @@ mod tests { | |||
90 | mod foo; | 90 | mod foo; |
91 | 91 | ||
92 | //- /foo.rs | 92 | //- /foo.rs |
93 | mod <|>bar; | 93 | mod $0bar; |
94 | 94 | ||
95 | //- /foo/bar.rs | 95 | //- /foo/bar.rs |
96 | // empty | 96 | // empty |
@@ -107,7 +107,7 @@ mod tests { | |||
107 | //- /lib.rs | 107 | //- /lib.rs |
108 | mod foo { | 108 | mod foo { |
109 | mod bar { | 109 | mod bar { |
110 | mod baz { <|> } | 110 | mod baz { $0 } |
111 | } | 111 | } |
112 | } | 112 | } |
113 | ", | 113 | ", |
@@ -123,7 +123,7 @@ mod tests { | |||
123 | //- /main.rs | 123 | //- /main.rs |
124 | mod foo; | 124 | mod foo; |
125 | //- /foo.rs | 125 | //- /foo.rs |
126 | <|> | 126 | $0 |
127 | "#, | 127 | "#, |
128 | ); | 128 | ); |
129 | assert_eq!(analysis.crate_for(file_id).unwrap().len(), 1); | 129 | assert_eq!(analysis.crate_for(file_id).unwrap().len(), 1); |
diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index 21b2d7ca1..7d4757e02 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs | |||
@@ -3,7 +3,7 @@ | |||
3 | //! or `ast::NameRef`. If it's a `ast::NameRef`, at the classification step we | 3 | //! or `ast::NameRef`. If it's a `ast::NameRef`, at the classification step we |
4 | //! try to resolve the direct tree parent of this element, otherwise we | 4 | //! try to resolve the direct tree parent of this element, otherwise we |
5 | //! already have a definition and just need to get its HIR together with | 5 | //! already have a definition and just need to get its HIR together with |
6 | //! some information that is needed for futher steps of searching. | 6 | //! some information that is needed for further steps of searching. |
7 | //! After that, we collect files that might contain references and look | 7 | //! After that, we collect files that might contain references and look |
8 | //! for text occurrences of the identifier. If there's an `ast::NameRef` | 8 | //! for text occurrences of the identifier. If there's an `ast::NameRef` |
9 | //! at the index that the match starts at and its tree parent is | 9 | //! at the index that the match starts at and its tree parent is |
@@ -13,15 +13,15 @@ pub(crate) mod rename; | |||
13 | 13 | ||
14 | use hir::Semantics; | 14 | use hir::Semantics; |
15 | use ide_db::{ | 15 | use ide_db::{ |
16 | base_db::FileId, | ||
16 | defs::{Definition, NameClass, NameRefClass}, | 17 | defs::{Definition, NameClass, NameRefClass}, |
17 | search::Reference, | 18 | search::{FileReference, ReferenceAccess, ReferenceKind, SearchScope, UsageSearchResult}, |
18 | search::{ReferenceAccess, ReferenceKind, SearchScope}, | ||
19 | RootDatabase, | 19 | RootDatabase, |
20 | }; | 20 | }; |
21 | use syntax::{ | 21 | use syntax::{ |
22 | algo::find_node_at_offset, | 22 | algo::find_node_at_offset, |
23 | ast::{self, NameOwner}, | 23 | ast::{self, NameOwner}, |
24 | match_ast, AstNode, SyntaxKind, SyntaxNode, TextRange, TokenAtOffset, | 24 | match_ast, AstNode, SyntaxNode, TextRange, TokenAtOffset, T, |
25 | }; | 25 | }; |
26 | 26 | ||
27 | use crate::{display::TryToNav, FilePosition, FileRange, NavigationTarget, RangeInfo, SymbolKind}; | 27 | use crate::{display::TryToNav, FilePosition, FileRange, NavigationTarget, RangeInfo, SymbolKind}; |
@@ -29,7 +29,7 @@ use crate::{display::TryToNav, FilePosition, FileRange, NavigationTarget, RangeI | |||
29 | #[derive(Debug, Clone)] | 29 | #[derive(Debug, Clone)] |
30 | pub struct ReferenceSearchResult { | 30 | pub struct ReferenceSearchResult { |
31 | declaration: Declaration, | 31 | declaration: Declaration, |
32 | references: Vec<Reference>, | 32 | references: UsageSearchResult, |
33 | } | 33 | } |
34 | 34 | ||
35 | #[derive(Debug, Clone)] | 35 | #[derive(Debug, Clone)] |
@@ -48,10 +48,21 @@ impl ReferenceSearchResult { | |||
48 | &self.declaration.nav | 48 | &self.declaration.nav |
49 | } | 49 | } |
50 | 50 | ||
51 | pub fn references(&self) -> &[Reference] { | 51 | pub fn references(&self) -> &UsageSearchResult { |
52 | &self.references | 52 | &self.references |
53 | } | 53 | } |
54 | 54 | ||
55 | pub fn references_with_declaration(mut self) -> UsageSearchResult { | ||
56 | let decl_ref = FileReference { | ||
57 | range: self.declaration.nav.focus_or_full_range(), | ||
58 | kind: self.declaration.kind, | ||
59 | access: self.declaration.access, | ||
60 | }; | ||
61 | let file_id = self.declaration.nav.file_id; | ||
62 | self.references.references.entry(file_id).or_default().push(decl_ref); | ||
63 | self.references | ||
64 | } | ||
65 | |||
55 | /// Total number of references | 66 | /// Total number of references |
56 | /// At least 1 since all valid references should | 67 | /// At least 1 since all valid references should |
57 | /// Have a declaration | 68 | /// Have a declaration |
@@ -63,21 +74,11 @@ impl ReferenceSearchResult { | |||
63 | // allow turning ReferenceSearchResult into an iterator | 74 | // allow turning ReferenceSearchResult into an iterator |
64 | // over References | 75 | // over References |
65 | impl IntoIterator for ReferenceSearchResult { | 76 | impl IntoIterator for ReferenceSearchResult { |
66 | type Item = Reference; | 77 | type Item = (FileId, Vec<FileReference>); |
67 | type IntoIter = std::vec::IntoIter<Reference>; | 78 | type IntoIter = std::collections::hash_map::IntoIter<FileId, Vec<FileReference>>; |
68 | 79 | ||
69 | fn into_iter(mut self) -> Self::IntoIter { | 80 | fn into_iter(self) -> Self::IntoIter { |
70 | let mut v = Vec::with_capacity(self.len()); | 81 | self.references_with_declaration().into_iter() |
71 | v.push(Reference { | ||
72 | file_range: FileRange { | ||
73 | file_id: self.declaration.nav.file_id, | ||
74 | range: self.declaration.nav.focus_or_full_range(), | ||
75 | }, | ||
76 | kind: self.declaration.kind, | ||
77 | access: self.declaration.access, | ||
78 | }); | ||
79 | v.append(&mut self.references); | ||
80 | v.into_iter() | ||
81 | } | 82 | } |
82 | } | 83 | } |
83 | 84 | ||
@@ -109,13 +110,12 @@ pub(crate) fn find_all_refs( | |||
109 | 110 | ||
110 | let RangeInfo { range, info: def } = find_name(&sema, &syntax, position, opt_name)?; | 111 | let RangeInfo { range, info: def } = find_name(&sema, &syntax, position, opt_name)?; |
111 | 112 | ||
112 | let references = def | 113 | let mut usages = def.usages(sema).set_scope(search_scope).all(); |
113 | .usages(sema) | 114 | usages |
114 | .set_scope(search_scope) | 115 | .references |
115 | .all() | 116 | .values_mut() |
116 | .into_iter() | 117 | .for_each(|it| it.retain(|r| search_kind == ReferenceKind::Other || search_kind == r.kind)); |
117 | .filter(|r| search_kind == ReferenceKind::Other || search_kind == r.kind) | 118 | usages.references.retain(|_, it| !it.is_empty()); |
118 | .collect(); | ||
119 | 119 | ||
120 | let nav = def.try_to_nav(sema.db)?; | 120 | let nav = def.try_to_nav(sema.db)?; |
121 | let decl_range = nav.focus_or_full_range(); | 121 | let decl_range = nav.focus_or_full_range(); |
@@ -130,13 +130,16 @@ pub(crate) fn find_all_refs( | |||
130 | kind = ReferenceKind::FieldShorthandForLocal; | 130 | kind = ReferenceKind::FieldShorthandForLocal; |
131 | } | 131 | } |
132 | } | 132 | } |
133 | } else if matches!(def, Definition::LifetimeParam(_) | Definition::Label(_)) { | 133 | } else if matches!( |
134 | def, | ||
135 | Definition::GenericParam(hir::GenericParam::LifetimeParam(_)) | Definition::Label(_) | ||
136 | ) { | ||
134 | kind = ReferenceKind::Lifetime; | 137 | kind = ReferenceKind::Lifetime; |
135 | }; | 138 | }; |
136 | 139 | ||
137 | let declaration = Declaration { nav, kind, access: decl_access(&def, &syntax, decl_range) }; | 140 | let declaration = Declaration { nav, kind, access: decl_access(&def, &syntax, decl_range) }; |
138 | 141 | ||
139 | Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references })) | 142 | Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references: usages })) |
140 | } | 143 | } |
141 | 144 | ||
142 | fn find_name( | 145 | fn find_name( |
@@ -200,7 +203,7 @@ fn get_struct_def_name_for_struct_literal_search( | |||
200 | position: FilePosition, | 203 | position: FilePosition, |
201 | ) -> Option<ast::Name> { | 204 | ) -> Option<ast::Name> { |
202 | if let TokenAtOffset::Between(ref left, ref right) = syntax.token_at_offset(position.offset) { | 205 | if let TokenAtOffset::Between(ref left, ref right) = syntax.token_at_offset(position.offset) { |
203 | if right.kind() != SyntaxKind::L_CURLY && right.kind() != SyntaxKind::L_PAREN { | 206 | if right.kind() != T!['{'] && right.kind() != T!['('] { |
204 | return None; | 207 | return None; |
205 | } | 208 | } |
206 | if let Some(name) = | 209 | if let Some(name) = |
@@ -227,7 +230,7 @@ fn get_enum_def_name_for_struct_literal_search( | |||
227 | position: FilePosition, | 230 | position: FilePosition, |
228 | ) -> Option<ast::Name> { | 231 | ) -> Option<ast::Name> { |
229 | if let TokenAtOffset::Between(ref left, ref right) = syntax.token_at_offset(position.offset) { | 232 | if let TokenAtOffset::Between(ref left, ref right) = syntax.token_at_offset(position.offset) { |
230 | if right.kind() != SyntaxKind::L_CURLY && right.kind() != SyntaxKind::L_PAREN { | 233 | if right.kind() != T!['{'] && right.kind() != T!['('] { |
231 | return None; | 234 | return None; |
232 | } | 235 | } |
233 | if let Some(name) = | 236 | if let Some(name) = |
@@ -252,8 +255,8 @@ fn try_find_self_references( | |||
252 | syntax: &SyntaxNode, | 255 | syntax: &SyntaxNode, |
253 | position: FilePosition, | 256 | position: FilePosition, |
254 | ) -> Option<RangeInfo<ReferenceSearchResult>> { | 257 | ) -> Option<RangeInfo<ReferenceSearchResult>> { |
255 | let self_token = | 258 | let FilePosition { file_id, offset } = position; |
256 | syntax.token_at_offset(position.offset).find(|t| t.kind() == SyntaxKind::SELF_KW)?; | 259 | let self_token = syntax.token_at_offset(offset).find(|t| t.kind() == T![self])?; |
257 | let parent = self_token.parent(); | 260 | let parent = self_token.parent(); |
258 | match_ast! { | 261 | match_ast! { |
259 | match parent { | 262 | match parent { |
@@ -274,7 +277,7 @@ fn try_find_self_references( | |||
274 | 277 | ||
275 | let declaration = Declaration { | 278 | let declaration = Declaration { |
276 | nav: NavigationTarget { | 279 | nav: NavigationTarget { |
277 | file_id: position.file_id, | 280 | file_id, |
278 | full_range: self_param.syntax().text_range(), | 281 | full_range: self_param.syntax().text_range(), |
279 | focus_range: Some(param_self_token.text_range()), | 282 | focus_range: Some(param_self_token.text_range()), |
280 | name: param_self_token.text().clone(), | 283 | name: param_self_token.text().clone(), |
@@ -290,7 +293,7 @@ fn try_find_self_references( | |||
290 | ReferenceAccess::Read | 293 | ReferenceAccess::Read |
291 | }), | 294 | }), |
292 | }; | 295 | }; |
293 | let references = function | 296 | let refs = function |
294 | .body() | 297 | .body() |
295 | .map(|body| { | 298 | .map(|body| { |
296 | body.syntax() | 299 | body.syntax() |
@@ -304,14 +307,16 @@ fn try_find_self_references( | |||
304 | None | 307 | None |
305 | } | 308 | } |
306 | }) | 309 | }) |
307 | .map(|token| Reference { | 310 | .map(|token| FileReference { |
308 | file_range: FileRange { file_id: position.file_id, range: token.text_range() }, | 311 | range: token.text_range(), |
309 | kind: ReferenceKind::SelfKw, | 312 | kind: ReferenceKind::SelfKw, |
310 | access: declaration.access, // FIXME: properly check access kind here instead of copying it from the declaration | 313 | access: declaration.access, // FIXME: properly check access kind here instead of copying it from the declaration |
311 | }) | 314 | }) |
312 | .collect() | 315 | .collect() |
313 | }) | 316 | }) |
314 | .unwrap_or_default(); | 317 | .unwrap_or_default(); |
318 | let mut references = UsageSearchResult::default(); | ||
319 | references.references.insert(file_id, refs); | ||
315 | 320 | ||
316 | Some(RangeInfo::new( | 321 | Some(RangeInfo::new( |
317 | param_self_token.text_range(), | 322 | param_self_token.text_range(), |
@@ -331,7 +336,7 @@ mod tests { | |||
331 | fn test_struct_literal_after_space() { | 336 | fn test_struct_literal_after_space() { |
332 | check( | 337 | check( |
333 | r#" | 338 | r#" |
334 | struct Foo <|>{ | 339 | struct Foo $0{ |
335 | a: i32, | 340 | a: i32, |
336 | } | 341 | } |
337 | impl Foo { | 342 | impl Foo { |
@@ -354,7 +359,7 @@ fn main() { | |||
354 | fn test_struct_literal_before_space() { | 359 | fn test_struct_literal_before_space() { |
355 | check( | 360 | check( |
356 | r#" | 361 | r#" |
357 | struct Foo<|> {} | 362 | struct Foo$0 {} |
358 | fn main() { | 363 | fn main() { |
359 | let f: Foo; | 364 | let f: Foo; |
360 | f = Foo {}; | 365 | f = Foo {}; |
@@ -373,7 +378,7 @@ struct Foo<|> {} | |||
373 | fn test_struct_literal_with_generic_type() { | 378 | fn test_struct_literal_with_generic_type() { |
374 | check( | 379 | check( |
375 | r#" | 380 | r#" |
376 | struct Foo<T> <|>{} | 381 | struct Foo<T> $0{} |
377 | fn main() { | 382 | fn main() { |
378 | let f: Foo::<i32>; | 383 | let f: Foo::<i32>; |
379 | f = Foo {}; | 384 | f = Foo {}; |
@@ -391,7 +396,7 @@ struct Foo<T> <|>{} | |||
391 | fn test_struct_literal_for_tuple() { | 396 | fn test_struct_literal_for_tuple() { |
392 | check( | 397 | check( |
393 | r#" | 398 | r#" |
394 | struct Foo<|>(i32); | 399 | struct Foo$0(i32); |
395 | 400 | ||
396 | fn main() { | 401 | fn main() { |
397 | let f: Foo; | 402 | let f: Foo; |
@@ -410,7 +415,7 @@ fn main() { | |||
410 | fn test_enum_after_space() { | 415 | fn test_enum_after_space() { |
411 | check( | 416 | check( |
412 | r#" | 417 | r#" |
413 | enum Foo <|>{ | 418 | enum Foo $0{ |
414 | A, | 419 | A, |
415 | B, | 420 | B, |
416 | } | 421 | } |
@@ -431,7 +436,7 @@ fn main() { | |||
431 | fn test_enum_before_space() { | 436 | fn test_enum_before_space() { |
432 | check( | 437 | check( |
433 | r#" | 438 | r#" |
434 | enum Foo<|> { | 439 | enum Foo$0 { |
435 | A, | 440 | A, |
436 | B, | 441 | B, |
437 | } | 442 | } |
@@ -453,7 +458,7 @@ fn main() { | |||
453 | fn test_enum_with_generic_type() { | 458 | fn test_enum_with_generic_type() { |
454 | check( | 459 | check( |
455 | r#" | 460 | r#" |
456 | enum Foo<T> <|>{ | 461 | enum Foo<T> $0{ |
457 | A(T), | 462 | A(T), |
458 | B, | 463 | B, |
459 | } | 464 | } |
@@ -474,7 +479,7 @@ fn main() { | |||
474 | fn test_enum_for_tuple() { | 479 | fn test_enum_for_tuple() { |
475 | check( | 480 | check( |
476 | r#" | 481 | r#" |
477 | enum Foo<|>{ | 482 | enum Foo$0{ |
478 | A(i8), | 483 | A(i8), |
479 | B(i8), | 484 | B(i8), |
480 | } | 485 | } |
@@ -498,7 +503,7 @@ fn main() { | |||
498 | fn main() { | 503 | fn main() { |
499 | let mut i = 1; | 504 | let mut i = 1; |
500 | let j = 1; | 505 | let j = 1; |
501 | i = i<|> + j; | 506 | i = i$0 + j; |
502 | 507 | ||
503 | { | 508 | { |
504 | i = 0; | 509 | i = 0; |
@@ -522,7 +527,7 @@ fn main() { | |||
522 | check( | 527 | check( |
523 | r#" | 528 | r#" |
524 | fn foo() { | 529 | fn foo() { |
525 | let spam<|> = 92; | 530 | let spam$0 = 92; |
526 | spam + spam | 531 | spam + spam |
527 | } | 532 | } |
528 | fn bar() { | 533 | fn bar() { |
@@ -543,7 +548,7 @@ fn bar() { | |||
543 | fn test_find_all_refs_for_param_inside() { | 548 | fn test_find_all_refs_for_param_inside() { |
544 | check( | 549 | check( |
545 | r#" | 550 | r#" |
546 | fn foo(i : u32) -> u32 { i<|> } | 551 | fn foo(i : u32) -> u32 { i$0 } |
547 | "#, | 552 | "#, |
548 | expect![[r#" | 553 | expect![[r#" |
549 | i ValueParam FileId(0) 7..8 Other | 554 | i ValueParam FileId(0) 7..8 Other |
@@ -557,7 +562,7 @@ fn foo(i : u32) -> u32 { i<|> } | |||
557 | fn test_find_all_refs_for_fn_param() { | 562 | fn test_find_all_refs_for_fn_param() { |
558 | check( | 563 | check( |
559 | r#" | 564 | r#" |
560 | fn foo(i<|> : u32) -> u32 { i } | 565 | fn foo(i$0 : u32) -> u32 { i } |
561 | "#, | 566 | "#, |
562 | expect![[r#" | 567 | expect![[r#" |
563 | i ValueParam FileId(0) 7..8 Other | 568 | i ValueParam FileId(0) 7..8 Other |
@@ -573,7 +578,7 @@ fn foo(i<|> : u32) -> u32 { i } | |||
573 | r#" | 578 | r#" |
574 | //- /lib.rs | 579 | //- /lib.rs |
575 | struct Foo { | 580 | struct Foo { |
576 | pub spam<|>: u32, | 581 | pub spam$0: u32, |
577 | } | 582 | } |
578 | 583 | ||
579 | fn main(s: Foo) { | 584 | fn main(s: Foo) { |
@@ -594,7 +599,7 @@ fn main(s: Foo) { | |||
594 | r#" | 599 | r#" |
595 | struct Foo; | 600 | struct Foo; |
596 | impl Foo { | 601 | impl Foo { |
597 | fn f<|>(&self) { } | 602 | fn f$0(&self) { } |
598 | } | 603 | } |
599 | "#, | 604 | "#, |
600 | expect![[r#" | 605 | expect![[r#" |
@@ -610,7 +615,7 @@ impl Foo { | |||
610 | r#" | 615 | r#" |
611 | enum Foo { | 616 | enum Foo { |
612 | A, | 617 | A, |
613 | B<|>, | 618 | B$0, |
614 | C, | 619 | C, |
615 | } | 620 | } |
616 | "#, | 621 | "#, |
@@ -627,7 +632,7 @@ enum Foo { | |||
627 | r#" | 632 | r#" |
628 | enum Foo { | 633 | enum Foo { |
629 | A, | 634 | A, |
630 | B { field<|>: u8 }, | 635 | B { field$0: u8 }, |
631 | C, | 636 | C, |
632 | } | 637 | } |
633 | "#, | 638 | "#, |
@@ -669,7 +674,7 @@ pub struct Bar { | |||
669 | } | 674 | } |
670 | 675 | ||
671 | fn f() { | 676 | fn f() { |
672 | let i = foo::Foo<|> { n: 5 }; | 677 | let i = foo::Foo$0 { n: 5 }; |
673 | } | 678 | } |
674 | "#, | 679 | "#, |
675 | expect![[r#" | 680 | expect![[r#" |
@@ -689,7 +694,7 @@ fn f() { | |||
689 | check( | 694 | check( |
690 | r#" | 695 | r#" |
691 | //- /lib.rs | 696 | //- /lib.rs |
692 | mod foo<|>; | 697 | mod foo$0; |
693 | 698 | ||
694 | use foo::Foo; | 699 | use foo::Foo; |
695 | 700 | ||
@@ -726,7 +731,7 @@ fn f() { | |||
726 | } | 731 | } |
727 | 732 | ||
728 | //- /foo/some.rs | 733 | //- /foo/some.rs |
729 | pub(super) struct Foo<|> { | 734 | pub(super) struct Foo$0 { |
730 | pub n: u32, | 735 | pub n: u32, |
731 | } | 736 | } |
732 | "#, | 737 | "#, |
@@ -746,7 +751,7 @@ pub(super) struct Foo<|> { | |||
746 | mod foo; | 751 | mod foo; |
747 | mod bar; | 752 | mod bar; |
748 | 753 | ||
749 | pub fn quux<|>() {} | 754 | pub fn quux$0() {} |
750 | 755 | ||
751 | //- /foo.rs | 756 | //- /foo.rs |
752 | fn f() { super::quux(); } | 757 | fn f() { super::quux(); } |
@@ -782,7 +787,7 @@ pub(super) struct Foo<|> { | |||
782 | check( | 787 | check( |
783 | r#" | 788 | r#" |
784 | #[macro_export] | 789 | #[macro_export] |
785 | macro_rules! m1<|> { () => (()) } | 790 | macro_rules! m1$0 { () => (()) } |
786 | 791 | ||
787 | fn foo() { | 792 | fn foo() { |
788 | m1(); | 793 | m1(); |
@@ -803,7 +808,7 @@ fn foo() { | |||
803 | check( | 808 | check( |
804 | r#" | 809 | r#" |
805 | fn foo() { | 810 | fn foo() { |
806 | let mut i<|> = 0; | 811 | let mut i$0 = 0; |
807 | i = i + 1; | 812 | i = i + 1; |
808 | } | 813 | } |
809 | "#, | 814 | "#, |
@@ -826,7 +831,7 @@ struct S { | |||
826 | 831 | ||
827 | fn foo() { | 832 | fn foo() { |
828 | let mut s = S{f: 0}; | 833 | let mut s = S{f: 0}; |
829 | s.f<|> = 0; | 834 | s.f$0 = 0; |
830 | } | 835 | } |
831 | "#, | 836 | "#, |
832 | expect![[r#" | 837 | expect![[r#" |
@@ -843,7 +848,7 @@ fn foo() { | |||
843 | check( | 848 | check( |
844 | r#" | 849 | r#" |
845 | fn foo() { | 850 | fn foo() { |
846 | let i<|>; | 851 | let i$0; |
847 | i = 1; | 852 | i = 1; |
848 | } | 853 | } |
849 | "#, | 854 | "#, |
@@ -863,7 +868,7 @@ mod foo { | |||
863 | pub struct Foo; | 868 | pub struct Foo; |
864 | 869 | ||
865 | impl Foo { | 870 | impl Foo { |
866 | pub fn new<|>() -> Foo { Foo } | 871 | pub fn new$0() -> Foo { Foo } |
867 | } | 872 | } |
868 | } | 873 | } |
869 | 874 | ||
@@ -886,7 +891,7 @@ fn main() { | |||
886 | //- /lib.rs | 891 | //- /lib.rs |
887 | mod foo { mod bar; } | 892 | mod foo { mod bar; } |
888 | 893 | ||
889 | fn f<|>() {} | 894 | fn f$0() {} |
890 | 895 | ||
891 | //- /foo/bar.rs | 896 | //- /foo/bar.rs |
892 | use crate::f; | 897 | use crate::f; |
@@ -907,7 +912,7 @@ fn g() { f(); } | |||
907 | check( | 912 | check( |
908 | r#" | 913 | r#" |
909 | struct S { | 914 | struct S { |
910 | field<|>: u8, | 915 | field$0: u8, |
911 | } | 916 | } |
912 | 917 | ||
913 | fn f(s: S) { | 918 | fn f(s: S) { |
@@ -930,7 +935,7 @@ fn f(s: S) { | |||
930 | r#" | 935 | r#" |
931 | enum En { | 936 | enum En { |
932 | Variant { | 937 | Variant { |
933 | field<|>: u8, | 938 | field$0: u8, |
934 | } | 939 | } |
935 | } | 940 | } |
936 | 941 | ||
@@ -955,7 +960,7 @@ fn f(e: En) { | |||
955 | mod m { | 960 | mod m { |
956 | pub enum En { | 961 | pub enum En { |
957 | Variant { | 962 | Variant { |
958 | field<|>: u8, | 963 | field$0: u8, |
959 | } | 964 | } |
960 | } | 965 | } |
961 | } | 966 | } |
@@ -980,7 +985,7 @@ struct Foo { bar: i32 } | |||
980 | 985 | ||
981 | impl Foo { | 986 | impl Foo { |
982 | fn foo(self) { | 987 | fn foo(self) { |
983 | let x = self<|>.bar; | 988 | let x = self$0.bar; |
984 | if true { | 989 | if true { |
985 | let _ = match () { | 990 | let _ = match () { |
986 | () => self, | 991 | () => self, |
@@ -1016,12 +1021,14 @@ impl Foo { | |||
1016 | actual += "\n\n"; | 1021 | actual += "\n\n"; |
1017 | } | 1022 | } |
1018 | 1023 | ||
1019 | for r in &refs.references { | 1024 | for (file_id, references) in refs.references { |
1020 | format_to!(actual, "{:?} {:?} {:?}", r.file_range.file_id, r.file_range.range, r.kind); | 1025 | for r in references { |
1021 | if let Some(access) = r.access { | 1026 | format_to!(actual, "{:?} {:?} {:?}", file_id, r.range, r.kind); |
1022 | format_to!(actual, " {:?}", access); | 1027 | if let Some(access) = r.access { |
1028 | format_to!(actual, " {:?}", access); | ||
1029 | } | ||
1030 | actual += "\n"; | ||
1023 | } | 1031 | } |
1024 | actual += "\n"; | ||
1025 | } | 1032 | } |
1026 | expect.assert_eq(&actual) | 1033 | expect.assert_eq(&actual) |
1027 | } | 1034 | } |
@@ -1032,7 +1039,7 @@ impl Foo { | |||
1032 | r#" | 1039 | r#" |
1033 | trait Foo<'a> {} | 1040 | trait Foo<'a> {} |
1034 | impl<'a> Foo<'a> for &'a () {} | 1041 | impl<'a> Foo<'a> for &'a () {} |
1035 | fn foo<'a, 'b: 'a>(x: &'a<|> ()) -> &'a () where &'a (): Foo<'a> { | 1042 | fn foo<'a, 'b: 'a>(x: &'a$0 ()) -> &'a () where &'a (): Foo<'a> { |
1036 | fn bar<'a>(_: &'a ()) {} | 1043 | fn bar<'a>(_: &'a ()) {} |
1037 | x | 1044 | x |
1038 | } | 1045 | } |
@@ -1053,7 +1060,7 @@ fn foo<'a, 'b: 'a>(x: &'a<|> ()) -> &'a () where &'a (): Foo<'a> { | |||
1053 | fn test_find_lifetimes_type_alias() { | 1060 | fn test_find_lifetimes_type_alias() { |
1054 | check( | 1061 | check( |
1055 | r#" | 1062 | r#" |
1056 | type Foo<'a, T> where T: 'a<|> = &'a T; | 1063 | type Foo<'a, T> where T: 'a$0 = &'a T; |
1057 | "#, | 1064 | "#, |
1058 | expect![[r#" | 1065 | expect![[r#" |
1059 | 'a LifetimeParam FileId(0) 9..11 9..11 Lifetime | 1066 | 'a LifetimeParam FileId(0) 9..11 9..11 Lifetime |
@@ -1072,7 +1079,7 @@ trait Foo<'a> { | |||
1072 | fn foo() -> &'a (); | 1079 | fn foo() -> &'a (); |
1073 | } | 1080 | } |
1074 | impl<'a> Foo<'a> for &'a () { | 1081 | impl<'a> Foo<'a> for &'a () { |
1075 | fn foo() -> &'a<|> () { | 1082 | fn foo() -> &'a$0 () { |
1076 | unimplemented!() | 1083 | unimplemented!() |
1077 | } | 1084 | } |
1078 | } | 1085 | } |
@@ -1093,7 +1100,7 @@ impl<'a> Foo<'a> for &'a () { | |||
1093 | r#" | 1100 | r#" |
1094 | macro_rules! foo {($i:ident) => {$i} } | 1101 | macro_rules! foo {($i:ident) => {$i} } |
1095 | fn main() { | 1102 | fn main() { |
1096 | let a<|> = "test"; | 1103 | let a$0 = "test"; |
1097 | foo!(a); | 1104 | foo!(a); |
1098 | } | 1105 | } |
1099 | "#, | 1106 | "#, |
@@ -1112,7 +1119,7 @@ fn main() { | |||
1112 | macro_rules! foo {($i:ident) => {$i} } | 1119 | macro_rules! foo {($i:ident) => {$i} } |
1113 | fn main() { | 1120 | fn main() { |
1114 | let a = "test"; | 1121 | let a = "test"; |
1115 | foo!(a<|>); | 1122 | foo!(a$0); |
1116 | } | 1123 | } |
1117 | "#, | 1124 | "#, |
1118 | expect![[r#" | 1125 | expect![[r#" |
@@ -1130,7 +1137,7 @@ fn main() { | |||
1130 | fn foo<'a>() -> &'a () { | 1137 | fn foo<'a>() -> &'a () { |
1131 | 'a: loop { | 1138 | 'a: loop { |
1132 | 'b: loop { | 1139 | 'b: loop { |
1133 | continue 'a<|>; | 1140 | continue 'a$0; |
1134 | } | 1141 | } |
1135 | break 'a; | 1142 | break 'a; |
1136 | } | 1143 | } |
@@ -1144,4 +1151,20 @@ fn foo<'a>() -> &'a () { | |||
1144 | "#]], | 1151 | "#]], |
1145 | ); | 1152 | ); |
1146 | } | 1153 | } |
1154 | |||
1155 | #[test] | ||
1156 | fn test_find_const_param() { | ||
1157 | check( | ||
1158 | r#" | ||
1159 | fn foo<const FOO$0: usize>() -> usize { | ||
1160 | FOO | ||
1161 | } | ||
1162 | "#, | ||
1163 | expect![[r#" | ||
1164 | FOO ConstParam FileId(0) 7..23 13..16 Other | ||
1165 | |||
1166 | FileId(0) 42..45 Other | ||
1167 | "#]], | ||
1168 | ); | ||
1169 | } | ||
1147 | } | 1170 | } |
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index 854bf194e..c3ae568c2 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs | |||
@@ -1,29 +1,30 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | use std::{ | 2 | use std::{ |
3 | convert::TryInto, | 3 | convert::TryInto, |
4 | error::Error, | ||
5 | fmt::{self, Display}, | 4 | fmt::{self, Display}, |
6 | }; | 5 | }; |
7 | 6 | ||
8 | use hir::{Module, ModuleDef, ModuleSource, Semantics}; | 7 | use hir::{Module, ModuleDef, ModuleSource, Semantics}; |
9 | use ide_db::base_db::{AnchoredPathBuf, FileId, FileRange, SourceDatabaseExt}; | ||
10 | use ide_db::{ | 8 | use ide_db::{ |
9 | base_db::{AnchoredPathBuf, FileId, FileRange, SourceDatabaseExt}, | ||
11 | defs::{Definition, NameClass, NameRefClass}, | 10 | defs::{Definition, NameClass, NameRefClass}, |
11 | search::FileReference, | ||
12 | RootDatabase, | 12 | RootDatabase, |
13 | }; | 13 | }; |
14 | use syntax::{ | 14 | use syntax::{ |
15 | algo::find_node_at_offset, | 15 | algo::find_node_at_offset, |
16 | ast::{self, NameOwner}, | 16 | ast::{self, NameOwner}, |
17 | lex_single_syntax_kind, match_ast, AstNode, SyntaxKind, SyntaxNode, SyntaxToken, | 17 | lex_single_syntax_kind, match_ast, AstNode, SyntaxKind, SyntaxNode, SyntaxToken, T, |
18 | }; | 18 | }; |
19 | use test_utils::mark; | 19 | use test_utils::mark; |
20 | use text_edit::TextEdit; | 20 | use text_edit::TextEdit; |
21 | 21 | ||
22 | use crate::{ | 22 | use crate::{ |
23 | references::find_all_refs, FilePosition, FileSystemEdit, RangeInfo, Reference, ReferenceKind, | 23 | FilePosition, FileSystemEdit, RangeInfo, ReferenceKind, ReferenceSearchResult, SourceChange, |
24 | SourceChange, SourceFileEdit, TextRange, TextSize, | 24 | SourceFileEdit, TextRange, TextSize, |
25 | }; | 25 | }; |
26 | 26 | ||
27 | type RenameResult<T> = Result<T, RenameError>; | ||
27 | #[derive(Debug)] | 28 | #[derive(Debug)] |
28 | pub struct RenameError(pub(crate) String); | 29 | pub struct RenameError(pub(crate) String); |
29 | 30 | ||
@@ -33,26 +34,30 @@ impl fmt::Display for RenameError { | |||
33 | } | 34 | } |
34 | } | 35 | } |
35 | 36 | ||
36 | impl Error for RenameError {} | 37 | macro_rules! format_err { |
38 | ($fmt:expr) => {RenameError(format!($fmt))}; | ||
39 | ($fmt:expr, $($arg:tt)+) => {RenameError(format!($fmt, $($arg)+))} | ||
40 | } | ||
41 | |||
42 | macro_rules! bail { | ||
43 | ($($tokens:tt)*) => {return Err(format_err!($($tokens)*))} | ||
44 | } | ||
37 | 45 | ||
38 | pub(crate) fn prepare_rename( | 46 | pub(crate) fn prepare_rename( |
39 | db: &RootDatabase, | 47 | db: &RootDatabase, |
40 | position: FilePosition, | 48 | position: FilePosition, |
41 | ) -> Result<RangeInfo<()>, RenameError> { | 49 | ) -> RenameResult<RangeInfo<()>> { |
42 | let sema = Semantics::new(db); | 50 | let sema = Semantics::new(db); |
43 | let source_file = sema.parse(position.file_id); | 51 | let source_file = sema.parse(position.file_id); |
44 | let syntax = source_file.syntax(); | 52 | let syntax = source_file.syntax(); |
45 | if let Some(module) = find_module_at_offset(&sema, position, syntax) { | 53 | if let Some(module) = find_module_at_offset(&sema, position, syntax) { |
46 | rename_mod(&sema, position, module, "dummy") | 54 | rename_mod(&sema, position, module, "dummy") |
47 | } else if let Some(self_token) = | 55 | } else if let Some(self_token) = |
48 | syntax.token_at_offset(position.offset).find(|t| t.kind() == SyntaxKind::SELF_KW) | 56 | syntax.token_at_offset(position.offset).find(|t| t.kind() == T![self]) |
49 | { | 57 | { |
50 | rename_self_to_param(&sema, position, self_token, "dummy") | 58 | rename_self_to_param(&sema, position, self_token, "dummy") |
51 | } else { | 59 | } else { |
52 | let range = match find_all_refs(&sema, position, None) { | 60 | let RangeInfo { range, .. } = find_all_refs(&sema, position)?; |
53 | Some(RangeInfo { range, .. }) => range, | ||
54 | None => return Err(RenameError("No references found at position".to_string())), | ||
55 | }; | ||
56 | Ok(RangeInfo::new(range, SourceChange::from(vec![]))) | 61 | Ok(RangeInfo::new(range, SourceChange::from(vec![]))) |
57 | } | 62 | } |
58 | .map(|info| RangeInfo::new(info.range, ())) | 63 | .map(|info| RangeInfo::new(info.range, ())) |
@@ -62,7 +67,7 @@ pub(crate) fn rename( | |||
62 | db: &RootDatabase, | 67 | db: &RootDatabase, |
63 | position: FilePosition, | 68 | position: FilePosition, |
64 | new_name: &str, | 69 | new_name: &str, |
65 | ) -> Result<RangeInfo<SourceChange>, RenameError> { | 70 | ) -> RenameResult<RangeInfo<SourceChange>> { |
66 | let sema = Semantics::new(db); | 71 | let sema = Semantics::new(db); |
67 | rename_with_semantics(&sema, position, new_name) | 72 | rename_with_semantics(&sema, position, new_name) |
68 | } | 73 | } |
@@ -71,42 +76,18 @@ pub(crate) fn rename_with_semantics( | |||
71 | sema: &Semantics<RootDatabase>, | 76 | sema: &Semantics<RootDatabase>, |
72 | position: FilePosition, | 77 | position: FilePosition, |
73 | new_name: &str, | 78 | new_name: &str, |
74 | ) -> Result<RangeInfo<SourceChange>, RenameError> { | 79 | ) -> RenameResult<RangeInfo<SourceChange>> { |
75 | let is_lifetime_name = match lex_single_syntax_kind(new_name) { | ||
76 | Some(res) => match res { | ||
77 | (SyntaxKind::IDENT, _) => false, | ||
78 | (SyntaxKind::UNDERSCORE, _) => false, | ||
79 | (SyntaxKind::SELF_KW, _) => return rename_to_self(&sema, position), | ||
80 | (SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => true, | ||
81 | (SyntaxKind::LIFETIME_IDENT, _) => { | ||
82 | return Err(RenameError(format!( | ||
83 | "Invalid name `{0}`: Cannot rename lifetime to {0}", | ||
84 | new_name | ||
85 | ))) | ||
86 | } | ||
87 | (_, Some(syntax_error)) => { | ||
88 | return Err(RenameError(format!("Invalid name `{}`: {}", new_name, syntax_error))) | ||
89 | } | ||
90 | (_, None) => { | ||
91 | return Err(RenameError(format!("Invalid name `{}`: not an identifier", new_name))) | ||
92 | } | ||
93 | }, | ||
94 | None => return Err(RenameError(format!("Invalid name `{}`: not an identifier", new_name))), | ||
95 | }; | ||
96 | |||
97 | let source_file = sema.parse(position.file_id); | 80 | let source_file = sema.parse(position.file_id); |
98 | let syntax = source_file.syntax(); | 81 | let syntax = source_file.syntax(); |
99 | // this is here to prevent lifetime renames from happening on modules and self | 82 | |
100 | if is_lifetime_name { | 83 | if let Some(module) = find_module_at_offset(&sema, position, syntax) { |
101 | rename_reference(&sema, position, new_name, is_lifetime_name) | ||
102 | } else if let Some(module) = find_module_at_offset(&sema, position, syntax) { | ||
103 | rename_mod(&sema, position, module, new_name) | 84 | rename_mod(&sema, position, module, new_name) |
104 | } else if let Some(self_token) = | 85 | } else if let Some(self_token) = |
105 | syntax.token_at_offset(position.offset).find(|t| t.kind() == SyntaxKind::SELF_KW) | 86 | syntax.token_at_offset(position.offset).find(|t| t.kind() == T![self]) |
106 | { | 87 | { |
107 | rename_self_to_param(&sema, position, self_token, new_name) | 88 | rename_self_to_param(&sema, position, self_token, new_name) |
108 | } else { | 89 | } else { |
109 | rename_reference(&sema, position, new_name, is_lifetime_name) | 90 | rename_reference(&sema, position, new_name) |
110 | } | 91 | } |
111 | } | 92 | } |
112 | 93 | ||
@@ -127,6 +108,33 @@ pub(crate) fn will_rename_file( | |||
127 | Some(change) | 108 | Some(change) |
128 | } | 109 | } |
129 | 110 | ||
111 | #[derive(Debug, PartialEq)] | ||
112 | enum IdentifierKind { | ||
113 | Ident, | ||
114 | Lifetime, | ||
115 | ToSelf, | ||
116 | Underscore, | ||
117 | } | ||
118 | |||
119 | fn check_identifier(new_name: &str) -> RenameResult<IdentifierKind> { | ||
120 | match lex_single_syntax_kind(new_name) { | ||
121 | Some(res) => match res { | ||
122 | (SyntaxKind::IDENT, _) => Ok(IdentifierKind::Ident), | ||
123 | (T![_], _) => Ok(IdentifierKind::Underscore), | ||
124 | (T![self], _) => Ok(IdentifierKind::ToSelf), | ||
125 | (SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => { | ||
126 | Ok(IdentifierKind::Lifetime) | ||
127 | } | ||
128 | (SyntaxKind::LIFETIME_IDENT, _) => { | ||
129 | bail!("Invalid name `{0}`: Cannot rename lifetime to {0}", new_name) | ||
130 | } | ||
131 | (_, Some(syntax_error)) => bail!("Invalid name `{}`: {}", new_name, syntax_error), | ||
132 | (_, None) => bail!("Invalid name `{}`: not an identifier", new_name), | ||
133 | }, | ||
134 | None => bail!("Invalid name `{}`: not an identifier", new_name), | ||
135 | } | ||
136 | } | ||
137 | |||
130 | fn find_module_at_offset( | 138 | fn find_module_at_offset( |
131 | sema: &Semantics<RootDatabase>, | 139 | sema: &Semantics<RootDatabase>, |
132 | position: FilePosition, | 140 | position: FilePosition, |
@@ -155,39 +163,54 @@ fn find_module_at_offset( | |||
155 | Some(module) | 163 | Some(module) |
156 | } | 164 | } |
157 | 165 | ||
158 | fn source_edit_from_reference( | 166 | fn find_all_refs( |
167 | sema: &Semantics<RootDatabase>, | ||
168 | position: FilePosition, | ||
169 | ) -> RenameResult<RangeInfo<ReferenceSearchResult>> { | ||
170 | crate::references::find_all_refs(sema, position, None) | ||
171 | .ok_or_else(|| format_err!("No references found at position")) | ||
172 | } | ||
173 | |||
174 | fn source_edit_from_references( | ||
159 | sema: &Semantics<RootDatabase>, | 175 | sema: &Semantics<RootDatabase>, |
160 | reference: Reference, | 176 | file_id: FileId, |
177 | references: &[FileReference], | ||
161 | new_name: &str, | 178 | new_name: &str, |
162 | ) -> SourceFileEdit { | 179 | ) -> SourceFileEdit { |
163 | let mut replacement_text = String::new(); | 180 | let mut edit = TextEdit::builder(); |
164 | let range = match reference.kind { | 181 | for reference in references { |
165 | ReferenceKind::FieldShorthandForField => { | 182 | let mut replacement_text = String::new(); |
166 | mark::hit!(test_rename_struct_field_for_shorthand); | 183 | let range = match reference.kind { |
167 | replacement_text.push_str(new_name); | 184 | ReferenceKind::FieldShorthandForField => { |
168 | replacement_text.push_str(": "); | 185 | mark::hit!(test_rename_struct_field_for_shorthand); |
169 | TextRange::new(reference.file_range.range.start(), reference.file_range.range.start()) | 186 | replacement_text.push_str(new_name); |
170 | } | 187 | replacement_text.push_str(": "); |
171 | ReferenceKind::FieldShorthandForLocal => { | 188 | TextRange::new(reference.range.start(), reference.range.start()) |
172 | mark::hit!(test_rename_local_for_field_shorthand); | 189 | } |
173 | replacement_text.push_str(": "); | 190 | ReferenceKind::FieldShorthandForLocal => { |
174 | replacement_text.push_str(new_name); | 191 | mark::hit!(test_rename_local_for_field_shorthand); |
175 | TextRange::new(reference.file_range.range.end(), reference.file_range.range.end()) | 192 | replacement_text.push_str(": "); |
176 | } | 193 | replacement_text.push_str(new_name); |
177 | ReferenceKind::RecordFieldExprOrPat => { | 194 | TextRange::new(reference.range.end(), reference.range.end()) |
178 | mark::hit!(test_rename_field_expr_pat); | 195 | } |
179 | replacement_text.push_str(new_name); | 196 | ReferenceKind::RecordFieldExprOrPat => { |
180 | edit_text_range_for_record_field_expr_or_pat(sema, reference.file_range, new_name) | 197 | mark::hit!(test_rename_field_expr_pat); |
181 | } | 198 | replacement_text.push_str(new_name); |
182 | _ => { | 199 | edit_text_range_for_record_field_expr_or_pat( |
183 | replacement_text.push_str(new_name); | 200 | sema, |
184 | reference.file_range.range | 201 | FileRange { file_id, range: reference.range }, |
185 | } | 202 | new_name, |
186 | }; | 203 | ) |
187 | SourceFileEdit { | 204 | } |
188 | file_id: reference.file_range.file_id, | 205 | _ => { |
189 | edit: TextEdit::replace(range, replacement_text), | 206 | replacement_text.push_str(new_name); |
207 | reference.range | ||
208 | } | ||
209 | }; | ||
210 | edit.replace(range, replacement_text); | ||
190 | } | 211 | } |
212 | |||
213 | SourceFileEdit { file_id, edit: edit.finish() } | ||
191 | } | 214 | } |
192 | 215 | ||
193 | fn edit_text_range_for_record_field_expr_or_pat( | 216 | fn edit_text_range_for_record_field_expr_or_pat( |
@@ -223,7 +246,10 @@ fn rename_mod( | |||
223 | position: FilePosition, | 246 | position: FilePosition, |
224 | module: Module, | 247 | module: Module, |
225 | new_name: &str, | 248 | new_name: &str, |
226 | ) -> Result<RangeInfo<SourceChange>, RenameError> { | 249 | ) -> RenameResult<RangeInfo<SourceChange>> { |
250 | if IdentifierKind::Ident != check_identifier(new_name)? { | ||
251 | bail!("Invalid name `{0}`: cannot rename module to {0}", new_name); | ||
252 | } | ||
227 | let mut source_file_edits = Vec::new(); | 253 | let mut source_file_edits = Vec::new(); |
228 | let mut file_system_edits = Vec::new(); | 254 | let mut file_system_edits = Vec::new(); |
229 | 255 | ||
@@ -254,12 +280,10 @@ fn rename_mod( | |||
254 | source_file_edits.push(edit); | 280 | source_file_edits.push(edit); |
255 | } | 281 | } |
256 | 282 | ||
257 | let RangeInfo { range, info: refs } = find_all_refs(sema, position, None) | 283 | let RangeInfo { range, info: refs } = find_all_refs(sema, position)?; |
258 | .ok_or_else(|| RenameError("No references found at position".to_string()))?; | 284 | let ref_edits = refs.references().iter().map(|(&file_id, references)| { |
259 | let ref_edits = refs | 285 | source_edit_from_references(sema, file_id, references, new_name) |
260 | .references | 286 | }); |
261 | .into_iter() | ||
262 | .map(|reference| source_edit_from_reference(sema, reference, new_name)); | ||
263 | source_file_edits.extend(ref_edits); | 287 | source_file_edits.extend(ref_edits); |
264 | 288 | ||
265 | Ok(RangeInfo::new(range, SourceChange::from_edits(source_file_edits, file_system_edits))) | 289 | Ok(RangeInfo::new(range, SourceChange::from_edits(source_file_edits, file_system_edits))) |
@@ -274,27 +298,26 @@ fn rename_to_self( | |||
274 | 298 | ||
275 | let (fn_def, fn_ast) = find_node_at_offset::<ast::Fn>(syn, position.offset) | 299 | let (fn_def, fn_ast) = find_node_at_offset::<ast::Fn>(syn, position.offset) |
276 | .and_then(|fn_ast| sema.to_def(&fn_ast).zip(Some(fn_ast))) | 300 | .and_then(|fn_ast| sema.to_def(&fn_ast).zip(Some(fn_ast))) |
277 | .ok_or_else(|| RenameError("No surrounding method declaration found".to_string()))?; | 301 | .ok_or_else(|| format_err!("No surrounding method declaration found"))?; |
278 | let param_range = fn_ast | 302 | let param_range = fn_ast |
279 | .param_list() | 303 | .param_list() |
280 | .and_then(|p| p.params().next()) | 304 | .and_then(|p| p.params().next()) |
281 | .ok_or_else(|| RenameError("Method has no parameters".to_string()))? | 305 | .ok_or_else(|| format_err!("Method has no parameters"))? |
282 | .syntax() | 306 | .syntax() |
283 | .text_range(); | 307 | .text_range(); |
284 | if !param_range.contains(position.offset) { | 308 | if !param_range.contains(position.offset) { |
285 | return Err(RenameError("Only the first parameter can be self".to_string())); | 309 | bail!("Only the first parameter can be self"); |
286 | } | 310 | } |
287 | 311 | ||
288 | let impl_block = find_node_at_offset::<ast::Impl>(syn, position.offset) | 312 | let impl_block = find_node_at_offset::<ast::Impl>(syn, position.offset) |
289 | .and_then(|def| sema.to_def(&def)) | 313 | .and_then(|def| sema.to_def(&def)) |
290 | .ok_or_else(|| RenameError("No impl block found for function".to_string()))?; | 314 | .ok_or_else(|| format_err!("No impl block found for function"))?; |
291 | if fn_def.self_param(sema.db).is_some() { | 315 | if fn_def.self_param(sema.db).is_some() { |
292 | return Err(RenameError("Method already has a self parameter".to_string())); | 316 | bail!("Method already has a self parameter"); |
293 | } | 317 | } |
294 | 318 | ||
295 | let params = fn_def.assoc_fn_params(sema.db); | 319 | let params = fn_def.assoc_fn_params(sema.db); |
296 | let first_param = | 320 | let first_param = params.first().ok_or_else(|| format_err!("Method has no parameters"))?; |
297 | params.first().ok_or_else(|| RenameError("Method has no parameters".into()))?; | ||
298 | let first_param_ty = first_param.ty(); | 321 | let first_param_ty = first_param.ty(); |
299 | let impl_ty = impl_block.target_ty(sema.db); | 322 | let impl_ty = impl_block.target_ty(sema.db); |
300 | let (ty, self_param) = if impl_ty.remove_ref().is_some() { | 323 | let (ty, self_param) = if impl_ty.remove_ref().is_some() { |
@@ -307,23 +330,17 @@ fn rename_to_self( | |||
307 | }; | 330 | }; |
308 | 331 | ||
309 | if ty != impl_ty { | 332 | if ty != impl_ty { |
310 | return Err(RenameError("Parameter type differs from impl block type".to_string())); | 333 | bail!("Parameter type differs from impl block type"); |
311 | } | 334 | } |
312 | 335 | ||
313 | let RangeInfo { range, info: refs } = find_all_refs(sema, position, None) | 336 | let RangeInfo { range, info: refs } = find_all_refs(sema, position)?; |
314 | .ok_or_else(|| RenameError("No reference found at position".to_string()))?; | ||
315 | |||
316 | let (param_ref, usages): (Vec<Reference>, Vec<Reference>) = refs | ||
317 | .into_iter() | ||
318 | .partition(|reference| param_range.intersect(reference.file_range.range).is_some()); | ||
319 | |||
320 | if param_ref.is_empty() { | ||
321 | return Err(RenameError("Parameter to rename not found".to_string())); | ||
322 | } | ||
323 | 337 | ||
324 | let mut edits = usages | 338 | let mut edits = refs |
325 | .into_iter() | 339 | .references() |
326 | .map(|reference| source_edit_from_reference(sema, reference, "self")) | 340 | .iter() |
341 | .map(|(&file_id, references)| { | ||
342 | source_edit_from_references(sema, file_id, references, "self") | ||
343 | }) | ||
327 | .collect::<Vec<_>>(); | 344 | .collect::<Vec<_>>(); |
328 | 345 | ||
329 | edits.push(SourceFileEdit { | 346 | edits.push(SourceFileEdit { |
@@ -367,12 +384,22 @@ fn rename_self_to_param( | |||
367 | self_token: SyntaxToken, | 384 | self_token: SyntaxToken, |
368 | new_name: &str, | 385 | new_name: &str, |
369 | ) -> Result<RangeInfo<SourceChange>, RenameError> { | 386 | ) -> Result<RangeInfo<SourceChange>, RenameError> { |
387 | let ident_kind = check_identifier(new_name)?; | ||
388 | match ident_kind { | ||
389 | IdentifierKind::Lifetime => bail!("Invalid name `{}`: not an identifier", new_name), | ||
390 | IdentifierKind::ToSelf => { | ||
391 | // no-op | ||
392 | mark::hit!(rename_self_to_self); | ||
393 | return Ok(RangeInfo::new(self_token.text_range(), SourceChange::default())); | ||
394 | } | ||
395 | _ => (), | ||
396 | } | ||
370 | let source_file = sema.parse(position.file_id); | 397 | let source_file = sema.parse(position.file_id); |
371 | let syn = source_file.syntax(); | 398 | let syn = source_file.syntax(); |
372 | 399 | ||
373 | let text = sema.db.file_text(position.file_id); | 400 | let text = sema.db.file_text(position.file_id); |
374 | let fn_def = find_node_at_offset::<ast::Fn>(syn, position.offset) | 401 | let fn_def = find_node_at_offset::<ast::Fn>(syn, position.offset) |
375 | .ok_or_else(|| RenameError("No surrounding method declaration found".to_string()))?; | 402 | .ok_or_else(|| format_err!("No surrounding method declaration found"))?; |
376 | let search_range = fn_def.syntax().text_range(); | 403 | let search_range = fn_def.syntax().text_range(); |
377 | 404 | ||
378 | let mut edits: Vec<SourceFileEdit> = vec![]; | 405 | let mut edits: Vec<SourceFileEdit> = vec![]; |
@@ -382,12 +409,10 @@ fn rename_self_to_param( | |||
382 | if !search_range.contains_inclusive(offset) { | 409 | if !search_range.contains_inclusive(offset) { |
383 | continue; | 410 | continue; |
384 | } | 411 | } |
385 | if let Some(ref usage) = | 412 | if let Some(ref usage) = syn.token_at_offset(offset).find(|t| t.kind() == T![self]) { |
386 | syn.token_at_offset(offset).find(|t| t.kind() == SyntaxKind::SELF_KW) | ||
387 | { | ||
388 | let edit = if let Some(ref self_param) = ast::SelfParam::cast(usage.parent()) { | 413 | let edit = if let Some(ref self_param) = ast::SelfParam::cast(usage.parent()) { |
389 | text_edit_from_self_param(syn, self_param, new_name) | 414 | text_edit_from_self_param(syn, self_param, new_name) |
390 | .ok_or_else(|| RenameError("No target type found".to_string()))? | 415 | .ok_or_else(|| format_err!("No target type found"))? |
391 | } else { | 416 | } else { |
392 | TextEdit::replace(usage.text_range(), String::from(new_name)) | 417 | TextEdit::replace(usage.text_range(), String::from(new_name)) |
393 | }; | 418 | }; |
@@ -395,6 +420,10 @@ fn rename_self_to_param( | |||
395 | } | 420 | } |
396 | } | 421 | } |
397 | 422 | ||
423 | if edits.len() > 1 && ident_kind == IdentifierKind::Underscore { | ||
424 | bail!("Cannot rename reference to `_` as it is being referenced multiple times"); | ||
425 | } | ||
426 | |||
398 | let range = ast::SelfParam::cast(self_token.parent()) | 427 | let range = ast::SelfParam::cast(self_token.parent()) |
399 | .map_or(self_token.text_range(), |p| p.syntax().text_range()); | 428 | .map_or(self_token.text_range(), |p| p.syntax().text_range()); |
400 | 429 | ||
@@ -405,35 +434,43 @@ fn rename_reference( | |||
405 | sema: &Semantics<RootDatabase>, | 434 | sema: &Semantics<RootDatabase>, |
406 | position: FilePosition, | 435 | position: FilePosition, |
407 | new_name: &str, | 436 | new_name: &str, |
408 | is_lifetime_name: bool, | ||
409 | ) -> Result<RangeInfo<SourceChange>, RenameError> { | 437 | ) -> Result<RangeInfo<SourceChange>, RenameError> { |
410 | let RangeInfo { range, info: refs } = match find_all_refs(sema, position, None) { | 438 | let ident_kind = check_identifier(new_name)?; |
411 | Some(range_info) => range_info, | 439 | let RangeInfo { range, info: refs } = find_all_refs(sema, position)?; |
412 | None => return Err(RenameError("No references found at position".to_string())), | 440 | |
413 | }; | 441 | match (ident_kind, &refs.declaration.kind) { |
414 | 442 | (IdentifierKind::ToSelf, ReferenceKind::Lifetime) | |
415 | match (refs.declaration.kind == ReferenceKind::Lifetime, is_lifetime_name) { | 443 | | (IdentifierKind::Underscore, ReferenceKind::Lifetime) |
416 | (true, false) => { | 444 | | (IdentifierKind::Ident, ReferenceKind::Lifetime) => { |
417 | return Err(RenameError(format!( | 445 | mark::hit!(rename_not_a_lifetime_ident_ref); |
418 | "Invalid name `{}`: not a lifetime identifier", | 446 | bail!("Invalid name `{}`: not a lifetime identifier", new_name) |
419 | new_name | ||
420 | ))) | ||
421 | } | 447 | } |
422 | (false, true) => { | 448 | (IdentifierKind::Lifetime, ReferenceKind::Lifetime) => mark::hit!(rename_lifetime), |
423 | return Err(RenameError(format!("Invalid name `{}`: not an identifier", new_name))) | 449 | (IdentifierKind::Lifetime, _) => { |
450 | mark::hit!(rename_not_an_ident_ref); | ||
451 | bail!("Invalid name `{}`: not an identifier", new_name) | ||
424 | } | 452 | } |
425 | _ => (), | 453 | (IdentifierKind::ToSelf, ReferenceKind::SelfKw) => { |
454 | unreachable!("rename_self_to_param should've been called instead") | ||
455 | } | ||
456 | (IdentifierKind::ToSelf, _) => { | ||
457 | mark::hit!(rename_to_self); | ||
458 | return rename_to_self(sema, position); | ||
459 | } | ||
460 | (IdentifierKind::Underscore, _) if !refs.references.is_empty() => { | ||
461 | mark::hit!(rename_underscore_multiple); | ||
462 | bail!("Cannot rename reference to `_` as it is being referenced multiple times") | ||
463 | } | ||
464 | (IdentifierKind::Ident, _) | (IdentifierKind::Underscore, _) => mark::hit!(rename_ident), | ||
426 | } | 465 | } |
427 | 466 | ||
428 | let edit = refs | 467 | let edit = refs |
429 | .into_iter() | 468 | .into_iter() |
430 | .map(|reference| source_edit_from_reference(sema, reference, new_name)) | 469 | .map(|(file_id, references)| { |
470 | source_edit_from_references(sema, file_id, &references, new_name) | ||
471 | }) | ||
431 | .collect::<Vec<_>>(); | 472 | .collect::<Vec<_>>(); |
432 | 473 | ||
433 | if edit.is_empty() { | ||
434 | return Err(RenameError("No references found at position".to_string())); | ||
435 | } | ||
436 | |||
437 | Ok(RangeInfo::new(range, SourceChange::from(edit))) | 474 | Ok(RangeInfo::new(range, SourceChange::from(edit))) |
438 | } | 475 | } |
439 | 476 | ||
@@ -462,9 +499,11 @@ mod tests { | |||
462 | text_edit_builder.replace(indel.delete, indel.insert); | 499 | text_edit_builder.replace(indel.delete, indel.insert); |
463 | } | 500 | } |
464 | } | 501 | } |
465 | let mut result = analysis.file_text(file_id.unwrap()).unwrap().to_string(); | 502 | if let Some(file_id) = file_id { |
466 | text_edit_builder.finish().apply(&mut result); | 503 | let mut result = analysis.file_text(file_id).unwrap().to_string(); |
467 | assert_eq_text!(ra_fixture_after, &*result); | 504 | text_edit_builder.finish().apply(&mut result); |
505 | assert_eq_text!(ra_fixture_after, &*result); | ||
506 | } | ||
468 | } | 507 | } |
469 | Err(err) => { | 508 | Err(err) => { |
470 | if ra_fixture_after.starts_with("error:") { | 509 | if ra_fixture_after.starts_with("error:") { |
@@ -493,19 +532,19 @@ mod tests { | |||
493 | 532 | ||
494 | #[test] | 533 | #[test] |
495 | fn test_rename_to_underscore() { | 534 | fn test_rename_to_underscore() { |
496 | check("_", r#"fn main() { let i<|> = 1; }"#, r#"fn main() { let _ = 1; }"#); | 535 | check("_", r#"fn main() { let i$0 = 1; }"#, r#"fn main() { let _ = 1; }"#); |
497 | } | 536 | } |
498 | 537 | ||
499 | #[test] | 538 | #[test] |
500 | fn test_rename_to_raw_identifier() { | 539 | fn test_rename_to_raw_identifier() { |
501 | check("r#fn", r#"fn main() { let i<|> = 1; }"#, r#"fn main() { let r#fn = 1; }"#); | 540 | check("r#fn", r#"fn main() { let i$0 = 1; }"#, r#"fn main() { let r#fn = 1; }"#); |
502 | } | 541 | } |
503 | 542 | ||
504 | #[test] | 543 | #[test] |
505 | fn test_rename_to_invalid_identifier1() { | 544 | fn test_rename_to_invalid_identifier1() { |
506 | check( | 545 | check( |
507 | "invalid!", | 546 | "invalid!", |
508 | r#"fn main() { let i<|> = 1; }"#, | 547 | r#"fn main() { let i$0 = 1; }"#, |
509 | "error: Invalid name `invalid!`: not an identifier", | 548 | "error: Invalid name `invalid!`: not an identifier", |
510 | ); | 549 | ); |
511 | } | 550 | } |
@@ -514,7 +553,7 @@ mod tests { | |||
514 | fn test_rename_to_invalid_identifier2() { | 553 | fn test_rename_to_invalid_identifier2() { |
515 | check( | 554 | check( |
516 | "multiple tokens", | 555 | "multiple tokens", |
517 | r#"fn main() { let i<|> = 1; }"#, | 556 | r#"fn main() { let i$0 = 1; }"#, |
518 | "error: Invalid name `multiple tokens`: not an identifier", | 557 | "error: Invalid name `multiple tokens`: not an identifier", |
519 | ); | 558 | ); |
520 | } | 559 | } |
@@ -523,38 +562,60 @@ mod tests { | |||
523 | fn test_rename_to_invalid_identifier3() { | 562 | fn test_rename_to_invalid_identifier3() { |
524 | check( | 563 | check( |
525 | "let", | 564 | "let", |
526 | r#"fn main() { let i<|> = 1; }"#, | 565 | r#"fn main() { let i$0 = 1; }"#, |
527 | "error: Invalid name `let`: not an identifier", | 566 | "error: Invalid name `let`: not an identifier", |
528 | ); | 567 | ); |
529 | } | 568 | } |
530 | 569 | ||
531 | #[test] | 570 | #[test] |
532 | fn test_rename_to_invalid_identifier_lifetime() { | 571 | fn test_rename_to_invalid_identifier_lifetime() { |
572 | mark::check!(rename_not_an_ident_ref); | ||
533 | check( | 573 | check( |
534 | "'foo", | 574 | "'foo", |
535 | r#"fn main() { let i<|> = 1; }"#, | 575 | r#"fn main() { let i$0 = 1; }"#, |
536 | "error: Invalid name `'foo`: not an identifier", | 576 | "error: Invalid name `'foo`: not an identifier", |
537 | ); | 577 | ); |
538 | } | 578 | } |
539 | 579 | ||
540 | #[test] | 580 | #[test] |
541 | fn test_rename_to_invalid_identifier_lifetime2() { | 581 | fn test_rename_to_invalid_identifier_lifetime2() { |
582 | mark::check!(rename_not_a_lifetime_ident_ref); | ||
542 | check( | 583 | check( |
543 | "foo", | 584 | "foo", |
544 | r#"fn main<'a>(_: &'a<|> ()) {}"#, | 585 | r#"fn main<'a>(_: &'a$0 ()) {}"#, |
545 | "error: Invalid name `foo`: not a lifetime identifier", | 586 | "error: Invalid name `foo`: not a lifetime identifier", |
546 | ); | 587 | ); |
547 | } | 588 | } |
548 | 589 | ||
549 | #[test] | 590 | #[test] |
591 | fn test_rename_to_underscore_invalid() { | ||
592 | mark::check!(rename_underscore_multiple); | ||
593 | check( | ||
594 | "_", | ||
595 | r#"fn main(foo$0: ()) {foo;}"#, | ||
596 | "error: Cannot rename reference to `_` as it is being referenced multiple times", | ||
597 | ); | ||
598 | } | ||
599 | |||
600 | #[test] | ||
601 | fn test_rename_mod_invalid() { | ||
602 | check( | ||
603 | "'foo", | ||
604 | r#"mod foo$0 {}"#, | ||
605 | "error: Invalid name `'foo`: cannot rename module to 'foo", | ||
606 | ); | ||
607 | } | ||
608 | |||
609 | #[test] | ||
550 | fn test_rename_for_local() { | 610 | fn test_rename_for_local() { |
611 | mark::check!(rename_ident); | ||
551 | check( | 612 | check( |
552 | "k", | 613 | "k", |
553 | r#" | 614 | r#" |
554 | fn main() { | 615 | fn main() { |
555 | let mut i = 1; | 616 | let mut i = 1; |
556 | let j = 1; | 617 | let j = 1; |
557 | i = i<|> + j; | 618 | i = i$0 + j; |
558 | 619 | ||
559 | { i = 0; } | 620 | { i = 0; } |
560 | 621 | ||
@@ -579,7 +640,7 @@ fn main() { | |||
579 | fn test_rename_unresolved_reference() { | 640 | fn test_rename_unresolved_reference() { |
580 | check( | 641 | check( |
581 | "new_name", | 642 | "new_name", |
582 | r#"fn main() { let _ = unresolved_ref<|>; }"#, | 643 | r#"fn main() { let _ = unresolved_ref$0; }"#, |
583 | "error: No references found at position", | 644 | "error: No references found at position", |
584 | ); | 645 | ); |
585 | } | 646 | } |
@@ -591,7 +652,7 @@ fn main() { | |||
591 | r#" | 652 | r#" |
592 | macro_rules! foo {($i:ident) => {$i} } | 653 | macro_rules! foo {($i:ident) => {$i} } |
593 | fn main() { | 654 | fn main() { |
594 | let a<|> = "test"; | 655 | let a$0 = "test"; |
595 | foo!(a); | 656 | foo!(a); |
596 | } | 657 | } |
597 | "#, | 658 | "#, |
@@ -613,7 +674,7 @@ fn main() { | |||
613 | macro_rules! foo {($i:ident) => {$i} } | 674 | macro_rules! foo {($i:ident) => {$i} } |
614 | fn main() { | 675 | fn main() { |
615 | let a = "test"; | 676 | let a = "test"; |
616 | foo!(a<|>); | 677 | foo!(a$0); |
617 | } | 678 | } |
618 | "#, | 679 | "#, |
619 | r#" | 680 | r#" |
@@ -634,7 +695,7 @@ fn main() { | |||
634 | macro_rules! define_fn {($id:ident) => { fn $id{} }} | 695 | macro_rules! define_fn {($id:ident) => { fn $id{} }} |
635 | define_fn!(foo); | 696 | define_fn!(foo); |
636 | fn main() { | 697 | fn main() { |
637 | fo<|>o(); | 698 | fo$0o(); |
638 | } | 699 | } |
639 | "#, | 700 | "#, |
640 | r#" | 701 | r#" |
@@ -653,7 +714,7 @@ fn main() { | |||
653 | "bar", | 714 | "bar", |
654 | r#" | 715 | r#" |
655 | macro_rules! define_fn {($id:ident) => { fn $id{} }} | 716 | macro_rules! define_fn {($id:ident) => { fn $id{} }} |
656 | define_fn!(fo<|>o); | 717 | define_fn!(fo$0o); |
657 | fn main() { | 718 | fn main() { |
658 | foo(); | 719 | foo(); |
659 | } | 720 | } |
@@ -670,17 +731,17 @@ fn main() { | |||
670 | 731 | ||
671 | #[test] | 732 | #[test] |
672 | fn test_rename_for_param_inside() { | 733 | fn test_rename_for_param_inside() { |
673 | check("j", r#"fn foo(i : u32) -> u32 { i<|> }"#, r#"fn foo(j : u32) -> u32 { j }"#); | 734 | check("j", r#"fn foo(i : u32) -> u32 { i$0 }"#, r#"fn foo(j : u32) -> u32 { j }"#); |
674 | } | 735 | } |
675 | 736 | ||
676 | #[test] | 737 | #[test] |
677 | fn test_rename_refs_for_fn_param() { | 738 | fn test_rename_refs_for_fn_param() { |
678 | check("j", r#"fn foo(i<|> : u32) -> u32 { i }"#, r#"fn foo(j : u32) -> u32 { j }"#); | 739 | check("j", r#"fn foo(i$0 : u32) -> u32 { i }"#, r#"fn foo(j : u32) -> u32 { j }"#); |
679 | } | 740 | } |
680 | 741 | ||
681 | #[test] | 742 | #[test] |
682 | fn test_rename_for_mut_param() { | 743 | fn test_rename_for_mut_param() { |
683 | check("j", r#"fn foo(mut i<|> : u32) -> u32 { i }"#, r#"fn foo(mut j : u32) -> u32 { j }"#); | 744 | check("j", r#"fn foo(mut i$0 : u32) -> u32 { i }"#, r#"fn foo(mut j : u32) -> u32 { j }"#); |
684 | } | 745 | } |
685 | 746 | ||
686 | #[test] | 747 | #[test] |
@@ -688,7 +749,7 @@ fn main() { | |||
688 | check( | 749 | check( |
689 | "j", | 750 | "j", |
690 | r#" | 751 | r#" |
691 | struct Foo { i<|>: i32 } | 752 | struct Foo { i$0: i32 } |
692 | 753 | ||
693 | impl Foo { | 754 | impl Foo { |
694 | fn new(i: i32) -> Self { | 755 | fn new(i: i32) -> Self { |
@@ -714,7 +775,7 @@ impl Foo { | |||
714 | check( | 775 | check( |
715 | "j", | 776 | "j", |
716 | r#" | 777 | r#" |
717 | struct Foo { i<|>: i32 } | 778 | struct Foo { i$0: i32 } |
718 | 779 | ||
719 | impl Foo { | 780 | impl Foo { |
720 | fn new(i: i32) -> Self { | 781 | fn new(i: i32) -> Self { |
@@ -743,7 +804,7 @@ impl Foo { | |||
743 | struct Foo { i: i32 } | 804 | struct Foo { i: i32 } |
744 | 805 | ||
745 | impl Foo { | 806 | impl Foo { |
746 | fn new(i<|>: i32) -> Self { | 807 | fn new(i$0: i32) -> Self { |
747 | Self { i } | 808 | Self { i } |
748 | } | 809 | } |
749 | } | 810 | } |
@@ -765,7 +826,7 @@ impl Foo { | |||
765 | check( | 826 | check( |
766 | "j", | 827 | "j", |
767 | r#" | 828 | r#" |
768 | struct Foo { i<|>: i32 } | 829 | struct Foo { i$0: i32 } |
769 | struct Bar { i: i32 } | 830 | struct Bar { i: i32 } |
770 | 831 | ||
771 | impl Bar { | 832 | impl Bar { |
@@ -794,7 +855,7 @@ impl Bar { | |||
794 | r#" | 855 | r#" |
795 | struct Foo { i: i32 } | 856 | struct Foo { i: i32 } |
796 | 857 | ||
797 | fn baz(i<|>: i32) -> Self { | 858 | fn baz(i$0: i32) -> Self { |
798 | let x = Foo { i }; | 859 | let x = Foo { i }; |
799 | { | 860 | { |
800 | let i = 0; | 861 | let i = 0; |
@@ -825,7 +886,7 @@ fn baz(j: i32) -> Self { | |||
825 | mod bar; | 886 | mod bar; |
826 | 887 | ||
827 | //- /bar.rs | 888 | //- /bar.rs |
828 | mod foo<|>; | 889 | mod foo$0; |
829 | 890 | ||
830 | //- /bar/foo.rs | 891 | //- /bar/foo.rs |
831 | // empty | 892 | // empty |
@@ -883,7 +944,7 @@ fn main() {} | |||
883 | pub struct FooContent; | 944 | pub struct FooContent; |
884 | 945 | ||
885 | //- /bar.rs | 946 | //- /bar.rs |
886 | use crate::foo<|>::FooContent; | 947 | use crate::foo$0::FooContent; |
887 | "#, | 948 | "#, |
888 | expect![[r#" | 949 | expect![[r#" |
889 | RangeInfo { | 950 | RangeInfo { |
@@ -943,9 +1004,9 @@ use crate::foo<|>::FooContent; | |||
943 | "foo2", | 1004 | "foo2", |
944 | r#" | 1005 | r#" |
945 | //- /lib.rs | 1006 | //- /lib.rs |
946 | mod fo<|>o; | 1007 | mod fo$0o; |
947 | //- /foo/mod.rs | 1008 | //- /foo/mod.rs |
948 | // emtpy | 1009 | // empty |
949 | "#, | 1010 | "#, |
950 | expect![[r#" | 1011 | expect![[r#" |
951 | RangeInfo { | 1012 | RangeInfo { |
@@ -992,10 +1053,10 @@ mod fo<|>o; | |||
992 | "bar", | 1053 | "bar", |
993 | r#" | 1054 | r#" |
994 | //- /lib.rs | 1055 | //- /lib.rs |
995 | mod outer { mod fo<|>o; } | 1056 | mod outer { mod fo$0o; } |
996 | 1057 | ||
997 | //- /outer/foo.rs | 1058 | //- /outer/foo.rs |
998 | // emtpy | 1059 | // empty |
999 | "#, | 1060 | "#, |
1000 | expect![[r#" | 1061 | expect![[r#" |
1001 | RangeInfo { | 1062 | RangeInfo { |
@@ -1041,7 +1102,7 @@ mod outer { mod fo<|>o; } | |||
1041 | check( | 1102 | check( |
1042 | "baz", | 1103 | "baz", |
1043 | r#" | 1104 | r#" |
1044 | mod <|>foo { pub fn bar() {} } | 1105 | mod $0foo { pub fn bar() {} } |
1045 | 1106 | ||
1046 | fn main() { foo::bar(); } | 1107 | fn main() { foo::bar(); } |
1047 | "#, | 1108 | "#, |
@@ -1065,7 +1126,7 @@ fn f() { | |||
1065 | } | 1126 | } |
1066 | 1127 | ||
1067 | //- /bar.rs | 1128 | //- /bar.rs |
1068 | pub mod foo<|>; | 1129 | pub mod foo$0; |
1069 | 1130 | ||
1070 | //- /bar/foo.rs | 1131 | //- /bar/foo.rs |
1071 | // pub fn fun() {} | 1132 | // pub fn fun() {} |
@@ -1128,7 +1189,7 @@ pub mod foo<|>; | |||
1128 | "Baz", | 1189 | "Baz", |
1129 | r#" | 1190 | r#" |
1130 | mod foo { | 1191 | mod foo { |
1131 | pub enum Foo { Bar<|> } | 1192 | pub enum Foo { Bar$0 } |
1132 | } | 1193 | } |
1133 | 1194 | ||
1134 | fn func(f: foo::Foo) { | 1195 | fn func(f: foo::Foo) { |
@@ -1157,7 +1218,7 @@ fn func(f: foo::Foo) { | |||
1157 | "baz", | 1218 | "baz", |
1158 | r#" | 1219 | r#" |
1159 | mod foo { | 1220 | mod foo { |
1160 | pub struct Foo { pub bar<|>: uint } | 1221 | pub struct Foo { pub bar$0: uint } |
1161 | } | 1222 | } |
1162 | 1223 | ||
1163 | fn foo(f: foo::Foo) { | 1224 | fn foo(f: foo::Foo) { |
@@ -1178,13 +1239,14 @@ fn foo(f: foo::Foo) { | |||
1178 | 1239 | ||
1179 | #[test] | 1240 | #[test] |
1180 | fn test_parameter_to_self() { | 1241 | fn test_parameter_to_self() { |
1242 | mark::check!(rename_to_self); | ||
1181 | check( | 1243 | check( |
1182 | "self", | 1244 | "self", |
1183 | r#" | 1245 | r#" |
1184 | struct Foo { i: i32 } | 1246 | struct Foo { i: i32 } |
1185 | 1247 | ||
1186 | impl Foo { | 1248 | impl Foo { |
1187 | fn f(foo<|>: &mut Foo) -> i32 { | 1249 | fn f(foo$0: &mut Foo) -> i32 { |
1188 | foo.i | 1250 | foo.i |
1189 | } | 1251 | } |
1190 | } | 1252 | } |
@@ -1205,7 +1267,7 @@ impl Foo { | |||
1205 | struct Foo { i: i32 } | 1267 | struct Foo { i: i32 } |
1206 | 1268 | ||
1207 | impl Foo { | 1269 | impl Foo { |
1208 | fn f(foo<|>: Foo) -> i32 { | 1270 | fn f(foo$0: Foo) -> i32 { |
1209 | foo.i | 1271 | foo.i |
1210 | } | 1272 | } |
1211 | } | 1273 | } |
@@ -1229,7 +1291,7 @@ impl Foo { | |||
1229 | r#" | 1291 | r#" |
1230 | struct Foo { i: i32 } | 1292 | struct Foo { i: i32 } |
1231 | 1293 | ||
1232 | fn f(foo<|>: &mut Foo) -> i32 { | 1294 | fn f(foo$0: &mut Foo) -> i32 { |
1233 | foo.i | 1295 | foo.i |
1234 | } | 1296 | } |
1235 | "#, | 1297 | "#, |
@@ -1242,7 +1304,7 @@ struct Foo { i: i32 } | |||
1242 | struct Bar; | 1304 | struct Bar; |
1243 | 1305 | ||
1244 | impl Bar { | 1306 | impl Bar { |
1245 | fn f(foo<|>: &mut Foo) -> i32 { | 1307 | fn f(foo$0: &mut Foo) -> i32 { |
1246 | foo.i | 1308 | foo.i |
1247 | } | 1309 | } |
1248 | } | 1310 | } |
@@ -1258,7 +1320,7 @@ impl Bar { | |||
1258 | r#" | 1320 | r#" |
1259 | struct Foo { i: i32 } | 1321 | struct Foo { i: i32 } |
1260 | impl Foo { | 1322 | impl Foo { |
1261 | fn f(x: (), foo<|>: &mut Foo) -> i32 { | 1323 | fn f(x: (), foo$0: &mut Foo) -> i32 { |
1262 | foo.i | 1324 | foo.i |
1263 | } | 1325 | } |
1264 | } | 1326 | } |
@@ -1274,7 +1336,7 @@ impl Foo { | |||
1274 | r#" | 1336 | r#" |
1275 | struct Foo { i: i32 } | 1337 | struct Foo { i: i32 } |
1276 | impl &Foo { | 1338 | impl &Foo { |
1277 | fn f(foo<|>: &Foo) -> i32 { | 1339 | fn f(foo$0: &Foo) -> i32 { |
1278 | foo.i | 1340 | foo.i |
1279 | } | 1341 | } |
1280 | } | 1342 | } |
@@ -1298,7 +1360,7 @@ impl &Foo { | |||
1298 | struct Foo { i: i32 } | 1360 | struct Foo { i: i32 } |
1299 | 1361 | ||
1300 | impl Foo { | 1362 | impl Foo { |
1301 | fn f(&mut <|>self) -> i32 { | 1363 | fn f(&mut $0self) -> i32 { |
1302 | self.i | 1364 | self.i |
1303 | } | 1365 | } |
1304 | } | 1366 | } |
@@ -1323,7 +1385,7 @@ impl Foo { | |||
1323 | struct Foo { i: i32 } | 1385 | struct Foo { i: i32 } |
1324 | 1386 | ||
1325 | impl Foo { | 1387 | impl Foo { |
1326 | fn f(<|>self) -> i32 { | 1388 | fn f($0self) -> i32 { |
1327 | self.i | 1389 | self.i |
1328 | } | 1390 | } |
1329 | } | 1391 | } |
@@ -1350,7 +1412,7 @@ struct Foo { i: i32 } | |||
1350 | impl Foo { | 1412 | impl Foo { |
1351 | fn f(&self) -> i32 { | 1413 | fn f(&self) -> i32 { |
1352 | let self_var = 1; | 1414 | let self_var = 1; |
1353 | self<|>.i | 1415 | self$0.i |
1354 | } | 1416 | } |
1355 | } | 1417 | } |
1356 | "#, | 1418 | "#, |
@@ -1373,7 +1435,7 @@ impl Foo { | |||
1373 | check( | 1435 | check( |
1374 | "bar", | 1436 | "bar", |
1375 | r#" | 1437 | r#" |
1376 | struct Foo { i<|>: i32 } | 1438 | struct Foo { i$0: i32 } |
1377 | 1439 | ||
1378 | fn foo(bar: i32) -> Foo { | 1440 | fn foo(bar: i32) -> Foo { |
1379 | Foo { i: bar } | 1441 | Foo { i: bar } |
@@ -1394,7 +1456,7 @@ fn foo(bar: i32) -> Foo { | |||
1394 | check( | 1456 | check( |
1395 | "baz", | 1457 | "baz", |
1396 | r#" | 1458 | r#" |
1397 | struct Foo { i<|>: i32 } | 1459 | struct Foo { i$0: i32 } |
1398 | 1460 | ||
1399 | fn foo(foo: Foo) { | 1461 | fn foo(foo: Foo) { |
1400 | let Foo { i: baz } = foo; | 1462 | let Foo { i: baz } = foo; |
@@ -1433,7 +1495,7 @@ struct Foo { | |||
1433 | 1495 | ||
1434 | fn foo(foo: Foo) { | 1496 | fn foo(foo: Foo) { |
1435 | let Foo { i: b } = foo; | 1497 | let Foo { i: b } = foo; |
1436 | let _ = b<|>; | 1498 | let _ = b$0; |
1437 | } | 1499 | } |
1438 | "#, | 1500 | "#, |
1439 | expected_fixture, | 1501 | expected_fixture, |
@@ -1447,7 +1509,7 @@ struct Foo { | |||
1447 | 1509 | ||
1448 | fn foo(foo: Foo) { | 1510 | fn foo(foo: Foo) { |
1449 | let Foo { i } = foo; | 1511 | let Foo { i } = foo; |
1450 | let _ = i<|>; | 1512 | let _ = i$0; |
1451 | } | 1513 | } |
1452 | "#, | 1514 | "#, |
1453 | expected_fixture, | 1515 | expected_fixture, |
@@ -1464,7 +1526,7 @@ struct Foo { | |||
1464 | } | 1526 | } |
1465 | 1527 | ||
1466 | fn foo(Foo { i }: foo) -> i32 { | 1528 | fn foo(Foo { i }: foo) -> i32 { |
1467 | i<|> | 1529 | i$0 |
1468 | } | 1530 | } |
1469 | "#, | 1531 | "#, |
1470 | r#" | 1532 | r#" |
@@ -1481,6 +1543,7 @@ fn foo(Foo { i: bar }: foo) -> i32 { | |||
1481 | 1543 | ||
1482 | #[test] | 1544 | #[test] |
1483 | fn test_rename_lifetimes() { | 1545 | fn test_rename_lifetimes() { |
1546 | mark::check!(rename_lifetime); | ||
1484 | check( | 1547 | check( |
1485 | "'yeeee", | 1548 | "'yeeee", |
1486 | r#" | 1549 | r#" |
@@ -1488,7 +1551,7 @@ trait Foo<'a> { | |||
1488 | fn foo() -> &'a (); | 1551 | fn foo() -> &'a (); |
1489 | } | 1552 | } |
1490 | impl<'a> Foo<'a> for &'a () { | 1553 | impl<'a> Foo<'a> for &'a () { |
1491 | fn foo() -> &'a<|> () { | 1554 | fn foo() -> &'a$0 () { |
1492 | unimplemented!() | 1555 | unimplemented!() |
1493 | } | 1556 | } |
1494 | } | 1557 | } |
@@ -1520,7 +1583,7 @@ fn main() { | |||
1520 | let test_variable = CustomOption::Some(22); | 1583 | let test_variable = CustomOption::Some(22); |
1521 | 1584 | ||
1522 | match test_variable { | 1585 | match test_variable { |
1523 | CustomOption::Some(foo<|>) if foo == 11 => {} | 1586 | CustomOption::Some(foo$0) if foo == 11 => {} |
1524 | _ => (), | 1587 | _ => (), |
1525 | } | 1588 | } |
1526 | }"#, | 1589 | }"#, |
@@ -1549,7 +1612,7 @@ fn main() { | |||
1549 | fn foo<'a>() -> &'a () { | 1612 | fn foo<'a>() -> &'a () { |
1550 | 'a: { | 1613 | 'a: { |
1551 | 'b: loop { | 1614 | 'b: loop { |
1552 | break 'a<|>; | 1615 | break 'a$0; |
1553 | } | 1616 | } |
1554 | } | 1617 | } |
1555 | } | 1618 | } |
@@ -1565,4 +1628,24 @@ fn foo<'a>() -> &'a () { | |||
1565 | "#, | 1628 | "#, |
1566 | ) | 1629 | ) |
1567 | } | 1630 | } |
1631 | |||
1632 | #[test] | ||
1633 | fn test_self_to_self() { | ||
1634 | mark::check!(rename_self_to_self); | ||
1635 | check( | ||
1636 | "self", | ||
1637 | r#" | ||
1638 | struct Foo; | ||
1639 | impl Foo { | ||
1640 | fn foo(self$0) {} | ||
1641 | } | ||
1642 | "#, | ||
1643 | r#" | ||
1644 | struct Foo; | ||
1645 | impl Foo { | ||
1646 | fn foo(self) {} | ||
1647 | } | ||
1648 | "#, | ||
1649 | ) | ||
1650 | } | ||
1568 | } | 1651 | } |
diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index 891183266..f5ee7de86 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs | |||
@@ -2,11 +2,11 @@ use std::fmt; | |||
2 | 2 | ||
3 | use assists::utils::test_related_attribute; | 3 | use assists::utils::test_related_attribute; |
4 | use cfg::CfgExpr; | 4 | use cfg::CfgExpr; |
5 | use hir::{AsAssocItem, HasAttrs, InFile, Semantics}; | 5 | use hir::{AsAssocItem, HasAttrs, HasSource, Semantics}; |
6 | use ide_db::RootDatabase; | 6 | use ide_db::{defs::Definition, RootDatabase}; |
7 | use itertools::Itertools; | 7 | use itertools::Itertools; |
8 | use syntax::{ | 8 | use syntax::{ |
9 | ast::{self, AstNode, AttrsOwner, ModuleItemOwner, NameOwner}, | 9 | ast::{self, AstNode, AttrsOwner, ModuleItemOwner}, |
10 | match_ast, SyntaxNode, | 10 | match_ast, SyntaxNode, |
11 | }; | 11 | }; |
12 | 12 | ||
@@ -96,41 +96,45 @@ impl Runnable { | |||
96 | pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> { | 96 | pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> { |
97 | let sema = Semantics::new(db); | 97 | let sema = Semantics::new(db); |
98 | let source_file = sema.parse(file_id); | 98 | let source_file = sema.parse(file_id); |
99 | source_file.syntax().descendants().filter_map(|i| runnable(&sema, i, file_id)).collect() | 99 | source_file |
100 | } | 100 | .syntax() |
101 | 101 | .descendants() | |
102 | pub(crate) fn runnable( | 102 | .filter_map(|item| { |
103 | sema: &Semantics<RootDatabase>, | 103 | let runnable = match_ast! { |
104 | item: SyntaxNode, | 104 | match item { |
105 | file_id: FileId, | 105 | ast::Fn(func) => { |
106 | ) -> Option<Runnable> { | 106 | let def = sema.to_def(&func)?; |
107 | let runnable_item = match_ast! { | 107 | runnable_fn(&sema, def) |
108 | match (item.clone()) { | 108 | }, |
109 | ast::Fn(it) => runnable_fn(sema, it, file_id), | 109 | ast::Module(it) => runnable_mod(&sema, it), |
110 | ast::Module(it) => runnable_mod(sema, it), | 110 | _ => None, |
111 | _ => None, | 111 | } |
112 | } | 112 | }; |
113 | }; | 113 | runnable.or_else(|| match doc_owner_to_def(&sema, item)? { |
114 | runnable_item.or_else(|| runnable_doctest(sema, item)) | 114 | Definition::ModuleDef(def) => module_def_doctest(&sema, def), |
115 | _ => None, | ||
116 | }) | ||
117 | }) | ||
118 | .collect() | ||
115 | } | 119 | } |
116 | 120 | ||
117 | fn runnable_fn(sema: &Semantics<RootDatabase>, func: ast::Fn, file_id: FileId) -> Option<Runnable> { | 121 | pub(crate) fn runnable_fn(sema: &Semantics<RootDatabase>, def: hir::Function) -> Option<Runnable> { |
118 | let def = sema.to_def(&func)?; | 122 | let func = def.source(sema.db)?; |
119 | let name_string = func.name()?.text().to_string(); | 123 | let name_string = def.name(sema.db).to_string(); |
120 | 124 | ||
121 | let kind = if name_string == "main" { | 125 | let kind = if name_string == "main" { |
122 | RunnableKind::Bin | 126 | RunnableKind::Bin |
123 | } else { | 127 | } else { |
124 | let canonical_path = sema.to_def(&func).and_then(|def| { | 128 | let canonical_path = { |
125 | let def: hir::ModuleDef = def.into(); | 129 | let def: hir::ModuleDef = def.into(); |
126 | def.canonical_path(sema.db) | 130 | def.canonical_path(sema.db) |
127 | }); | 131 | }; |
128 | let test_id = canonical_path.map(TestId::Path).unwrap_or(TestId::Name(name_string)); | 132 | let test_id = canonical_path.map(TestId::Path).unwrap_or(TestId::Name(name_string)); |
129 | 133 | ||
130 | if test_related_attribute(&func).is_some() { | 134 | if test_related_attribute(&func.value).is_some() { |
131 | let attr = TestAttr::from_fn(&func); | 135 | let attr = TestAttr::from_fn(&func.value); |
132 | RunnableKind::Test { test_id, attr } | 136 | RunnableKind::Test { test_id, attr } |
133 | } else if func.has_atom_attr("bench") { | 137 | } else if func.value.has_atom_attr("bench") { |
134 | RunnableKind::Bench { test_id } | 138 | RunnableKind::Bench { test_id } |
135 | } else { | 139 | } else { |
136 | return None; | 140 | return None; |
@@ -139,27 +143,56 @@ fn runnable_fn(sema: &Semantics<RootDatabase>, func: ast::Fn, file_id: FileId) - | |||
139 | 143 | ||
140 | let nav = NavigationTarget::from_named( | 144 | let nav = NavigationTarget::from_named( |
141 | sema.db, | 145 | sema.db, |
142 | InFile::new(file_id.into(), &func), | 146 | func.as_ref().map(|it| it as &dyn ast::NameOwner), |
143 | SymbolKind::Function, | 147 | SymbolKind::Function, |
144 | ); | 148 | ); |
145 | let cfg = def.attrs(sema.db).cfg(); | 149 | let cfg = def.attrs(sema.db).cfg(); |
146 | Some(Runnable { nav, kind, cfg }) | 150 | Some(Runnable { nav, kind, cfg }) |
147 | } | 151 | } |
148 | 152 | ||
149 | fn runnable_doctest(sema: &Semantics<RootDatabase>, item: SyntaxNode) -> Option<Runnable> { | 153 | pub(crate) fn runnable_mod( |
150 | match_ast! { | 154 | sema: &Semantics<RootDatabase>, |
155 | module: ast::Module, | ||
156 | ) -> Option<Runnable> { | ||
157 | if !has_test_function_or_multiple_test_submodules(&module) { | ||
158 | return None; | ||
159 | } | ||
160 | let module_def = sema.to_def(&module)?; | ||
161 | |||
162 | let path = module_def | ||
163 | .path_to_root(sema.db) | ||
164 | .into_iter() | ||
165 | .rev() | ||
166 | .filter_map(|it| it.name(sema.db)) | ||
167 | .join("::"); | ||
168 | |||
169 | let def = sema.to_def(&module)?; | ||
170 | let attrs = def.attrs(sema.db); | ||
171 | let cfg = attrs.cfg(); | ||
172 | let nav = module_def.to_nav(sema.db); | ||
173 | Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg }) | ||
174 | } | ||
175 | |||
176 | // FIXME: figure out a proper API here. | ||
177 | pub(crate) fn doc_owner_to_def( | ||
178 | sema: &Semantics<RootDatabase>, | ||
179 | item: SyntaxNode, | ||
180 | ) -> Option<Definition> { | ||
181 | let res: hir::ModuleDef = match_ast! { | ||
151 | match item { | 182 | match item { |
152 | ast::Fn(it) => module_def_doctest(sema, sema.to_def(&it)?.into()), | 183 | ast::SourceFile(it) => sema.scope(&item).module()?.into(), |
153 | ast::Struct(it) => module_def_doctest(sema, sema.to_def(&it)?.into()), | 184 | ast::Fn(it) => sema.to_def(&it)?.into(), |
154 | ast::Enum(it) => module_def_doctest(sema, sema.to_def(&it)?.into()), | 185 | ast::Struct(it) => sema.to_def(&it)?.into(), |
155 | ast::Union(it) => module_def_doctest(sema, sema.to_def(&it)?.into()), | 186 | ast::Enum(it) => sema.to_def(&it)?.into(), |
156 | ast::Trait(it) => module_def_doctest(sema, sema.to_def(&it)?.into()), | 187 | ast::Union(it) => sema.to_def(&it)?.into(), |
157 | ast::Const(it) => module_def_doctest(sema, sema.to_def(&it)?.into()), | 188 | ast::Trait(it) => sema.to_def(&it)?.into(), |
158 | ast::Static(it) => module_def_doctest(sema, sema.to_def(&it)?.into()), | 189 | ast::Const(it) => sema.to_def(&it)?.into(), |
159 | ast::TypeAlias(it) => module_def_doctest(sema, sema.to_def(&it)?.into()), | 190 | ast::Static(it) => sema.to_def(&it)?.into(), |
160 | _ => None, | 191 | ast::TypeAlias(it) => sema.to_def(&it)?.into(), |
192 | _ => return None, | ||
161 | } | 193 | } |
162 | } | 194 | }; |
195 | Some(Definition::ModuleDef(res)) | ||
163 | } | 196 | } |
164 | 197 | ||
165 | fn module_def_doctest(sema: &Semantics<RootDatabase>, def: hir::ModuleDef) -> Option<Runnable> { | 198 | fn module_def_doctest(sema: &Semantics<RootDatabase>, def: hir::ModuleDef) -> Option<Runnable> { |
@@ -230,7 +263,7 @@ impl TestAttr { | |||
230 | 263 | ||
231 | const RUSTDOC_FENCE: &str = "```"; | 264 | const RUSTDOC_FENCE: &str = "```"; |
232 | const RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUNNABLE: &[&str] = | 265 | const RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUNNABLE: &[&str] = |
233 | &["", "rust", "should_panic", "edition2015", "edition2018"]; | 266 | &["", "rust", "should_panic", "edition2015", "edition2018", "edition2021"]; |
234 | 267 | ||
235 | fn has_runnable_doc_test(attrs: &hir::Attrs) -> bool { | 268 | fn has_runnable_doc_test(attrs: &hir::Attrs) -> bool { |
236 | attrs.docs().map_or(false, |doc| { | 269 | attrs.docs().map_or(false, |doc| { |
@@ -254,26 +287,6 @@ fn has_runnable_doc_test(attrs: &hir::Attrs) -> bool { | |||
254 | }) | 287 | }) |
255 | } | 288 | } |
256 | 289 | ||
257 | fn runnable_mod(sema: &Semantics<RootDatabase>, module: ast::Module) -> Option<Runnable> { | ||
258 | if !has_test_function_or_multiple_test_submodules(&module) { | ||
259 | return None; | ||
260 | } | ||
261 | let module_def = sema.to_def(&module)?; | ||
262 | |||
263 | let path = module_def | ||
264 | .path_to_root(sema.db) | ||
265 | .into_iter() | ||
266 | .rev() | ||
267 | .filter_map(|it| it.name(sema.db)) | ||
268 | .join("::"); | ||
269 | |||
270 | let def = sema.to_def(&module)?; | ||
271 | let attrs = def.attrs(sema.db); | ||
272 | let cfg = attrs.cfg(); | ||
273 | let nav = module_def.to_nav(sema.db); | ||
274 | Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg }) | ||
275 | } | ||
276 | |||
277 | // We could create runnables for modules with number_of_test_submodules > 0, | 290 | // We could create runnables for modules with number_of_test_submodules > 0, |
278 | // but that bloats the runnables for no real benefit, since all tests can be run by the submodule already | 291 | // but that bloats the runnables for no real benefit, since all tests can be run by the submodule already |
279 | fn has_test_function_or_multiple_test_submodules(module: &ast::Module) -> bool { | 292 | fn has_test_function_or_multiple_test_submodules(module: &ast::Module) -> bool { |
@@ -330,7 +343,7 @@ mod tests { | |||
330 | check( | 343 | check( |
331 | r#" | 344 | r#" |
332 | //- /lib.rs | 345 | //- /lib.rs |
333 | <|> | 346 | $0 |
334 | fn main() {} | 347 | fn main() {} |
335 | 348 | ||
336 | #[test] | 349 | #[test] |
@@ -426,7 +439,7 @@ fn bench() {} | |||
426 | check( | 439 | check( |
427 | r#" | 440 | r#" |
428 | //- /lib.rs | 441 | //- /lib.rs |
429 | <|> | 442 | $0 |
430 | fn main() {} | 443 | fn main() {} |
431 | 444 | ||
432 | /// ``` | 445 | /// ``` |
@@ -574,7 +587,7 @@ struct StructWithRunnable(String); | |||
574 | check( | 587 | check( |
575 | r#" | 588 | r#" |
576 | //- /lib.rs | 589 | //- /lib.rs |
577 | <|> | 590 | $0 |
578 | fn main() {} | 591 | fn main() {} |
579 | 592 | ||
580 | struct Data; | 593 | struct Data; |
@@ -626,7 +639,7 @@ impl Data { | |||
626 | check( | 639 | check( |
627 | r#" | 640 | r#" |
628 | //- /lib.rs | 641 | //- /lib.rs |
629 | <|> | 642 | $0 |
630 | mod test_mod { | 643 | mod test_mod { |
631 | #[test] | 644 | #[test] |
632 | fn test_foo1() {} | 645 | fn test_foo1() {} |
@@ -680,7 +693,7 @@ mod test_mod { | |||
680 | check( | 693 | check( |
681 | r#" | 694 | r#" |
682 | //- /lib.rs | 695 | //- /lib.rs |
683 | <|> | 696 | $0 |
684 | mod root_tests { | 697 | mod root_tests { |
685 | mod nested_tests_0 { | 698 | mod nested_tests_0 { |
686 | mod nested_tests_1 { | 699 | mod nested_tests_1 { |
@@ -820,7 +833,7 @@ mod root_tests { | |||
820 | check( | 833 | check( |
821 | r#" | 834 | r#" |
822 | //- /lib.rs crate:foo cfg:feature=foo | 835 | //- /lib.rs crate:foo cfg:feature=foo |
823 | <|> | 836 | $0 |
824 | #[test] | 837 | #[test] |
825 | #[cfg(feature = "foo")] | 838 | #[cfg(feature = "foo")] |
826 | fn test_foo1() {} | 839 | fn test_foo1() {} |
@@ -865,7 +878,7 @@ fn test_foo1() {} | |||
865 | check( | 878 | check( |
866 | r#" | 879 | r#" |
867 | //- /lib.rs crate:foo cfg:feature=foo,feature=bar | 880 | //- /lib.rs crate:foo cfg:feature=foo,feature=bar |
868 | <|> | 881 | $0 |
869 | #[test] | 882 | #[test] |
870 | #[cfg(all(feature = "foo", feature = "bar"))] | 883 | #[cfg(all(feature = "foo", feature = "bar"))] |
871 | fn test_foo1() {} | 884 | fn test_foo1() {} |
@@ -920,7 +933,7 @@ fn test_foo1() {} | |||
920 | check( | 933 | check( |
921 | r#" | 934 | r#" |
922 | //- /lib.rs | 935 | //- /lib.rs |
923 | <|> | 936 | $0 |
924 | mod test_mod { | 937 | mod test_mod { |
925 | fn foo1() {} | 938 | fn foo1() {} |
926 | } | 939 | } |
@@ -939,7 +952,7 @@ mod test_mod { | |||
939 | //- /lib.rs | 952 | //- /lib.rs |
940 | mod foo; | 953 | mod foo; |
941 | //- /foo.rs | 954 | //- /foo.rs |
942 | struct Foo;<|> | 955 | struct Foo;$0 |
943 | impl Foo { | 956 | impl Foo { |
944 | /// ``` | 957 | /// ``` |
945 | /// let x = 5; | 958 | /// let x = 5; |
diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 5ad96581b..f2d4da78d 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs | |||
@@ -1,35 +1,39 @@ | |||
1 | pub(crate) mod tags; | ||
2 | |||
3 | mod highlights; | ||
4 | mod injector; | ||
5 | |||
6 | mod highlight; | ||
1 | mod format; | 7 | mod format; |
2 | mod html; | ||
3 | mod injection; | ||
4 | mod macro_rules; | 8 | mod macro_rules; |
5 | pub(crate) mod tags; | 9 | mod inject; |
10 | |||
11 | mod html; | ||
6 | #[cfg(test)] | 12 | #[cfg(test)] |
7 | mod tests; | 13 | mod tests; |
8 | 14 | ||
9 | use hir::{AsAssocItem, Local, Name, Semantics, VariantDef}; | 15 | use hir::{Name, Semantics}; |
10 | use ide_db::{ | 16 | use ide_db::RootDatabase; |
11 | defs::{Definition, NameClass, NameRefClass}, | ||
12 | RootDatabase, | ||
13 | }; | ||
14 | use rustc_hash::FxHashMap; | 17 | use rustc_hash::FxHashMap; |
15 | use syntax::{ | 18 | use syntax::{ |
16 | ast::{self, HasFormatSpecifier}, | 19 | ast::{self, HasFormatSpecifier}, |
17 | AstNode, AstToken, Direction, NodeOrToken, SyntaxElement, | 20 | AstNode, AstToken, Direction, NodeOrToken, |
18 | SyntaxKind::{self, *}, | 21 | SyntaxKind::*, |
19 | SyntaxNode, SyntaxToken, TextRange, WalkEvent, T, | 22 | SyntaxNode, TextRange, WalkEvent, T, |
20 | }; | 23 | }; |
21 | 24 | ||
22 | use crate::{ | 25 | use crate::{ |
23 | syntax_highlighting::{ | 26 | syntax_highlighting::{ |
24 | format::FormatStringHighlighter, macro_rules::MacroRulesHighlighter, tags::Highlight, | 27 | format::highlight_format_string, highlights::Highlights, |
28 | macro_rules::MacroRulesHighlighter, tags::Highlight, | ||
25 | }, | 29 | }, |
26 | FileId, HighlightModifier, HighlightTag, SymbolKind, | 30 | FileId, HlMod, HlTag, SymbolKind, |
27 | }; | 31 | }; |
28 | 32 | ||
29 | pub(crate) use html::highlight_as_html; | 33 | pub(crate) use html::highlight_as_html; |
30 | 34 | ||
31 | #[derive(Debug, Clone)] | 35 | #[derive(Debug, Clone, Copy)] |
32 | pub struct HighlightedRange { | 36 | pub struct HlRange { |
33 | pub range: TextRange, | 37 | pub range: TextRange, |
34 | pub highlight: Highlight, | 38 | pub highlight: Highlight, |
35 | pub binding_hash: Option<u64>, | 39 | pub binding_hash: Option<u64>, |
@@ -49,7 +53,7 @@ pub(crate) fn highlight( | |||
49 | file_id: FileId, | 53 | file_id: FileId, |
50 | range_to_highlight: Option<TextRange>, | 54 | range_to_highlight: Option<TextRange>, |
51 | syntactic_name_ref_highlighting: bool, | 55 | syntactic_name_ref_highlighting: bool, |
52 | ) -> Vec<HighlightedRange> { | 56 | ) -> Vec<HlRange> { |
53 | let _p = profile::span("highlight"); | 57 | let _p = profile::span("highlight"); |
54 | let sema = Semantics::new(db); | 58 | let sema = Semantics::new(db); |
55 | 59 | ||
@@ -68,28 +72,30 @@ pub(crate) fn highlight( | |||
68 | } | 72 | } |
69 | }; | 73 | }; |
70 | 74 | ||
75 | let mut hl = highlights::Highlights::new(range_to_highlight); | ||
76 | traverse(&mut hl, &sema, &root, range_to_highlight, syntactic_name_ref_highlighting); | ||
77 | hl.to_vec() | ||
78 | } | ||
79 | |||
80 | fn traverse( | ||
81 | hl: &mut Highlights, | ||
82 | sema: &Semantics<RootDatabase>, | ||
83 | root: &SyntaxNode, | ||
84 | range_to_highlight: TextRange, | ||
85 | syntactic_name_ref_highlighting: bool, | ||
86 | ) { | ||
71 | let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default(); | 87 | let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default(); |
72 | // We use a stack for the DFS traversal below. | ||
73 | // When we leave a node, the we use it to flatten the highlighted ranges. | ||
74 | let mut stack = HighlightedRangeStack::new(); | ||
75 | 88 | ||
76 | let mut current_macro_call: Option<ast::MacroCall> = None; | 89 | let mut current_macro_call: Option<ast::MacroCall> = None; |
77 | let mut current_macro_rules: Option<ast::MacroRules> = None; | 90 | let mut current_macro_rules: Option<ast::MacroRules> = None; |
78 | let mut format_string_highlighter = FormatStringHighlighter::default(); | ||
79 | let mut macro_rules_highlighter = MacroRulesHighlighter::default(); | 91 | let mut macro_rules_highlighter = MacroRulesHighlighter::default(); |
80 | let mut inside_attribute = false; | 92 | let mut inside_attribute = false; |
81 | 93 | ||
82 | // Walk all nodes, keeping track of whether we are inside a macro or not. | 94 | // Walk all nodes, keeping track of whether we are inside a macro or not. |
83 | // If in macro, expand it first and highlight the expanded code. | 95 | // If in macro, expand it first and highlight the expanded code. |
84 | for event in root.preorder_with_tokens() { | 96 | for event in root.preorder_with_tokens() { |
85 | match &event { | ||
86 | WalkEvent::Enter(_) => stack.push(), | ||
87 | WalkEvent::Leave(_) => stack.pop(), | ||
88 | }; | ||
89 | |||
90 | let event_range = match &event { | 97 | let event_range = match &event { |
91 | WalkEvent::Enter(it) => it.text_range(), | 98 | WalkEvent::Enter(it) | WalkEvent::Leave(it) => it.text_range(), |
92 | WalkEvent::Leave(it) => it.text_range(), | ||
93 | }; | 99 | }; |
94 | 100 | ||
95 | // Element outside of the viewport, no need to highlight | 101 | // Element outside of the viewport, no need to highlight |
@@ -101,9 +107,9 @@ pub(crate) fn highlight( | |||
101 | match event.clone().map(|it| it.into_node().and_then(ast::MacroCall::cast)) { | 107 | match event.clone().map(|it| it.into_node().and_then(ast::MacroCall::cast)) { |
102 | WalkEvent::Enter(Some(mc)) => { | 108 | WalkEvent::Enter(Some(mc)) => { |
103 | if let Some(range) = macro_call_range(&mc) { | 109 | if let Some(range) = macro_call_range(&mc) { |
104 | stack.add(HighlightedRange { | 110 | hl.add(HlRange { |
105 | range, | 111 | range, |
106 | highlight: HighlightTag::Symbol(SymbolKind::Macro).into(), | 112 | highlight: HlTag::Symbol(SymbolKind::Macro).into(), |
107 | binding_hash: None, | 113 | binding_hash: None, |
108 | }); | 114 | }); |
109 | } | 115 | } |
@@ -113,7 +119,6 @@ pub(crate) fn highlight( | |||
113 | WalkEvent::Leave(Some(mc)) => { | 119 | WalkEvent::Leave(Some(mc)) => { |
114 | assert_eq!(current_macro_call, Some(mc)); | 120 | assert_eq!(current_macro_call, Some(mc)); |
115 | current_macro_call = None; | 121 | current_macro_call = None; |
116 | format_string_highlighter = FormatStringHighlighter::default(); | ||
117 | } | 122 | } |
118 | _ => (), | 123 | _ => (), |
119 | } | 124 | } |
@@ -131,33 +136,24 @@ pub(crate) fn highlight( | |||
131 | } | 136 | } |
132 | _ => (), | 137 | _ => (), |
133 | } | 138 | } |
134 | |||
135 | match &event { | 139 | match &event { |
136 | // Check for Rust code in documentation | ||
137 | WalkEvent::Leave(NodeOrToken::Node(node)) => { | ||
138 | if ast::Attr::can_cast(node.kind()) { | ||
139 | inside_attribute = false | ||
140 | } | ||
141 | if let Some((doctest, range_mapping, new_comments)) = | ||
142 | injection::extract_doc_comments(node) | ||
143 | { | ||
144 | injection::highlight_doc_comment( | ||
145 | doctest, | ||
146 | range_mapping, | ||
147 | new_comments, | ||
148 | &mut stack, | ||
149 | ); | ||
150 | } | ||
151 | } | ||
152 | WalkEvent::Enter(NodeOrToken::Node(node)) if ast::Attr::can_cast(node.kind()) => { | 140 | WalkEvent::Enter(NodeOrToken::Node(node)) if ast::Attr::can_cast(node.kind()) => { |
153 | inside_attribute = true | 141 | inside_attribute = true |
154 | } | 142 | } |
143 | WalkEvent::Leave(NodeOrToken::Node(node)) if ast::Attr::can_cast(node.kind()) => { | ||
144 | inside_attribute = false | ||
145 | } | ||
155 | _ => (), | 146 | _ => (), |
156 | } | 147 | } |
157 | 148 | ||
158 | let element = match event { | 149 | let element = match event { |
159 | WalkEvent::Enter(it) => it, | 150 | WalkEvent::Enter(it) => it, |
160 | WalkEvent::Leave(_) => continue, | 151 | WalkEvent::Leave(it) => { |
152 | if let Some(node) = it.as_node() { | ||
153 | inject::doc_comment(hl, node); | ||
154 | } | ||
155 | continue; | ||
156 | } | ||
161 | }; | 157 | }; |
162 | 158 | ||
163 | let range = element.text_range(); | 159 | let range = element.text_range(); |
@@ -177,8 +173,6 @@ pub(crate) fn highlight( | |||
177 | let token = sema.descend_into_macros(token.clone()); | 173 | let token = sema.descend_into_macros(token.clone()); |
178 | let parent = token.parent(); | 174 | let parent = token.parent(); |
179 | 175 | ||
180 | format_string_highlighter.check_for_format_string(&parent); | ||
181 | |||
182 | // We only care Name and Name_ref | 176 | // We only care Name and Name_ref |
183 | match (token.kind(), parent.kind()) { | 177 | match (token.kind(), parent.kind()) { |
184 | (IDENT, NAME) | (IDENT, NAME_REF) => parent.into(), | 178 | (IDENT, NAME) | (IDENT, NAME_REF) => parent.into(), |
@@ -191,213 +185,45 @@ pub(crate) fn highlight( | |||
191 | if let Some(token) = element.as_token().cloned().and_then(ast::String::cast) { | 185 | if let Some(token) = element.as_token().cloned().and_then(ast::String::cast) { |
192 | if token.is_raw() { | 186 | if token.is_raw() { |
193 | let expanded = element_to_highlight.as_token().unwrap().clone(); | 187 | let expanded = element_to_highlight.as_token().unwrap().clone(); |
194 | if injection::highlight_injection(&mut stack, &sema, token, expanded).is_some() { | 188 | if inject::ra_fixture(hl, &sema, token, expanded).is_some() { |
195 | continue; | 189 | continue; |
196 | } | 190 | } |
197 | } | 191 | } |
198 | } | 192 | } |
199 | 193 | ||
200 | if let Some((mut highlight, binding_hash)) = highlight_element( | 194 | if let Some(_) = macro_rules_highlighter.highlight(element_to_highlight.clone()) { |
195 | continue; | ||
196 | } | ||
197 | |||
198 | if let Some((mut highlight, binding_hash)) = highlight::element( | ||
201 | &sema, | 199 | &sema, |
202 | &mut bindings_shadow_count, | 200 | &mut bindings_shadow_count, |
203 | syntactic_name_ref_highlighting, | 201 | syntactic_name_ref_highlighting, |
204 | element_to_highlight.clone(), | 202 | element_to_highlight.clone(), |
205 | ) { | 203 | ) { |
206 | if inside_attribute { | 204 | if inside_attribute { |
207 | highlight = highlight | HighlightModifier::Attribute; | 205 | highlight = highlight | HlMod::Attribute; |
208 | } | ||
209 | |||
210 | if macro_rules_highlighter.highlight(element_to_highlight.clone()).is_none() { | ||
211 | stack.add(HighlightedRange { range, highlight, binding_hash }); | ||
212 | } | 206 | } |
213 | 207 | ||
214 | if let Some(string) = | 208 | hl.add(HlRange { range, highlight, binding_hash }); |
215 | element_to_highlight.as_token().cloned().and_then(ast::String::cast) | ||
216 | { | ||
217 | format_string_highlighter.highlight_format_string(&mut stack, &string, range); | ||
218 | // Highlight escape sequences | ||
219 | if let Some(char_ranges) = string.char_ranges() { | ||
220 | stack.push(); | ||
221 | for (piece_range, _) in char_ranges.iter().filter(|(_, char)| char.is_ok()) { | ||
222 | if string.text()[piece_range.start().into()..].starts_with('\\') { | ||
223 | stack.add(HighlightedRange { | ||
224 | range: piece_range + range.start(), | ||
225 | highlight: HighlightTag::EscapeSequence.into(), | ||
226 | binding_hash: None, | ||
227 | }); | ||
228 | } | ||
229 | } | ||
230 | stack.pop_and_inject(None); | ||
231 | } | ||
232 | } | ||
233 | } | 209 | } |
234 | } | ||
235 | |||
236 | stack.flattened() | ||
237 | } | ||
238 | |||
239 | #[derive(Debug)] | ||
240 | struct HighlightedRangeStack { | ||
241 | stack: Vec<Vec<HighlightedRange>>, | ||
242 | } | ||
243 | |||
244 | /// We use a stack to implement the flattening logic for the highlighted | ||
245 | /// syntax ranges. | ||
246 | impl HighlightedRangeStack { | ||
247 | fn new() -> Self { | ||
248 | Self { stack: vec![Vec::new()] } | ||
249 | } | ||
250 | |||
251 | fn push(&mut self) { | ||
252 | self.stack.push(Vec::new()); | ||
253 | } | ||
254 | |||
255 | /// Flattens the highlighted ranges. | ||
256 | /// | ||
257 | /// For example `#[cfg(feature = "foo")]` contains the nested ranges: | ||
258 | /// 1) parent-range: Attribute [0, 23) | ||
259 | /// 2) child-range: String [16, 21) | ||
260 | /// | ||
261 | /// The following code implements the flattening, for our example this results to: | ||
262 | /// `[Attribute [0, 16), String [16, 21), Attribute [21, 23)]` | ||
263 | fn pop(&mut self) { | ||
264 | let children = self.stack.pop().unwrap(); | ||
265 | let prev = self.stack.last_mut().unwrap(); | ||
266 | let needs_flattening = !children.is_empty() | ||
267 | && !prev.is_empty() | ||
268 | && prev.last().unwrap().range.contains_range(children.first().unwrap().range); | ||
269 | if !needs_flattening { | ||
270 | prev.extend(children); | ||
271 | } else { | ||
272 | let mut parent = prev.pop().unwrap(); | ||
273 | for ele in children { | ||
274 | assert!(parent.range.contains_range(ele.range)); | ||
275 | |||
276 | let cloned = Self::intersect(&mut parent, &ele); | ||
277 | if !parent.range.is_empty() { | ||
278 | prev.push(parent); | ||
279 | } | ||
280 | prev.push(ele); | ||
281 | parent = cloned; | ||
282 | } | ||
283 | if !parent.range.is_empty() { | ||
284 | prev.push(parent); | ||
285 | } | ||
286 | } | ||
287 | } | ||
288 | |||
289 | /// Intersects the `HighlightedRange` `parent` with `child`. | ||
290 | /// `parent` is mutated in place, becoming the range before `child`. | ||
291 | /// Returns the range (of the same type as `parent`) *after* `child`. | ||
292 | fn intersect(parent: &mut HighlightedRange, child: &HighlightedRange) -> HighlightedRange { | ||
293 | assert!(parent.range.contains_range(child.range)); | ||
294 | |||
295 | let mut cloned = parent.clone(); | ||
296 | parent.range = TextRange::new(parent.range.start(), child.range.start()); | ||
297 | cloned.range = TextRange::new(child.range.end(), cloned.range.end()); | ||
298 | 210 | ||
299 | cloned | 211 | if let Some(string) = element_to_highlight.as_token().cloned().and_then(ast::String::cast) { |
300 | } | 212 | highlight_format_string(hl, &string, range); |
301 | 213 | // Highlight escape sequences | |
302 | /// Remove the `HighlightRange` of `parent` that's currently covered by `child`. | 214 | if let Some(char_ranges) = string.char_ranges() { |
303 | fn intersect_partial(parent: &mut HighlightedRange, child: &HighlightedRange) { | 215 | for (piece_range, _) in char_ranges.iter().filter(|(_, char)| char.is_ok()) { |
304 | assert!( | 216 | if string.text()[piece_range.start().into()..].starts_with('\\') { |
305 | parent.range.start() <= child.range.start() | 217 | hl.add(HlRange { |
306 | && parent.range.end() >= child.range.start() | 218 | range: piece_range + range.start(), |
307 | && child.range.end() > parent.range.end() | 219 | highlight: HlTag::EscapeSequence.into(), |
308 | ); | 220 | binding_hash: None, |
309 | 221 | }); | |
310 | parent.range = TextRange::new(parent.range.start(), child.range.start()); | ||
311 | } | ||
312 | |||
313 | /// Similar to `pop`, but can modify arbitrary prior ranges (where `pop`) | ||
314 | /// can only modify the last range currently on the stack. | ||
315 | /// Can be used to do injections that span multiple ranges, like the | ||
316 | /// doctest injection below. | ||
317 | /// If `overwrite_parent` is non-optional, the highlighting of the parent range | ||
318 | /// is overwritten with the argument. | ||
319 | /// | ||
320 | /// Note that `pop` can be simulated by `pop_and_inject(false)` but the | ||
321 | /// latter is computationally more expensive. | ||
322 | fn pop_and_inject(&mut self, overwrite_parent: Option<Highlight>) { | ||
323 | let mut children = self.stack.pop().unwrap(); | ||
324 | let prev = self.stack.last_mut().unwrap(); | ||
325 | children.sort_by_key(|range| range.range.start()); | ||
326 | prev.sort_by_key(|range| range.range.start()); | ||
327 | |||
328 | for child in children { | ||
329 | if let Some(idx) = | ||
330 | prev.iter().position(|parent| parent.range.contains_range(child.range)) | ||
331 | { | ||
332 | if let Some(tag) = overwrite_parent { | ||
333 | prev[idx].highlight = tag; | ||
334 | } | ||
335 | |||
336 | let cloned = Self::intersect(&mut prev[idx], &child); | ||
337 | let insert_idx = if prev[idx].range.is_empty() { | ||
338 | prev.remove(idx); | ||
339 | idx | ||
340 | } else { | ||
341 | idx + 1 | ||
342 | }; | ||
343 | prev.insert(insert_idx, child); | ||
344 | if !cloned.range.is_empty() { | ||
345 | prev.insert(insert_idx + 1, cloned); | ||
346 | } | ||
347 | } else { | ||
348 | let maybe_idx = | ||
349 | prev.iter().position(|parent| parent.range.contains(child.range.start())); | ||
350 | match (overwrite_parent, maybe_idx) { | ||
351 | (Some(_), Some(idx)) => { | ||
352 | Self::intersect_partial(&mut prev[idx], &child); | ||
353 | let insert_idx = if prev[idx].range.is_empty() { | ||
354 | prev.remove(idx); | ||
355 | idx | ||
356 | } else { | ||
357 | idx + 1 | ||
358 | }; | ||
359 | prev.insert(insert_idx, child); | ||
360 | } | ||
361 | (_, None) => { | ||
362 | let idx = prev | ||
363 | .binary_search_by_key(&child.range.start(), |range| range.range.start()) | ||
364 | .unwrap_or_else(|x| x); | ||
365 | prev.insert(idx, child); | ||
366 | } | ||
367 | _ => { | ||
368 | unreachable!("child range should be completely contained in parent range"); | ||
369 | } | 222 | } |
370 | } | 223 | } |
371 | } | 224 | } |
372 | } | 225 | } |
373 | } | 226 | } |
374 | |||
375 | fn add(&mut self, range: HighlightedRange) { | ||
376 | self.stack | ||
377 | .last_mut() | ||
378 | .expect("during DFS traversal, the stack must not be empty") | ||
379 | .push(range) | ||
380 | } | ||
381 | |||
382 | fn flattened(mut self) -> Vec<HighlightedRange> { | ||
383 | assert_eq!( | ||
384 | self.stack.len(), | ||
385 | 1, | ||
386 | "after DFS traversal, the stack should only contain a single element" | ||
387 | ); | ||
388 | let mut res = self.stack.pop().unwrap(); | ||
389 | res.sort_by_key(|range| range.range.start()); | ||
390 | // Check that ranges are sorted and disjoint | ||
391 | for (left, right) in res.iter().zip(res.iter().skip(1)) { | ||
392 | assert!( | ||
393 | left.range.end() <= right.range.start(), | ||
394 | "left: {:#?}, right: {:#?}", | ||
395 | left, | ||
396 | right | ||
397 | ); | ||
398 | } | ||
399 | res | ||
400 | } | ||
401 | } | 227 | } |
402 | 228 | ||
403 | fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> { | 229 | fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> { |
@@ -415,523 +241,3 @@ fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> { | |||
415 | 241 | ||
416 | Some(TextRange::new(range_start, range_end)) | 242 | Some(TextRange::new(range_start, range_end)) |
417 | } | 243 | } |
418 | |||
419 | /// Returns true if the parent nodes of `node` all match the `SyntaxKind`s in `kinds` exactly. | ||
420 | fn parents_match(mut node: NodeOrToken<SyntaxNode, SyntaxToken>, mut kinds: &[SyntaxKind]) -> bool { | ||
421 | while let (Some(parent), [kind, rest @ ..]) = (&node.parent(), kinds) { | ||
422 | if parent.kind() != *kind { | ||
423 | return false; | ||
424 | } | ||
425 | |||
426 | // FIXME: Would be nice to get parent out of the match, but binding by-move and by-value | ||
427 | // in the same pattern is unstable: rust-lang/rust#68354. | ||
428 | node = node.parent().unwrap().into(); | ||
429 | kinds = rest; | ||
430 | } | ||
431 | |||
432 | // Only true if we matched all expected kinds | ||
433 | kinds.len() == 0 | ||
434 | } | ||
435 | |||
436 | fn is_consumed_lvalue( | ||
437 | node: NodeOrToken<SyntaxNode, SyntaxToken>, | ||
438 | local: &Local, | ||
439 | db: &RootDatabase, | ||
440 | ) -> bool { | ||
441 | // When lvalues are passed as arguments and they're not Copy, then mark them as Consuming. | ||
442 | parents_match(node, &[PATH_SEGMENT, PATH, PATH_EXPR, ARG_LIST]) && !local.ty(db).is_copy(db) | ||
443 | } | ||
444 | |||
445 | fn highlight_element( | ||
446 | sema: &Semantics<RootDatabase>, | ||
447 | bindings_shadow_count: &mut FxHashMap<Name, u32>, | ||
448 | syntactic_name_ref_highlighting: bool, | ||
449 | element: SyntaxElement, | ||
450 | ) -> Option<(Highlight, Option<u64>)> { | ||
451 | let db = sema.db; | ||
452 | let mut binding_hash = None; | ||
453 | let highlight: Highlight = match element.kind() { | ||
454 | FN => { | ||
455 | bindings_shadow_count.clear(); | ||
456 | return None; | ||
457 | } | ||
458 | |||
459 | // Highlight definitions depending on the "type" of the definition. | ||
460 | NAME => { | ||
461 | let name = element.into_node().and_then(ast::Name::cast).unwrap(); | ||
462 | let name_kind = NameClass::classify(sema, &name); | ||
463 | |||
464 | if let Some(NameClass::Definition(Definition::Local(local))) = &name_kind { | ||
465 | if let Some(name) = local.name(db) { | ||
466 | let shadow_count = bindings_shadow_count.entry(name.clone()).or_default(); | ||
467 | *shadow_count += 1; | ||
468 | binding_hash = Some(calc_binding_hash(&name, *shadow_count)) | ||
469 | } | ||
470 | }; | ||
471 | |||
472 | match name_kind { | ||
473 | Some(NameClass::ExternCrate(_)) => HighlightTag::Symbol(SymbolKind::Module).into(), | ||
474 | Some(NameClass::Definition(def)) => { | ||
475 | highlight_def(db, def) | HighlightModifier::Definition | ||
476 | } | ||
477 | Some(NameClass::ConstReference(def)) => highlight_def(db, def), | ||
478 | Some(NameClass::PatFieldShorthand { field_ref, .. }) => { | ||
479 | let mut h = HighlightTag::Symbol(SymbolKind::Field).into(); | ||
480 | if let Definition::Field(field) = field_ref { | ||
481 | if let VariantDef::Union(_) = field.parent_def(db) { | ||
482 | h |= HighlightModifier::Unsafe; | ||
483 | } | ||
484 | } | ||
485 | |||
486 | h | ||
487 | } | ||
488 | None => highlight_name_by_syntax(name) | HighlightModifier::Definition, | ||
489 | } | ||
490 | } | ||
491 | |||
492 | // Highlight references like the definitions they resolve to | ||
493 | NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => { | ||
494 | // even though we track whether we are in an attribute or not we still need this special case | ||
495 | // as otherwise we would emit unresolved references for name refs inside attributes | ||
496 | Highlight::from(HighlightTag::Symbol(SymbolKind::Function)) | ||
497 | } | ||
498 | NAME_REF => { | ||
499 | let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap(); | ||
500 | highlight_func_by_name_ref(sema, &name_ref).unwrap_or_else(|| { | ||
501 | match NameRefClass::classify(sema, &name_ref) { | ||
502 | Some(name_kind) => match name_kind { | ||
503 | NameRefClass::ExternCrate(_) => { | ||
504 | HighlightTag::Symbol(SymbolKind::Module).into() | ||
505 | } | ||
506 | NameRefClass::Definition(def) => { | ||
507 | if let Definition::Local(local) = &def { | ||
508 | if let Some(name) = local.name(db) { | ||
509 | let shadow_count = | ||
510 | bindings_shadow_count.entry(name.clone()).or_default(); | ||
511 | binding_hash = Some(calc_binding_hash(&name, *shadow_count)) | ||
512 | } | ||
513 | }; | ||
514 | |||
515 | let mut h = highlight_def(db, def); | ||
516 | |||
517 | if let Definition::Local(local) = &def { | ||
518 | if is_consumed_lvalue(name_ref.syntax().clone().into(), local, db) { | ||
519 | h |= HighlightModifier::Consuming; | ||
520 | } | ||
521 | } | ||
522 | |||
523 | if let Some(parent) = name_ref.syntax().parent() { | ||
524 | if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) { | ||
525 | if let Definition::Field(field) = def { | ||
526 | if let VariantDef::Union(_) = field.parent_def(db) { | ||
527 | h |= HighlightModifier::Unsafe; | ||
528 | } | ||
529 | } | ||
530 | } | ||
531 | } | ||
532 | |||
533 | h | ||
534 | } | ||
535 | NameRefClass::FieldShorthand { .. } => { | ||
536 | HighlightTag::Symbol(SymbolKind::Field).into() | ||
537 | } | ||
538 | }, | ||
539 | None if syntactic_name_ref_highlighting => { | ||
540 | highlight_name_ref_by_syntax(name_ref, sema) | ||
541 | } | ||
542 | None => HighlightTag::UnresolvedReference.into(), | ||
543 | } | ||
544 | }) | ||
545 | } | ||
546 | |||
547 | // Simple token-based highlighting | ||
548 | COMMENT => { | ||
549 | let comment = element.into_token().and_then(ast::Comment::cast)?; | ||
550 | let h = HighlightTag::Comment; | ||
551 | match comment.kind().doc { | ||
552 | Some(_) => h | HighlightModifier::Documentation, | ||
553 | None => h.into(), | ||
554 | } | ||
555 | } | ||
556 | STRING | BYTE_STRING => HighlightTag::StringLiteral.into(), | ||
557 | ATTR => HighlightTag::Attribute.into(), | ||
558 | INT_NUMBER | FLOAT_NUMBER => HighlightTag::NumericLiteral.into(), | ||
559 | BYTE => HighlightTag::ByteLiteral.into(), | ||
560 | CHAR => HighlightTag::CharLiteral.into(), | ||
561 | QUESTION => Highlight::new(HighlightTag::Operator) | HighlightModifier::ControlFlow, | ||
562 | LIFETIME => { | ||
563 | let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap(); | ||
564 | |||
565 | match NameClass::classify_lifetime(sema, &lifetime) { | ||
566 | Some(NameClass::Definition(def)) => { | ||
567 | highlight_def(db, def) | HighlightModifier::Definition | ||
568 | } | ||
569 | None => match NameRefClass::classify_lifetime(sema, &lifetime) { | ||
570 | Some(NameRefClass::Definition(def)) => highlight_def(db, def), | ||
571 | _ => Highlight::new(HighlightTag::Symbol(SymbolKind::LifetimeParam)), | ||
572 | }, | ||
573 | _ => { | ||
574 | Highlight::new(HighlightTag::Symbol(SymbolKind::LifetimeParam)) | ||
575 | | HighlightModifier::Definition | ||
576 | } | ||
577 | } | ||
578 | } | ||
579 | p if p.is_punct() => match p { | ||
580 | T![&] => { | ||
581 | let h = HighlightTag::Operator.into(); | ||
582 | let is_unsafe = element | ||
583 | .parent() | ||
584 | .and_then(ast::RefExpr::cast) | ||
585 | .map(|ref_expr| sema.is_unsafe_ref_expr(&ref_expr)) | ||
586 | .unwrap_or(false); | ||
587 | if is_unsafe { | ||
588 | h | HighlightModifier::Unsafe | ||
589 | } else { | ||
590 | h | ||
591 | } | ||
592 | } | ||
593 | T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => { | ||
594 | HighlightTag::Operator.into() | ||
595 | } | ||
596 | T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => { | ||
597 | HighlightTag::Symbol(SymbolKind::Macro).into() | ||
598 | } | ||
599 | T![!] if element.parent().and_then(ast::NeverType::cast).is_some() => { | ||
600 | HighlightTag::BuiltinType.into() | ||
601 | } | ||
602 | T![*] if element.parent().and_then(ast::PtrType::cast).is_some() => { | ||
603 | HighlightTag::Keyword.into() | ||
604 | } | ||
605 | T![*] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { | ||
606 | let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?; | ||
607 | |||
608 | let expr = prefix_expr.expr()?; | ||
609 | let ty = sema.type_of_expr(&expr)?; | ||
610 | if ty.is_raw_ptr() { | ||
611 | HighlightTag::Operator | HighlightModifier::Unsafe | ||
612 | } else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() { | ||
613 | HighlightTag::Operator.into() | ||
614 | } else { | ||
615 | HighlightTag::Punctuation.into() | ||
616 | } | ||
617 | } | ||
618 | T![-] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { | ||
619 | let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?; | ||
620 | |||
621 | let expr = prefix_expr.expr()?; | ||
622 | match expr { | ||
623 | ast::Expr::Literal(_) => HighlightTag::NumericLiteral, | ||
624 | _ => HighlightTag::Operator, | ||
625 | } | ||
626 | .into() | ||
627 | } | ||
628 | _ if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { | ||
629 | HighlightTag::Operator.into() | ||
630 | } | ||
631 | _ if element.parent().and_then(ast::BinExpr::cast).is_some() => { | ||
632 | HighlightTag::Operator.into() | ||
633 | } | ||
634 | _ if element.parent().and_then(ast::RangeExpr::cast).is_some() => { | ||
635 | HighlightTag::Operator.into() | ||
636 | } | ||
637 | _ if element.parent().and_then(ast::RangePat::cast).is_some() => { | ||
638 | HighlightTag::Operator.into() | ||
639 | } | ||
640 | _ if element.parent().and_then(ast::RestPat::cast).is_some() => { | ||
641 | HighlightTag::Operator.into() | ||
642 | } | ||
643 | _ if element.parent().and_then(ast::Attr::cast).is_some() => { | ||
644 | HighlightTag::Attribute.into() | ||
645 | } | ||
646 | _ => HighlightTag::Punctuation.into(), | ||
647 | }, | ||
648 | |||
649 | k if k.is_keyword() => { | ||
650 | let h = Highlight::new(HighlightTag::Keyword); | ||
651 | match k { | ||
652 | T![break] | ||
653 | | T![continue] | ||
654 | | T![else] | ||
655 | | T![if] | ||
656 | | T![loop] | ||
657 | | T![match] | ||
658 | | T![return] | ||
659 | | T![while] | ||
660 | | T![in] => h | HighlightModifier::ControlFlow, | ||
661 | T![for] if !is_child_of_impl(&element) => h | HighlightModifier::ControlFlow, | ||
662 | T![unsafe] => h | HighlightModifier::Unsafe, | ||
663 | T![true] | T![false] => HighlightTag::BoolLiteral.into(), | ||
664 | T![self] => { | ||
665 | let self_param_is_mut = element | ||
666 | .parent() | ||
667 | .and_then(ast::SelfParam::cast) | ||
668 | .and_then(|p| p.mut_token()) | ||
669 | .is_some(); | ||
670 | let self_path = &element | ||
671 | .parent() | ||
672 | .as_ref() | ||
673 | .and_then(SyntaxNode::parent) | ||
674 | .and_then(ast::Path::cast) | ||
675 | .and_then(|p| sema.resolve_path(&p)); | ||
676 | let mut h = HighlightTag::Symbol(SymbolKind::SelfParam).into(); | ||
677 | if self_param_is_mut | ||
678 | || matches!(self_path, | ||
679 | Some(hir::PathResolution::Local(local)) | ||
680 | if local.is_self(db) | ||
681 | && (local.is_mut(db) || local.ty(db).is_mutable_reference()) | ||
682 | ) | ||
683 | { | ||
684 | h |= HighlightModifier::Mutable | ||
685 | } | ||
686 | |||
687 | if let Some(hir::PathResolution::Local(local)) = self_path { | ||
688 | if is_consumed_lvalue(element, &local, db) { | ||
689 | h |= HighlightModifier::Consuming; | ||
690 | } | ||
691 | } |