diff --git a/README.md b/README.md index 93eaa70..0d51acd 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,5 @@ Library subjugation (got the raw files of the egui_code_editor for some internal Added find and replace function Added multi line tab and shift+tab Added Ctrl+E : comment multiline +Fixed Ctr+Z (was already in library, tried to make my own, and then found the better one) +Added indent recognition (when there is a line break, the indentaion level is kept) diff --git a/src/calcifer.rs b/src/calcifer.rs index 161bfe1..4118552 100644 --- a/src/calcifer.rs +++ b/src/calcifer.rs @@ -262,7 +262,7 @@ impl super::Calcifer { let new_tab = tools::Tab { path: path.into(), - code: fs::read_to_string(path).expect("Not able to read the file"), + code: fs::read_to_string(path).expect("Not able to read the file").replace(" ", "\t"), language: path.to_str().unwrap().split('.').last().unwrap().into(), saved: true, ..tools::Tab::default() @@ -271,7 +271,7 @@ impl super::Calcifer { return tools::TabNumber::from_index(self.tabs.len() - 1) } - + fn new_tab(&mut self) -> tools::TabNumber { self.tabs.push(tools::Tab::default()); return tools::TabNumber::from_index(self.tabs.len() - 1) diff --git a/src/calcifer/code_editor/mod.rs b/src/calcifer/code_editor/mod.rs index ac4b11c..70845ae 100644 --- a/src/calcifer/code_editor/mod.rs +++ b/src/calcifer/code_editor/mod.rs @@ -268,8 +268,10 @@ impl CodeEditor { let mut extend : isize = 0; if output.response.has_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) { - println!("line break"); - //get previous line number of tabs, and 2 charcters before cursor + if let Some(range) = last_cursor { + (*text, extend) = self.new_line(range.clone(), text.clone()); + get_new_cursor = false; + } } if output.response.has_focus() && ui.input(|i| i.key_pressed(egui::Key::E) && i.modifiers.ctrl) { @@ -305,16 +307,19 @@ impl CodeEditor { *last_cursor = output.state.clone().ccursor_range(); } else { if let Some(cursor_range) = last_cursor.clone() { - let start = min(cursor_range.primary.index, cursor_range.secondary.index); + let mut start = min(cursor_range.primary.index, cursor_range.secondary.index); let end = max(cursor_range.primary.index, cursor_range.secondary.index); let extended = match end as isize + extend { // Check for overflow or negative result value if value < 0 => 0, value => value as usize, }; + if start == end { + start = extended; + } let cursor = Some(CCursorRange { - primary : CCursor::new(start), - secondary : CCursor::new(max(start, extended)), + primary : CCursor::new(start.clone()), + secondary : CCursor::new(max(start.clone(), extended)), }); output.state.set_ccursor_range(cursor.clone()); output.state.store(ui.ctx(), output.response.id); @@ -409,4 +414,23 @@ impl CodeEditor { fn delta_char(&self, text : String, modifier: &str) -> isize { (modifier.len() * text.match_indices(&"\n".to_string()).collect::>().len()) as isize } + + fn new_line(&self, cursor_range : CCursorRange, text : String) -> (String, isize) { + let cursor = min(cursor_range.primary.index, cursor_range.secondary.index); + + let mut last_line_break = max(0, cursor - 1); + + while last_line_break > 0 && text.char_at(last_line_break) != '\n' { + last_line_break -= 1; + } + let indent_depth = text.slice(last_line_break..cursor).match_indices(&"\t".to_string()).collect::>().len(); + + let new_indent_depth = indent_depth.clone(); + + let mut new_text : String = text.clone().slice(..(cursor + 1)).to_string(); + new_text.push_str(&"\t".repeat(new_indent_depth.clone())); + new_text.push_str(text.clone().slice((cursor + 1)..)); + + (new_text.clone().to_string(), (new_indent_depth + 1) as isize) + } }