Skip to content

Commit efdb3de

Browse files
Merge pull request #21298 from J3m3/item-tree-pretty
feat: pretty print attributes up to `cfg(false)`
2 parents 02f53e8 + dd33c82 commit efdb3de

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

crates/cfg/src/cfg_expr.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,36 @@ pub enum CfgExpr {
5656
Not(Box<CfgExpr>),
5757
}
5858

59+
impl fmt::Display for CfgExpr {
60+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61+
match self {
62+
CfgExpr::Atom(atom) => atom.fmt(f),
63+
CfgExpr::All(exprs) => {
64+
write!(f, "all(")?;
65+
for (i, expr) in exprs.iter().enumerate() {
66+
if i > 0 {
67+
write!(f, ", ")?;
68+
}
69+
expr.fmt(f)?;
70+
}
71+
write!(f, ")")
72+
}
73+
CfgExpr::Any(exprs) => {
74+
write!(f, "any(")?;
75+
for (i, expr) in exprs.iter().enumerate() {
76+
if i > 0 {
77+
write!(f, ", ")?;
78+
}
79+
expr.fmt(f)?;
80+
}
81+
write!(f, ")")
82+
}
83+
CfgExpr::Not(expr) => write!(f, "not({})", expr),
84+
CfgExpr::Invalid => write!(f, "invalid"),
85+
}
86+
}
87+
}
88+
5989
impl From<CfgAtom> for CfgExpr {
6090
fn from(atom: CfgAtom) -> Self {
6191
CfgExpr::Atom(atom)

crates/hir-def/src/item_tree/pretty.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ impl Printer<'_> {
8686
}
8787

8888
fn print_attrs(&mut self, attrs: &AttrsOrCfg, inner: bool, separated_by: &str) {
89-
let AttrsOrCfg::Enabled { attrs } = attrs else {
90-
w!(self, "#[cfg(false)]{separated_by}");
91-
return;
89+
let (cfg_disabled_expr, attrs) = match attrs {
90+
AttrsOrCfg::Enabled { attrs } => (None, attrs),
91+
AttrsOrCfg::CfgDisabled(inner_box) => (Some(&inner_box.0), &inner_box.1),
9292
};
9393
let inner = if inner { "!" } else { "" };
9494
for attr in &*attrs.as_ref() {
@@ -101,6 +101,9 @@ impl Printer<'_> {
101101
separated_by,
102102
);
103103
}
104+
if let Some(expr) = cfg_disabled_expr {
105+
w!(self, "#{inner}[cfg({expr})]{separated_by}");
106+
}
104107
}
105108

106109
fn print_attrs_of(&mut self, of: ModItemId, separated_by: &str) {

0 commit comments

Comments
 (0)