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