mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
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 <aleksey.kladov@gmail.com>
This commit is contained in:
commit
c42db0bbd7
36 changed files with 950 additions and 988 deletions
|
@ -1,4 +1,4 @@
|
|||
use hir::{Ty, Def};
|
||||
use hir::{Ty, AdtDef};
|
||||
|
||||
use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind};
|
||||
use crate::completion::completion_item::CompletionKind;
|
||||
|
@ -28,8 +28,8 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
|
|||
Ty::Adt {
|
||||
def_id, ref substs, ..
|
||||
} => {
|
||||
match def_id.resolve(ctx.db) {
|
||||
Def::Struct(s) => {
|
||||
match def_id {
|
||||
AdtDef::Struct(s) => {
|
||||
for field in s.fields(ctx.db) {
|
||||
CompletionItem::new(
|
||||
CompletionKind::Reference,
|
||||
|
@ -41,8 +41,9 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
|
|||
.add_to(acc);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO unions
|
||||
_ => {}
|
||||
AdtDef::Enum(_) => (),
|
||||
}
|
||||
}
|
||||
Ty::Tuple(fields) => {
|
||||
|
|
|
@ -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,7 +26,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
|
|||
.add_to(acc);
|
||||
}
|
||||
}
|
||||
hir::Def::Enum(e) => {
|
||||
hir::ModuleDef::Enum(e) => {
|
||||
e.variants(ctx.db)
|
||||
.into_iter()
|
||||
.for_each(|(variant_name, variant)| {
|
||||
|
|
|
@ -127,7 +127,7 @@ impl<'a> CompletionContext<'a> {
|
|||
.ancestors()
|
||||
.take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
|
||||
.find_map(ast::FnDef::cast);
|
||||
match (&self.module, self.function_syntax) {
|
||||
match (self.module, self.function_syntax) {
|
||||
(Some(module), Some(fn_def)) => {
|
||||
let function = source_binder::function_from_module(self.db, module, fn_def);
|
||||
self.function = Some(function);
|
||||
|
|
|
@ -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,24 @@ 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::Function(func) => return self.from_function(ctx, func),
|
||||
hir::ModuleDef::Struct(it) => (CompletionItemKind::Struct, it.docs(ctx.db)),
|
||||
hir::ModuleDef::Enum(it) => (CompletionItemKind::Enum, it.docs(ctx.db)),
|
||||
hir::ModuleDef::EnumVariant(it) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)),
|
||||
hir::ModuleDef::Const(it) => (CompletionItemKind::Const, it.docs(ctx.db)),
|
||||
hir::ModuleDef::Static(it) => (CompletionItemKind::Static, it.docs(ctx.db)),
|
||||
hir::ModuleDef::Trait(it) => (CompletionItemKind::Trait, it.docs(ctx.db)),
|
||||
hir::ModuleDef::Type(it) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)),
|
||||
};
|
||||
self.kind = Some(kind);
|
||||
self.documentation = docs;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -63,13 +63,11 @@ pub(crate) fn reference_definition(
|
|||
let infer_result = function.infer(db);
|
||||
let syntax_mapping = function.body_syntax_mapping(db);
|
||||
let expr = ast::Expr::cast(method_call.syntax()).unwrap();
|
||||
if let Some(def_id) = syntax_mapping
|
||||
if let Some(func) = syntax_mapping
|
||||
.node_expr(expr)
|
||||
.and_then(|it| infer_result.method_resolution(it))
|
||||
{
|
||||
if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)) {
|
||||
return Exact(target);
|
||||
}
|
||||
return Exact(NavigationTarget::from_function(db, func));
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -84,7 +82,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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ use ra_syntax::{
|
|||
SyntaxNode, AstNode, SmolStr, TextRange, ast,
|
||||
SyntaxKind::{self, NAME},
|
||||
};
|
||||
use hir::{Def, ModuleSource};
|
||||
use hir::{ModuleSource};
|
||||
|
||||
use crate::{FileSymbol, db::RootDatabase};
|
||||
|
||||
|
@ -96,45 +96,69 @@ impl NavigationTarget {
|
|||
NavigationTarget::from_module(db, module)
|
||||
}
|
||||
|
||||
pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget {
|
||||
let (file_id, fn_def) = func.source(db);
|
||||
NavigationTarget::from_named(file_id.original_file(db), &*fn_def)
|
||||
}
|
||||
|
||||
// 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> {
|
||||
let res = match def {
|
||||
Def::Struct(s) => {
|
||||
pub(crate) fn from_def(
|
||||
db: &RootDatabase,
|
||||
module_def: hir::ModuleDef,
|
||||
) -> Option<NavigationTarget> {
|
||||
match module_def {
|
||||
hir::ModuleDef::Module(module) => Some(NavigationTarget::from_module(db, module)),
|
||||
hir::ModuleDef::Function(func) => Some(NavigationTarget::from_function(db, func)),
|
||||
hir::ModuleDef::Struct(s) => {
|
||||
let (file_id, node) = s.source(db);
|
||||
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
||||
Some(NavigationTarget::from_named(
|
||||
file_id.original_file(db),
|
||||
&*node,
|
||||
))
|
||||
}
|
||||
Def::Enum(e) => {
|
||||
hir::ModuleDef::Const(s) => {
|
||||
let (file_id, node) = s.source(db);
|
||||
Some(NavigationTarget::from_named(
|
||||
file_id.original_file(db),
|
||||
&*node,
|
||||
))
|
||||
}
|
||||
hir::ModuleDef::Static(s) => {
|
||||
let (file_id, node) = s.source(db);
|
||||
Some(NavigationTarget::from_named(
|
||||
file_id.original_file(db),
|
||||
&*node,
|
||||
))
|
||||
}
|
||||
hir::ModuleDef::Enum(e) => {
|
||||
let (file_id, node) = e.source(db);
|
||||
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
||||
Some(NavigationTarget::from_named(
|
||||
file_id.original_file(db),
|
||||
&*node,
|
||||
))
|
||||
}
|
||||
Def::EnumVariant(ev) => {
|
||||
let (file_id, node) = ev.source(db);
|
||||
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
||||
hir::ModuleDef::EnumVariant(var) => {
|
||||
let (file_id, node) = var.source(db);
|
||||
Some(NavigationTarget::from_named(
|
||||
file_id.original_file(db),
|
||||
&*node,
|
||||
))
|
||||
}
|
||||
Def::Function(f) => {
|
||||
let (file_id, node) = f.source(db);
|
||||
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
||||
hir::ModuleDef::Trait(e) => {
|
||||
let (file_id, node) = e.source(db);
|
||||
Some(NavigationTarget::from_named(
|
||||
file_id.original_file(db),
|
||||
&*node,
|
||||
))
|
||||
}
|
||||
Def::Trait(f) => {
|
||||
let (file_id, node) = f.source(db);
|
||||
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
||||
hir::ModuleDef::Type(e) => {
|
||||
let (file_id, node) = e.source(db);
|
||||
Some(NavigationTarget::from_named(
|
||||
file_id.original_file(db),
|
||||
&*node,
|
||||
))
|
||||
}
|
||||
Def::Type(f) => {
|
||||
let (file_id, node) = f.source(db);
|
||||
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
||||
}
|
||||
Def::Static(f) => {
|
||||
let (file_id, node) = f.source(db);
|
||||
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
||||
}
|
||||
Def::Const(f) => {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue