From 49a104bc67a601bce37afa55565abe7fa08035fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=BE=D0=BC=D0=B0=D0=BD=20=D0=A7=D1=83=D0=BC=D0=B0?= =?UTF-8?q?=D0=BA?= Date: Fri, 27 Feb 2026 19:04:26 +0300 Subject: [PATCH] custom_quotes --- Cargo.toml | 2 +- src/highlighting.rs | 6 +++--- src/syntax/asm.rs | 3 ++- src/syntax/lua.rs | 3 ++- src/syntax/mod.rs | 23 +++++++++++++++++------ src/syntax/python.rs | 3 ++- src/syntax/rust.rs | 3 ++- src/syntax/shell.rs | 3 ++- src/syntax/sql.rs | 3 ++- src/tests.rs | 17 +++++++++++++++++ 10 files changed, 50 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8be04bc..7be76a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "egui_code_editor" authors = ["Roman Chumak "] -version = "0.2.20" +version = "0.2.21" edition = "2024" license = "MIT" repository = "https://github.com/p4ymak/egui_code_editor" diff --git a/src/highlighting.rs b/src/highlighting.rs index 79b21e7..e5535e8 100644 --- a/src/highlighting.rs +++ b/src/highlighting.rs @@ -1,7 +1,6 @@ #[cfg(feature = "editor")] use super::Editor; - -use super::syntax::{Syntax, TokenType, QUOTES, SEPARATORS}; +use super::syntax::{SEPARATORS, Syntax, TokenType}; use std::mem; #[derive(Default, Debug, PartialEq, PartialOrd, Eq, Ord)] @@ -34,6 +33,7 @@ impl Token { token = self.drain(self.ty); TokenType::Whitespace(c) } + c if syntax.quotes.contains(&c) => TokenType::Str(c), c if syntax.is_keyword(c.to_string().as_str()) => TokenType::Keyword, c if syntax.is_type(c.to_string().as_str()) => TokenType::Type, c if syntax.is_special(c.to_string().as_str()) => TokenType::Special, @@ -136,7 +136,7 @@ impl Token { c if !c.is_alphanumeric() && !SEPARATORS.contains(&c) => { tokens.extend(self.drain(self.ty)); self.buffer.push(c); - self.ty = if QUOTES.contains(&c) { + self.ty = if syntax.quotes.contains(&c) { Ty::Str(c) } else { Ty::Punctuation(c) diff --git a/src/syntax/asm.rs b/src/syntax/asm.rs index f772886..8e74ba0 100644 --- a/src/syntax/asm.rs +++ b/src/syntax/asm.rs @@ -1,4 +1,4 @@ -use super::Syntax; +use super::{DEFAULT_QUOTES, Syntax}; use std::collections::BTreeSet; impl Syntax { @@ -8,6 +8,7 @@ impl Syntax { case_sensitive: false, comment: ";", comment_multiline: ["/*", "*/"], + quotes: DEFAULT_QUOTES.into(), hyperlinks: BTreeSet::from(["http"]), keywords: BTreeSet::from([ "vaddpd", diff --git a/src/syntax/lua.rs b/src/syntax/lua.rs index 8899024..e25e38b 100644 --- a/src/syntax/lua.rs +++ b/src/syntax/lua.rs @@ -1,4 +1,4 @@ -use super::Syntax; +use super::{DEFAULT_QUOTES, Syntax}; use std::collections::BTreeSet; impl Syntax { @@ -8,6 +8,7 @@ impl Syntax { case_sensitive: true, comment: "--", comment_multiline: ["--[[", "]]"], + quotes: DEFAULT_QUOTES.into(), hyperlinks: BTreeSet::from(["http"]), keywords: BTreeSet::from([ "and", "break", "do", "else", "elseif", "end", "for", "function", "if", "in", diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 88ac5cc..f3895f1 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -10,7 +10,10 @@ use std::collections::BTreeSet; use std::hash::{Hash, Hasher}; pub const SEPARATORS: [char; 1] = ['_']; -pub const QUOTES: [char; 3] = ['\'', '"', '`']; +pub const DEFAULT_QUOTES: [char; 3] = ['\'', '"', '`']; +pub const WHITESPACE_SPACE: char = ' '; +pub const WHITESPACE_TAB: char = '\t'; +pub const WHITESPACE_NEWLINE: char = '\n'; type MultiLine = bool; type Float = bool; @@ -67,10 +70,10 @@ impl std::fmt::Debug for TokenType { TokenType::Type => name.push_str("Type"), TokenType::Whitespace(c) => { name.push_str("Whitespace"); - match c { - ' ' => name.push_str(" Space"), - '\t' => name.push_str(" Tab"), - '\n' => name.push_str(" New Line"), + match *c { + WHITESPACE_SPACE => name.push_str(" Space"), + WHITESPACE_TAB => name.push_str(" Tab"), + WHITESPACE_NEWLINE => name.push_str(" New Line"), _ => (), }; } @@ -83,7 +86,6 @@ impl From for TokenType { fn from(c: char) -> Self { match c { c if c.is_whitespace() => TokenType::Whitespace(c), - c if QUOTES.contains(&c) => TokenType::Str(c), c if c.is_numeric() => TokenType::Numeric(false), c if c.is_alphabetic() || SEPARATORS.contains(&c) => TokenType::Literal, c if c.is_ascii_punctuation() => TokenType::Punctuation(c), @@ -99,6 +101,7 @@ pub struct Syntax { pub case_sensitive: bool, pub comment: &'static str, pub comment_multiline: [&'static str; 2], + pub quotes: BTreeSet, pub hyperlinks: BTreeSet<&'static str>, pub keywords: BTreeSet<&'static str>, pub types: BTreeSet<&'static str>, @@ -118,6 +121,7 @@ impl Syntax { pub fn new(language: &'static str) -> Self { Syntax { language, + quotes: DEFAULT_QUOTES.into(), ..Default::default() } } @@ -136,6 +140,12 @@ impl Syntax { ..self } } + pub fn with_quotes>>(self, quotes: T) -> Self { + Syntax { + quotes: quotes.into(), + ..self + } + } pub fn with_hyperlinks>>(self, hyperlinks: T) -> Self { Syntax { hyperlinks: hyperlinks.into(), @@ -200,6 +210,7 @@ impl Syntax { case_sensitive: false, comment, comment_multiline: [comment; 2], + quotes: DEFAULT_QUOTES.into(), hyperlinks: BTreeSet::new(), keywords: BTreeSet::new(), types: BTreeSet::new(), diff --git a/src/syntax/python.rs b/src/syntax/python.rs index 8ce0492..a361660 100644 --- a/src/syntax/python.rs +++ b/src/syntax/python.rs @@ -1,4 +1,4 @@ -use super::Syntax; +use super::{DEFAULT_QUOTES, Syntax}; use std::collections::BTreeSet; impl Syntax { @@ -8,6 +8,7 @@ impl Syntax { case_sensitive: true, comment: "#", comment_multiline: [r#"'''"#, r#"'''"#], + quotes: DEFAULT_QUOTES.into(), hyperlinks: BTreeSet::from(["http"]), keywords: BTreeSet::from([ "and", "as", "assert", "break", "class", "continue", "def", "del", "elif", "else", diff --git a/src/syntax/rust.rs b/src/syntax/rust.rs index 6d7278c..608f6c6 100644 --- a/src/syntax/rust.rs +++ b/src/syntax/rust.rs @@ -1,4 +1,4 @@ -use super::Syntax; +use super::{DEFAULT_QUOTES, Syntax}; use std::collections::BTreeSet; impl Syntax { @@ -8,6 +8,7 @@ impl Syntax { case_sensitive: true, comment: "//", comment_multiline: ["/*", "*/"], + quotes: DEFAULT_QUOTES.into(), hyperlinks: BTreeSet::from(["http"]), keywords: BTreeSet::from([ "as", "break", "const", "continue", "crate", "else", "enum", "extern", "fn", "for", diff --git a/src/syntax/shell.rs b/src/syntax/shell.rs index 154c859..965d9f2 100644 --- a/src/syntax/shell.rs +++ b/src/syntax/shell.rs @@ -1,4 +1,4 @@ -use super::Syntax; +use super::{DEFAULT_QUOTES, Syntax}; use std::collections::BTreeSet; impl Syntax { @@ -7,6 +7,7 @@ impl Syntax { language: "Shell", case_sensitive: true, comment: "#", + quotes: DEFAULT_QUOTES.into(), hyperlinks: BTreeSet::from(["http"]), keywords: BTreeSet::from([ "echo", "read", "set", "unset", "readonly", "shift", "export", "if", "fi", "else", diff --git a/src/syntax/sql.rs b/src/syntax/sql.rs index 2b5b5d9..59d2b74 100644 --- a/src/syntax/sql.rs +++ b/src/syntax/sql.rs @@ -1,4 +1,4 @@ -use super::Syntax; +use super::{DEFAULT_QUOTES, Syntax}; use std::collections::BTreeSet; impl Syntax { @@ -8,6 +8,7 @@ impl Syntax { case_sensitive: false, comment: "--", comment_multiline: ["/*", "*/"], + quotes: DEFAULT_QUOTES.into(), hyperlinks: BTreeSet::from(["http"]), keywords: BTreeSet::from([ "ADD", diff --git a/src/tests.rs b/src/tests.rs index fa35dc7..00c810f 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -8,6 +8,23 @@ fn numeric_float() { ); } +#[test] +fn qustom_quotes() { + assert_eq!( + Token::default().tokens( + &Syntax::default().with_quotes(['@']), + "@string@ \"punctuation\"" + ), + [ + Token::new(TokenType::Str('@'), "@string@"), + Token::new(TokenType::Whitespace(' '), " "), + Token::new(TokenType::Punctuation('"'), "\""), + Token::new(TokenType::Literal, "punctuation"), + Token::new(TokenType::Punctuation('"'), "\""), + ] + ); +} + #[test] fn numeric_float_desription() { assert_eq!(