text edit shortcut structure

This commit is contained in:
Penwing 2024-01-23 10:46:09 +01:00
parent 53d9531646
commit 618879100e
5 changed files with 93 additions and 31 deletions

View file

@ -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

View file

@ -155,19 +155,7 @@ impl super::Calcifer {
.with_theme(self.theme)
.with_syntax(tools::to_syntax(&current_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 &current_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<PathBuf> {
@ -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);

View file

@ -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<CCursorRange>) {
pub fn show(&mut self, ui: &mut egui::Ui, text: &mut String, history: &mut Vec<String>, last_cursor: &mut Option<CCursorRange>, vertical_offset: &mut f32, override_cursor: Option<CCursorRange>) {
//let mut text_edit_output: Option<TextEditOutput> = None;
let mut code_editor = |ui: &mut egui::Ui| {
ui.horizontal_top(|h| {
@ -197,6 +197,9 @@ impl CodeEditor {
let layout_job = highlight(ui.ctx(), self, string);
ui.fonts(|f| f.layout_job(layout_job))
};
let previous_text = text.clone();
let mut output = egui::TextEdit::multiline(text)
.id_source(&self.id)
.lock_focus(true)
@ -206,13 +209,73 @@ impl CodeEditor {
.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 &current_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
}
}

View file

@ -87,10 +87,6 @@ impl eframe::App for Calcifer {
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();
if self.searching {

View file

@ -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<String>,
pub scroll_offset : f32,
pub last_cursor : Option<CCursorRange>,
}
impl Default for Tab {
@ -89,6 +91,7 @@ impl Default for Tab {
saved: false,
history: vec![],
scroll_offset: 0.0,
last_cursor: None,
}
}
}