]> git.lizzy.rs Git - rust.git/blob - crates/ide_db/src/helpers.rs
Merge #7326
[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 pub mod import_assets;
4
5 use hir::{Crate, Enum, Module, ScopeDef, Semantics, Trait};
6 use syntax::ast::{self, make};
7
8 use crate::RootDatabase;
9
10 /// Converts the mod path struct into its ast representation.
11 pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
12     let _p = profile::span("mod_path_to_ast");
13
14     let mut segments = Vec::new();
15     let mut is_abs = false;
16     match path.kind {
17         hir::PathKind::Plain => {}
18         hir::PathKind::Super(0) => segments.push(make::path_segment_self()),
19         hir::PathKind::Super(n) => segments.extend((0..n).map(|_| make::path_segment_super())),
20         hir::PathKind::DollarCrate(_) | hir::PathKind::Crate => {
21             segments.push(make::path_segment_crate())
22         }
23         hir::PathKind::Abs => is_abs = true,
24     }
25
26     segments.extend(
27         path.segments
28             .iter()
29             .map(|segment| make::path_segment(make::name_ref(&segment.to_string()))),
30     );
31     make::path_from_segments(segments, is_abs)
32 }
33
34 /// Helps with finding well-know things inside the standard library. This is
35 /// somewhat similar to the known paths infra inside hir, but it different; We
36 /// want to make sure that IDE specific paths don't become interesting inside
37 /// the compiler itself as well.
38 pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Option<Crate>);
39
40 #[allow(non_snake_case)]
41 impl FamousDefs<'_, '_> {
42     pub const FIXTURE: &'static str = include_str!("helpers/famous_defs_fixture.rs");
43
44     pub fn core(&self) -> Option<Crate> {
45         self.find_crate("core")
46     }
47
48     pub fn core_convert_From(&self) -> Option<Trait> {
49         self.find_trait("core:convert:From")
50     }
51
52     pub fn core_option_Option(&self) -> Option<Enum> {
53         self.find_enum("core:option:Option")
54     }
55
56     pub fn core_default_Default(&self) -> Option<Trait> {
57         self.find_trait("core:default:Default")
58     }
59
60     pub fn core_iter_Iterator(&self) -> Option<Trait> {
61         self.find_trait("core:iter:traits:iterator:Iterator")
62     }
63
64     pub fn core_iter(&self) -> Option<Module> {
65         self.find_module("core:iter")
66     }
67
68     fn find_trait(&self, path: &str) -> Option<Trait> {
69         match self.find_def(path)? {
70             hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
71             _ => None,
72         }
73     }
74
75     fn find_enum(&self, path: &str) -> Option<Enum> {
76         match self.find_def(path)? {
77             hir::ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(it))) => Some(it),
78             _ => None,
79         }
80     }
81
82     fn find_module(&self, path: &str) -> Option<Module> {
83         match self.find_def(path)? {
84             hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(it)) => Some(it),
85             _ => None,
86         }
87     }
88
89     fn find_crate(&self, name: &str) -> Option<Crate> {
90         let krate = self.1?;
91         let db = self.0.db;
92         let res =
93             krate.dependencies(db).into_iter().find(|dep| dep.name.to_string() == name)?.krate;
94         Some(res)
95     }
96
97     fn find_def(&self, path: &str) -> Option<ScopeDef> {
98         let db = self.0.db;
99         let mut path = path.split(':');
100         let trait_ = path.next_back()?;
101         let std_crate = path.next()?;
102         let std_crate = self.find_crate(std_crate)?;
103         let mut module = std_crate.root_module(db);
104         for segment in path {
105             module = module.children(db).find_map(|child| {
106                 let name = child.name(db)?;
107                 if name.to_string() == segment {
108                     Some(child)
109                 } else {
110                     None
111                 }
112             })?;
113         }
114         let def =
115             module.scope(db, None).into_iter().find(|(name, _def)| name.to_string() == trait_)?.1;
116         Some(def)
117     }
118 }
119
120 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
121 pub struct SnippetCap {
122     _private: (),
123 }
124
125 impl SnippetCap {
126     pub const fn new(allow_snippets: bool) -> Option<SnippetCap> {
127         if allow_snippets {
128             Some(SnippetCap { _private: () })
129         } else {
130             None
131         }
132     }
133 }