1 //! A module with ide helpers for high-level ide features.
6 use std::collections::VecDeque;
10 use hir::{Crate, Enum, ItemInNs, MacroDef, Module, ModuleDef, Name, ScopeDef, Semantics, Trait};
11 use syntax::ast::{self, make};
13 use crate::RootDatabase;
15 pub fn item_name(db: &RootDatabase, item: ItemInNs) -> Option<Name> {
17 ItemInNs::Types(module_def_id) => ModuleDef::from(module_def_id).name(db),
18 ItemInNs::Values(module_def_id) => ModuleDef::from(module_def_id).name(db),
19 ItemInNs::Macros(macro_def_id) => MacroDef::from(macro_def_id).name(db),
23 /// Converts the mod path struct into its ast representation.
24 pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
25 let _p = profile::span("mod_path_to_ast");
27 let mut segments = Vec::new();
28 let mut is_abs = false;
30 hir::PathKind::Plain => {}
31 hir::PathKind::Super(0) => segments.push(make::path_segment_self()),
32 hir::PathKind::Super(n) => segments.extend((0..n).map(|_| make::path_segment_super())),
33 hir::PathKind::DollarCrate(_) | hir::PathKind::Crate => {
34 segments.push(make::path_segment_crate())
36 hir::PathKind::Abs => is_abs = true,
42 .map(|segment| make::path_segment(make::name_ref(&segment.to_string()))),
44 make::path_from_segments(segments, is_abs)
47 /// Iterates all `ModuleDef`s and `Impl` blocks of the given file.
48 pub fn visit_file_defs(
49 sema: &Semantics<RootDatabase>,
51 cb: &mut dyn FnMut(Either<hir::ModuleDef, hir::Impl>),
54 let module = match sema.to_module_def(file_id) {
58 let mut defs: VecDeque<_> = module.declarations(db).into();
59 while let Some(def) = defs.pop_front() {
60 if let ModuleDef::Module(submodule) = def {
61 if let hir::ModuleSource::Module(_) = submodule.definition_source(db).value {
62 defs.extend(submodule.declarations(db));
63 submodule.impl_defs(db).into_iter().for_each(|impl_| cb(Either::Right(impl_)));
66 cb(Either::Left(def));
68 module.impl_defs(db).into_iter().for_each(|impl_| cb(Either::Right(impl_)));
71 /// Helps with finding well-know things inside the standard library. This is
72 /// somewhat similar to the known paths infra inside hir, but it different; We
73 /// want to make sure that IDE specific paths don't become interesting inside
74 /// the compiler itself as well.
75 pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Option<Crate>);
77 #[allow(non_snake_case)]
78 impl FamousDefs<'_, '_> {
79 pub const FIXTURE: &'static str = include_str!("helpers/famous_defs_fixture.rs");
81 pub fn std(&self) -> Option<Crate> {
82 self.find_crate("std")
85 pub fn core(&self) -> Option<Crate> {
86 self.find_crate("core")
89 pub fn core_cmp_Ord(&self) -> Option<Trait> {
90 self.find_trait("core:cmp:Ord")
93 pub fn core_convert_From(&self) -> Option<Trait> {
94 self.find_trait("core:convert:From")
97 pub fn core_convert_Into(&self) -> Option<Trait> {
98 self.find_trait("core:convert:Into")
101 pub fn core_option_Option(&self) -> Option<Enum> {
102 self.find_enum("core:option:Option")
105 pub fn core_default_Default(&self) -> Option<Trait> {
106 self.find_trait("core:default:Default")
109 pub fn core_iter_Iterator(&self) -> Option<Trait> {
110 self.find_trait("core:iter:traits:iterator:Iterator")
113 pub fn core_iter(&self) -> Option<Module> {
114 self.find_module("core:iter")
117 pub fn core_ops_Deref(&self) -> Option<Trait> {
118 self.find_trait("core:ops:Deref")
121 fn find_trait(&self, path: &str) -> Option<Trait> {
122 match self.find_def(path)? {
123 hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
128 fn find_enum(&self, path: &str) -> Option<Enum> {
129 match self.find_def(path)? {
130 hir::ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(it))) => Some(it),
135 fn find_module(&self, path: &str) -> Option<Module> {
136 match self.find_def(path)? {
137 hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(it)) => Some(it),
142 fn find_crate(&self, name: &str) -> Option<Crate> {
146 krate.dependencies(db).into_iter().find(|dep| dep.name.to_string() == name)?.krate;
150 fn find_def(&self, path: &str) -> Option<ScopeDef> {
152 let mut path = path.split(':');
153 let trait_ = path.next_back()?;
154 let std_crate = path.next()?;
155 let std_crate = self.find_crate(std_crate)?;
156 let mut module = std_crate.root_module(db);
157 for segment in path {
158 module = module.children(db).find_map(|child| {
159 let name = child.name(db)?;
160 if name.to_string() == segment {
168 module.scope(db, None).into_iter().find(|(name, _def)| name.to_string() == trait_)?.1;
173 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
174 pub struct SnippetCap {
179 pub const fn new(allow_snippets: bool) -> Option<SnippetCap> {
181 Some(SnippetCap { _private: () })