From ab559f170ee02e3bdd9aeeb55933bb143b520c34 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 19:10:53 +0300 Subject: move hygiene to hir_expand --- crates/ra_hir_expand/src/hygiene.rs | 46 +++++++++++++++++++++++++++++++++++++ crates/ra_hir_expand/src/lib.rs | 12 +--------- 2 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 crates/ra_hir_expand/src/hygiene.rs (limited to 'crates/ra_hir_expand/src') 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 @@ +//! This modules handles hygiene information. +//! +//! Specifically, `ast` + `Hygiene` allows you to create a `Name`. Note that, at +//! this moment, this is horribly incomplete and handles only `$crate`. +use ra_db::CrateId; +use ra_syntax::ast; + +use crate::{ + db::AstDatabase, + either::Either, + name::{AsName, Name}, + HirFileId, HirFileIdRepr, +}; + +#[derive(Debug)] +pub struct Hygiene { + // This is what `$crate` expands to + def_crate: Option, +} + +impl Hygiene { + pub fn new(db: &impl AstDatabase, file_id: HirFileId) -> Hygiene { + let def_crate = match file_id.0 { + HirFileIdRepr::FileId(_) => None, + HirFileIdRepr::MacroFile(macro_file) => { + let loc = db.lookup_intern_macro(macro_file.macro_call_id); + Some(loc.def.krate) + } + }; + Hygiene { def_crate } + } + + pub fn new_unhygienic() -> Hygiene { + Hygiene { def_crate: None } + } + + // FIXME: this should just return name + pub fn name_ref_to_name(&self, name_ref: ast::NameRef) -> Either { + if let Some(def_crate) = self.def_crate { + if name_ref.text() == "$crate" { + return Either::B(def_crate); + } + } + Either::A(name_ref.as_name()) + } +} 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; pub mod ast_id_map; pub mod either; pub mod name; +pub mod hygiene; use std::hash::{Hash, Hasher}; @@ -61,17 +62,6 @@ impl HirFileId { } } } - - /// Get the crate which the macro lives in, if it is a macro file. - pub fn macro_crate(self, db: &dyn db::AstDatabase) -> Option { - match self.0 { - HirFileIdRepr::FileId(_) => None, - HirFileIdRepr::MacroFile(macro_file) => { - let loc = db.lookup_intern_macro(macro_file.macro_call_id); - Some(loc.def.krate) - } - } - } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -- cgit v1.2.3