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 = r#"//- /libcore.rs crate:core
44 fn from(t: T) -> Self;
55 pub use self::traits::{collect::IntoIterator, iterator::Iterator};
57 pub(crate) mod iterator {
58 use crate::option::Option;
61 fn next(&mut self) -> Option<Self::Item>;
62 fn by_ref(&mut self) -> &mut Self {
65 fn take(self, n: usize) -> crate::iter::Take<Self> {
66 crate::iter::Take { inner: self }
70 impl<I: Iterator> Iterator for &mut I {
72 fn next(&mut self) -> Option<I::Item> {
77 pub(crate) mod collect {
78 pub trait IntoIterator {
84 pub use self::sources::*;
85 pub(crate) mod sources {
87 use crate::option::Option::{self, *};
88 pub struct Repeat<A> {
92 pub fn repeat<T>(elt: T) -> Repeat<T> {
93 Repeat { element: elt }
96 impl<A> Iterator for Repeat<A> {
99 fn next(&mut self) -> Option<A> {
105 pub use self::adapters::*;
106 pub(crate) mod adapters {
108 use crate::option::Option::{self, *};
109 pub struct Take<I> { pub(crate) inner: I }
110 impl<I> Iterator for Take<I> where I: Iterator {
111 type Item = <I as Iterator>::Item;
112 fn next(&mut self) -> Option<<I as Iterator>::Item> {
120 pub enum Option<T> { None, Some(T)}
124 pub use crate::{convert::From, iter::{IntoIterator, Iterator}, option::Option::{self, *}, default::Default};
130 pub fn core(&self) -> Option<Crate> {
131 self.find_crate("core")
134 pub fn core_convert_From(&self) -> Option<Trait> {
135 self.find_trait("core:convert:From")
138 pub fn core_option_Option(&self) -> Option<Enum> {
139 self.find_enum("core:option:Option")
142 pub fn core_default_Default(&self) -> Option<Trait> {
143 self.find_trait("core:default:Default")
146 pub fn core_iter_Iterator(&self) -> Option<Trait> {
147 self.find_trait("core:iter:traits:iterator:Iterator")
150 pub fn core_iter(&self) -> Option<Module> {
151 self.find_module("core:iter")
154 fn find_trait(&self, path: &str) -> Option<Trait> {
155 match self.find_def(path)? {
156 hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
161 fn find_enum(&self, path: &str) -> Option<Enum> {
162 match self.find_def(path)? {
163 hir::ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(it))) => Some(it),
168 fn find_module(&self, path: &str) -> Option<Module> {
169 match self.find_def(path)? {
170 hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(it)) => Some(it),
175 fn find_crate(&self, name: &str) -> Option<Crate> {
179 krate.dependencies(db).into_iter().find(|dep| dep.name.to_string() == name)?.krate;
183 fn find_def(&self, path: &str) -> Option<ScopeDef> {
185 let mut path = path.split(':');
186 let trait_ = path.next_back()?;
187 let std_crate = path.next()?;
188 let std_crate = self.find_crate(std_crate)?;
189 let mut module = std_crate.root_module(db);
190 for segment in path {
191 module = module.children(db).find_map(|child| {
192 let name = child.name(db)?;
193 if name.to_string() == segment {
201 module.scope(db, None).into_iter().find(|(name, _def)| name.to_string() == trait_)?.1;
206 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
207 pub struct SnippetCap {
212 pub const fn new(allow_snippets: bool) -> Option<SnippetCap> {
214 Some(SnippetCap { _private: () })