From 6d262b0f868e28d84b8f145e75837228648024f6 Mon Sep 17 00:00:00 2001 From: Hauke Date: Tue, 5 Aug 2025 09:31:47 +0200 Subject: [PATCH 1/3] updates textstrokes to add closing pairs --- crates/rnote-engine/src/strokes/textstroke.rs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/crates/rnote-engine/src/strokes/textstroke.rs b/crates/rnote-engine/src/strokes/textstroke.rs index 83816854d5..c759ec0698 100644 --- a/crates/rnote-engine/src/strokes/textstroke.rs +++ b/crates/rnote-engine/src/strokes/textstroke.rs @@ -585,7 +585,27 @@ 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); + + } + + fn get_auto_closing_pair(open: &str) -> Option<&'static str>{ + match open { + "(" => Some(")"), + "[" => Some("]"), + "{" => Some("}"), + "'" => Some("'"), + "\"" => Some("\""), + _ => None, + + } } pub fn remove_grapheme_before_cursor(&mut self, cursor: &mut GraphemeCursor) { From 7154c128c54c4679df11209507bb6414d072de63 Mon Sep 17 00:00:00 2001 From: Hauke Date: Wed, 6 Aug 2025 11:49:08 +0200 Subject: [PATCH 2/3] prefix from previos line is added after pressing enter --- crates/rnote-engine/src/strokes/textstroke.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/crates/rnote-engine/src/strokes/textstroke.rs b/crates/rnote-engine/src/strokes/textstroke.rs index f3e8552861..06cd2c5818 100644 --- a/crates/rnote-engine/src/strokes/textstroke.rs +++ b/crates/rnote-engine/src/strokes/textstroke.rs @@ -594,6 +594,14 @@ impl TextStroke { *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>{ @@ -608,6 +616,13 @@ impl TextStroke { } } + 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) { if !self.text.is_empty() && self.text.len() >= cursor.cur_cursor() { let cur_pos = cursor.cur_cursor(); From 77b5ad5c64f8577ba99062986629e1dac8339ce2 Mon Sep 17 00:00:00 2001 From: Hauke Date: Wed, 6 Aug 2025 12:09:50 +0200 Subject: [PATCH 3/3] corrects format --- crates/rnote-engine/src/strokes/textstroke.rs | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/crates/rnote-engine/src/strokes/textstroke.rs b/crates/rnote-engine/src/strokes/textstroke.rs index 06cd2c5818..1bb5881053 100644 --- a/crates/rnote-engine/src/strokes/textstroke.rs +++ b/crates/rnote-engine/src/strokes/textstroke.rs @@ -586,25 +586,28 @@ impl TextStroke { 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); + 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"{ + 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); + *cursor = + GraphemeCursor::new(cursor.cur_cursor() + prefix.len(), self.text.len(), true); } - } - fn get_auto_closing_pair(open: &str) -> Option<&'static str>{ + fn get_auto_closing_pair(open: &str) -> Option<&'static str> { match open { "(" => Some(")"), "[" => Some("]"), @@ -612,15 +615,17 @@ impl TextStroke { "'" => Some("'"), "\"" => Some("\""), _ => None, - } } - fn get_last_line_prefix(&self) -> String{ - self.text.lines().last().unwrap_or("").chars() - .take_while(|&ch| matches!(ch, ' ' | '\t' | '-' | '*' | '.')) - .collect() - + 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) {