From 3ab1519cb27b927074ed7fbbb18a856e6e7fabb8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 23 Jan 2019 23:14:13 +0300 Subject: Change ids strategy this is a part of larghish hir refactoring which aims to * replace per-source-root module trees with per crate trees * switch from a monotyped DedId to type-specific ids --- crates/ra_ide_api/src/completion/complete_path.rs | 37 +++++++------ .../ra_ide_api/src/completion/completion_item.rs | 61 ++++++++-------------- crates/ra_ide_api/src/db.rs | 1 + crates/ra_ide_api/src/goto_definition.rs | 4 +- crates/ra_ide_api/src/navigation_target.rs | 13 ++++- crates/ra_ide_api/src/rename.rs | 25 +++++---- .../src/snapshots/tests__rename_mod.snap | 10 ++-- 7 files changed, 76 insertions(+), 75 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index e44b76c4a..0d7942496 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) { Some(it) => it, None => return, }; - match def_id.resolve(ctx.db) { - hir::Def::Module(module) => { + match def_id { + hir::ModuleDef::Module(module) => { let module_scope = module.scope(ctx.db); for (name, res) in module_scope.entries() { CompletionItem::new( @@ -26,21 +26,24 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { .add_to(acc); } } - hir::Def::Enum(e) => { - e.variants(ctx.db) - .into_iter() - .for_each(|(variant_name, variant)| { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - variant_name.to_string(), - ) - .kind(CompletionItemKind::EnumVariant) - .set_documentation(variant.docs(ctx.db)) - .add_to(acc) - }); - } - _ => return, + + hir::ModuleDef::Def(def_id) => match def_id.resolve(ctx.db) { + hir::Def::Enum(e) => { + e.variants(ctx.db) + .into_iter() + .for_each(|(variant_name, variant)| { + CompletionItem::new( + CompletionKind::Reference, + ctx.source_range(), + variant_name.to_string(), + ) + .kind(CompletionItemKind::EnumVariant) + .set_documentation(variant.docs(ctx.db)) + .add_to(acc) + }); + } + _ => return, + }, }; } diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index 18c151932..5d6718a8d 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 @@ -use hir::{Docs, Documentation, PerNs}; - -use crate::completion::completion_context::CompletionContext; +use hir::{Docs, Documentation}; use ra_syntax::{ ast::{self, AstNode}, TextRange, @@ -8,6 +6,8 @@ use ra_syntax::{ use ra_text_edit::TextEdit; use test_utils::tested_by; +use crate::completion::completion_context::CompletionContext; + /// `CompletionItem` describes a single completion variant in the editor pop-up. /// It is basically a POD with various properties. To construct a /// `CompletionItem`, use `new` method and the `Builder` struct. @@ -209,41 +209,26 @@ impl Builder { ctx: &CompletionContext, resolution: &hir::Resolution, ) -> Builder { - let resolved = resolution.def_id.map(|d| d.resolve(ctx.db)); - let (kind, docs) = match resolved { - PerNs { - types: Some(hir::Def::Module(..)), - .. - } => (CompletionItemKind::Module, None), - PerNs { - types: Some(hir::Def::Struct(s)), - .. - } => (CompletionItemKind::Struct, s.docs(ctx.db)), - PerNs { - types: Some(hir::Def::Enum(e)), - .. - } => (CompletionItemKind::Enum, e.docs(ctx.db)), - PerNs { - types: Some(hir::Def::Trait(t)), - .. - } => (CompletionItemKind::Trait, t.docs(ctx.db)), - PerNs { - types: Some(hir::Def::Type(t)), - .. - } => (CompletionItemKind::TypeAlias, t.docs(ctx.db)), - PerNs { - values: Some(hir::Def::Const(c)), - .. - } => (CompletionItemKind::Const, c.docs(ctx.db)), - PerNs { - values: Some(hir::Def::Static(s)), - .. - } => (CompletionItemKind::Static, s.docs(ctx.db)), - PerNs { - values: Some(hir::Def::Function(function)), - .. - } => return self.from_function(ctx, function), - _ => return self, + let def = resolution + .def_id + .take_types() + .or(resolution.def_id.take_values()); + let def = match def { + None => return self, + Some(it) => it, + }; + let (kind, docs) = match def { + hir::ModuleDef::Module(_) => (CompletionItemKind::Module, None), + hir::ModuleDef::Def(def_id) => match def_id.resolve(ctx.db) { + hir::Def::Struct(it) => (CompletionItemKind::Struct, it.docs(ctx.db)), + hir::Def::Enum(it) => (CompletionItemKind::Enum, it.docs(ctx.db)), + hir::Def::Trait(it) => (CompletionItemKind::Trait, it.docs(ctx.db)), + hir::Def::Type(it) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)), + hir::Def::Const(it) => (CompletionItemKind::Const, it.docs(ctx.db)), + hir::Def::Static(it) => (CompletionItemKind::Static, it.docs(ctx.db)), + hir::Def::Function(function) => return self.from_function(ctx, function), + _ => return self, + }, }; self.kind = Some(kind); 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! { fn file_relative_path() for ra_db::FileRelativePathQuery; fn file_source_root() for ra_db::FileSourceRootQuery; fn source_root() for ra_db::SourceRootQuery; + fn source_root_crates() for ra_db::SourceRootCratesQuery; fn local_roots() for ra_db::LocalRootsQuery; fn library_roots() for ra_db::LibraryRootsQuery; 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..323bb1cc1 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -67,7 +67,7 @@ pub(crate) fn reference_definition( .node_expr(expr) .and_then(|it| infer_result.method_resolution(it)) { - if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)) { + if let Some(target) = NavigationTarget::from_def(db, hir::ModuleDef::Def(def_id)) { return Exact(target); } }; @@ -84,7 +84,7 @@ pub(crate) fn reference_definition( { let resolved = module.resolve_path(db, &path); if let Some(def_id) = resolved.take_types().or(resolved.take_values()) { - if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)) { + if let Some(target) = NavigationTarget::from_def(db, def_id) { return Exact(target); } } diff --git a/crates/ra_ide_api/src/navigation_target.rs b/crates/ra_ide_api/src/navigation_target.rs index 21c15c0c0..1eb177665 100644 --- a/crates/ra_ide_api/src/navigation_target.rs +++ b/crates/ra_ide_api/src/navigation_target.rs @@ -97,7 +97,17 @@ impl NavigationTarget { } // TODO once Def::Item is gone, this should be able to always return a NavigationTarget - pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Option { + pub(crate) fn from_def( + db: &RootDatabase, + module_def: hir::ModuleDef, + ) -> Option { + let def = match module_def { + hir::ModuleDef::Def(def_id) => def_id.resolve(db), + hir::ModuleDef::Module(module) => { + return Some(NavigationTarget::from_module(db, module)); + } + }; + let res = match def { Def::Struct(s) => { let (file_id, node) = s.source(db); @@ -131,7 +141,6 @@ impl NavigationTarget { let (file_id, node) = f.source(db); NavigationTarget::from_named(file_id.original_file(db), &*node) } - Def::Module(m) => NavigationTarget::from_module(db, m), Def::Item => return None, }; Some(res) 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( ) -> Option { let mut source_file_edits = Vec::new(); let mut file_system_edits = Vec::new(); - if let Some(module) = module_from_declaration(db, position.file_id, &ast_module) { let (file_id, module_source) = module.definition_source(db); match module_source { @@ -223,11 +222,15 @@ mod tests { fn test_rename_mod() { let (analysis, position) = analysis_and_position( " - //- /bar.rs - mod fo<|>o; - //- /bar/foo.rs - // emtpy - ", + //- /lib.rs + mod bar; + + //- /bar.rs + mod foo<|>; + + //- /bar/foo.rs + // emtpy + ", ); let new_name = "foo2"; let source_change = analysis.rename(position, new_name).unwrap(); @@ -238,11 +241,11 @@ mod tests { fn test_rename_mod_in_dir() { let (analysis, position) = analysis_and_position( " - //- /lib.rs - mod fo<|>o; - //- /foo/mod.rs - // emtpy - ", + //- /lib.rs + mod fo<|>o; + //- /foo/mod.rs + // emtpy + ", ); let new_name = "foo2"; 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 @@ --- -created: "2019-01-22T14:45:00.975229300+00:00" -creator: insta@0.4.0 +created: "2019-01-24T08:39:53.759318522+00:00" +creator: insta@0.5.2 expression: "&source_change" -source: "crates\\ra_ide_api\\src\\rename.rs" +source: crates/ra_ide_api/src/rename.rs --- Some( SourceChange { @@ -10,7 +10,7 @@ Some( source_file_edits: [ SourceFileEdit { file_id: FileId( - 1 + 2 ), edit: TextEdit { atoms: [ @@ -25,7 +25,7 @@ Some( file_system_edits: [ MoveFile { src: FileId( - 2 + 3 ), dst_source_root: SourceRootId( 0 -- cgit v1.2.3