aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/code_model_api.rs4
-rw-r--r--crates/ra_hir/src/code_model_impl/module.rs2
-rw-r--r--crates/ra_hir/src/expr.rs4
-rw-r--r--crates/ra_hir/src/ids.rs7
-rw-r--r--crates/ra_hir/src/ty.rs6
-rw-r--r--crates/ra_ide_api/src/completion/complete_dot.rs2
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs2
-rw-r--r--crates/ra_ide_api/src/completion/completion_item.rs2
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs6
-rw-r--r--crates/ra_ide_api/src/navigation_target.rs34
-rw-r--r--crates/ra_ide_api/src/parent_module.rs2
11 files changed, 29 insertions, 42 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index f6d1d6b23..f28e077c6 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -297,8 +297,8 @@ impl Function {
297 self.def_id 297 self.def_id
298 } 298 }
299 299
300 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::FnDef>)> { 300 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::FnDef>) {
301 Ok(def_id_to_ast(db, self.def_id)) 301 def_id_to_ast(db, self.def_id)
302 } 302 }
303 303
304 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> { 304 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> {
diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs
index f27523753..331b0500e 100644
--- a/crates/ra_hir/src/code_model_impl/module.rs
+++ b/crates/ra_hir/src/code_model_impl/module.rs
@@ -148,7 +148,7 @@ impl Module {
148 } else { 148 } else {
149 return Ok(PerNs::none()); 149 return Ok(PerNs::none());
150 }; 150 };
151 let module = match curr.resolve(db)? { 151 let module = match curr.resolve(db) {
152 Def::Module(it) => it, 152 Def::Module(it) => it,
153 Def::Enum(e) => { 153 Def::Enum(e) => {
154 if segments.len() == idx + 1 { 154 if segments.len() == idx + 1 {
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index 5081466a2..a1e8da348 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -832,10 +832,10 @@ pub(crate) fn body_syntax_mapping(
832 db: &impl HirDatabase, 832 db: &impl HirDatabase,
833 def_id: DefId, 833 def_id: DefId,
834) -> Cancelable<Arc<BodySyntaxMapping>> { 834) -> Cancelable<Arc<BodySyntaxMapping>> {
835 let def = def_id.resolve(db)?; 835 let def = def_id.resolve(db);
836 836
837 let body_syntax_mapping = match def { 837 let body_syntax_mapping = match def {
838 Def::Function(f) => collect_fn_body_syntax(&f.source(db)?.1), 838 Def::Function(f) => collect_fn_body_syntax(&f.source(db).1),
839 // TODO: consts, etc. 839 // TODO: consts, etc.
840 _ => panic!("Trying to get body for item type without body"), 840 _ => panic!("Trying to get body for item type without body"),
841 }; 841 };
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index 7b572061a..3cbf8070f 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -159,9 +159,9 @@ impl DefId {
159 db.as_ref().id2loc(self) 159 db.as_ref().id2loc(self)
160 } 160 }
161 161
162 pub fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> { 162 pub fn resolve(self, db: &impl HirDatabase) -> Def {
163 let loc = self.loc(db); 163 let loc = self.loc(db);
164 let res = match loc.kind { 164 match loc.kind {
165 DefKind::Module => { 165 DefKind::Module => {
166 let module = Module::from_module_id(db, loc.source_root_id, loc.module_id); 166 let module = Module::from_module_id(db, loc.source_root_id, loc.module_id);
167 Def::Module(module) 167 Def::Module(module)
@@ -195,8 +195,7 @@ impl DefId {
195 195
196 DefKind::StructCtor => Def::Item, 196 DefKind::StructCtor => Def::Item,
197 DefKind::Item => Def::Item, 197 DefKind::Item => Def::Item,
198 }; 198 }
199 Ok(res)
200 } 199 }
201 200
202 pub(crate) fn source(self, db: &impl HirDatabase) -> (HirFileId, TreeArc<SyntaxNode>) { 201 pub(crate) fn source(self, db: &impl HirDatabase) -> (HirFileId, TreeArc<SyntaxNode>) {
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index b0642d905..3607969ed 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -481,7 +481,7 @@ pub(crate) fn type_for_enum_variant(db: &impl HirDatabase, ev: EnumVariant) -> C
481} 481}
482 482
483pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Ty> { 483pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Ty> {
484 let def = def_id.resolve(db)?; 484 let def = def_id.resolve(db);
485 match def { 485 match def {
486 Def::Module(..) => { 486 Def::Module(..) => {
487 log::debug!("trying to get type for module {:?}", def_id); 487 log::debug!("trying to get type for module {:?}", def_id);
@@ -507,7 +507,7 @@ pub(super) fn type_for_field(
507 def_id: DefId, 507 def_id: DefId,
508 field: Name, 508 field: Name,
509) -> Cancelable<Option<Ty>> { 509) -> Cancelable<Option<Ty>> {
510 let def = def_id.resolve(db)?; 510 let def = def_id.resolve(db);
511 let variant_data = match def { 511 let variant_data = match def {
512 Def::Struct(s) => s.variant_data(db)?, 512 Def::Struct(s) => s.variant_data(db)?,
513 Def::EnumVariant(ev) => ev.variant_data(db), 513 Def::EnumVariant(ev) => ev.variant_data(db),
@@ -877,7 +877,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
877 } else { 877 } else {
878 return Ok((Ty::Unknown, None)); 878 return Ok((Ty::Unknown, None));
879 }; 879 };
880 Ok(match def_id.resolve(self.db)? { 880 Ok(match def_id.resolve(self.db) {
881 Def::Struct(s) => { 881 Def::Struct(s) => {
882 let ty = type_for_struct(self.db, s)?; 882 let ty = type_for_struct(self.db, s)?;
883 (ty, Some(def_id)) 883 (ty, Some(def_id))
diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs
index bf6d91a09..886dc54d4 100644
--- a/crates/ra_ide_api/src/completion/complete_dot.rs
+++ b/crates/ra_ide_api/src/completion/complete_dot.rs
@@ -27,7 +27,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
27 for receiver in receiver.autoderef(ctx.db) { 27 for receiver in receiver.autoderef(ctx.db) {
28 match receiver { 28 match receiver {
29 Ty::Adt { def_id, .. } => { 29 Ty::Adt { def_id, .. } => {
30 match def_id.resolve(ctx.db)? { 30 match def_id.resolve(ctx.db) {
31 Def::Struct(s) => { 31 Def::Struct(s) => {
32 for field in s.fields(ctx.db) { 32 for field in s.fields(ctx.db) {
33 CompletionItem::new( 33 CompletionItem::new(
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs
index 6388197be..207a509b3 100644
--- a/crates/ra_ide_api/src/completion/complete_path.rs
+++ b/crates/ra_ide_api/src/completion/complete_path.rs
@@ -12,7 +12,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C
12 Some(it) => it, 12 Some(it) => it,
13 None => return Ok(()), 13 None => return Ok(()),
14 }; 14 };
15 match def_id.resolve(ctx.db)? { 15 match def_id.resolve(ctx.db) {
16 hir::Def::Module(module) => { 16 hir::Def::Module(module) => {
17 let module_scope = module.scope(ctx.db)?; 17 let module_scope = module.scope(ctx.db)?;
18 for (name, res) in module_scope.entries() { 18 for (name, res) in module_scope.entries() {
diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs
index b75d65de3..11d00f78c 100644
--- a/crates/ra_ide_api/src/completion/completion_item.rs
+++ b/crates/ra_ide_api/src/completion/completion_item.rs
@@ -144,7 +144,7 @@ impl Builder {
144 ctx: &CompletionContext, 144 ctx: &CompletionContext,
145 resolution: &hir::Resolution, 145 resolution: &hir::Resolution,
146 ) -> Builder { 146 ) -> Builder {
147 let resolved = resolution.def_id.and_then(|d| d.resolve(ctx.db).ok()); 147 let resolved = resolution.def_id.map(|d| d.resolve(ctx.db));
148 let kind = match resolved { 148 let kind = match resolved {
149 PerNs { 149 PerNs {
150 types: Some(hir::Def::Module(..)), 150 types: Some(hir::Def::Module(..)),
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs
index 591f36cce..7229293a4 100644
--- a/crates/ra_ide_api/src/goto_definition.rs
+++ b/crates/ra_ide_api/src/goto_definition.rs
@@ -70,7 +70,7 @@ pub(crate) fn reference_definition(
70 .node_expr(expr) 70 .node_expr(expr)
71 .and_then(|it| infer_result.method_resolution(it)) 71 .and_then(|it| infer_result.method_resolution(it))
72 { 72 {
73 if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)?)? { 73 if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)) {
74 return Ok(Exact(target)); 74 return Ok(Exact(target));
75 } 75 }
76 }; 76 };
@@ -87,7 +87,7 @@ pub(crate) fn reference_definition(
87 { 87 {
88 let resolved = module.resolve_path(db, &path)?; 88 let resolved = module.resolve_path(db, &path)?;
89 if let Some(def_id) = resolved.take_types().or(resolved.take_values()) { 89 if let Some(def_id) = resolved.take_types().or(resolved.take_values()) {
90 if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)?)? { 90 if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)) {
91 return Ok(Exact(target)); 91 return Ok(Exact(target));
92 } 92 }
93 } 93 }
@@ -112,7 +112,7 @@ fn name_definition(
112 if let Some(child_module) = 112 if let Some(child_module) =
113 hir::source_binder::module_from_declaration(db, file_id, module) 113 hir::source_binder::module_from_declaration(db, file_id, module)
114 { 114 {
115 let nav = NavigationTarget::from_module(db, child_module)?; 115 let nav = NavigationTarget::from_module(db, child_module);
116 return Ok(Some(vec![nav])); 116 return Ok(Some(vec![nav]));
117 } 117 }
118 } 118 }
diff --git a/crates/ra_ide_api/src/navigation_target.rs b/crates/ra_ide_api/src/navigation_target.rs
index 965dfc0ca..21c15c0c0 100644
--- a/crates/ra_ide_api/src/navigation_target.rs
+++ b/crates/ra_ide_api/src/navigation_target.rs
@@ -1,4 +1,4 @@
1use ra_db::{FileId, Cancelable}; 1use ra_db::FileId;
2use ra_syntax::{ 2use ra_syntax::{
3 SyntaxNode, AstNode, SmolStr, TextRange, ast, 3 SyntaxNode, AstNode, SmolStr, TextRange, ast,
4 SyntaxKind::{self, NAME}, 4 SyntaxKind::{self, NAME},
@@ -69,47 +69,35 @@ impl NavigationTarget {
69 } 69 }
70 } 70 }
71 71
72 pub(crate) fn from_module( 72 pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
73 db: &RootDatabase,
74 module: hir::Module,
75 ) -> Cancelable<NavigationTarget> {
76 let (file_id, source) = module.definition_source(db); 73 let (file_id, source) = module.definition_source(db);
77 let name = module 74 let name = module
78 .name(db) 75 .name(db)
79 .map(|it| it.to_string().into()) 76 .map(|it| it.to_string().into())
80 .unwrap_or_default(); 77 .unwrap_or_default();
81 let res = match source { 78 match source {
82 ModuleSource::SourceFile(node) => { 79 ModuleSource::SourceFile(node) => {
83 NavigationTarget::from_syntax(file_id, name, None, node.syntax()) 80 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
84 } 81 }
85 ModuleSource::Module(node) => { 82 ModuleSource::Module(node) => {
86 NavigationTarget::from_syntax(file_id, name, None, node.syntax()) 83 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
87 } 84 }
88 }; 85 }
89 Ok(res)
90 } 86 }
91 87
92 pub(crate) fn from_module_to_decl( 88 pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
93 db: &RootDatabase,
94 module: hir::Module,
95 ) -> Cancelable<NavigationTarget> {
96 let name = module 89 let name = module
97 .name(db) 90 .name(db)
98 .map(|it| it.to_string().into()) 91 .map(|it| it.to_string().into())
99 .unwrap_or_default(); 92 .unwrap_or_default();
100 if let Some((file_id, source)) = module.declaration_source(db) { 93 if let Some((file_id, source)) = module.declaration_source(db) {
101 return Ok(NavigationTarget::from_syntax( 94 return NavigationTarget::from_syntax(file_id, name, None, source.syntax());
102 file_id,
103 name,
104 None,
105 source.syntax(),
106 ));
107 } 95 }
108 NavigationTarget::from_module(db, module) 96 NavigationTarget::from_module(db, module)
109 } 97 }
110 98
111 // TODO once Def::Item is gone, this should be able to always return a NavigationTarget 99 // TODO once Def::Item is gone, this should be able to always return a NavigationTarget
112 pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> { 100 pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Option<NavigationTarget> {
113 let res = match def { 101 let res = match def {
114 Def::Struct(s) => { 102 Def::Struct(s) => {
115 let (file_id, node) = s.source(db); 103 let (file_id, node) = s.source(db);
@@ -124,7 +112,7 @@ impl NavigationTarget {
124 NavigationTarget::from_named(file_id.original_file(db), &*node) 112 NavigationTarget::from_named(file_id.original_file(db), &*node)
125 } 113 }
126 Def::Function(f) => { 114 Def::Function(f) => {
127 let (file_id, node) = f.source(db)?; 115 let (file_id, node) = f.source(db);
128 NavigationTarget::from_named(file_id.original_file(db), &*node) 116 NavigationTarget::from_named(file_id.original_file(db), &*node)
129 } 117 }
130 Def::Trait(f) => { 118 Def::Trait(f) => {
@@ -143,10 +131,10 @@ impl NavigationTarget {
143 let (file_id, node) = f.source(db); 131 let (file_id, node) = f.source(db);
144 NavigationTarget::from_named(file_id.original_file(db), &*node) 132 NavigationTarget::from_named(file_id.original_file(db), &*node)
145 } 133 }
146 Def::Module(m) => NavigationTarget::from_module(db, m)?, 134 Def::Module(m) => NavigationTarget::from_module(db, m),
147 Def::Item => return Ok(None), 135 Def::Item => return None,
148 }; 136 };
149 Ok(Some(res)) 137 Some(res)
150 } 138 }
151 139
152 #[cfg(test)] 140 #[cfg(test)]
diff --git a/crates/ra_ide_api/src/parent_module.rs b/crates/ra_ide_api/src/parent_module.rs
index 451304739..379b3f3a4 100644
--- a/crates/ra_ide_api/src/parent_module.rs
+++ b/crates/ra_ide_api/src/parent_module.rs
@@ -12,7 +12,7 @@ pub(crate) fn parent_module(
12 None => return Ok(Vec::new()), 12 None => return Ok(Vec::new()),
13 Some(it) => it, 13 Some(it) => it,
14 }; 14 };
15 let nav = NavigationTarget::from_module_to_decl(db, module)?; 15 let nav = NavigationTarget::from_module_to_decl(db, module);
16 Ok(vec![nav]) 16 Ok(vec![nav])
17} 17}
18 18