aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-24 22:56:13 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-24 22:56:13 +0000
commitc42db0bbd750fae19a91f0a0354240ea6c3bafce (patch)
treebeb7030248280fd8c67eb2b2c9cc4b19c6074c17 /crates/ra_ide_api/src
parentb308375b82a33687f93468d75c7cc628b83a1351 (diff)
parent31d3a56b1865c33ef54e5d76e606965c87676695 (diff)
Merge #623
623: WIP: module id is not def id r=matklad a=matklad This achieves two things: * makes module_tree & item_map per crate, not per source_root * begins the refactoring to remove universal `DefId` in favor of having separate ids for each kind of `Def`. Currently, only modules get a differnt ID though. Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r--crates/ra_ide_api/src/completion/complete_dot.rs9
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs6
-rw-r--r--crates/ra_ide_api/src/completion/completion_context.rs2
-rw-r--r--crates/ra_ide_api/src/completion/completion_item.rs59
-rw-r--r--crates/ra_ide_api/src/db.rs1
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs8
-rw-r--r--crates/ra_ide_api/src/navigation_target.rs86
-rw-r--r--crates/ra_ide_api/src/rename.rs25
-rw-r--r--crates/ra_ide_api/src/snapshots/tests__rename_mod.snap10
9 files changed, 108 insertions, 98 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs
index 07007d03f..6a9358d33 100644
--- a/crates/ra_ide_api/src/completion/complete_dot.rs
+++ b/crates/ra_ide_api/src/completion/complete_dot.rs
@@ -1,4 +1,4 @@
1use hir::{Ty, Def}; 1use hir::{Ty, AdtDef};
2 2
3use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind}; 3use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind};
4use crate::completion::completion_item::CompletionKind; 4use crate::completion::completion_item::CompletionKind;
@@ -28,8 +28,8 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
28 Ty::Adt { 28 Ty::Adt {
29 def_id, ref substs, .. 29 def_id, ref substs, ..
30 } => { 30 } => {
31 match def_id.resolve(ctx.db) { 31 match def_id {
32 Def::Struct(s) => { 32 AdtDef::Struct(s) => {
33 for field in s.fields(ctx.db) { 33 for field in s.fields(ctx.db) {
34 CompletionItem::new( 34 CompletionItem::new(
35 CompletionKind::Reference, 35 CompletionKind::Reference,
@@ -41,8 +41,9 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
41 .add_to(acc); 41 .add_to(acc);
42 } 42 }
43 } 43 }
44
44 // TODO unions 45 // TODO unions
45 _ => {} 46 AdtDef::Enum(_) => (),
46 } 47 }
47 } 48 }
48 Ty::Tuple(fields) => { 49 Ty::Tuple(fields) => {
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs
index e44b76c4a..e72586e2e 100644
--- a/crates/ra_ide_api/src/completion/complete_path.rs
+++ b/crates/ra_ide_api/src/completion/complete_path.rs
@@ -13,8 +13,8 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
13 Some(it) => it, 13 Some(it) => it,
14 None => return, 14 None => return,
15 }; 15 };
16 match def_id.resolve(ctx.db) { 16 match def_id {
17 hir::Def::Module(module) => { 17 hir::ModuleDef::Module(module) => {
18 let module_scope = module.scope(ctx.db); 18 let module_scope = module.scope(ctx.db);
19 for (name, res) in module_scope.entries() { 19 for (name, res) in module_scope.entries() {
20 CompletionItem::new( 20 CompletionItem::new(
@@ -26,7 +26,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
26 .add_to(acc); 26 .add_to(acc);
27 } 27 }
28 } 28 }
29 hir::Def::Enum(e) => { 29 hir::ModuleDef::Enum(e) => {
30 e.variants(ctx.db) 30 e.variants(ctx.db)
31 .into_iter() 31 .into_iter()
32 .for_each(|(variant_name, variant)| { 32 .for_each(|(variant_name, variant)| {
diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs
index ca2069e2a..578af6e5b 100644
--- a/crates/ra_ide_api/src/completion/completion_context.rs
+++ b/crates/ra_ide_api/src/completion/completion_context.rs
@@ -127,7 +127,7 @@ impl<'a> CompletionContext<'a> {
127 .ancestors() 127 .ancestors()
128 .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE) 128 .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
129 .find_map(ast::FnDef::cast); 129 .find_map(ast::FnDef::cast);
130 match (&self.module, self.function_syntax) { 130 match (self.module, self.function_syntax) {
131 (Some(module), Some(fn_def)) => { 131 (Some(module), Some(fn_def)) => {
132 let function = source_binder::function_from_module(self.db, module, fn_def); 132 let function = source_binder::function_from_module(self.db, module, fn_def);
133 self.function = Some(function); 133 self.function = Some(function);
diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs
index 18c151932..3ba6c33ee 100644
--- a/crates/ra_ide_api/src/completion/completion_item.rs
+++ b/crates/ra_ide_api/src/completion/completion_item.rs
@@ -1,6 +1,4 @@
1use hir::{Docs, Documentation, PerNs}; 1use hir::{Docs, Documentation};
2
3use crate::completion::completion_context::CompletionContext;
4use ra_syntax::{ 2use ra_syntax::{
5 ast::{self, AstNode}, 3 ast::{self, AstNode},
6 TextRange, 4 TextRange,
@@ -8,6 +6,8 @@ use ra_syntax::{
8use ra_text_edit::TextEdit; 6use ra_text_edit::TextEdit;
9use test_utils::tested_by; 7use test_utils::tested_by;
10 8
9use crate::completion::completion_context::CompletionContext;
10
11/// `CompletionItem` describes a single completion variant in the editor pop-up. 11/// `CompletionItem` describes a single completion variant in the editor pop-up.
12/// It is basically a POD with various properties. To construct a 12/// It is basically a POD with various properties. To construct a
13/// `CompletionItem`, use `new` method and the `Builder` struct. 13/// `CompletionItem`, use `new` method and the `Builder` struct.
@@ -209,41 +209,24 @@ impl Builder {
209 ctx: &CompletionContext, 209 ctx: &CompletionContext,
210 resolution: &hir::Resolution, 210 resolution: &hir::Resolution,
211 ) -> Builder { 211 ) -> Builder {
212 let resolved = resolution.def_id.map(|d| d.resolve(ctx.db)); 212 let def = resolution
213 let (kind, docs) = match resolved { 213 .def_id
214 PerNs { 214 .take_types()
215 types: Some(hir::Def::Module(..)), 215 .or(resolution.def_id.take_values());
216 .. 216 let def = match def {
217 } => (CompletionItemKind::Module, None), 217 None => return self,
218 PerNs { 218 Some(it) => it,
219 types: Some(hir::Def::Struct(s)), 219 };
220 .. 220 let (kind, docs) = match def {
221 } => (CompletionItemKind::Struct, s.docs(ctx.db)), 221 hir::ModuleDef::Module(_) => (CompletionItemKind::Module, None),
222 PerNs { 222 hir::ModuleDef::Function(func) => return self.from_function(ctx, func),
223 types: Some(hir::Def::Enum(e)), 223 hir::ModuleDef::Struct(it) => (CompletionItemKind::Struct, it.docs(ctx.db)),
224 .. 224 hir::ModuleDef::Enum(it) => (CompletionItemKind::Enum, it.docs(ctx.db)),
225 } => (CompletionItemKind::Enum, e.docs(ctx.db)), 225 hir::ModuleDef::EnumVariant(it) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)),
226 PerNs { 226 hir::ModuleDef::Const(it) => (CompletionItemKind::Const, it.docs(ctx.db)),
227 types: Some(hir::Def::Trait(t)), 227 hir::ModuleDef::Static(it) => (CompletionItemKind::Static, it.docs(ctx.db)),
228 .. 228 hir::ModuleDef::Trait(it) => (CompletionItemKind::Trait, it.docs(ctx.db)),
229 } => (CompletionItemKind::Trait, t.docs(ctx.db)), 229 hir::ModuleDef::Type(it) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)),
230 PerNs {
231 types: Some(hir::Def::Type(t)),
232 ..
233 } => (CompletionItemKind::TypeAlias, t.docs(ctx.db)),
234 PerNs {
235 values: Some(hir::Def::Const(c)),
236 ..
237 } => (CompletionItemKind::Const, c.docs(ctx.db)),
238 PerNs {
239 values: Some(hir::Def::Static(s)),
240 ..
241 } => (CompletionItemKind::Static, s.docs(ctx.db)),
242 PerNs {
243 values: Some(hir::Def::Function(function)),
244 ..
245 } => return self.from_function(ctx, function),
246 _ => return self,
247 }; 230 };
248 self.kind = Some(kind); 231 self.kind = Some(kind);
249 self.documentation = docs; 232 self.documentation = docs;
diff --git a/crates/ra_ide_api/src/db.rs b/crates/ra_ide_api/src/db.rs
index ba0eb1cb8..bff6b7237 100644
--- a/crates/ra_ide_api/src/db.rs
+++ b/crates/ra_ide_api/src/db.rs
@@ -72,6 +72,7 @@ salsa::database_storage! {
72 fn file_relative_path() for ra_db::FileRelativePathQuery; 72 fn file_relative_path() for ra_db::FileRelativePathQuery;
73 fn file_source_root() for ra_db::FileSourceRootQuery; 73 fn file_source_root() for ra_db::FileSourceRootQuery;
74 fn source_root() for ra_db::SourceRootQuery; 74 fn source_root() for ra_db::SourceRootQuery;
75 fn source_root_crates() for ra_db::SourceRootCratesQuery;
75 fn local_roots() for ra_db::LocalRootsQuery; 76 fn local_roots() for ra_db::LocalRootsQuery;
76 fn library_roots() for ra_db::LibraryRootsQuery; 77 fn library_roots() for ra_db::LibraryRootsQuery;
77 fn crate_graph() for ra_db::CrateGraphQuery; 78 fn crate_graph() for ra_db::CrateGraphQuery;
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs
index b1becca03..46bdde00d 100644
--- a/crates/ra_ide_api/src/goto_definition.rs
+++ b/crates/ra_ide_api/src/goto_definition.rs
@@ -63,13 +63,11 @@ pub(crate) fn reference_definition(
63 let infer_result = function.infer(db); 63 let infer_result = function.infer(db);
64 let syntax_mapping = function.body_syntax_mapping(db); 64 let syntax_mapping = function.body_syntax_mapping(db);
65 let expr = ast::Expr::cast(method_call.syntax()).unwrap(); 65 let expr = ast::Expr::cast(method_call.syntax()).unwrap();
66 if let Some(def_id) = syntax_mapping 66 if let Some(func) = syntax_mapping
67 .node_expr(expr) 67 .node_expr(expr)
68 .and_then(|it| infer_result.method_resolution(it)) 68 .and_then(|it| infer_result.method_resolution(it))
69 { 69 {
70 if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)) { 70 return Exact(NavigationTarget::from_function(db, func));
71 return Exact(target);
72 }
73 }; 71 };
74 } 72 }
75 } 73 }
@@ -84,7 +82,7 @@ pub(crate) fn reference_definition(
84 { 82 {
85 let resolved = module.resolve_path(db, &path); 83 let resolved = module.resolve_path(db, &path);
86 if let Some(def_id) = resolved.take_types().or(resolved.take_values()) { 84 if let Some(def_id) = resolved.take_types().or(resolved.take_values()) {
87 if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)) { 85 if let Some(target) = NavigationTarget::from_def(db, def_id) {
88 return Exact(target); 86 return Exact(target);
89 } 87 }
90 } 88 }
diff --git a/crates/ra_ide_api/src/navigation_target.rs b/crates/ra_ide_api/src/navigation_target.rs
index 21c15c0c0..c5be8e01b 100644
--- a/crates/ra_ide_api/src/navigation_target.rs
+++ b/crates/ra_ide_api/src/navigation_target.rs
@@ -3,7 +3,7 @@ use ra_syntax::{
3 SyntaxNode, AstNode, SmolStr, TextRange, ast, 3 SyntaxNode, AstNode, SmolStr, TextRange, ast,
4 SyntaxKind::{self, NAME}, 4 SyntaxKind::{self, NAME},
5}; 5};
6use hir::{Def, ModuleSource}; 6use hir::{ModuleSource};
7 7
8use crate::{FileSymbol, db::RootDatabase}; 8use crate::{FileSymbol, db::RootDatabase};
9 9
@@ -96,45 +96,69 @@ impl NavigationTarget {
96 NavigationTarget::from_module(db, module) 96 NavigationTarget::from_module(db, module)
97 } 97 }
98 98
99 pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget {
100 let (file_id, fn_def) = func.source(db);
101 NavigationTarget::from_named(file_id.original_file(db), &*fn_def)
102 }
103
99 // TODO once Def::Item is gone, this should be able to always return a NavigationTarget 104 // TODO once Def::Item is gone, this should be able to always return a NavigationTarget
100 pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Option<NavigationTarget> { 105 pub(crate) fn from_def(
101 let res = match def { 106 db: &RootDatabase,
102 Def::Struct(s) => { 107 module_def: hir::ModuleDef,
108 ) -> Option<NavigationTarget> {
109 match module_def {
110 hir::ModuleDef::Module(module) => Some(NavigationTarget::from_module(db, module)),
111 hir::ModuleDef::Function(func) => Some(NavigationTarget::from_function(db, func)),
112 hir::ModuleDef::Struct(s) => {
103 let (file_id, node) = s.source(db); 113 let (file_id, node) = s.source(db);
104 NavigationTarget::from_named(file_id.original_file(db), &*node) 114 Some(NavigationTarget::from_named(
115 file_id.original_file(db),
116 &*node,
117 ))
105 } 118 }
106 Def::Enum(e) => { 119 hir::ModuleDef::Const(s) => {
107 let (file_id, node) = e.source(db); 120 let (file_id, node) = s.source(db);
108 NavigationTarget::from_named(file_id.original_file(db), &*node) 121 Some(NavigationTarget::from_named(
109 } 122 file_id.original_file(db),
110 Def::EnumVariant(ev) => { 123 &*node,
111 let (file_id, node) = ev.source(db); 124 ))
112 NavigationTarget::from_named(file_id.original_file(db), &*node)
113 } 125 }
114 Def::Function(f) => { 126 hir::ModuleDef::Static(s) => {
115 let (file_id, node) = f.source(db); 127 let (file_id, node) = s.source(db);
116 NavigationTarget::from_named(file_id.original_file(db), &*node) 128 Some(NavigationTarget::from_named(
129 file_id.original_file(db),
130 &*node,
131 ))
117 } 132 }
118 Def::Trait(f) => { 133 hir::ModuleDef::Enum(e) => {
119 let (file_id, node) = f.source(db); 134 let (file_id, node) = e.source(db);
120 NavigationTarget::from_named(file_id.original_file(db), &*node) 135 Some(NavigationTarget::from_named(
136 file_id.original_file(db),
137 &*node,
138 ))
121 } 139 }
122 Def::Type(f) => { 140 hir::ModuleDef::EnumVariant(var) => {
123 let (file_id, node) = f.source(db); 141 let (file_id, node) = var.source(db);
124 NavigationTarget::from_named(file_id.original_file(db), &*node) 142 Some(NavigationTarget::from_named(
143 file_id.original_file(db),
144 &*node,
145 ))
125 } 146 }
126 Def::Static(f) => { 147 hir::ModuleDef::Trait(e) => {
127 let (file_id, node) = f.source(db); 148 let (file_id, node) = e.source(db);
128 NavigationTarget::from_named(file_id.original_file(db), &*node) 149 Some(NavigationTarget::from_named(
150 file_id.original_file(db),
151 &*node,
152 ))
129 } 153 }
130 Def::Const(f) => { 154 hir::ModuleDef::Type(e) => {
131 let (file_id, node) = f.source(db); 155 let (file_id, node) = e.source(db);
132 NavigationTarget::from_named(file_id.original_file(db), &*node) 156 Some(NavigationTarget::from_named(
157 file_id.original_file(db),
158 &*node,
159 ))
133 } 160 }
134 Def::Module(m) => NavigationTarget::from_module(db, m), 161 }
135 Def::Item => return None,
136 };
137 Some(res)
138 } 162 }
139 163
140 #[cfg(test)] 164 #[cfg(test)]
diff --git a/crates/ra_ide_api/src/rename.rs b/crates/ra_ide_api/src/rename.rs
index 53dc273c6..5b767addd 100644
--- a/crates/ra_ide_api/src/rename.rs
+++ b/crates/ra_ide_api/src/rename.rs
@@ -57,7 +57,6 @@ fn rename_mod(
57) -> Option<SourceChange> { 57) -> Option<SourceChange> {
58 let mut source_file_edits = Vec::new(); 58 let mut source_file_edits = Vec::new();
59 let mut file_system_edits = Vec::new(); 59 let mut file_system_edits = Vec::new();
60
61 if let Some(module) = module_from_declaration(db, position.file_id, &ast_module) { 60 if let Some(module) = module_from_declaration(db, position.file_id, &ast_module) {
62 let (file_id, module_source) = module.definition_source(db); 61 let (file_id, module_source) = module.definition_source(db);
63 match module_source { 62 match module_source {
@@ -223,11 +222,15 @@ mod tests {
223 fn test_rename_mod() { 222 fn test_rename_mod() {
224 let (analysis, position) = analysis_and_position( 223 let (analysis, position) = analysis_and_position(
225 " 224 "
226 //- /bar.rs 225 //- /lib.rs
227 mod fo<|>o; 226 mod bar;
228 //- /bar/foo.rs 227
229 // emtpy 228 //- /bar.rs
230 ", 229 mod foo<|>;
230
231 //- /bar/foo.rs
232 // emtpy
233 ",
231 ); 234 );
232 let new_name = "foo2"; 235 let new_name = "foo2";
233 let source_change = analysis.rename(position, new_name).unwrap(); 236 let source_change = analysis.rename(position, new_name).unwrap();
@@ -238,11 +241,11 @@ mod tests {
238 fn test_rename_mod_in_dir() { 241 fn test_rename_mod_in_dir() {
239 let (analysis, position) = analysis_and_position( 242 let (analysis, position) = analysis_and_position(
240 " 243 "
241 //- /lib.rs 244 //- /lib.rs
242 mod fo<|>o; 245 mod fo<|>o;
243 //- /foo/mod.rs 246 //- /foo/mod.rs
244 // emtpy 247 // emtpy
245 ", 248 ",
246 ); 249 );
247 let new_name = "foo2"; 250 let new_name = "foo2";
248 let source_change = analysis.rename(position, new_name).unwrap(); 251 let source_change = analysis.rename(position, new_name).unwrap();
diff --git a/crates/ra_ide_api/src/snapshots/tests__rename_mod.snap b/crates/ra_ide_api/src/snapshots/tests__rename_mod.snap
index 3267d1ac5..890426db7 100644
--- a/crates/ra_ide_api/src/snapshots/tests__rename_mod.snap
+++ b/crates/ra_ide_api/src/snapshots/tests__rename_mod.snap
@@ -1,8 +1,8 @@
1--- 1---
2created: "2019-01-22T14:45:00.975229300+00:00" 2created: "2019-01-24T08:39:53.759318522+00:00"
3creator: insta@0.4.0 3creator: insta@0.5.2
4expression: "&source_change" 4expression: "&source_change"
5source: "crates\\ra_ide_api\\src\\rename.rs" 5source: crates/ra_ide_api/src/rename.rs
6--- 6---
7Some( 7Some(
8 SourceChange { 8 SourceChange {
@@ -10,7 +10,7 @@ Some(
10 source_file_edits: [ 10 source_file_edits: [
11 SourceFileEdit { 11 SourceFileEdit {
12 file_id: FileId( 12 file_id: FileId(
13 1 13 2
14 ), 14 ),
15 edit: TextEdit { 15 edit: TextEdit {
16 atoms: [ 16 atoms: [
@@ -25,7 +25,7 @@ Some(
25 file_system_edits: [ 25 file_system_edits: [
26 MoveFile { 26 MoveFile {
27 src: FileId( 27 src: FileId(
28 2 28 3
29 ), 29 ),
30 dst_source_root: SourceRootId( 30 dst_source_root: SourceRootId(
31 0 31 0