[ty] Truncate Literal type display in some situations (#20928)

This commit is contained in:
Mark Z. Ding 2025-10-17 07:50:58 -04:00 committed by GitHub
parent baaa8dad3a
commit fc3b341529
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 158 additions and 37 deletions

View file

@ -38,7 +38,7 @@ pub struct DisplaySettings<'db> {
/// Class names that should be displayed fully qualified
/// (e.g., `module.ClassName` instead of just `ClassName`)
pub qualified: Rc<FxHashMap<&'db str, QualificationLevel>>,
/// Whether long unions are displayed in full
/// Whether long unions and literals are displayed in full
pub preserve_full_unions: bool,
}
@ -1328,6 +1328,44 @@ impl Display for DisplayParameter<'_> {
}
}
#[derive(Debug, Copy, Clone)]
struct TruncationPolicy {
max: usize,
max_when_elided: usize,
}
impl TruncationPolicy {
fn display_limit(self, total: usize, preserve_full: bool) -> usize {
if preserve_full {
return total;
}
let limit = if total > self.max {
self.max_when_elided
} else {
self.max
};
limit.min(total)
}
}
#[derive(Debug)]
struct DisplayOmitted {
count: usize,
singular: &'static str,
plural: &'static str,
}
impl Display for DisplayOmitted {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let noun = if self.count == 1 {
self.singular
} else {
self.plural
};
write!(f, "... omitted {} {}", self.count, noun)
}
}
impl<'db> UnionType<'db> {
fn display_with(
&'db self,
@ -1348,8 +1386,10 @@ struct DisplayUnionType<'db> {
settings: DisplaySettings<'db>,
}
const MAX_DISPLAYED_UNION_ITEMS: usize = 5;
const MAX_DISPLAYED_UNION_ITEMS_WHEN_ELIDED: usize = 3;
const UNION_POLICY: TruncationPolicy = TruncationPolicy {
max: 5,
max_when_elided: 3,
};
impl Display for DisplayUnionType<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
@ -1379,16 +1419,8 @@ impl Display for DisplayUnionType<'_> {
let mut join = f.join(" | ");
let display_limit = if self.settings.preserve_full_unions {
total_entries
} else {
let limit = if total_entries > MAX_DISPLAYED_UNION_ITEMS {
MAX_DISPLAYED_UNION_ITEMS_WHEN_ELIDED
} else {
MAX_DISPLAYED_UNION_ITEMS
};
limit.min(total_entries)
};
let display_limit =
UNION_POLICY.display_limit(total_entries, self.settings.preserve_full_unions);
let mut condensed_types = Some(condensed_types);
let mut displayed_entries = 0usize;
@ -1420,8 +1452,10 @@ impl Display for DisplayUnionType<'_> {
if !self.settings.preserve_full_unions {
let omitted_entries = total_entries.saturating_sub(displayed_entries);
if omitted_entries > 0 {
join.entry(&DisplayUnionOmitted {
join.entry(&DisplayOmitted {
count: omitted_entries,
singular: "union element",
plural: "union elements",
});
}
}
@ -1437,38 +1471,45 @@ impl fmt::Debug for DisplayUnionType<'_> {
Display::fmt(self, f)
}
}
struct DisplayUnionOmitted {
count: usize,
}
impl Display for DisplayUnionOmitted {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let plural = if self.count == 1 {
"element"
} else {
"elements"
};
write!(f, "... omitted {} union {}", self.count, plural)
}
}
struct DisplayLiteralGroup<'db> {
literals: Vec<Type<'db>>,
db: &'db dyn Db,
settings: DisplaySettings<'db>,
}
const LITERAL_POLICY: TruncationPolicy = TruncationPolicy {
max: 7,
max_when_elided: 5,
};
impl Display for DisplayLiteralGroup<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("Literal[")?;
f.join(", ")
.entries(
self.literals
.iter()
.map(|ty| ty.representation(self.db, self.settings.singleline())),
)
.finish()?;
let total_entries = self.literals.len();
let display_limit =
LITERAL_POLICY.display_limit(total_entries, self.settings.preserve_full_unions);
let mut join = f.join(", ");
for lit in self.literals.iter().take(display_limit) {
let rep = lit.representation(self.db, self.settings.singleline());
join.entry(&rep);
}
if !self.settings.preserve_full_unions {
let omitted_entries = total_entries.saturating_sub(display_limit);
if omitted_entries > 0 {
join.entry(&DisplayOmitted {
count: omitted_entries,
singular: "literal",
plural: "literals",
});
}
}
join.finish()?;
f.write_str("]")
}
}