diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-02-22 15:59:58 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-02-22 15:59:58 +0000 |
commit | e91375c763a74ebf3784f2d1df542095eb8a159d (patch) | |
tree | 6c6020aea89d7ffbd88131974a9701f39573ec94 /crates/ra_ide/src/display/navigation_target.rs | |
parent | 46dbe4dc80ec1cca9677a5c40348712e9e713f12 (diff) | |
parent | fda118f4e90fb3341f0aa2a6dbb583acbf39aabd (diff) |
Merge #3274
3274: Simplify r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide/src/display/navigation_target.rs')
-rw-r--r-- | crates/ra_ide/src/display/navigation_target.rs | 66 |
1 files changed, 41 insertions, 25 deletions
diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs index 096c41c81..b42cb477e 100644 --- a/crates/ra_ide/src/display/navigation_target.rs +++ b/crates/ra_ide/src/display/navigation_target.rs | |||
@@ -11,7 +11,7 @@ use ra_syntax::{ | |||
11 | TextRange, | 11 | TextRange, |
12 | }; | 12 | }; |
13 | 13 | ||
14 | use crate::{expand::original_range, FileSymbol}; | 14 | use crate::{expand::original_range, references::NameDefinition, FileSymbol}; |
15 | 15 | ||
16 | use super::short_label::ShortLabel; | 16 | use super::short_label::ShortLabel; |
17 | 17 | ||
@@ -36,6 +36,10 @@ pub(crate) trait ToNav { | |||
36 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget; | 36 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget; |
37 | } | 37 | } |
38 | 38 | ||
39 | pub(crate) trait TryToNav { | ||
40 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget>; | ||
41 | } | ||
42 | |||
39 | impl NavigationTarget { | 43 | impl NavigationTarget { |
40 | /// When `focus_range` is specified, returns it. otherwise | 44 | /// When `focus_range` is specified, returns it. otherwise |
41 | /// returns `full_range` | 45 | /// returns `full_range` |
@@ -96,26 +100,6 @@ impl NavigationTarget { | |||
96 | module.to_nav(db) | 100 | module.to_nav(db) |
97 | } | 101 | } |
98 | 102 | ||
99 | pub(crate) fn from_def( | ||
100 | db: &RootDatabase, | ||
101 | module_def: hir::ModuleDef, | ||
102 | ) -> Option<NavigationTarget> { | ||
103 | let nav = match module_def { | ||
104 | hir::ModuleDef::Module(module) => module.to_nav(db), | ||
105 | hir::ModuleDef::Function(it) => it.to_nav(db), | ||
106 | hir::ModuleDef::Adt(it) => it.to_nav(db), | ||
107 | hir::ModuleDef::Const(it) => it.to_nav(db), | ||
108 | hir::ModuleDef::Static(it) => it.to_nav(db), | ||
109 | hir::ModuleDef::EnumVariant(it) => it.to_nav(db), | ||
110 | hir::ModuleDef::Trait(it) => it.to_nav(db), | ||
111 | hir::ModuleDef::TypeAlias(it) => it.to_nav(db), | ||
112 | hir::ModuleDef::BuiltinType(..) => { | ||
113 | return None; | ||
114 | } | ||
115 | }; | ||
116 | Some(nav) | ||
117 | } | ||
118 | |||
119 | #[cfg(test)] | 103 | #[cfg(test)] |
120 | pub(crate) fn assert_match(&self, expected: &str) { | 104 | pub(crate) fn assert_match(&self, expected: &str) { |
121 | let actual = self.debug_render(); | 105 | let actual = self.debug_render(); |
@@ -201,6 +185,36 @@ impl ToNav for FileSymbol { | |||
201 | } | 185 | } |
202 | } | 186 | } |
203 | 187 | ||
188 | impl TryToNav for NameDefinition { | ||
189 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { | ||
190 | match self { | ||
191 | NameDefinition::Macro(it) => Some(it.to_nav(db)), | ||
192 | NameDefinition::StructField(it) => Some(it.to_nav(db)), | ||
193 | NameDefinition::ModuleDef(it) => it.try_to_nav(db), | ||
194 | NameDefinition::SelfType(it) => Some(it.to_nav(db)), | ||
195 | NameDefinition::Local(it) => Some(it.to_nav(db)), | ||
196 | NameDefinition::TypeParam(it) => Some(it.to_nav(db)), | ||
197 | } | ||
198 | } | ||
199 | } | ||
200 | |||
201 | impl TryToNav for hir::ModuleDef { | ||
202 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { | ||
203 | let res = match self { | ||
204 | hir::ModuleDef::Module(it) => it.to_nav(db), | ||
205 | hir::ModuleDef::Function(it) => it.to_nav(db), | ||
206 | hir::ModuleDef::Adt(it) => it.to_nav(db), | ||
207 | hir::ModuleDef::EnumVariant(it) => it.to_nav(db), | ||
208 | hir::ModuleDef::Const(it) => it.to_nav(db), | ||
209 | hir::ModuleDef::Static(it) => it.to_nav(db), | ||
210 | hir::ModuleDef::Trait(it) => it.to_nav(db), | ||
211 | hir::ModuleDef::TypeAlias(it) => it.to_nav(db), | ||
212 | hir::ModuleDef::BuiltinType(_) => return None, | ||
213 | }; | ||
214 | Some(res) | ||
215 | } | ||
216 | } | ||
217 | |||
204 | pub(crate) trait ToNavFromAst {} | 218 | pub(crate) trait ToNavFromAst {} |
205 | impl ToNavFromAst for hir::Function {} | 219 | impl ToNavFromAst for hir::Function {} |
206 | impl ToNavFromAst for hir::Const {} | 220 | impl ToNavFromAst for hir::Const {} |
@@ -232,15 +246,17 @@ impl ToNav for hir::Module { | |||
232 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 246 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { |
233 | let src = self.definition_source(db); | 247 | let src = self.definition_source(db); |
234 | let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default(); | 248 | let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default(); |
235 | let syntax = match &src.value { | 249 | let (syntax, focus) = match &src.value { |
236 | ModuleSource::SourceFile(node) => node.syntax(), | 250 | ModuleSource::SourceFile(node) => (node.syntax(), None), |
237 | ModuleSource::Module(node) => node.syntax(), | 251 | ModuleSource::Module(node) => { |
252 | (node.syntax(), node.name().map(|it| it.syntax().text_range())) | ||
253 | } | ||
238 | }; | 254 | }; |
239 | let frange = original_range(db, src.with_value(syntax)); | 255 | let frange = original_range(db, src.with_value(syntax)); |
240 | NavigationTarget::from_syntax( | 256 | NavigationTarget::from_syntax( |
241 | frange.file_id, | 257 | frange.file_id, |
242 | name, | 258 | name, |
243 | None, | 259 | focus, |
244 | frange.range, | 260 | frange.range, |
245 | syntax.kind(), | 261 | syntax.kind(), |
246 | None, | 262 | None, |