From fd26344b5fb96dc757c3dc7aa523d2151b6d1e0e Mon Sep 17 00:00:00 2001 From: candle Date: Tue, 22 Jul 2025 23:09:01 -0400 Subject: [PATCH] moving lanugage/theme stuff out --- src/app/theme.rs | 10 +++++ src/ui/central_panel.rs | 1 + src/ui/central_panel/editor.rs | 72 ++----------------------------- src/ui/central_panel/languages.rs | 55 +++++++++++++++++++++++ 4 files changed, 69 insertions(+), 69 deletions(-) create mode 100644 src/ui/central_panel/languages.rs diff --git a/src/app/theme.rs b/src/app/theme.rs index 7761053..52d1f6d 100644 --- a/src/app/theme.rs +++ b/src/app/theme.rs @@ -1,4 +1,5 @@ use eframe::egui; +use egui_extras::syntax_highlighting::CodeTheme; #[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize, Default)] pub enum Theme { @@ -194,3 +195,12 @@ fn detect_system_dark_mode() -> bool { true } } + +pub fn create_code_theme_from_visuals(visuals: &egui::Visuals, font_size: f32) -> CodeTheme { + if visuals.dark_mode { + CodeTheme::dark(font_size) + } else { + CodeTheme::light(font_size) + } +} + diff --git a/src/ui/central_panel.rs b/src/ui/central_panel.rs index 5ebab4e..6f6e7e6 100644 --- a/src/ui/central_panel.rs +++ b/src/ui/central_panel.rs @@ -1,6 +1,7 @@ mod editor; mod find_highlight; mod line_numbers; +mod languages; use crate::app::TextEditor; use eframe::egui; diff --git a/src/ui/central_panel/editor.rs b/src/ui/central_panel/editor.rs index 2b8fdf3..0995422 100644 --- a/src/ui/central_panel/editor.rs +++ b/src/ui/central_panel/editor.rs @@ -1,75 +1,9 @@ use crate::app::TextEditor; use eframe::egui; -use egui_extras::syntax_highlighting::{self, CodeTheme}; +use egui_extras::syntax_highlighting::{self}; use super::find_highlight; -fn get_language_from_extension(file_path: Option<&std::path::Path>) -> String { - if let Some(path) = file_path { - if let Some(extension) = path.extension().and_then(|ext| ext.to_str()) { - match extension.to_lowercase().as_str() { - "rs" => "rs".to_string(), - "py" => "py".to_string(), - "js" => "js".to_string(), - "ts" => "ts".to_string(), - "tsx" => "tsx".to_string(), - "jsx" => "jsx".to_string(), - "c" => "c".to_string(), - "cpp" | "cc" | "cxx" => "cpp".to_string(), - "h" | "hpp" => "cpp".to_string(), - "java" => "java".to_string(), - "go" => "go".to_string(), - "php" => "php".to_string(), - "rb" => "rb".to_string(), - "cs" => "cs".to_string(), - "swift" => "swift".to_string(), - "kt" => "kt".to_string(), - "scala" => "scala".to_string(), - "sh" | "bash" | "zsh" | "fish" => "sh".to_string(), - "html" | "htm" => "html".to_string(), - "xml" => "xml".to_string(), - "css" => "css".to_string(), - "scss" | "sass" => "scss".to_string(), - "json" => "json".to_string(), - "yaml" | "yml" => "yaml".to_string(), - "toml" => "toml".to_string(), - "md" | "markdown" => "md".to_string(), - "sql" => "sql".to_string(), - "lua" => "lua".to_string(), - "vim" => "vim".to_string(), - "dockerfile" => "dockerfile".to_string(), - "makefile" => "makefile".to_string(), - _ => "txt".to_string(), - } - } else { - // Check filename for special cases - if let Some(filename) = path.file_name().and_then(|name| name.to_str()) { - match filename.to_lowercase().as_str() { - "dockerfile" => "dockerfile".to_string(), - "makefile" => "makefile".to_string(), - "cargo.toml" | "pyproject.toml" => "toml".to_string(), - "package.json" => "json".to_string(), - _ => "txt".to_string(), - } - } else { - "txt".to_string() - } - } - } else { - "txt".to_string() - } -} - -fn create_code_theme_from_visuals(visuals: &egui::Visuals, font_size: f32) -> CodeTheme { - // For now, just use the appropriate base theme (dark/light) - // The base themes should automatically work well with the system colors - // since egui's syntax highlighting respects the overall UI theme - if visuals.dark_mode { - CodeTheme::dark(font_size) - } else { - CodeTheme::light(font_size) - } -} pub(super) fn editor_view_ui(ui: &mut egui::Ui, app: &mut TextEditor) -> egui::Response { let _current_match_position = app.get_current_match_position(); @@ -166,8 +100,8 @@ pub(super) fn editor_view_ui(ui: &mut egui::Ui, app: &mut TextEditor) -> egui::R f32::INFINITY }; - let language = get_language_from_extension(active_tab.file_path.as_deref()); - let theme = create_code_theme_from_visuals(ui.visuals(), font_size); + let language = super::languages::get_language_from_extension(active_tab.file_path.as_deref()); + let theme = crate::app::theme::create_code_theme_from_visuals(ui.visuals(), font_size); let mut layouter = |ui: &egui::Ui, string: &dyn egui::TextBuffer, wrap_width: f32| { let text = string.as_str(); diff --git a/src/ui/central_panel/languages.rs b/src/ui/central_panel/languages.rs new file mode 100644 index 0000000..73525c8 --- /dev/null +++ b/src/ui/central_panel/languages.rs @@ -0,0 +1,55 @@ +pub fn get_language_from_extension(file_path: Option<&std::path::Path>) -> String { + if let Some(path) = file_path { + if let Some(extension) = path.extension().and_then(|ext| ext.to_str()) { + match extension.to_lowercase().as_str() { + "rs" => "rs".to_string(), + "py" => "py".to_string(), + "js" => "js".to_string(), + "ts" => "ts".to_string(), + "tsx" => "tsx".to_string(), + "jsx" => "jsx".to_string(), + "c" => "c".to_string(), + "cpp" | "cc" | "cxx" => "cpp".to_string(), + "h" | "hpp" => "cpp".to_string(), + "java" => "java".to_string(), + "go" => "go".to_string(), + "php" => "php".to_string(), + "rb" => "rb".to_string(), + "cs" => "cs".to_string(), + "swift" => "swift".to_string(), + "kt" => "kt".to_string(), + "scala" => "scala".to_string(), + "sh" | "bash" | "zsh" | "fish" => "sh".to_string(), + "html" | "htm" => "html".to_string(), + "xml" => "xml".to_string(), + "css" => "css".to_string(), + "scss" | "sass" => "scss".to_string(), + "json" => "json".to_string(), + "yaml" | "yml" => "yaml".to_string(), + "toml" => "toml".to_string(), + "md" | "markdown" => "md".to_string(), + "sql" => "sql".to_string(), + "lua" => "lua".to_string(), + "vim" => "vim".to_string(), + "dockerfile" => "dockerfile".to_string(), + "makefile" => "makefile".to_string(), + _ => "txt".to_string(), + } + } else { + // Check filename for special cases + if let Some(filename) = path.file_name().and_then(|name| name.to_str()) { + match filename.to_lowercase().as_str() { + "dockerfile" => "dockerfile".to_string(), + "makefile" => "makefile".to_string(), + "cargo.toml" | "pyproject.toml" => "toml".to_string(), + "package.json" => "json".to_string(), + _ => "txt".to_string(), + } + } else { + "txt".to_string() + } + } + } else { + "txt".to_string() + } +}