Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-expand/src/mod_path.rs')
| -rw-r--r-- | crates/hir-expand/src/mod_path.rs | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/crates/hir-expand/src/mod_path.rs b/crates/hir-expand/src/mod_path.rs index d38e4a52a8..af59733b9f 100644 --- a/crates/hir-expand/src/mod_path.rs +++ b/crates/hir-expand/src/mod_path.rs @@ -20,6 +20,9 @@ pub struct ModPath { segments: Vec<Name>, } +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct EscapedModPath<'a>(&'a ModPath); + #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum PathKind { Plain, @@ -97,10 +100,12 @@ impl ModPath { _ => None, } } -} -impl Display for ModPath { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + pub fn escaped(&self) -> EscapedModPath { + EscapedModPath(self) + } + + fn _fmt(&self, f: &mut fmt::Formatter<'_>, escaped: bool) -> fmt::Result { let mut first_segment = true; let mut add_segment = |s| -> fmt::Result { if !first_segment { @@ -127,12 +132,28 @@ impl Display for ModPath { f.write_str("::")?; } first_segment = false; - segment.fmt(f)?; + if escaped { + segment.escaped().fmt(f)? + } else { + segment.fmt(f)? + }; } Ok(()) } } +impl Display for ModPath { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self._fmt(f, false) + } +} + +impl<'a> Display for EscapedModPath<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0._fmt(f, true) + } +} + impl From<Name> for ModPath { fn from(name: Name) -> ModPath { ModPath::from_segments(PathKind::Plain, iter::once(name)) |