From 145723b1ef4fa23f1f6665b8907dfe79d0bf83cf Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 27 Feb 2024 11:05:16 +0100 Subject: New context system (#3497) --- tests/typ/bugs/2650-cjk-latin-spacing-meta.typ | 3 +- tests/typ/bugs/cite-locate.typ | 2 +- tests/typ/bugs/math-realize.typ | 2 +- tests/typ/bugs/pagebreak-numbering.typ | 1 - tests/typ/compiler/content-field.typ | 5 +- tests/typ/compiler/methods.typ | 21 ++- tests/typ/compiler/recursion.typ | 4 +- tests/typ/compiler/selector-logical.typ | 7 +- tests/typ/layout/page-marginals.typ | 6 +- tests/typ/math/attach-p3.typ | 4 +- tests/typ/meta/context-compatibility.typ | 29 ++++ tests/typ/meta/context.typ | 181 +++++++++++++++++++++++++ tests/typ/meta/counter.typ | 18 +-- tests/typ/meta/figure-caption.typ | 2 +- tests/typ/meta/query-before-after.typ | 7 +- tests/typ/meta/query-figure.typ | 6 +- tests/typ/meta/query-header.typ | 20 ++- tests/typ/meta/state.typ | 26 ++-- 18 files changed, 272 insertions(+), 72 deletions(-) create mode 100644 tests/typ/meta/context-compatibility.typ create mode 100644 tests/typ/meta/context.typ (limited to 'tests/typ') diff --git a/tests/typ/bugs/2650-cjk-latin-spacing-meta.typ b/tests/typ/bugs/2650-cjk-latin-spacing-meta.typ index e234e651..12c7ea41 100644 --- a/tests/typ/bugs/2650-cjk-latin-spacing-meta.typ +++ b/tests/typ/bugs/2650-cjk-latin-spacing-meta.typ @@ -1,6 +1,5 @@ // https://github.com/typst/typst/issues/2650 -#let with-locate(body) = locate(loc => body) 测a试 -测#with-locate[a]试 +测#context [a]试 diff --git a/tests/typ/bugs/cite-locate.typ b/tests/typ/bugs/cite-locate.typ index 4eb95b54..c576c45e 100644 --- a/tests/typ/bugs/cite-locate.typ +++ b/tests/typ/bugs/cite-locate.typ @@ -17,7 +17,7 @@ caption: [A pirate @arrgh in @intro], ) -#locate(loc => [Citation @distress on page #loc.page()]) +#context [Citation @distress on page #here().page()] #pagebreak() #bibliography("/files/works.bib", style: "chicago-notes") diff --git a/tests/typ/bugs/math-realize.typ b/tests/typ/bugs/math-realize.typ index d7991542..10d8b78e 100644 --- a/tests/typ/bugs/math-realize.typ +++ b/tests/typ/bugs/math-realize.typ @@ -4,7 +4,7 @@ --- #let my = $pi$ #let f1 = box(baseline: 10pt, [f]) -#let f2 = style(sty => f1) +#let f2 = context f1 #show math.vec: [nope] $ pi a $ diff --git a/tests/typ/bugs/pagebreak-numbering.typ b/tests/typ/bugs/pagebreak-numbering.typ index d805c2c6..a9fae3e4 100644 --- a/tests/typ/bugs/pagebreak-numbering.typ +++ b/tests/typ/bugs/pagebreak-numbering.typ @@ -10,4 +10,3 @@ This and next page should not be numbered #counter(page).update(1) This page should - diff --git a/tests/typ/compiler/content-field.typ b/tests/typ/compiler/content-field.typ index dab4ec4b..96ce1dca 100644 --- a/tests/typ/compiler/content-field.typ +++ b/tests/typ/compiler/content-field.typ @@ -17,10 +17,7 @@ --- // Test it with query. #set raw(lang: "rust") -#locate(loc => { - let elem = query(, loc).first() - elem.lang -}) +#context query().first().lang `raw` --- diff --git a/tests/typ/compiler/methods.typ b/tests/typ/compiler/methods.typ index 9eed5aed..2dce8892 100644 --- a/tests/typ/compiler/methods.typ +++ b/tests/typ/compiler/methods.typ @@ -83,20 +83,19 @@ --- // Test length `to-absolute` method. - #set text(size: 12pt) -#style(styles => { - test((6pt).to-absolute(styles), 6pt) - test((6pt + 10em).to-absolute(styles), 126pt) - test((10em).to-absolute(styles), 120pt) -}) +#context { + test((6pt).to-absolute(), 6pt) + test((6pt + 10em).to-absolute(), 126pt) + test((10em).to-absolute(), 120pt) +} #set text(size: 64pt) -#style(styles => { - test((6pt).to-absolute(styles), 6pt) - test((6pt + 10em).to-absolute(styles), 646pt) - test((10em).to-absolute(styles), 640pt) -}) +#context { + test((6pt).to-absolute(), 6pt) + test((6pt + 10em).to-absolute(), 646pt) + test((10em).to-absolute(), 640pt) +} --- // Error: 2-21 cannot convert a length with non-zero em units (`-6pt + 10.5em`) to pt diff --git a/tests/typ/compiler/recursion.typ b/tests/typ/compiler/recursion.typ index f5f870b5..421b638b 100644 --- a/tests/typ/compiler/recursion.typ +++ b/tests/typ/compiler/recursion.typ @@ -44,8 +44,8 @@ --- // Test cyclic imports during layout. -// Error: 14-37 maximum layout depth exceeded -// Hint: 14-37 try to reduce the amount of nesting in your layout +// Error: 2-38 maximum show rule depth exceeded +// Hint: 2-38 check whether the show rule matches its own output #layout(_ => include "recursion.typ") --- diff --git a/tests/typ/compiler/selector-logical.typ b/tests/typ/compiler/selector-logical.typ index 64f97384..5369e4c7 100644 --- a/tests/typ/compiler/selector-logical.typ +++ b/tests/typ/compiler/selector-logical.typ @@ -13,10 +13,9 @@ #figure([Iguana], kind: "iguana", supplement: none) == I -#let test-selector(selector, ref) = locate(loc => { - let elems = query(selector, loc) - test(elems.map(e => e.body), ref) -}) +#let test-selector(selector, ref) = context { + test(query(selector).map(e => e.body), ref) +} // Test `or`. #test-selector( diff --git a/tests/typ/layout/page-marginals.typ b/tests/typ/layout/page-marginals.typ index d65591e6..589fb299 100644 --- a/tests/typ/layout/page-marginals.typ +++ b/tests/typ/layout/page-marginals.typ @@ -6,10 +6,10 @@ h(1fr) text(0.8em)[_Chapter 1_] }, - footer: align(center)[\~ #counter(page).display() \~], - background: counter(page).display(n => if n <= 2 { + footer: context align(center)[\~ #counter(page).display() \~], + background: context if counter(page).get().first() <= 2 { place(center + horizon, circle(radius: 1cm, fill: luma(90%))) - }) + } ) But, soft! what light through yonder window breaks? It is the east, and Juliet diff --git a/tests/typ/math/attach-p3.typ b/tests/typ/math/attach-p3.typ index c681af2a..3591c248 100644 --- a/tests/typ/math/attach-p3.typ +++ b/tests/typ/math/attach-p3.typ @@ -11,8 +11,8 @@ $ scripts(sum)_1^2 != sum_1^2 $ $ limits(integral)_a^b != integral_a^b $ --- -// Error: 30-34 unknown variable: oops -$ attach(A, t: #locate(it => oops)) $ +// Error: 25-29 unknown variable: oops +$ attach(A, t: #context oops) $ --- // Show and let rules for limits and scripts diff --git a/tests/typ/meta/context-compatibility.typ b/tests/typ/meta/context-compatibility.typ new file mode 100644 index 00000000..60124255 --- /dev/null +++ b/tests/typ/meta/context-compatibility.typ @@ -0,0 +1,29 @@ +// Test compatibility with the pre-context way of things. +// Ref: false + +--- +#let s = state("x", 0) +#let compute(expr) = [ + #s.update(x => + eval(expr.replace("x", str(x))) + ) + New value is #s.display(). +] + +#locate(loc => { + let elem = query(, loc).first() + test(s.at(elem.location()), 13) +}) + +#compute("10") \ +#compute("x + 3") \ +*Here.* \ +#compute("x * 2") \ +#compute("x - 5") + +--- +#style(styles => measure([it], styles).width < 20pt) + +--- +#counter(heading).update(10) +#counter(heading).display(n => test(n, 10)) diff --git a/tests/typ/meta/context.typ b/tests/typ/meta/context.typ new file mode 100644 index 00000000..729d9fa2 --- /dev/null +++ b/tests/typ/meta/context.typ @@ -0,0 +1,181 @@ +// Test context expressions. +// Ref: false + +--- +// Test that context body is parsed as atomic expression. +#let c = [#context "hello".] +#test(c.children.first().func(), (context none).func()) +#test(c.children.last(), [.]) + +--- +// Test that manual construction is forbidden. +// Error: 2-25 cannot be constructed manually +#(context none).func()() + +--- +// Test that `here()` yields the context element's location. +#context test(query(here()).first().func(), (context none).func()) + +--- +// Test whether context is retained in nested function. +#let translate(..args) = args.named().at(text.lang) +#set text(lang: "de") +#context test(translate(de: "Inhalt", en: "Contents"), "Inhalt") + +--- +// Test whether context is retained in built-in callback. +#set text(lang: "de") +#context test( + ("en", "de", "fr").sorted(key: v => v != text.lang), + ("de", "en", "fr"), +) + +--- +// Test `locate` + `here`. +#context test(here().position().y, 10pt) + +--- +// Test `locate`. +#v(10pt) += Introduction +#context test(locate().position().y, 20pt) + +--- +// Error: 10-25 label `` does not exist in the document +#context locate() + +--- += Introduction += Introduction + +// Error: 10-25 label `` occurs multiple times in the document +#context locate() + +--- +#v(10pt) += Introduction +#context test(locate(heading).position().y, 20pt) + +--- +// Error: 10-25 selector does not match any element +#context locate(heading) + +--- += Introduction += Introduction + +// Error: 10-25 selector matches multiple elements +#context locate(heading) + +--- +// Test `counter`. +#let c = counter("heading") +#c.update(2) +#c.update(n => n + 2) +#context test(c.get(), (4,)) +#c.update(n => n - 3) +#context test(c.at(here()), (1,)) + +--- +// Test `state.at` outside of context. +// Error: 2-26 can only be used when context is known +// Hint: 2-26 try wrapping this in a `context` expression +// Hint: 2-26 the `context` expression should wrap everything that depends on this function +#state("key").at(