1 //! A module with ide helpers for high-level ide features.
5 use hir::{Crate, Enum, ItemInNs, MacroDef, Module, ModuleDef, Name, ScopeDef, Semantics, Trait};
6 use syntax::ast::{self, make};
8 use crate::RootDatabase;
10 pub fn item_name(db: &RootDatabase, item: ItemInNs) -> Option<Name> {
12 ItemInNs::Types(module_def_id) => ModuleDef::from(module_def_id).name(db),
13 ItemInNs::Values(module_def_id) => ModuleDef::from(module_def_id).name(db),
14 ItemInNs::Macros(macro_def_id) => MacroDef::from(macro_def_id).name(db),
18 /// Converts the mod path struct into its ast representation.
19 pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
20 let _p = profile::span("mod_path_to_ast");
22 let mut segments = Vec::new();
23 let mut is_abs = false;
25 hir::PathKind::Plain => {}
26 hir::PathKind::Super(0) => segments.push(make::path_segment_self()),
27 hir::PathKind::Super(n) => segments.extend((0..n).map(|_| make::path_segment_super())),
28 hir::PathKind::DollarCrate(_) | hir::PathKind::Crate => {
29 segments.push(make::path_segment_crate())
31 hir::PathKind::Abs => is_abs = true,
37 .map(|segment| make::path_segment(make::name_ref(&segment.to_string()))),
39 make::path_from_segments(segments, is_abs)
42 /// Helps with finding well-know things inside the standard library. This is
43 /// somewhat similar to the known paths infra inside hir, but it different; We
44 /// want to make sure that IDE specific paths don't become interesting inside
45 /// the compiler itself as well.
46 pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Option<Crate>);
48 #[allow(non_snake_case)]
49 impl FamousDefs<'_, '_> {
50 pub const FIXTURE: &'static str = include_str!("helpers/famous_defs_fixture.rs");
52 pub fn std(&self) -> Option<Crate> {
53 self.find_crate("std")
56 pub fn core(&self) -> Option<Crate> {
57 self.find_crate("core")
60 pub fn core_cmp_Ord(&self) -> Option<Trait> {
61 self.find_trait("core:cmp:Ord")
64 pub fn core_convert_From(&self) -> Option<Trait> {
65 self.find_trait("core:convert:From")
68 pub fn core_option_Option(&self) -> Option<Enum> {
69 self.find_enum("core:option:Option")
72 pub fn core_default_Default(&self) -> Option<Trait> {
73 self.find_trait("core:default:Default")
76 pub fn core_iter_Iterator(&self) -> Option<Trait> {
77 self.find_trait("core:iter:traits:iterator:Iterator")
80 pub fn core_iter(&self) -> Option<Module> {
81 self.find_module("core:iter")
84 fn find_trait(&self, path: &str) -> Option<Trait> {
85 match self.find_def(path)? {
86 hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
91 fn find_enum(&self, path: &str) -> Option<Enum> {
92 match self.find_def(path)? {
93 hir::ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(it))) => Some(it),
98 fn find_module(&self, path: &str) -> Option<Module> {
99 match self.find_def(path)? {
100 hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(it)) => Some(it),
105 fn find_crate(&self, name: &str) -> Option<Crate> {
109 krate.dependencies(db).into_iter().find(|dep| dep.name.to_string() == name)?.krate;
113 fn find_def(&self, path: &str) -> Option<ScopeDef> {
115 let mut path = path.split(':');
116 let trait_ = path.next_back()?;
117 let std_crate = path.next()?;
118 let std_crate = self.find_crate(std_crate)?;
119 let mut module = std_crate.root_module(db);
120 for segment in path {
121 module = module.children(db).find_map(|child| {
122 let name = child.name(db)?;
123 if name.to_string() == segment {
131 module.scope(db, None).into_iter().find(|(name, _def)| name.to_string() == trait_)?.1;
136 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
137 pub struct SnippetCap {
142 pub const fn new(allow_snippets: bool) -> Option<SnippetCap> {
144 Some(SnippetCap { _private: () })