mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 14:13:58 +00:00
Merge #10745
10745: internal: Replace some more ide usages of ModuleDef with Definition r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
23a980af62
4 changed files with 132 additions and 130 deletions
|
@ -1,7 +1,7 @@
|
||||||
use either::Either;
|
|
||||||
use hir::{HasSource, InFile, Semantics};
|
use hir::{HasSource, InFile, Semantics};
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
base_db::{FileId, FilePosition, FileRange},
|
base_db::{FileId, FilePosition, FileRange},
|
||||||
|
defs::Definition,
|
||||||
helpers::visit_file_defs,
|
helpers::visit_file_defs,
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
|
@ -62,91 +62,83 @@ pub(crate) fn annotations(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
visit_file_defs(&Semantics::new(db), file_id, &mut |def| match def {
|
visit_file_defs(&Semantics::new(db), file_id, &mut |def| {
|
||||||
Either::Left(def) => {
|
let range = match def {
|
||||||
let range = match def {
|
Definition::Const(konst) if config.annotate_references => {
|
||||||
hir::ModuleDef::Const(konst) if config.annotate_references => {
|
konst.source(db).and_then(|node| name_range(&node, file_id))
|
||||||
konst.source(db).and_then(|node| name_range(&node, file_id))
|
}
|
||||||
}
|
Definition::Trait(trait_) if config.annotate_references || config.annotate_impls => {
|
||||||
hir::ModuleDef::Trait(trait_)
|
trait_.source(db).and_then(|node| name_range(&node, file_id))
|
||||||
if config.annotate_references || config.annotate_impls =>
|
}
|
||||||
{
|
Definition::Adt(adt) => match adt {
|
||||||
trait_.source(db).and_then(|node| name_range(&node, file_id))
|
hir::Adt::Enum(enum_) => {
|
||||||
}
|
if config.annotate_enum_variant_references {
|
||||||
hir::ModuleDef::Adt(adt) => match adt {
|
enum_
|
||||||
hir::Adt::Enum(enum_) => {
|
.variants(db)
|
||||||
if config.annotate_enum_variant_references {
|
.into_iter()
|
||||||
enum_
|
.map(|variant| {
|
||||||
.variants(db)
|
variant.source(db).and_then(|node| name_range(&node, file_id))
|
||||||
.into_iter()
|
})
|
||||||
.map(|variant| {
|
.filter_map(std::convert::identity)
|
||||||
variant.source(db).and_then(|node| name_range(&node, file_id))
|
.for_each(|range| {
|
||||||
|
annotations.push(Annotation {
|
||||||
|
range,
|
||||||
|
kind: AnnotationKind::HasReferences {
|
||||||
|
position: FilePosition { file_id, offset: range.start() },
|
||||||
|
data: None,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.filter_map(std::convert::identity)
|
})
|
||||||
.for_each(|range| {
|
|
||||||
annotations.push(Annotation {
|
|
||||||
range,
|
|
||||||
kind: AnnotationKind::HasReferences {
|
|
||||||
position: FilePosition {
|
|
||||||
file_id,
|
|
||||||
offset: range.start(),
|
|
||||||
},
|
|
||||||
data: None,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if config.annotate_references || config.annotate_impls {
|
|
||||||
enum_.source(db).and_then(|node| name_range(&node, file_id))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_ => {
|
if config.annotate_references || config.annotate_impls {
|
||||||
if config.annotate_references || config.annotate_impls {
|
enum_.source(db).and_then(|node| name_range(&node, file_id))
|
||||||
adt.source(db).and_then(|node| name_range(&node, file_id))
|
} else {
|
||||||
} else {
|
None
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
if config.annotate_references || config.annotate_impls {
|
||||||
|
adt.source(db).and_then(|node| name_range(&node, file_id))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let (range, offset) = match range {
|
||||||
|
Some(range) => (range, range.start()),
|
||||||
|
None => return,
|
||||||
|
};
|
||||||
|
|
||||||
|
if config.annotate_impls && !matches!(def, Definition::Const(_)) {
|
||||||
|
annotations.push(Annotation {
|
||||||
|
range,
|
||||||
|
kind: AnnotationKind::HasImpls {
|
||||||
|
position: FilePosition { file_id, offset },
|
||||||
|
data: None,
|
||||||
},
|
},
|
||||||
_ => None,
|
});
|
||||||
};
|
}
|
||||||
|
if config.annotate_references {
|
||||||
|
annotations.push(Annotation {
|
||||||
|
range,
|
||||||
|
kind: AnnotationKind::HasReferences {
|
||||||
|
position: FilePosition { file_id, offset },
|
||||||
|
data: None,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let (range, offset) = match range {
|
fn name_range<T: HasName>(node: &InFile<T>, file_id: FileId) -> Option<TextRange> {
|
||||||
Some(range) => (range, range.start()),
|
if node.file_id == file_id.into() {
|
||||||
None => return,
|
node.value.name().map(|it| it.syntax().text_range())
|
||||||
};
|
} else {
|
||||||
|
// Node is outside the file we are adding annotations to (e.g. macros).
|
||||||
if config.annotate_impls && !matches!(def, hir::ModuleDef::Const(_)) {
|
None
|
||||||
annotations.push(Annotation {
|
|
||||||
range,
|
|
||||||
kind: AnnotationKind::HasImpls {
|
|
||||||
position: FilePosition { file_id, offset },
|
|
||||||
data: None,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if config.annotate_references {
|
|
||||||
annotations.push(Annotation {
|
|
||||||
range,
|
|
||||||
kind: AnnotationKind::HasReferences {
|
|
||||||
position: FilePosition { file_id, offset },
|
|
||||||
data: None,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn name_range<T: HasName>(node: &InFile<T>, file_id: FileId) -> Option<TextRange> {
|
|
||||||
if node.file_id == file_id.into() {
|
|
||||||
node.value.name().map(|it| it.syntax().text_range())
|
|
||||||
} else {
|
|
||||||
// Node is outside the file we are adding annotations to (e.g. macros).
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Either::Right(_) => (),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if config.annotate_method_references {
|
if config.annotate_method_references {
|
||||||
|
|
|
@ -2,11 +2,11 @@ use std::fmt;
|
||||||
|
|
||||||
use ast::HasName;
|
use ast::HasName;
|
||||||
use cfg::CfgExpr;
|
use cfg::CfgExpr;
|
||||||
use either::Either;
|
|
||||||
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, InFile, Semantics};
|
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, InFile, Semantics};
|
||||||
use ide_assists::utils::test_related_attribute;
|
use ide_assists::utils::test_related_attribute;
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
base_db::{FilePosition, FileRange},
|
base_db::{FilePosition, FileRange},
|
||||||
|
defs::Definition,
|
||||||
helpers::visit_file_defs,
|
helpers::visit_file_defs,
|
||||||
search::SearchScope,
|
search::SearchScope,
|
||||||
RootDatabase, SymbolKind,
|
RootDatabase, SymbolKind,
|
||||||
|
@ -138,8 +138,8 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
|
||||||
}) {
|
}) {
|
||||||
if let Some(def) = def {
|
if let Some(def) = def {
|
||||||
let file_id = match def {
|
let file_id = match def {
|
||||||
hir::ModuleDef::Module(it) => it.declaration_source(db).map(|src| src.file_id),
|
Definition::Module(it) => it.declaration_source(db).map(|src| src.file_id),
|
||||||
hir::ModuleDef::Function(it) => it.source(db).map(|src| src.file_id),
|
Definition::Function(it) => it.source(db).map(|src| src.file_id),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
if let Some(file_id) = file_id.filter(|file| file.call_node(db).is_some()) {
|
if let Some(file_id) = file_id.filter(|file| file.call_node(db).is_some()) {
|
||||||
|
@ -150,32 +150,25 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
|
||||||
res.push(runnable);
|
res.push(runnable);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
visit_file_defs(&sema, file_id, &mut |def| match def {
|
visit_file_defs(&sema, file_id, &mut |def| {
|
||||||
Either::Left(def) => {
|
let runnable = match def {
|
||||||
let runnable = match def {
|
Definition::Module(it) => runnable_mod(&sema, it),
|
||||||
hir::ModuleDef::Module(it) => runnable_mod(&sema, it),
|
Definition::Function(it) => runnable_fn(&sema, it),
|
||||||
hir::ModuleDef::Function(it) => runnable_fn(&sema, it),
|
Definition::SelfType(impl_) => runnable_impl(&sema, &impl_),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
add_opt(runnable.or_else(|| module_def_doctest(sema.db, def)), Some(def));
|
add_opt(runnable.or_else(|| module_def_doctest(sema.db, def)), Some(def));
|
||||||
}
|
if let Definition::SelfType(impl_) = def {
|
||||||
Either::Right(impl_) => {
|
impl_.items(db).into_iter().for_each(|assoc| {
|
||||||
add_opt(runnable_impl(&sema, &impl_), None);
|
let runnable = match assoc {
|
||||||
impl_
|
hir::AssocItem::Function(it) => {
|
||||||
.items(db)
|
runnable_fn(&sema, it).or_else(|| module_def_doctest(sema.db, it.into()))
|
||||||
.into_iter()
|
}
|
||||||
.map(|assoc| {
|
hir::AssocItem::Const(it) => module_def_doctest(sema.db, it.into()),
|
||||||
(
|
hir::AssocItem::TypeAlias(it) => module_def_doctest(sema.db, it.into()),
|
||||||
match assoc {
|
};
|
||||||
hir::AssocItem::Function(it) => runnable_fn(&sema, it)
|
add_opt(runnable, Some(assoc.into()))
|
||||||
.or_else(|| module_def_doctest(sema.db, it.into())),
|
});
|
||||||
hir::AssocItem::Const(it) => module_def_doctest(sema.db, it.into()),
|
|
||||||
hir::AssocItem::TypeAlias(it) => module_def_doctest(sema.db, it.into()),
|
|
||||||
},
|
|
||||||
assoc,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.for_each(|(r, assoc)| add_opt(r, Some(assoc.into())));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -392,17 +385,19 @@ fn runnable_mod_outline_definition(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn module_def_doctest(db: &RootDatabase, def: hir::ModuleDef) -> Option<Runnable> {
|
fn module_def_doctest(db: &RootDatabase, def: Definition) -> Option<Runnable> {
|
||||||
let attrs = match def {
|
let attrs = match def {
|
||||||
hir::ModuleDef::Module(it) => it.attrs(db),
|
Definition::Module(it) => it.attrs(db),
|
||||||
hir::ModuleDef::Function(it) => it.attrs(db),
|
Definition::Function(it) => it.attrs(db),
|
||||||
hir::ModuleDef::Adt(it) => it.attrs(db),
|
Definition::Adt(it) => it.attrs(db),
|
||||||
hir::ModuleDef::Variant(it) => it.attrs(db),
|
Definition::Variant(it) => it.attrs(db),
|
||||||
hir::ModuleDef::Const(it) => it.attrs(db),
|
Definition::Const(it) => it.attrs(db),
|
||||||
hir::ModuleDef::Static(it) => it.attrs(db),
|
Definition::Static(it) => it.attrs(db),
|
||||||
hir::ModuleDef::Trait(it) => it.attrs(db),
|
Definition::Trait(it) => it.attrs(db),
|
||||||
hir::ModuleDef::TypeAlias(it) => it.attrs(db),
|
Definition::TypeAlias(it) => it.attrs(db),
|
||||||
hir::ModuleDef::BuiltinType(_) => return None,
|
Definition::Macro(it) => it.attrs(db),
|
||||||
|
Definition::SelfType(it) => it.attrs(db),
|
||||||
|
_ => return None,
|
||||||
};
|
};
|
||||||
if !has_runnable_doc_test(&attrs) {
|
if !has_runnable_doc_test(&attrs) {
|
||||||
return None;
|
return None;
|
||||||
|
@ -440,7 +435,7 @@ fn module_def_doctest(db: &RootDatabase, def: hir::ModuleDef) -> Option<Runnable
|
||||||
let test_id = path.map_or_else(|| TestId::Name(def_name.to_string()), TestId::Path);
|
let test_id = path.map_or_else(|| TestId::Name(def_name.to_string()), TestId::Path);
|
||||||
|
|
||||||
let mut nav = match def {
|
let mut nav = match def {
|
||||||
hir::ModuleDef::Module(def) => NavigationTarget::from_module_to_decl(db, def),
|
Definition::Module(def) => NavigationTarget::from_module_to_decl(db, def),
|
||||||
def => def.try_to_nav(db)?,
|
def => def.try_to_nav(db)?,
|
||||||
};
|
};
|
||||||
nav.focus_range = None;
|
nav.focus_range = None;
|
||||||
|
|
|
@ -516,6 +516,18 @@ impl NameRefClass {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl_from!(
|
||||||
|
Field, Module, Function, Adt, Variant, Const, Static, Trait, TypeAlias, BuiltinType, Local,
|
||||||
|
GenericParam, Label
|
||||||
|
for Definition
|
||||||
|
);
|
||||||
|
|
||||||
|
impl From<Impl> for Definition {
|
||||||
|
fn from(impl_: Impl) -> Self {
|
||||||
|
Definition::SelfType(impl_)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl AsAssocItem for Definition {
|
impl AsAssocItem for Definition {
|
||||||
fn as_assoc_item(self, db: &dyn hir::db::HirDatabase) -> Option<AssocItem> {
|
fn as_assoc_item(self, db: &dyn hir::db::HirDatabase) -> Option<AssocItem> {
|
||||||
match self {
|
match self {
|
||||||
|
@ -527,11 +539,15 @@ impl AsAssocItem for Definition {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_from!(
|
impl From<AssocItem> for Definition {
|
||||||
Field, Module, Function, Adt, Variant, Const, Static, Trait, TypeAlias, BuiltinType, Local,
|
fn from(assoc_item: AssocItem) -> Self {
|
||||||
GenericParam, Label
|
match assoc_item {
|
||||||
for Definition
|
AssocItem::Function(it) => Definition::Function(it),
|
||||||
);
|
AssocItem::Const(it) => Definition::Const(it),
|
||||||
|
AssocItem::TypeAlias(it) => Definition::TypeAlias(it),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<PathResolution> for Definition {
|
impl From<PathResolution> for Definition {
|
||||||
fn from(path_resolution: PathResolution) -> Self {
|
fn from(path_resolution: PathResolution) -> Self {
|
||||||
|
|
|
@ -10,7 +10,6 @@ pub mod rust_doc;
|
||||||
use std::{collections::VecDeque, iter};
|
use std::{collections::VecDeque, iter};
|
||||||
|
|
||||||
use base_db::FileId;
|
use base_db::FileId;
|
||||||
use either::Either;
|
|
||||||
use hir::{ItemInNs, MacroDef, ModuleDef, Name, PathResolution, Semantics};
|
use hir::{ItemInNs, MacroDef, ModuleDef, Name, PathResolution, Semantics};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
|
@ -19,7 +18,7 @@ use syntax::{
|
||||||
T,
|
T,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::RootDatabase;
|
use crate::{defs::Definition, RootDatabase};
|
||||||
|
|
||||||
pub use self::famous_defs::FamousDefs;
|
pub use self::famous_defs::FamousDefs;
|
||||||
|
|
||||||
|
@ -122,7 +121,7 @@ pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
|
||||||
pub fn visit_file_defs(
|
pub fn visit_file_defs(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
cb: &mut dyn FnMut(Either<hir::ModuleDef, hir::Impl>),
|
cb: &mut dyn FnMut(Definition),
|
||||||
) {
|
) {
|
||||||
let db = sema.db;
|
let db = sema.db;
|
||||||
let module = match sema.to_module_def(file_id) {
|
let module = match sema.to_module_def(file_id) {
|
||||||
|
@ -134,12 +133,12 @@ pub fn visit_file_defs(
|
||||||
if let ModuleDef::Module(submodule) = def {
|
if let ModuleDef::Module(submodule) = def {
|
||||||
if let hir::ModuleSource::Module(_) = submodule.definition_source(db).value {
|
if let hir::ModuleSource::Module(_) = submodule.definition_source(db).value {
|
||||||
defs.extend(submodule.declarations(db));
|
defs.extend(submodule.declarations(db));
|
||||||
submodule.impl_defs(db).into_iter().for_each(|impl_| cb(Either::Right(impl_)));
|
submodule.impl_defs(db).into_iter().for_each(|impl_| cb(impl_.into()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cb(Either::Left(def));
|
cb(def.into());
|
||||||
}
|
}
|
||||||
module.impl_defs(db).into_iter().for_each(|impl_| cb(Either::Right(impl_)));
|
module.impl_defs(db).into_iter().for_each(|impl_| cb(impl_.into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
|
Loading…
Reference in a new issue