Show aliases of citation styles in docs and completions (#6696)

This commit is contained in:
Y.D.X. 2025-08-05 23:18:27 +08:00 committed by GitHub
parent 4a975fdb8b
commit b98954c78b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 53 additions and 3 deletions

View File

@ -442,9 +442,35 @@ impl Reflect for CslSource {
#[comemo::memoize]
fn input() -> CastInfo {
let source = std::iter::once(DataSource::input());
let names = ArchivedStyle::all().iter().map(|name| {
CastInfo::Value(name.names()[0].into_value(), name.display_name())
});
/// All possible names and their short documentation for `ArchivedStyle`, including aliases.
static ARCHIVED_STYLE_NAMES: LazyLock<Vec<(&&str, &'static str)>> =
LazyLock::new(|| {
ArchivedStyle::all()
.iter()
.flat_map(|name| {
let (main_name, aliases) = name
.names()
.split_first()
.expect("all ArchivedStyle should have at least one name");
std::iter::once((main_name, name.display_name())).chain(
aliases.iter().map(move |alias| {
// Leaking is okay here, because we are in a `LazyLock`.
let docs: &'static str = Box::leak(
format!("A short alias of `{main_name}`")
.into_boxed_str(),
);
(alias, docs)
}),
)
})
.collect()
});
let names = ARCHIVED_STYLE_NAMES
.iter()
.map(|(value, docs)| CastInfo::Value(value.into_value(), docs));
CastInfo::Union(source.into_iter().chain(names).collect())
}
@ -1056,4 +1082,28 @@ mod tests {
let _ = CslStyle::from_archived(archived);
}
}
#[test]
fn test_csl_source_cast_info_include_all_names() {
let CastInfo::Union(cast_info) = CslSource::input() else {
panic!("the cast info of CslSource should be a union");
};
let missing: Vec<_> = ArchivedStyle::all()
.iter()
.flat_map(|style| style.names())
.filter(|name| {
let found = cast_info.iter().any(|info| match info {
CastInfo::Value(Value::Str(n), _) => n.as_str() == **name,
_ => false,
});
!found
})
.collect();
assert!(
missing.is_empty(),
"missing style names in CslSource cast info: '{missing:?}'"
);
}
}