feat: support more path types and add path parameters (#1312) (#1331)

* feat: support more path types and add path parameters added in typst … (#1312)

* feat: support more path types and add path parameters added in typst 0.13.0

* test: update snapshot

* test: update snapshot
This commit is contained in:
Myriad-Dreamin 2025-02-20 00:43:47 +08:00 committed by GitHub
parent 5b72f760bb
commit 803cc1ffaf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 12 deletions

View file

@ -18,6 +18,7 @@ use crate::ty::*;
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, EnumIter)]
pub enum PathPreference {
Source { allow_package: bool },
Wasm,
Csv,
Image,
Json,
@ -36,6 +37,7 @@ impl PathPreference {
pub fn ext_matcher(&self) -> &'static RegexSet {
static SOURCE_REGSET: Lazy<RegexSet> =
Lazy::new(|| RegexSet::new([r"^typ$", r"^typc$"]).unwrap());
static WASM_REGSET: Lazy<RegexSet> = Lazy::new(|| RegexSet::new([r"^wasm$"]).unwrap());
static IMAGE_REGSET: Lazy<RegexSet> = Lazy::new(|| {
RegexSet::new([
r"^ico$", r"^bmp$", r"^png$", r"^webp$", r"^jpg$", r"^jpeg$", r"^jfif$", r"^tiff$",
@ -61,7 +63,8 @@ impl PathPreference {
static ALL_SPECIAL_REGSET: Lazy<RegexSet> = Lazy::new(|| {
RegexSet::new({
let patterns = SOURCE_REGSET.patterns();
let patterns = patterns.iter().chain(IMAGE_REGSET.patterns());
let patterns = patterns.iter().chain(WASM_REGSET.patterns());
let patterns = patterns.chain(IMAGE_REGSET.patterns());
let patterns = patterns.chain(JSON_REGSET.patterns());
let patterns = patterns.chain(YAML_REGSET.patterns());
let patterns = patterns.chain(XML_REGSET.patterns());
@ -77,6 +80,7 @@ impl PathPreference {
match self {
PathPreference::Source { .. } => &SOURCE_REGSET,
PathPreference::Wasm => &WASM_REGSET,
PathPreference::Csv => &CSV_REGSET,
PathPreference::Image => &IMAGE_REGSET,
PathPreference::Json => &JSON_REGSET,
@ -370,6 +374,7 @@ impl BuiltinTy {
PathPreference::None => "[any]",
PathPreference::Special => "[any]",
PathPreference::Source { .. } => "[source]",
PathPreference::Wasm => "[wasm]",
PathPreference::Csv => "[csv]",
PathPreference::Image => "[image]",
PathPreference::Json => "[json]",
@ -449,15 +454,18 @@ macro_rules! flow_record {
}
pub(super) fn param_mapping(func: &Func, param: &ParamInfo) -> Option<Ty> {
// todo: remove path params which is compatible with 0.12.0
match (func.name()?, param.name) {
("cbor", "path") => Some(literally(Path(PathPreference::None))),
("csv", "path") => Some(literally(Path(PathPreference::Csv))),
("image", "path") => Some(literally(Path(PathPreference::Image))),
("read", "path") => Some(literally(Path(PathPreference::None))),
("json", "path") => Some(literally(Path(PathPreference::Json))),
("yaml", "path") => Some(literally(Path(PathPreference::Yaml))),
("xml", "path") => Some(literally(Path(PathPreference::Xml))),
("toml", "path") => Some(literally(Path(PathPreference::Toml))),
("embed", "path") => Some(literally(Path(PathPreference::None))),
("cbor", "path" | "source") => Some(literally(Path(PathPreference::None))),
("plugin", "source") => Some(literally(Path(PathPreference::Wasm))),
("csv", "path" | "source") => Some(literally(Path(PathPreference::Csv))),
("image", "path" | "source") => Some(literally(Path(PathPreference::Image))),
("read", "path" | "source") => Some(literally(Path(PathPreference::None))),
("json", "path" | "source") => Some(literally(Path(PathPreference::Json))),
("yaml", "path" | "source") => Some(literally(Path(PathPreference::Yaml))),
("xml", "path" | "source") => Some(literally(Path(PathPreference::Xml))),
("toml", "path" | "source") => Some(literally(Path(PathPreference::Toml))),
("raw", "theme") => Some(literally(Path(PathPreference::RawTheme))),
("raw", "syntaxes") => Some(literally(Path(PathPreference::RawSyntax))),
("bibliography" | "cite", "style") => Some(Ty::iter_union([
@ -470,7 +478,13 @@ pub(super) fn param_mapping(func: &Func, param: &ParamInfo) -> Option<Ty> {
literally(RefLabel),
Ty::from_cast_info(&param.input),
])),
("bibliography", "path") => Some(literally(Path(PathPreference::Bibliography))),
("bibliography", "path" | "sources") => {
static BIB_PATH_TYPE: Lazy<Ty> = Lazy::new(|| {
let bib_path_ty = literally(Path(PathPreference::Bibliography));
Ty::iter_union([bib_path_ty.clone(), Ty::Array(bib_path_ty.into())])
});
Some(BIB_PATH_TYPE.clone())
}
("text", "size") => Some(literally(TextSize)),
("text", "font") => {
static FONT_TYPE: Lazy<Ty> = Lazy::new(|| {

View file

@ -374,7 +374,7 @@ fn e2e() {
});
let hash = replay_log(&tinymist_binary, &root.join("neovim"));
insta::assert_snapshot!(hash, @"siphash128_13:af8a934c44f864eea561172ae925c397");
insta::assert_snapshot!(hash, @"siphash128_13:b3c34687da7a40cef9adc4f56a1f4ba8");
}
{
@ -385,7 +385,7 @@ fn e2e() {
});
let hash = replay_log(&tinymist_binary, &root.join("vscode"));
insta::assert_snapshot!(hash, @"siphash128_13:cdc40c3628176d16291ca5e3780f5403");
insta::assert_snapshot!(hash, @"siphash128_13:28c1e597e2082585f5042d8ca3eb1a88");
}
}