Repository: typst/typst
File: crates/typst-library/src/introspection/counter.rs (Rust)

Code before:
    /// Displays the value of the counter with a numbering at the given location
    /// and returns the formatted output.
    #[func(contextual)]
    pub fn display(
        self,
        engine: &mut Engine,
        context: Tracked<Context>,
        span: Span,
        /// A [numbering pattern or a function]($numbering), which specifies how
        /// to display the counter. If given a function, that function receives
        /// each number of the counter as a separate argument. If the amount of
        /// numbers varies, e.g. for the heading argument, you can use an
        /// [argument sink]($arguments).
        ///
        /// If this is omitted or set to `{auto}`, displays the counter with the
        /// numbering style for the counted element or with the pattern
        /// `{"1.1"}` if no such style exists.
        #[default]
        numbering: Smart<Numbering>,
        /// The place at which the counter should be displayed.
        ///
        /// If a selector is used, it must match exactly one element in the
        /// document. The most useful kinds of selectors for this are
        /// [labels]($label) and [locations]($location).
        ///
        /// If this is omitted or set to `{auto}`, displays the counter at the
        /// current location. This is equivalent to using `{here()}`.
        #[named]
        #[default]
        at: Smart<LocatableSelector>,
        /// If enabled, displays the current and final top-level count together.
        /// Both can be styled through a single numbering pattern. This is used
        /// by the page numbering property to display the current and total
        /// number of pages when a pattern like `{"1 / 1"}` is given.
        #[named]
        #[default(false)]
        both: bool,
    ) -> SourceResult<Value> {
        let location = match at {
            Smart::Auto => context.location().at(span)?,
            Smart::Custom(ref selector) => {
                selector.resolve_unique(engine, context, span)?
            }
        };
        let state = if both {
            engine.introspect(CounterBothIntrospection(self.clone(), location, span))?
        } else {
            engine.introspect(CounterAtIntrospection(self.clone(), location, span))?
        };

        let numbering = numbering

Review comment (suggestion):
```suggestion
        /// If this is omitted or set to `{auto}`, this displays the counter at the
```
(and then rewrap)

Code after:
    #[func(constructor)]
    pub fn construct(
        /// The key that identifies this counter globally.
        ///
        /// - If it is a string, creates a custom counter that is only affected
        ///   by manual updates,
        /// - If it is the [`page`] function, counts through pages,
        /// - If it is a [selector], counts through elements that match the
        ///   selector. For example,
        ///   - provide an element function: counts elements of that type,
        ///   - provide a [`where`]($function.where) selector:
        ///     counts a type of element with specific fields,
        ///   - provide a [`{<label>}`]($label): counts elements with that label.
        key: CounterKey,
    ) -> Counter {
        Self::new(key)
    }

    /// Retrieves the value of the counter at the current location. Always
    /// returns an array of integers, even if the counter has just one number.
    ///
    /// This is equivalent to `{counter.at(here())}`.
    #[func(contextual)]
    pub fn get(
        &self,
        engine: &mut Engine,
        context: Tracked<Context>,
        span: Span,
    ) -> SourceResult<CounterState> {
        let loc = context.location().at(span)?;
        engine.introspect(CounterAtIntrospection(self.clone(), loc, span))
    }

    /// Displays the value of the counter.
    ///
    /// You can provide both a custom numbering and a custom location. Both
    /// default to `{auto}`, selecting sensible defaults (the numbering of
    /// the counted element and the current location, respectively).
    ///
    /// Returns the formatted output.
    #[func(contextual)]
    pub fn display(
        self,
        engine: &mut Engine,
        context: Tracked<Context>,
        span: Span,
        /// A [numbering pattern or a function]($numbering), which specifies how
        /// to display the counter. If given a function, that function receives
        /// each number of the counter as a separate argument. If the amount of
        /// numbers varies, e.g. for the heading argument, you can use an
        /// [argument sink]($arguments).