]> git.lizzy.rs Git - rust.git/blob - crates/ide_db/src/helpers.rs
Merge #7211
[rust.git] / crates / ide_db / src / helpers.rs
1 //! A module with ide helpers for high-level ide features.
2 pub mod insert_use;
3
4 use hir::{Crate, Enum, Module, ScopeDef, Semantics, Trait};
5 use syntax::ast::{self, make};
6
7 use crate::RootDatabase;
8
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");
12
13     let mut segments = Vec::new();
14     let mut is_abs = false;
15     match path.kind {
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())
21         }
22         hir::PathKind::Abs => is_abs = true,
23     }
24
25     segments.extend(
26         path.segments
27             .iter()
28             .map(|segment| make::path_segment(make::name_ref(&segment.to_string()))),
29     );
30     make::path_from_segments(segments, is_abs)
31 }
32
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>);
38
39 #[allow(non_snake_case)]
40 impl FamousDefs<'_, '_> {
41     pub const FIXTURE: &'static str = include_str!("helpers/famous_defs_fixture.rs");
42
43     pub fn core(&self) -> Option<Crate> {
44         self.find_crate("core")
45     }
46
47     pub fn core_convert_From(&self) -> Option<Trait> {
48         self.find_trait("core:convert:From")
49     }
50
51     pub fn core_option_Option(&self) -> Option<Enum> {
52         self.find_enum("core:option:Option")
53     }
54
55     pub fn core_default_Default(&self) -> Option<Trait> {
56         self.find_trait("core:default:Default")
57     }
58
59     pub fn core_iter_Iterator(&self) -> Option<Trait> {
60         self.find_trait("core:iter:traits:iterator:Iterator")
61     }
62
63     pub fn core_iter(&self) -> Option<Module> {
64         self.find_module("core:iter")
65     }
66
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),
70             _ => None,
71         }
72     }
73
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),
77             _ => None,
78         }
79     }
80
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),
84             _ => None,
85         }
86     }
87
88     fn find_crate(&self, name: &str) -> Option<Crate> {
89         let krate = self.1?;
90         let db = self.0.db;
91         let res =
92             krate.dependencies(db).into_iter().find(|dep| dep.name.to_string() == name)?.krate;
93         Some(res)
94     }
95
96     fn find_def(&self, path: &str) -> Option<ScopeDef> {
97         let db = self.0.db;
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 {
107                     Some(child)
108                 } else {
109                     None
110                 }
111             })?;
112         }
113         let def =
114             module.scope(db, None).into_iter().find(|(name, _def)| name.to_string() == trait_)?.1;
115         Some(def)
116     }
117 }
118
119 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
120 pub struct SnippetCap {
121     _private: (),
122 }
123
124 impl SnippetCap {
125     pub const fn new(allow_snippets: bool) -> Option<SnippetCap> {
126         if allow_snippets {
127             Some(SnippetCap { _private: () })
128         } else {
129             None
130         }
131     }
132 }