Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #21298 from J3m3/item-tree-pretty
feat: pretty print attributes up to `cfg(false)`
| -rw-r--r-- | crates/cfg/src/cfg_expr.rs | 30 | ||||
| -rw-r--r-- | crates/hir-def/src/item_tree/pretty.rs | 9 |
2 files changed, 36 insertions, 3 deletions
diff --git a/crates/cfg/src/cfg_expr.rs b/crates/cfg/src/cfg_expr.rs index 76e0aba859..a0e0dc5ff0 100644 --- a/crates/cfg/src/cfg_expr.rs +++ b/crates/cfg/src/cfg_expr.rs @@ -56,6 +56,36 @@ pub enum CfgExpr { Not(Box<CfgExpr>), } +impl fmt::Display for CfgExpr { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + CfgExpr::Atom(atom) => atom.fmt(f), + CfgExpr::All(exprs) => { + write!(f, "all(")?; + for (i, expr) in exprs.iter().enumerate() { + if i > 0 { + write!(f, ", ")?; + } + expr.fmt(f)?; + } + write!(f, ")") + } + CfgExpr::Any(exprs) => { + write!(f, "any(")?; + for (i, expr) in exprs.iter().enumerate() { + if i > 0 { + write!(f, ", ")?; + } + expr.fmt(f)?; + } + write!(f, ")") + } + CfgExpr::Not(expr) => write!(f, "not({})", expr), + CfgExpr::Invalid => write!(f, "invalid"), + } + } +} + impl From<CfgAtom> for CfgExpr { fn from(atom: CfgAtom) -> Self { CfgExpr::Atom(atom) diff --git a/crates/hir-def/src/item_tree/pretty.rs b/crates/hir-def/src/item_tree/pretty.rs index 66a2d14a73..c89299e6d8 100644 --- a/crates/hir-def/src/item_tree/pretty.rs +++ b/crates/hir-def/src/item_tree/pretty.rs @@ -86,9 +86,9 @@ impl Printer<'_> { } fn print_attrs(&mut self, attrs: &AttrsOrCfg, inner: bool, separated_by: &str) { - let AttrsOrCfg::Enabled { attrs } = attrs else { - w!(self, "#[cfg(false)]{separated_by}"); - return; + let (cfg_disabled_expr, attrs) = match attrs { + AttrsOrCfg::Enabled { attrs } => (None, attrs), + AttrsOrCfg::CfgDisabled(inner_box) => (Some(&inner_box.0), &inner_box.1), }; let inner = if inner { "!" } else { "" }; for attr in &*attrs.as_ref() { @@ -101,6 +101,9 @@ impl Printer<'_> { separated_by, ); } + if let Some(expr) = cfg_disabled_expr { + w!(self, "#{inner}[cfg({expr})]{separated_by}"); + } } fn print_attrs_of(&mut self, of: ModItemId, separated_by: &str) { |