indent recognition
This commit is contained in:
parent
467d2b698a
commit
59342fa980
|
@ -18,3 +18,5 @@ Library subjugation (got the raw files of the egui_code_editor for some internal
|
||||||
Added find and replace function
|
Added find and replace function
|
||||||
Added multi line tab and shift+tab
|
Added multi line tab and shift+tab
|
||||||
Added Ctrl+E : comment multiline
|
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)
|
||||||
|
|
|
@ -262,7 +262,7 @@ impl super::Calcifer {
|
||||||
|
|
||||||
let new_tab = tools::Tab {
|
let new_tab = tools::Tab {
|
||||||
path: path.into(),
|
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(),
|
language: path.to_str().unwrap().split('.').last().unwrap().into(),
|
||||||
saved: true,
|
saved: true,
|
||||||
..tools::Tab::default()
|
..tools::Tab::default()
|
||||||
|
|
|
@ -268,8 +268,10 @@ impl CodeEditor {
|
||||||
let mut extend : isize = 0;
|
let mut extend : isize = 0;
|
||||||
|
|
||||||
if output.response.has_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) {
|
if output.response.has_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) {
|
||||||
println!("line break");
|
if let Some(range) = last_cursor {
|
||||||
//get previous line number of tabs, and 2 charcters before 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) {
|
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();
|
*last_cursor = output.state.clone().ccursor_range();
|
||||||
} else {
|
} else {
|
||||||
if let Some(cursor_range) = last_cursor.clone() {
|
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 end = max(cursor_range.primary.index, cursor_range.secondary.index);
|
||||||
let extended = match end as isize + extend {
|
let extended = match end as isize + extend {
|
||||||
// Check for overflow or negative result
|
// Check for overflow or negative result
|
||||||
value if value < 0 => 0,
|
value if value < 0 => 0,
|
||||||
value => value as usize,
|
value => value as usize,
|
||||||
};
|
};
|
||||||
|
if start == end {
|
||||||
|
start = extended;
|
||||||
|
}
|
||||||
let cursor = Some(CCursorRange {
|
let cursor = Some(CCursorRange {
|
||||||
primary : CCursor::new(start),
|
primary : CCursor::new(start.clone()),
|
||||||
secondary : CCursor::new(max(start, extended)),
|
secondary : CCursor::new(max(start.clone(), extended)),
|
||||||
});
|
});
|
||||||
output.state.set_ccursor_range(cursor.clone());
|
output.state.set_ccursor_range(cursor.clone());
|
||||||
output.state.store(ui.ctx(), output.response.id);
|
output.state.store(ui.ctx(), output.response.id);
|
||||||
|
@ -409,4 +414,23 @@ impl CodeEditor {
|
||||||
fn delta_char(&self, text : String, modifier: &str) -> isize {
|
fn delta_char(&self, text : String, modifier: &str) -> isize {
|
||||||
(modifier.len() * text.match_indices(&"\n".to_string()).collect::<Vec<_>>().len()) as isize
|
(modifier.len() * text.match_indices(&"\n".to_string()).collect::<Vec<_>>().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::<Vec<_>>().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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue