Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions crates/rnote-engine/src/strokes/textstroke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down