diff options
Diffstat (limited to 'crates/ra_hir_def/src/hygiene.rs')
-rw-r--r-- | crates/ra_hir_def/src/hygiene.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/crates/ra_hir_def/src/hygiene.rs b/crates/ra_hir_def/src/hygiene.rs new file mode 100644 index 000000000..e1ae58a3b --- /dev/null +++ b/crates/ra_hir_def/src/hygiene.rs | |||
@@ -0,0 +1,40 @@ | |||
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 | // Should this be moved to `hir_expand`? Seems like it. | ||
6 | |||
7 | use hir_expand::{db::AstDatabase, HirFileId}; | ||
8 | use ra_db::CrateId; | ||
9 | use ra_syntax::ast; | ||
10 | |||
11 | use crate::{ | ||
12 | either::Either, | ||
13 | name::{AsName, Name}, | ||
14 | }; | ||
15 | |||
16 | #[derive(Debug)] | ||
17 | pub struct Hygiene { | ||
18 | // This is what `$crate` expands to | ||
19 | def_crate: Option<CrateId>, | ||
20 | } | ||
21 | |||
22 | impl Hygiene { | ||
23 | pub fn new(db: &impl AstDatabase, file_id: HirFileId) -> Hygiene { | ||
24 | Hygiene { def_crate: file_id.macro_crate(db) } | ||
25 | } | ||
26 | |||
27 | pub(crate) fn new_unhygienic() -> Hygiene { | ||
28 | Hygiene { def_crate: None } | ||
29 | } | ||
30 | |||
31 | // FIXME: this should just return name | ||
32 | pub(crate) fn name_ref_to_name(&self, name_ref: ast::NameRef) -> Either<Name, CrateId> { | ||
33 | if let Some(def_crate) = self.def_crate { | ||
34 | if name_ref.text() == "$crate" { | ||
35 | return Either::B(def_crate); | ||
36 | } | ||
37 | } | ||
38 | Either::A(name_ref.as_name()) | ||
39 | } | ||
40 | } | ||