From 4c0f1173abbdba917d2d25921e575e3981ddc15b Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 17 Sep 2024 11:23:08 +0200 Subject: [PATCH] Improve label repr --- crates/typst-syntax/src/lexer.rs | 11 ++++++----- crates/typst-syntax/src/lib.rs | 2 +- crates/typst/src/foundations/cast.rs | 2 +- crates/typst/src/foundations/label.rs | 7 ++++++- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/typst-syntax/src/lexer.rs b/crates/typst-syntax/src/lexer.rs index 721225c6e..3bb3f2978 100644 --- a/crates/typst-syntax/src/lexer.rs +++ b/crates/typst-syntax/src/lexer.rs @@ -934,13 +934,14 @@ fn is_math_id_continue(c: char) -> bool { is_xid_continue(c) && c != '_' } +/// Returns true if this string is valid in a label literal. +#[inline] +pub fn is_valid_label_literal(string: &str) -> bool { + !string.is_empty() && string.chars().all(is_valid_in_label_literal) +} + /// Whether a character can be part of a label literal's name. #[inline] fn is_valid_in_label_literal(c: char) -> bool { is_id_continue(c) || matches!(c, ':' | '.') } - -/// Returns true if this string is valid in a label literal. -pub fn is_valid_label_literal_id(id: &str) -> bool { - !id.is_empty() && id.chars().all(is_valid_in_label_literal) -} diff --git a/crates/typst-syntax/src/lib.rs b/crates/typst-syntax/src/lib.rs index 5e7b710fc..055c3d77d 100644 --- a/crates/typst-syntax/src/lib.rs +++ b/crates/typst-syntax/src/lib.rs @@ -19,7 +19,7 @@ pub use self::file::FileId; pub use self::highlight::{highlight, highlight_html, Tag}; pub use self::kind::SyntaxKind; pub use self::lexer::{ - is_id_continue, is_id_start, is_ident, is_newline, is_valid_label_literal_id, + is_id_continue, is_id_start, is_ident, is_newline, is_valid_label_literal, link_prefix, split_newlines, }; pub use self::node::{LinkedChildren, LinkedNode, Side, SyntaxError, SyntaxNode}; diff --git a/crates/typst/src/foundations/cast.rs b/crates/typst/src/foundations/cast.rs index 51e361289..2659af140 100644 --- a/crates/typst/src/foundations/cast.rs +++ b/crates/typst/src/foundations/cast.rs @@ -336,7 +336,7 @@ impl CastInfo { } } else if let Value::Str(s) = found { if !matching_type && parts.iter().any(|p| p == "label") { - if typst_syntax::is_valid_label_literal_id(s) { + if typst_syntax::is_valid_label_literal(s) { msg.hint(eco_format!( "use `<{s}>` or `label({})` to create a label", s.repr() diff --git a/crates/typst/src/foundations/label.rs b/crates/typst/src/foundations/label.rs index 8f15d3178..c510ba1ef 100644 --- a/crates/typst/src/foundations/label.rs +++ b/crates/typst/src/foundations/label.rs @@ -1,6 +1,7 @@ use ecow::{eco_format, EcoString}; use crate::foundations::{func, scope, ty, Repr}; +use crate::syntax::is_valid_label_literal; use crate::utils::PicoStr; /// A label for an element. @@ -66,7 +67,11 @@ impl Label { impl Repr for Label { fn repr(&self) -> EcoString { - eco_format!("<{}>", self.as_str()) + if is_valid_label_literal(self.as_str()) { + eco_format!("<{}>", self.as_str()) + } else { + eco_format!("label({})", self.as_str().repr()) + } } }