aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_expand/src')
-rw-r--r--crates/ra_hir_expand/src/hygiene.rs46
-rw-r--r--crates/ra_hir_expand/src/lib.rs12
2 files changed, 47 insertions, 11 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`.
5use ra_db::CrateId;
6use ra_syntax::ast;
7
8use crate::{
9 db::AstDatabase,
10 either::Either,
11 name::{AsName, Name},
12 HirFileId, HirFileIdRepr,
13};
14
15#[derive(Debug)]
16pub struct Hygiene {
17 // This is what `$crate` expands to
18 def_crate: Option<CrateId>,
19}
20
21impl 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}
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs
index cf28de3d8..5a0e5a19c 100644
--- a/crates/ra_hir_expand/src/lib.rs
+++ b/crates/ra_hir_expand/src/lib.rs
@@ -8,6 +8,7 @@ pub mod db;
8pub mod ast_id_map; 8pub mod ast_id_map;
9pub mod either; 9pub mod either;
10pub mod name; 10pub mod name;
11pub mod hygiene;
11 12
12use std::hash::{Hash, Hasher}; 13use std::hash::{Hash, Hasher};
13 14
@@ -61,17 +62,6 @@ impl HirFileId {
61 } 62 }
62 } 63 }
63 } 64 }
64
65 /// Get the crate which the macro lives in, if it is a macro file.
66 pub fn macro_crate(self, db: &dyn db::AstDatabase) -> Option<CrateId> {
67 match self.0 {
68 HirFileIdRepr::FileId(_) => None,
69 HirFileIdRepr::MacroFile(macro_file) => {
70 let loc = db.lookup_intern_macro(macro_file.macro_call_id);
71 Some(loc.def.krate)
72 }
73 }
74 }
75} 65}
76 66
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 67#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]