]> git.lizzy.rs Git - rust.git/blob - crates/ide_db/src/helpers.rs
Merge #7795
[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 std(&self) -> Option<Crate> {
45         self.find_crate("std")
46     }
47
48     pub fn core(&self) -> Option<Crate> {
49         self.find_crate("core")
50     }
51
52     pub fn core_cmp_Ord(&self) -> Option<Trait> {
53         self.find_trait("core:cmp:Ord")
54     }
55
56     pub fn core_convert_From(&self) -> Option<Trait> {
57         self.find_trait("core:convert:From")
58     }
59
60     pub fn core_option_Option(&self) -> Option<Enum> {
61         self.find_enum("core:option:Option")
62     }
63
64     pub fn core_default_Default(&self) -> Option<Trait> {
65         self.find_trait("core:default:Default")
66     }
67
68     pub fn core_iter_Iterator(&self) -> Option<Trait> {
69         self.find_trait("core:iter:traits:iterator:Iterator")
70     }
71
72     pub fn core_iter(&self) -> Option<Module> {
73         self.find_module("core:iter")
74     }
75
76     fn find_trait(&self, path: &str) -> Option<Trait> {
77         match self.find_def(path)? {
78             hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
79             _ => None,
80         }
81     }
82
83     fn find_enum(&self, path: &str) -> Option<Enum> {
84         match self.find_def(path)? {
85             hir::ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(it))) => Some(it),
86             _ => None,
87         }
88     }
89
90     fn find_module(&self, path: &str) -> Option<Module> {
91         match self.find_def(path)? {
92             hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(it)) => Some(it),
93             _ => None,
94         }
95     }
96
97     fn find_crate(&self, name: &str) -> Option<Crate> {
98         let krate = self.1?;
99         let db = self.0.db;
100         let res =
101             krate.dependencies(db).into_iter().find(|dep| dep.name.to_string() == name)?.krate;
102         Some(res)
103     }
104
105     fn find_def(&self, path: &str) -> Option<ScopeDef> {
106         let db = self.0.db;
107         let mut path = path.split(':');
108         let trait_ = path.next_back()?;
109         let std_crate = path.next()?;
110         let std_crate = self.find_crate(std_crate)?;
111         let mut module = std_crate.root_module(db);
112         for segment in path {
113             module = module.children(db).find_map(|child| {
114                 let name = child.name(db)?;
115                 if name.to_string() == segment {
116                     Some(child)
117                 } else {
118                     None
119                 }
120             })?;
121         }
122         let def =
123             module.scope(db, None).into_iter().find(|(name, _def)| name.to_string() == trait_)?.1;
124         Some(def)
125     }
126 }
127
128 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
129 pub struct SnippetCap {
130     _private: (),
131 }
132
133 impl SnippetCap {
134     pub const fn new(allow_snippets: bool) -> Option<SnippetCap> {
135         if allow_snippets {
136             Some(SnippetCap { _private: () })
137         } else {
138             None
139         }
140     }
141 }