diff --git a/crates/rnote-engine/src/strokes/textstroke.rs b/crates/rnote-engine/src/strokes/textstroke.rs index 2ca3b3e28d..1bb5881053 100644 --- a/crates/rnote-engine/src/strokes/textstroke.rs +++ b/crates/rnote-engine/src/strokes/textstroke.rs @@ -585,7 +585,47 @@ impl TextStroke { // translate the text attributes self.translate_attrs_after_cursor(cursor.cur_cursor(), text.len() as i32); + // adds ")" or "] after e. g. "(" + if let Some(closing) = Self::get_auto_closing_pair(text) { + self.text + .insert_str(cursor.cur_cursor() + text.len(), closing); + self.translate_attrs_after_cursor( + cursor.cur_cursor() + text.len(), + closing.len() as i32, + ); + } + *cursor = GraphemeCursor::new(cursor.cur_cursor() + text.len(), self.text.len(), true); + + if text == "\n" { + let prefix = self.get_last_line_prefix(); + self.text.insert_str(cursor.cur_cursor(), &prefix); + self.translate_attrs_after_cursor(cursor.cur_cursor(), prefix.len() as i32); + + *cursor = + GraphemeCursor::new(cursor.cur_cursor() + prefix.len(), self.text.len(), true); + } + } + + fn get_auto_closing_pair(open: &str) -> Option<&'static str> { + match open { + "(" => Some(")"), + "[" => Some("]"), + "{" => Some("}"), + "'" => Some("'"), + "\"" => Some("\""), + _ => None, + } + } + + fn get_last_line_prefix(&self) -> String { + self.text + .lines() + .last() + .unwrap_or("") + .chars() + .take_while(|&ch| matches!(ch, ' ' | '\t' | '-' | '*' | '.')) + .collect() } pub fn remove_grapheme_before_cursor(&mut self, cursor: &mut GraphemeCursor) {