Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "egui_code_editor"
authors = ["Roman Chumak <p4ymak@yandex.ru>"]
version = "0.2.20"
version = "0.2.21"
edition = "2024"
license = "MIT"
repository = "https://github.com/p4ymak/egui_code_editor"
Expand Down
6 changes: 3 additions & 3 deletions src/highlighting.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/syntax/asm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Syntax;
use super::{DEFAULT_QUOTES, Syntax};
use std::collections::BTreeSet;

impl Syntax {
Expand All @@ -8,6 +8,7 @@ impl Syntax {
case_sensitive: false,
comment: ";",
comment_multiline: ["/*", "*/"],
quotes: DEFAULT_QUOTES.into(),
hyperlinks: BTreeSet::from(["http"]),
keywords: BTreeSet::from([
"vaddpd",
Expand Down
3 changes: 2 additions & 1 deletion src/syntax/lua.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Syntax;
use super::{DEFAULT_QUOTES, Syntax};
use std::collections::BTreeSet;

impl Syntax {
Expand All @@ -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",
Expand Down
23 changes: 17 additions & 6 deletions src/syntax/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"),
_ => (),
};
}
Expand All @@ -83,7 +86,6 @@ impl From<char> 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),
Expand All @@ -99,6 +101,7 @@ pub struct Syntax {
pub case_sensitive: bool,
pub comment: &'static str,
pub comment_multiline: [&'static str; 2],
pub quotes: BTreeSet<char>,
pub hyperlinks: BTreeSet<&'static str>,
pub keywords: BTreeSet<&'static str>,
pub types: BTreeSet<&'static str>,
Expand All @@ -118,6 +121,7 @@ impl Syntax {
pub fn new(language: &'static str) -> Self {
Syntax {
language,
quotes: DEFAULT_QUOTES.into(),
..Default::default()
}
}
Expand All @@ -136,6 +140,12 @@ impl Syntax {
..self
}
}
pub fn with_quotes<T: Into<BTreeSet<char>>>(self, quotes: T) -> Self {
Syntax {
quotes: quotes.into(),
..self
}
}
pub fn with_hyperlinks<T: Into<BTreeSet<&'static str>>>(self, hyperlinks: T) -> Self {
Syntax {
hyperlinks: hyperlinks.into(),
Expand Down Expand Up @@ -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(),
Expand Down
3 changes: 2 additions & 1 deletion src/syntax/python.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Syntax;
use super::{DEFAULT_QUOTES, Syntax};
use std::collections::BTreeSet;

impl Syntax {
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/syntax/rust.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Syntax;
use super::{DEFAULT_QUOTES, Syntax};
use std::collections::BTreeSet;

impl Syntax {
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/syntax/shell.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Syntax;
use super::{DEFAULT_QUOTES, Syntax};
use std::collections::BTreeSet;

impl Syntax {
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/syntax/sql.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Syntax;
use super::{DEFAULT_QUOTES, Syntax};
use std::collections::BTreeSet;

impl Syntax {
Expand All @@ -8,6 +8,7 @@ impl Syntax {
case_sensitive: false,
comment: "--",
comment_multiline: ["/*", "*/"],
quotes: DEFAULT_QUOTES.into(),
hyperlinks: BTreeSet::from(["http"]),
keywords: BTreeSet::from([
"ADD",
Expand Down
17 changes: 17 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down