1 //! A module with ide helpers for high-level ide features.
5 use std::collections::VecDeque;
9 use hir::{Crate, Enum, ItemInNs, MacroDef, Module, ModuleDef, Name, ScopeDef, Semantics, Trait};
10 use syntax::ast::{self, make};
12 use crate::RootDatabase;
14 pub fn item_name(db: &RootDatabase, item: ItemInNs) -> Option<Name> {
16 ItemInNs::Types(module_def_id) => ModuleDef::from(module_def_id).name(db),
17 ItemInNs::Values(module_def_id) => ModuleDef::from(module_def_id).name(db),
18 ItemInNs::Macros(macro_def_id) => MacroDef::from(macro_def_id).name(db),
22 /// Converts the mod path struct into its ast representation.
23 pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
24 let _p = profile::span("mod_path_to_ast");
26 let mut segments = Vec::new();
27 let mut is_abs = false;
29 hir::PathKind::Plain => {}
30 hir::PathKind::Super(0) => segments.push(make::path_segment_self()),
31 hir::PathKind::Super(n) => segments.extend((0..n).map(|_| make::path_segment_super())),
32 hir::PathKind::DollarCrate(_) | hir::PathKind::Crate => {
33 segments.push(make::path_segment_crate())
35 hir::PathKind::Abs => is_abs = true,
41 .map(|segment| make::path_segment(make::name_ref(&segment.to_string()))),
43 make::path_from_segments(segments, is_abs)
46 /// Iterates all `ModuleDef`s and `Impl` blocks of the given file.
47 pub fn visit_file_defs(
48 sema: &Semantics<RootDatabase>,
50 cb: &mut dyn FnMut(Either<hir::ModuleDef, hir::Impl>),
53 let module = match sema.to_module_def(file_id) {
57 let mut defs: VecDeque<_> = module.declarations(db).into();
58 while let Some(def) = defs.pop_front() {
59 if let ModuleDef::Module(submodule) = def {
60 if let hir::ModuleSource::Module(_) = submodule.definition_source(db).value {
61 defs.extend(submodule.declarations(db));
62 submodule.impl_defs(db).into_iter().for_each(|impl_| cb(Either::Right(impl_)));
65 cb(Either::Left(def));
67 module.impl_defs(db).into_iter().for_each(|impl_| cb(Either::Right(impl_)));
70 /// Helps with finding well-know things inside the standard library. This is
71 /// somewhat similar to the known paths infra inside hir, but it different; We
72 /// want to make sure that IDE specific paths don't become interesting inside
73 /// the compiler itself as well.
74 pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Option<Crate>);
76 #[allow(non_snake_case)]
77 impl FamousDefs<'_, '_> {
78 pub const FIXTURE: &'static str = include_str!("helpers/famous_defs_fixture.rs");
80 pub fn std(&self) -> Option<Crate> {
81 self.find_crate("std")
84 pub fn core(&self) -> Option<Crate> {
85 self.find_crate("core")
88 pub fn core_cmp_Ord(&self) -> Option<Trait> {
89 self.find_trait("core:cmp:Ord")
92 pub fn core_convert_From(&self) -> Option<Trait> {
93 self.find_trait("core:convert:From")
96 pub fn core_convert_Into(&self) -> Option<Trait> {
97 self.find_trait("core:convert:Into")
100 pub fn core_option_Option(&self) -> Option<Enum> {
101 self.find_enum("core:option:Option")
104 pub fn core_default_Default(&self) -> Option<Trait> {
105 self.find_trait("core:default:Default")
108 pub fn core_iter_Iterator(&self) -> Option<Trait> {
109 self.find_trait("core:iter:traits:iterator:Iterator")
112 pub fn core_iter(&self) -> Option<Module> {
113 self.find_module("core:iter")
116 pub fn core_ops_Deref(&self) -> Option<Trait> {
117 self.find_trait("core:ops:Deref")
120 fn find_trait(&self, path: &str) -> Option<Trait> {
121 match self.find_def(path)? {
122 hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
127 fn find_enum(&self, path: &str) -> Option<Enum> {
128 match self.find_def(path)? {
129 hir::ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(it))) => Some(it),
134 fn find_module(&self, path: &str) -> Option<Module> {
135 match self.find_def(path)? {
136 hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(it)) => Some(it),
141 fn find_crate(&self, name: &str) -> Option<Crate> {
145 krate.dependencies(db).into_iter().find(|dep| dep.name.to_string() == name)?.krate;
149 fn find_def(&self, path: &str) -> Option<ScopeDef> {
151 let mut path = path.split(':');
152 let trait_ = path.next_back()?;
153 let std_crate = path.next()?;
154 let std_crate = self.find_crate(std_crate)?;
155 let mut module = std_crate.root_module(db);
156 for segment in path {
157 module = module.children(db).find_map(|child| {
158 let name = child.name(db)?;
159 if name.to_string() == segment {
167 module.scope(db, None).into_iter().find(|(name, _def)| name.to_string() == trait_)?.1;
172 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
173 pub struct SnippetCap {
178 pub const fn new(allow_snippets: bool) -> Option<SnippetCap> {
180 Some(SnippetCap { _private: () })