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
This commit is contained in:
Aleksey Kladov 2019-01-23 23:14:13 +03:00
parent cfb085ded8
commit 3ab1519cb2
26 changed files with 365 additions and 430 deletions

View file

@ -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,
},
};
}

View file

@ -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;

View file

@ -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;

View file

@ -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);
}
}

View file

@ -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<NavigationTarget> {
pub(crate) fn from_def(
db: &RootDatabase,
module_def: hir::ModuleDef,
) -> Option<NavigationTarget> {
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)

View file

@ -57,7 +57,6 @@ fn rename_mod(
) -> Option<SourceChange> {
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();

View file

@ -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