mirror of
https://github.com/typst/typst
synced 2025-06-14 16:16:24 +08:00
Improve label repr
This commit is contained in:
parent
8d495f952c
commit
4c0f1173ab
@ -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)
|
||||
}
|
||||
|
@ -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};
|
||||
|
@ -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()
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user