Deprecate some things (#4562)

This commit is contained in:
Laurenz 2024-07-20 14:52:17 +02:00 committed by GitHub
parent 96d456e267
commit 46ef8e1dfa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 97 additions and 28 deletions

View File

@ -7,7 +7,7 @@ use comemo::{Track, Tracked};
use ecow::{eco_vec, EcoString, EcoVec}; use ecow::{eco_vec, EcoString, EcoVec};
use smallvec::SmallVec; use smallvec::SmallVec;
use crate::diag::{SourceResult, Trace, Tracepoint}; use crate::diag::{warning, SourceResult, Trace, Tracepoint};
use crate::engine::Engine; use crate::engine::Engine;
use crate::foundations::{ use crate::foundations::{
cast, elem, func, ty, Content, Context, Element, Func, NativeElement, Packed, Repr, cast, elem, func, ty, Content, Context, Element, Func, NativeElement, Packed, Repr,
@ -33,6 +33,8 @@ use crate::utils::LazyHash;
/// ``` /// ```
#[func] #[func]
pub fn style( pub fn style(
/// The engine.
engine: &mut Engine,
/// The call site span. /// The call site span.
span: Span, span: Span,
/// A function to call with the styles. Its return value is displayed /// A function to call with the styles. Its return value is displayed
@ -43,6 +45,11 @@ pub fn style(
/// content that depends on the style context it appears in. /// content that depends on the style context it appears in.
func: Func, func: Func,
) -> Content { ) -> Content {
engine.sink.warn(warning!(
span, "`style` is deprecated";
hint: "use a `context` expression instead"
));
StyleElem::new(func).pack().spanned(span) StyleElem::new(func).pack().spanned(span)
} }

View File

@ -5,7 +5,7 @@ use comemo::{Track, Tracked, TrackedMut};
use ecow::{eco_format, eco_vec, EcoString, EcoVec}; use ecow::{eco_format, eco_vec, EcoString, EcoVec};
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use crate::diag::{bail, At, HintedStrResult, SourceResult}; use crate::diag::{bail, warning, At, HintedStrResult, SourceResult};
use crate::engine::{Engine, Route, Sink, Traced}; use crate::engine::{Engine, Route, Sink, Traced};
use crate::foundations::{ use crate::foundations::{
cast, elem, func, scope, select_where, ty, Args, Array, Construct, Content, Context, cast, elem, func, scope, select_where, ty, Args, Array, Construct, Content, Context,
@ -464,6 +464,11 @@ impl Counter {
if let Ok(loc) = context.location() { if let Ok(loc) = context.location() {
self.display_impl(engine, loc, numbering, both, context.styles().ok()) self.display_impl(engine, loc, numbering, both, context.styles().ok())
} else { } else {
engine.sink.warn(warning!(
span, "`counter.display` without context is deprecated";
hint: "use it in a `context` expression instead"
));
Ok(CounterDisplayElem::new(self, numbering, both) Ok(CounterDisplayElem::new(self, numbering, both)
.pack() .pack()
.spanned(span) .spanned(span)
@ -508,13 +513,19 @@ impl Counter {
context: Tracked<Context>, context: Tracked<Context>,
/// The callsite span. /// The callsite span.
span: Span, span: Span,
/// _Compatibility:_ This argument only exists for compatibility with /// _Compatibility:_ This argument is deprecated. It only exists for
/// Typst 0.10 and lower and shouldn't be used anymore. /// compatibility with Typst 0.10 and lower and shouldn't be used
/// anymore.
#[default] #[default]
location: Option<Location>, location: Option<Location>,
) -> SourceResult<CounterState> { ) -> SourceResult<CounterState> {
if location.is_none() { if location.is_none() {
context.location().at(span)?; context.location().at(span)?;
} else {
engine.sink.warn(warning!(
span, "calling `counter.final` with a location is deprecated";
hint: "try removing the location argument"
));
} }
let sequence = self.sequence(engine)?; let sequence = self.sequence(engine)?;

View File

@ -1,6 +1,6 @@
use comemo::{Track, Tracked}; use comemo::{Track, Tracked};
use crate::diag::{HintedStrResult, SourceResult}; use crate::diag::{warning, HintedStrResult, SourceResult};
use crate::engine::Engine; use crate::engine::Engine;
use crate::foundations::{ use crate::foundations::{
cast, elem, func, Content, Context, Func, LocatableSelector, NativeElement, Packed, cast, elem, func, Content, Context, Func, LocatableSelector, NativeElement, Packed,
@ -29,11 +29,12 @@ use crate::syntax::Span;
/// ///
/// # Compatibility /// # Compatibility
/// In Typst 0.10 and lower, the `locate` function took a closure that made the /// In Typst 0.10 and lower, the `locate` function took a closure that made the
/// current location in the document available (like [`here`] does now). /// current location in the document available (like [`here`] does now). This
/// Compatibility with the old way will remain for a while to give package /// usage pattern is deprecated. Compatibility with the old way will remain for
/// authors time to upgrade. To that effect, `locate` detects whether it /// a while to give package authors time to upgrade. To that effect, `locate`
/// received a selector or a user-defined function and adjusts its semantics /// detects whether it received a selector or a user-defined function and
/// accordingly. This behaviour will be removed in the future. /// adjusts its semantics accordingly. This behaviour will be removed in the
/// future.
#[func(contextual)] #[func(contextual)]
pub fn locate( pub fn locate(
/// The engine. /// The engine.
@ -56,6 +57,11 @@ pub fn locate(
LocateOutput::Location(selector.resolve_unique(engine.introspector, context)?) LocateOutput::Location(selector.resolve_unique(engine.introspector, context)?)
} }
LocateInput::Func(func) => { LocateInput::Func(func) => {
engine.sink.warn(warning!(
span, "`locate` with callback function is deprecated";
hint: "use a `context` expression instead"
));
LocateOutput::Content(LocateElem::new(func).pack().spanned(span)) LocateOutput::Content(LocateElem::new(func).pack().spanned(span))
} }
}) })

View File

@ -1,9 +1,10 @@
use comemo::Tracked; use comemo::Tracked;
use crate::diag::HintedStrResult; use crate::diag::{warning, HintedStrResult};
use crate::engine::Engine; use crate::engine::Engine;
use crate::foundations::{func, Array, Context, LocatableSelector, Value}; use crate::foundations::{func, Array, Context, LocatableSelector, Value};
use crate::introspection::Location; use crate::introspection::Location;
use crate::syntax::Span;
/// Finds elements in the document. /// Finds elements in the document.
/// ///
@ -141,6 +142,8 @@ pub fn query(
engine: &mut Engine, engine: &mut Engine,
/// The callsite context. /// The callsite context.
context: Tracked<Context>, context: Tracked<Context>,
/// The span of the `query` call.
span: Span,
/// Can be /// Can be
/// - an element function like a `heading` or `figure`, /// - an element function like a `heading` or `figure`,
/// - a `{<label>}`, /// - a `{<label>}`,
@ -149,13 +152,18 @@ pub fn query(
/// ///
/// Only [locatable]($location/#locatable) element functions are supported. /// Only [locatable]($location/#locatable) element functions are supported.
target: LocatableSelector, target: LocatableSelector,
/// _Compatibility:_ This argument only exists for compatibility with /// _Compatibility:_ This argument is deprecated. It only exists for
/// Typst 0.10 and lower and shouldn't be used anymore. /// compatibility with Typst 0.10 and lower and shouldn't be used anymore.
#[default] #[default]
location: Option<Location>, location: Option<Location>,
) -> HintedStrResult<Array> { ) -> HintedStrResult<Array> {
if location.is_none() { if location.is_none() {
context.introspect()?; context.introspect()?;
} else {
engine.sink.warn(warning!(
span, "calling `query` with a location is deprecated";
hint: "try removing the location argument"
));
} }
let vec = engine.introspector.query(&target.0); let vec = engine.introspector.query(&target.0);

View File

@ -1,7 +1,7 @@
use comemo::{Track, Tracked, TrackedMut}; use comemo::{Track, Tracked, TrackedMut};
use ecow::{eco_format, eco_vec, EcoString, EcoVec}; use ecow::{eco_format, eco_vec, EcoString, EcoVec};
use crate::diag::{bail, At, SourceResult}; use crate::diag::{bail, warning, At, SourceResult};
use crate::engine::{Engine, Route, Sink, Traced}; use crate::engine::{Engine, Route, Sink, Traced};
use crate::foundations::{ use crate::foundations::{
cast, elem, func, scope, select_where, ty, Args, Construct, Content, Context, Func, cast, elem, func, scope, select_where, ty, Args, Construct, Content, Context, Func,
@ -325,13 +325,19 @@ impl State {
context: Tracked<Context>, context: Tracked<Context>,
/// The callsite span. /// The callsite span.
span: Span, span: Span,
/// _Compatibility:_ This argument only exists for compatibility with /// _Compatibility:_ This argument is deprecated. It only exists for
/// Typst 0.10 and lower and shouldn't be used anymore. /// compatibility with Typst 0.10 and lower and shouldn't be used
/// anymore.
#[default] #[default]
location: Option<Location>, location: Option<Location>,
) -> SourceResult<Value> { ) -> SourceResult<Value> {
if location.is_none() { if location.is_none() {
context.location().at(span)?; context.location().at(span)?;
} else {
engine.sink.warn(warning!(
span, "calling `state.final` with a location is deprecated";
hint: "try removing the location argument"
));
} }
let sequence = self.sequence(engine)?; let sequence = self.sequence(engine)?;
@ -365,6 +371,8 @@ impl State {
#[func] #[func]
pub fn display( pub fn display(
self, self,
/// The engine.
engine: &mut Engine,
/// The span of the `display` call. /// The span of the `display` call.
span: Span, span: Span,
/// A function which receives the value of the state and can return /// A function which receives the value of the state and can return
@ -373,6 +381,11 @@ impl State {
#[default] #[default]
func: Option<Func>, func: Option<Func>,
) -> Content { ) -> Content {
engine.sink.warn(warning!(
span, "`state.display` is deprecated";
hint: "use `state.get` in a `context` expression instead"
));
StateDisplayElem::new(self, func).pack().spanned(span) StateDisplayElem::new(self, func).pack().spanned(span)
} }
} }

View File

@ -1,6 +1,6 @@
use comemo::Tracked; use comemo::Tracked;
use crate::diag::{At, SourceResult}; use crate::diag::{warning, At, SourceResult};
use crate::engine::Engine; use crate::engine::Engine;
use crate::foundations::{ use crate::foundations::{
dict, func, Content, Context, Dict, Resolve, Smart, StyleChain, Styles, dict, func, Content, Context, Dict, Resolve, Smart, StyleChain, Styles,
@ -76,13 +76,19 @@ pub fn measure(
height: Smart<Length>, height: Smart<Length>,
/// The content whose size to measure. /// The content whose size to measure.
content: Content, content: Content,
/// _Compatibility:_ This argument only exists for compatibility with /// _Compatibility:_ This argument is deprecated. It only exists for
/// Typst 0.10 and lower and shouldn't be used anymore. /// compatibility with Typst 0.10 and lower and shouldn't be used anymore.
#[default] #[default]
styles: Option<Styles>, styles: Option<Styles>,
) -> SourceResult<Dict> { ) -> SourceResult<Dict> {
let styles = match &styles { let styles = match &styles {
Some(styles) => StyleChain::new(styles), Some(styles) => {
engine.sink.warn(warning!(
span, "calling `measure` with a styles argument is deprecated";
hint: "try removing the styles argument"
));
StyleChain::new(styles)
}
None => context.styles().at(span)?, None => context.styles().at(span)?,
}; };

View File

@ -43,10 +43,16 @@
#s.update(x => #s.update(x =>
eval(expr.replace("x", str(x))) eval(expr.replace("x", str(x)))
) )
// Warning: 17-28 `state.display` is deprecated
// Hint: 17-28 use `state.get` in a `context` expression instead
New value is #s.display(). New value is #s.display().
] ]
// Warning: 1:2-6:3 `locate` with callback function is deprecated
// Hint: 1:2-6:3 use a `context` expression instead
#locate(loc => { #locate(loc => {
// Warning: 14-32 calling `query` with a location is deprecated
// Hint: 14-32 try removing the location argument
let elem = query(<here>, loc).first() let elem = query(<here>, loc).first()
test(s.at(elem.location()), 13) test(s.at(elem.location()), 13)
}) })
@ -58,8 +64,15 @@
#compute("x - 5") #compute("x - 5")
--- context-compatibility-styling --- --- context-compatibility-styling ---
// Warning: 2-53 `style` is deprecated
// Hint: 2-53 use a `context` expression instead
// Warning: 18-39 calling `measure` with a styles argument is deprecated
// Hint: 18-39 try removing the styles argument
#style(styles => measure([it], styles).width < 20pt) #style(styles => measure([it], styles).width < 20pt)
--- context-compatibility-counter-display --- --- context-compatibility-counter-display ---
#counter(heading).update(10) #counter(heading).update(10)
// Warning: 2-44 `counter.display` without context is deprecated
// Hint: 2-44 use it in a `context` expression instead
#counter(heading).display(n => test(n, 10)) #counter(heading).display(n => test(n, 10))

View File

@ -79,7 +79,7 @@ At Beta, it was #context {
--- issue-2480-counter-reset --- --- issue-2480-counter-reset ---
#let q = counter("question") #let q = counter("question")
#let step-show = q.step() + q.display("1") #let step-show = q.step() + context q.display("1")
#let g = grid(step-show, step-show, gutter: 2pt) #let g = grid(step-show, step-show, gutter: 2pt)
#g #g

View File

@ -282,17 +282,17 @@
rows: (auto, 2em, auto, auto), rows: (auto, 2em, auto, auto),
table.header( table.header(
[eeec], [eeec],
table.cell(rowspan: 2, count.step() + count.display()), table.cell(rowspan: 2, count.step() + context count.display()),
), ),
[d], [d],
block(width: 5em, fill: yellow, lorem(7)), block(width: 5em, fill: yellow, lorem(7)),
[d], [d],
table.footer( table.footer(
[eeec], [eeec],
table.cell(rowspan: 2, count.step() + count.display()), table.cell(rowspan: 2, count.step() + context count.display()),
) )
) )
#count.display() #context count.display()
--- grid-nested-with-footers --- --- grid-nested-with-footers ---
// Nested table with footer should repeat both footers // Nested table with footer should repeat both footers

View File

@ -265,13 +265,13 @@
rows: (auto, 2em, auto, auto), rows: (auto, 2em, auto, auto),
table.header( table.header(
[eeec], [eeec],
table.cell(rowspan: 2, count.step() + count.display()), table.cell(rowspan: 2, count.step() + context count.display()),
), ),
[d], [d],
block(width: 5em, fill: yellow, lorem(15)), block(width: 5em, fill: yellow, lorem(15)),
[d] [d]
) )
#count.display() #context count.display()
--- grid-header-expand --- --- grid-header-expand ---
// Ensure header expands to fit cell placed in it after its declaration // Ensure header expands to fit cell placed in it after its declaration

View File

@ -163,7 +163,7 @@
#let count = counter("count") #let count = counter("count")
#show grid.cell: it => { #show grid.cell: it => {
count.step() count.step()
count.display() context count.display()
} }
#grid( #grid(

View File

@ -34,7 +34,7 @@ Totally #h() ignored
h(1em) h(1em)
counter(heading).update(4) counter(heading).update(4)
[Hello ] [Hello ]
counter(heading).display() context counter(heading).display()
} }
--- trim-weak-space-line-beginning --- --- trim-weak-space-line-beginning ---

View File

@ -12,8 +12,13 @@ fi
--- fold-vec-order-meta --- --- fold-vec-order-meta ---
#let c = counter("mycounter") #let c = counter("mycounter")
#c.update(1) #c.update(1)
// Warning: 1:2-7:3 `locate` with callback function is deprecated
// Hint: 1:2-7:3 use a `context` expression instead
#locate(loc => [ #locate(loc => [
#c.update(2) #c.update(2)
#c.at(loc) \ #c.at(loc) \
// Warning: 12-36 `locate` with callback function is deprecated
// Hint: 12-36 use a `context` expression instead
Second: #locate(loc => c.at(loc)) Second: #locate(loc => c.at(loc))
]) ])