1 //! A module with ide helpers for high-level ide features.
4 use hir::{Crate, Enum, Module, ScopeDef, Semantics, Trait};
5 use syntax::ast::{self, make};
7 use crate::RootDatabase;
9 /// Converts the mod path struct into its ast representation.
10 pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
11 let _p = profile::span("mod_path_to_ast");
13 let mut segments = Vec::new();
14 let mut is_abs = false;
16 hir::PathKind::Plain => {}
17 hir::PathKind::Super(0) => segments.push(make::path_segment_self()),
18 hir::PathKind::Super(n) => segments.extend((0..n).map(|_| make::path_segment_super())),
19 hir::PathKind::DollarCrate(_) | hir::PathKind::Crate => {
20 segments.push(make::path_segment_crate())
22 hir::PathKind::Abs => is_abs = true,
28 .map(|segment| make::path_segment(make::name_ref(&segment.to_string()))),
30 make::path_from_segments(segments, is_abs)
33 /// Helps with finding well-know things inside the standard library. This is
34 /// somewhat similar to the known paths infra inside hir, but it different; We
35 /// want to make sure that IDE specific paths don't become interesting inside
36 /// the compiler itself as well.
37 pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Option<Crate>);
39 #[allow(non_snake_case)]
40 impl FamousDefs<'_, '_> {
41 pub const FIXTURE: &'static str = include_str!("helpers/famous_defs_fixture.rs");
43 pub fn core(&self) -> Option<Crate> {
44 self.find_crate("core")
47 pub fn core_convert_From(&self) -> Option<Trait> {
48 self.find_trait("core:convert:From")
51 pub fn core_option_Option(&self) -> Option<Enum> {
52 self.find_enum("core:option:Option")
55 pub fn core_default_Default(&self) -> Option<Trait> {
56 self.find_trait("core:default:Default")
59 pub fn core_iter_Iterator(&self) -> Option<Trait> {
60 self.find_trait("core:iter:traits:iterator:Iterator")
63 pub fn core_iter(&self) -> Option<Module> {
64 self.find_module("core:iter")
67 fn find_trait(&self, path: &str) -> Option<Trait> {
68 match self.find_def(path)? {
69 hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
74 fn find_enum(&self, path: &str) -> Option<Enum> {
75 match self.find_def(path)? {
76 hir::ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(it))) => Some(it),
81 fn find_module(&self, path: &str) -> Option<Module> {
82 match self.find_def(path)? {
83 hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(it)) => Some(it),
88 fn find_crate(&self, name: &str) -> Option<Crate> {
92 krate.dependencies(db).into_iter().find(|dep| dep.name.to_string() == name)?.krate;
96 fn find_def(&self, path: &str) -> Option<ScopeDef> {
98 let mut path = path.split(':');
99 let trait_ = path.next_back()?;
100 let std_crate = path.next()?;
101 let std_crate = self.find_crate(std_crate)?;
102 let mut module = std_crate.root_module(db);
103 for segment in path {
104 module = module.children(db).find_map(|child| {
105 let name = child.name(db)?;
106 if name.to_string() == segment {
114 module.scope(db, None).into_iter().find(|(name, _def)| name.to_string() == trait_)?.1;
119 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
120 pub struct SnippetCap {
125 pub const fn new(allow_snippets: bool) -> Option<SnippetCap> {
127 Some(SnippetCap { _private: () })