diff options
Diffstat (limited to 'crates/ra_hir_expand/src/hygiene.rs')
-rw-r--r-- | crates/ra_hir_expand/src/hygiene.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/crates/ra_hir_expand/src/hygiene.rs b/crates/ra_hir_expand/src/hygiene.rs new file mode 100644 index 000000000..77428ec99 --- /dev/null +++ b/crates/ra_hir_expand/src/hygiene.rs | |||
@@ -0,0 +1,46 @@ | |||
1 | //! This modules handles hygiene information. | ||
2 | //! | ||
3 | //! Specifically, `ast` + `Hygiene` allows you to create a `Name`. Note that, at | ||
4 | //! this moment, this is horribly incomplete and handles only `$crate`. | ||
5 | use ra_db::CrateId; | ||
6 | use ra_syntax::ast; | ||
7 | |||
8 | use crate::{ | ||
9 | db::AstDatabase, | ||
10 | either::Either, | ||
11 | name::{AsName, Name}, | ||
12 | HirFileId, HirFileIdRepr, | ||
13 | }; | ||
14 | |||
15 | #[derive(Debug)] | ||
16 | pub struct Hygiene { | ||
17 | // This is what `$crate` expands to | ||
18 | def_crate: Option<CrateId>, | ||
19 | } | ||
20 | |||
21 | impl Hygiene { | ||
22 | pub fn new(db: &impl AstDatabase, file_id: HirFileId) -> Hygiene { | ||
23 | let def_crate = match file_id.0 { | ||
24 | HirFileIdRepr::FileId(_) => None, | ||
25 | HirFileIdRepr::MacroFile(macro_file) => { | ||
26 | let loc = db.lookup_intern_macro(macro_file.macro_call_id); | ||
27 | Some(loc.def.krate) | ||
28 | } | ||
29 | }; | ||
30 | Hygiene { def_crate } | ||
31 | } | ||
32 | |||
33 | pub fn new_unhygienic() -> Hygiene { | ||
34 | Hygiene { def_crate: None } | ||
35 | } | ||
36 | |||
37 | // FIXME: this should just return name | ||
38 | pub fn name_ref_to_name(&self, name_ref: ast::NameRef) -> Either<Name, CrateId> { | ||
39 | if let Some(def_crate) = self.def_crate { | ||
40 | if name_ref.text() == "$crate" { | ||
41 | return Either::B(def_crate); | ||
42 | } | ||
43 | } | ||
44 | Either::A(name_ref.as_name()) | ||
45 | } | ||
46 | } | ||