diff options
| author | Polesznyák Márk <contact@pml68.dev> | 2025-09-22 19:17:48 +0200 |
|---|---|---|
| committer | Polesznyák Márk <contact@pml68.dev> | 2025-09-22 19:17:48 +0200 |
| commit | 56ce76cf42cd34900f649bdf08e2a001f32658a4 (patch) | |
| tree | 2c50279066474877286d8ed619c1838556cb7b91 /src/panes | |
| parent | ci(test): remove `--ignore` flag (diff) | |
| download | iced-builder-56ce76cf42cd34900f649bdf08e2a001f32658a4.tar.gz | |
feat(wip): iced_material based code highlighting
Diffstat (limited to '')
| -rw-r--r-- | src/panes/code_view.rs | 112 |
1 files changed, 102 insertions, 10 deletions
diff --git a/src/panes/code_view.rs b/src/panes/code_view.rs index 5e29634..2dc27e9 100644 --- a/src/panes/code_view.rs +++ b/src/panes/code_view.rs @@ -10,19 +10,111 @@ use crate::icon; use crate::types::{DesignerPane, Message}; // TODO: implement a highlight style for the material theme +//fn highlight_style(theme: &Theme, scope: &Scope) -> Format<Font> { +// let theme = if theme.is_dark() { +// iced::Theme::SolarizedDark +// } else { +// iced::Theme::SolarizedLight +// }; +// +// match scope { +// Scope::Custom { .. } | Scope::Other => Format { +// color: Some(theme.extended_palette().primary.strong.color), +// font: None, +// }, +// _ => Highlight::default_style(&theme, scope), +// } +//} + fn highlight_style(theme: &Theme, scope: &Scope) -> Format<Font> { - let theme = if theme.is_dark() { - iced::Theme::SolarizedDark - } else { - iced::Theme::SolarizedLight + let colors = theme.colors(); + + let color = match scope { + // Comments are low-priority and often de-emphasized + Scope::Comment => colors.outline.variant, + + // Strings and escape sequences are content-rich + Scope::String + | Scope::QuotedString + | Scope::QuotedSingle + | Scope::EscapeSequence => colors.tertiary.color, + + // Regex deserves distinct treatment, often looking like code but acting like a string + Scope::RegExp => colors.secondary.secondary_container, + + // Numeric literals and constants (user- or built-in) + Scope::Number | Scope::BuiltinConstant | Scope::UserDefinedConstant => { + colors.secondary.color + } + + // Variable identifiers and function call references + Scope::Variable | Scope::VariableFunction | Scope::VariableStart => { + colors.primary.color + } + + // Function and method declarations + Scope::FunctionName | Scope::LibraryFunction => { + colors.primary.primary_container + } + + // Keywords and language operators + Scope::Keyword + | Scope::KeywordOperator + | Scope::KeywordOther + | Scope::Operator + | Scope::Continuation => colors.secondary.secondary_container, + + // Module/class/namespace declarations + Scope::Class + | Scope::LibraryClass + | Scope::StorageType + | Scope::StorageModifier + | Scope::Storage => colors.tertiary.tertiary_container, + + // Import paths or directives + Scope::Import => colors.tertiary.color, + + // Error cases, invalid tokens + Scope::Exception | Scope::Invalid => colors.error.color, + + // HTML-like tags or markup tokens + Scope::TagName | Scope::TagStart => colors.primary.primary_container, + + // Special constants, macros, etc. + Scope::Special | Scope::SupportConstruct => { + colors.inverse.inverse_primary + } + + // Braces, parentheses, brackets — structural but low priority + Scope::Braces | Scope::Parantheses | Scope::Brackets => { + colors.outline.variant + } + + // Fallback + Scope::Other => colors.surface.on_surface_variant, + + // Handle user-defined custom scope + Scope::Custom { name, .. } => { + if name.contains("comment") { + colors.outline.variant + } else if name.contains("error") { + colors.error.color + } else if name.contains("string") { + colors.tertiary.color + } else if name.contains("keyword") { + colors.secondary.secondary_container + } else if name.contains("function") { + colors.primary.primary_container + } else { + // Default fallback for unknown custom scopes + colors.surface.on_surface_variant + } + } }; - match scope { - Scope::Custom { .. } | Scope::Other => Format { - color: Some(theme.extended_palette().primary.strong.color), - font: None, - }, - _ => Highlight::default_style(&theme, scope), + Format { + color: Some(color), + font: None, } } |
