From 618879100e028075c454a2a279d191d8c2ebd204 Mon Sep 17 00:00:00 2001 From: Penwing Date: Tue, 23 Jan 2024 10:46:09 +0100 Subject: [PATCH] text edit shortcut structure --- README.md | 2 +- src/calcifer.rs | 26 +--------- src/calcifer/code_editor/mod.rs | 89 ++++++++++++++++++++++++++++++++- src/main.rs | 4 -- src/tools/mod.rs | 3 ++ 5 files changed, 93 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index f1512c0..dcb912e 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,4 @@ Fixed scroll between tabs Library subjugation (got the raw files of the egui_code_editor for some internal modifications) # 1.0.2 : - +Added find and replace function diff --git a/src/calcifer.rs b/src/calcifer.rs index 2035a6a..f850813 100644 --- a/src/calcifer.rs +++ b/src/calcifer.rs @@ -155,19 +155,7 @@ impl super::Calcifer { .with_theme(self.theme) .with_syntax(tools::to_syntax(¤t_tab.language)) .with_numlines(true) - .show(ui, &mut current_tab.code, &mut current_tab.scroll_offset, override_cursor); - - if current_tab.history.len() < 1 { - current_tab.history.push(current_tab.code.clone()); - } - - if ¤t_tab.code != current_tab.history.last().expect("There should be an history") { - current_tab.history.push(current_tab.code.clone()); - current_tab.saved = false; - if current_tab.history.len() > super::HISTORY_LENGTH { - current_tab.history.remove(0); - } - } + .show(ui, &mut current_tab.code, &mut current_tab.history, &mut current_tab.last_cursor, &mut current_tab.scroll_offset, override_cursor); } pub fn save_tab(&self) -> Option { @@ -203,15 +191,6 @@ impl super::Calcifer { } } - pub fn undo(&mut self) { - let current_tab = &mut self.tabs[self.selected_tab.to_index()]; - if current_tab.history.len() < 2 { - return - } - current_tab.code = current_tab.history[current_tab.history.len() - 2].clone(); - current_tab.history.pop(); - } - pub fn from_app_state(app_state: tools::AppState) -> Self { let mut new = Self { theme: DEFAULT_THEMES[min(app_state.theme, DEFAULT_THEMES.len() - 1)], @@ -286,8 +265,7 @@ impl super::Calcifer { code: fs::read_to_string(path).expect("Not able to read the file"), language: path.to_str().unwrap().split('.').last().unwrap().into(), saved: true, - history: vec![], - scroll_offset: 0.0, + ..tools::Tab::default() }; self.tabs.push(new_tab); diff --git a/src/calcifer/code_editor/mod.rs b/src/calcifer/code_editor/mod.rs index 5de6cc1..83014f5 100644 --- a/src/calcifer/code_editor/mod.rs +++ b/src/calcifer/code_editor/mod.rs @@ -182,7 +182,7 @@ impl CodeEditor { } /// Show Code Editor - pub fn show(&mut self, ui: &mut egui::Ui, text: &mut String, vertical_offset: &mut f32, override_cursor: Option) { + pub fn show(&mut self, ui: &mut egui::Ui, text: &mut String, history: &mut Vec, last_cursor: &mut Option, vertical_offset: &mut f32, override_cursor: Option) { //let mut text_edit_output: Option = None; let mut code_editor = |ui: &mut egui::Ui| { ui.horizontal_top(|h| { @@ -197,7 +197,10 @@ impl CodeEditor { let layout_job = highlight(ui.ctx(), self, string); ui.fonts(|f| f.layout_job(layout_job)) }; - let mut output = egui::TextEdit::multiline(text) + + let previous_text = text.clone(); + + let mut output = egui::TextEdit::multiline(text) .id_source(&self.id) .lock_focus(true) .desired_rows(self.rows) @@ -205,14 +208,74 @@ impl CodeEditor { .desired_width(if self.shrink { 0.0 } else { f32::MAX }) .layouter(&mut layouter) .show(ui); + + let mut get_new_cursor : bool = true; + + if output.response.has_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) { + println!("line break"); + } + + if output.response.has_focus() && ui.input(|i| i.key_pressed(egui::Key::E) && i.modifiers.ctrl) { + println!("Ctrl+E"); + if let Some(range) = last_cursor { + *text = self.toggle_start_of_line(range.clone(), text.clone(), "//"); + get_new_cursor = false; + } + } + + if output.response.has_focus() && ui.input(|i| i.key_pressed(egui::Key::Tab)) { + println!("Tab"); + if let Some(range) = last_cursor { + if range.primary.index != range.secondary.index { + *text = self.add_start_of_line(range.clone(), previous_text.clone(), "\t"); + get_new_cursor = false; + } + } + } + + if output.response.has_focus() && ui.input(|i| i.key_pressed(egui::Key::Tab) && i.modifiers.shift) { + println!("Shift+Tab"); + if let Some(range) = last_cursor { + if range.primary.index != range.secondary.index { + *text = self.remove_start_of_line(range.clone(), previous_text.clone(), "\t"); + get_new_cursor = false; + } + } + } + + if output.response.has_focus() && ui.input( |i| i.key_pressed(egui::Key::Z) && i.modifiers.ctrl) { + println!("Ctrl+Z"); + //let current_tab = &mut self.tabs[self.selected_tab.to_index()]; + //if current_tab.history.len() > 1 { + //current_tab.code = current_tab.history[current_tab.history.len() - 2].clone(); + //current_tab.history.pop(); + //} + } + if get_new_cursor { + *last_cursor = output.state.clone().ccursor_range(); + } + if override_cursor != None { output.response.request_focus(); output.state.set_ccursor_range(override_cursor); output.state.store(ui.ctx(), output.response.id); } + //text_edit_output = Some(output); + + if history.len() < 1 { + history.push(text.clone()); + } + + //if ¤t_tab.code != current_tab.history.last().expect("There should be an history") { + //current_tab.history.push(current_tab.code.clone()); + //current_tab.saved = false; + //if current_tab.history.len() > super::HISTORY_LENGTH { + //current_tab.history.remove(0); + //} + //} }); }); }; @@ -229,4 +292,26 @@ impl CodeEditor { //text_edit_output.expect("TextEditOutput should exist at this point") } + + fn toggle_start_of_line(&self, cursor_range : CCursorRange, text : String, substring : &str) -> String { + let mut text_clone = text.clone(); + + + text_clone + } + + + fn add_start_of_line(&self, cursor_range : CCursorRange, text : String, substring : &str) -> String { + let mut text_clone = text.clone(); + + + text_clone + } + + fn remove_start_of_line(&self, cursor_range : CCursorRange, text : String, substring : &str) -> String { + let mut text_clone = text.clone(); + + + text_clone + } } diff --git a/src/main.rs b/src/main.rs index 76e31ec..0963c36 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,10 +86,6 @@ impl eframe::App for Calcifer { if ctx.input( |i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl && i.modifiers.shift) { self.handle_save_file(self.save_tab_as()); } - - if ctx.input( |i| i.key_pressed(egui::Key::Z) && i.modifiers.ctrl) { - self.undo(); - } if ctx.input( |i| i.key_pressed(egui::Key::F) && i.modifiers.ctrl) { self.searching = !self.searching.clone(); diff --git a/src/tools/mod.rs b/src/tools/mod.rs index 82441d4..cf7ef7d 100644 --- a/src/tools/mod.rs +++ b/src/tools/mod.rs @@ -1,6 +1,7 @@ use std::{process::Command, cmp::Ordering, env, path::PathBuf, fs::read_to_string, fs::write}; use crate::calcifer::code_editor::Syntax; use eframe::egui; +use egui::{text::CCursor, text_edit::CCursorRange}; use serde::{Serialize, Deserialize}; //pub mod themes; @@ -78,6 +79,7 @@ pub struct Tab { pub saved : bool, pub history : Vec, pub scroll_offset : f32, + pub last_cursor : Option, } impl Default for Tab { @@ -89,6 +91,7 @@ impl Default for Tab { saved: false, history: vec![], scroll_offset: 0.0, + last_cursor: None, } } }