diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-07 09:57:04 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-07 09:57:04 +0000 |
commit | b94e1eee8341cb2a16b89711d65b382549fd2bde (patch) | |
tree | 6d7d8c3e6ccc95a7f9a7c4398516393ff2786623 /crates/ra_ide_api | |
parent | b1a1d20e067c25fb80fbab43b2956b6747a8dd3c (diff) | |
parent | 064707c5a05e360d777b0b3ccbd8e30ad4acbda1 (diff) |
Merge #942
942: Hover for associated items in patterns r=matklad a=kjeremy
Co-authored-by: kjeremy <[email protected]>
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r-- | crates/ra_ide_api/src/goto_definition.rs | 27 | ||||
-rw-r--r-- | crates/ra_ide_api/src/hover.rs | 23 |
2 files changed, 46 insertions, 4 deletions
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 364263d9b..dd5f9f31c 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs | |||
@@ -100,6 +100,7 @@ pub(crate) fn reference_definition( | |||
100 | } | 100 | } |
101 | } | 101 | } |
102 | } | 102 | } |
103 | |||
103 | // Try name resolution | 104 | // Try name resolution |
104 | let resolver = hir::source_binder::resolver_for_node(db, file_id, name_ref.syntax()); | 105 | let resolver = hir::source_binder::resolver_for_node(db, file_id, name_ref.syntax()); |
105 | if let Some(path) = | 106 | if let Some(path) = |
@@ -126,17 +127,35 @@ pub(crate) fn reference_definition( | |||
126 | None => { | 127 | None => { |
127 | // If we failed to resolve then check associated items | 128 | // If we failed to resolve then check associated items |
128 | if let Some(function) = function { | 129 | if let Some(function) = function { |
129 | // Should we do this above and then grab path from the PathExpr? | 130 | // Resolve associated item for path expressions |
130 | if let Some(path_expr) = | 131 | if let Some(path_expr) = |
131 | name_ref.syntax().ancestors().find_map(ast::PathExpr::cast) | 132 | name_ref.syntax().ancestors().find_map(ast::PathExpr::cast) |
132 | { | 133 | { |
133 | let infer_result = function.infer(db); | 134 | let infer_result = function.infer(db); |
134 | let source_map = function.body_source_map(db); | 135 | let source_map = function.body_source_map(db); |
135 | let expr = ast::Expr::cast(path_expr.syntax()).unwrap(); | 136 | |
137 | if let Some(expr) = ast::Expr::cast(path_expr.syntax()) { | ||
138 | if let Some(res) = source_map | ||
139 | .node_expr(expr) | ||
140 | .and_then(|it| infer_result.assoc_resolutions_for_expr(it.into())) | ||
141 | { | ||
142 | return Exact(NavigationTarget::from_impl_item(db, res)); | ||
143 | } | ||
144 | } | ||
145 | } | ||
146 | |||
147 | // Resolve associated item for path patterns | ||
148 | if let Some(path_pat) = | ||
149 | name_ref.syntax().ancestors().find_map(ast::PathPat::cast) | ||
150 | { | ||
151 | let infer_result = function.infer(db); | ||
152 | let source_map = function.body_source_map(db); | ||
153 | |||
154 | let pat: &ast::Pat = path_pat.into(); | ||
136 | 155 | ||
137 | if let Some(res) = source_map | 156 | if let Some(res) = source_map |
138 | .node_expr(expr) | 157 | .node_pat(pat) |
139 | .and_then(|it| infer_result.assoc_resolutions_for_expr(it.into())) | 158 | .and_then(|it| infer_result.assoc_resolutions_for_pat(it.into())) |
140 | { | 159 | { |
141 | return Exact(NavigationTarget::from_impl_item(db, res)); | 160 | return Exact(NavigationTarget::from_impl_item(db, res)); |
142 | } | 161 | } |
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index bcd052c8b..638c24e31 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs | |||
@@ -534,4 +534,27 @@ mod tests { | |||
534 | assert_eq!(trim_markup_opt(hover.info.first()), Some("fn new() -> Thing")); | 534 | assert_eq!(trim_markup_opt(hover.info.first()), Some("fn new() -> Thing")); |
535 | assert_eq!(hover.info.is_exact(), true); | 535 | assert_eq!(hover.info.is_exact(), true); |
536 | } | 536 | } |
537 | |||
538 | #[test] | ||
539 | fn test_hover_infer_associated_const_in_pattern() { | ||
540 | let (analysis, position) = single_file_with_position( | ||
541 | " | ||
542 | struct X; | ||
543 | impl X { | ||
544 | const C: u32 = 1; | ||
545 | } | ||
546 | |||
547 | fn main() { | ||
548 | match 1 { | ||
549 | X::C<|> => {}, | ||
550 | 2 => {}, | ||
551 | _ => {} | ||
552 | }; | ||
553 | } | ||
554 | ", | ||
555 | ); | ||
556 | let hover = analysis.hover(position).unwrap().unwrap(); | ||
557 | assert_eq!(trim_markup_opt(hover.info.first()), Some("const C: u32")); | ||
558 | assert_eq!(hover.info.is_exact(), true); | ||
559 | } | ||
537 | } | 560 | } |