1 //! A module with ide helpers for high-level ide features.
7 use std::collections::VecDeque;
11 use hir::{Crate, Enum, ItemInNs, MacroDef, Module, ModuleDef, Name, ScopeDef, Semantics, Trait};
12 use syntax::ast::{self, make};
14 use crate::RootDatabase;
16 pub fn item_name(db: &RootDatabase, item: ItemInNs) -> Option<Name> {
18 ItemInNs::Types(module_def_id) => ModuleDef::from(module_def_id).name(db),
19 ItemInNs::Values(module_def_id) => ModuleDef::from(module_def_id).name(db),
20 ItemInNs::Macros(macro_def_id) => MacroDef::from(macro_def_id).name(db),
24 /// Converts the mod path struct into its ast representation.
25 pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
26 let _p = profile::span("mod_path_to_ast");
28 let mut segments = Vec::new();
29 let mut is_abs = false;
31 hir::PathKind::Plain => {}
32 hir::PathKind::Super(0) => segments.push(make::path_segment_self()),
33 hir::PathKind::Super(n) => segments.extend((0..n).map(|_| make::path_segment_super())),
34 hir::PathKind::DollarCrate(_) | hir::PathKind::Crate => {
35 segments.push(make::path_segment_crate())
37 hir::PathKind::Abs => is_abs = true,
43 .map(|segment| make::path_segment(make::name_ref(&segment.to_string()))),
45 make::path_from_segments(segments, is_abs)
48 /// Iterates all `ModuleDef`s and `Impl` blocks of the given file.
49 pub fn visit_file_defs(
50 sema: &Semantics<RootDatabase>,
52 cb: &mut dyn FnMut(Either<hir::ModuleDef, hir::Impl>),
55 let module = match sema.to_module_def(file_id) {
59 let mut defs: VecDeque<_> = module.declarations(db).into();
60 while let Some(def) = defs.pop_front() {
61 if let ModuleDef::Module(submodule) = def {
62 if let hir::ModuleSource::Module(_) = submodule.definition_source(db).value {
63 defs.extend(submodule.declarations(db));
64 submodule.impl_defs(db).into_iter().for_each(|impl_| cb(Either::Right(impl_)));
67 cb(Either::Left(def));
69 module.impl_defs(db).into_iter().for_each(|impl_| cb(Either::Right(impl_)));
72 /// Helps with finding well-know things inside the standard library. This is
73 /// somewhat similar to the known paths infra inside hir, but it different; We
74 /// want to make sure that IDE specific paths don't become interesting inside
75 /// the compiler itself as well.
76 pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Option<Crate>);
78 #[allow(non_snake_case)]
79 impl FamousDefs<'_, '_> {
80 pub const FIXTURE: &'static str = include_str!("helpers/famous_defs_fixture.rs");
82 pub fn std(&self) -> Option<Crate> {
83 self.find_crate("std")
86 pub fn core(&self) -> Option<Crate> {
87 self.find_crate("core")
90 pub fn core_cmp_Ord(&self) -> Option<Trait> {
91 self.find_trait("core:cmp:Ord")
94 pub fn core_convert_From(&self) -> Option<Trait> {
95 self.find_trait("core:convert:From")
98 pub fn core_convert_Into(&self) -> Option<Trait> {
99 self.find_trait("core:convert:Into")
102 pub fn core_option_Option(&self) -> Option<Enum> {
103 self.find_enum("core:option:Option")
106 pub fn core_default_Default(&self) -> Option<Trait> {
107 self.find_trait("core:default:Default")
110 pub fn core_iter_Iterator(&self) -> Option<Trait> {
111 self.find_trait("core:iter:traits:iterator:Iterator")
114 pub fn core_iter(&self) -> Option<Module> {
115 self.find_module("core:iter")
118 pub fn core_ops_Deref(&self) -> Option<Trait> {
119 self.find_trait("core:ops:Deref")
122 fn find_trait(&self, path: &str) -> Option<Trait> {
123 match self.find_def(path)? {
124 hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
129 fn find_enum(&self, path: &str) -> Option<Enum> {
130 match self.find_def(path)? {
131 hir::ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(it))) => Some(it),
136 fn find_module(&self, path: &str) -> Option<Module> {
137 match self.find_def(path)? {
138 hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(it)) => Some(it),
143 fn find_crate(&self, name: &str) -> Option<Crate> {
147 krate.dependencies(db).into_iter().find(|dep| dep.name.to_string() == name)?.krate;
151 fn find_def(&self, path: &str) -> Option<ScopeDef> {
153 let mut path = path.split(':');
154 let trait_ = path.next_back()?;
155 let std_crate = path.next()?;
156 let std_crate = self.find_crate(std_crate)?;
157 let mut module = std_crate.root_module(db);
158 for segment in path {
159 module = module.children(db).find_map(|child| {
160 let name = child.name(db)?;
161 if name.to_string() == segment {
169 module.scope(db, None).into_iter().find(|(name, _def)| name.to_string() == trait_)?.1;
174 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
175 pub struct SnippetCap {
180 pub const fn new(allow_snippets: bool) -> Option<SnippetCap> {
182 Some(SnippetCap { _private: () })