From 6f7ef266a6fdd0ef43f3c232d0bfbb92087ba03b Mon Sep 17 00:00:00 2001 From: imxyy_soope_ Date: Mon, 5 May 2025 12:01:49 +0800 Subject: [PATCH] feat: symbol display --- Cargo.lock | 45 ++++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 1 + src/ty/common.rs | 13 ++++++++++++- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2cf4c7b..17cb572 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "anyhow" version = "1.0.82" @@ -189,7 +198,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "976dd42dc7e85965fe702eb8164f21f450704bdde31faefd6471dba214cb594e" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -206,7 +215,7 @@ checksum = "0ce92ff622d6dadf7349484f42c93271a0d49b7cc4d466a936405bacbe10aa78" dependencies = [ "cfg-if", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -340,6 +349,7 @@ dependencies = [ "itertools", "once_cell", "rayon", + "regex", "rnix", "rpds", "rustyline", @@ -457,6 +467,35 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "rnix" version = "0.11.0" @@ -510,7 +549,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index db05ba1..bfe27cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,5 +19,6 @@ derive_more = { version = "2.0", features = [ "full" ] } crossbeam-channel = "0.5" ecow = "0.2" once_cell = "1.19" +regex = "1.11" rustyline = "15.0" diff --git a/src/ty/common.rs b/src/ty/common.rs index d66d839..58913b1 100644 --- a/src/ty/common.rs +++ b/src/ty/common.rs @@ -3,6 +3,7 @@ use std::ops::Deref; use derive_more::Constructor; use ecow::EcoString; +use regex::Regex; #[derive(Clone, Debug, PartialEq, Constructor)] pub struct Catchable { @@ -30,7 +31,17 @@ impl> From for Symbol { impl Display for Symbol { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { - write!(f, r#"Sym({})"#, self.0) + if self.normal() { + write!(f, r#""{}""#, self.0) + } else { + write!(f, "{}", self.0) + } + } +} + +impl Symbol { + fn normal(&self) -> bool { + !Regex::new(r#"^[a-zA-Z\_][a-zA-Z0-9\_\'\-]*$"#).unwrap().is_match(self) } }