MCPcopy Create free account
hub / github.com/Dimev/mininotes / insert_string

Method insert_string

src/editor.rs:507–545  ·  view source on GitHub ↗

insert a string of characters, at the start byte indicated assumes that start points at a correct grapheme boundary record indicates whether or not to record this action in the history

(
        &mut self,
        start: usize,
        string: &str,
        record: bool,
        store_cursor: bool,
        move_cursor_after: bool,
    )

Source from the content-addressed store, hash-verified

505 /// assumes that start points at a correct grapheme boundary
506 /// record indicates whether or not to record this action in the history
507 pub fn insert_string(
508 &mut self,
509 start: usize,
510 string: &str,
511 record: bool,
512 store_cursor: bool,
513 move_cursor_after: bool,
514 ) {
515 // clear this
516 self.clear_selection();
517
518 // get the start position in chars
519 let start_char = self.text.byte_to_char(start);
520
521 // insert the text
522 self.text.insert(start_char, string);
523
524 // insert the change, if it was anything
525 if record && !string.is_empty() {
526 self.do_change(EditorAction::Insert(start, string.to_string()));
527 }
528
529 // fix cursor
530 if self.cursor > start {
531 // after the range, so move it over the right amount of bytes to compensate for the added range
532 self.cursor += string.len();
533 }
534
535 // move the cursor to after the inserted text
536 if move_cursor_after {
537 // move it after the inserted item
538 self.cursor = start + string.len();
539 }
540
541 // and recalculate the target column
542 if store_cursor {
543 self.target_column = self.get_cursor_column();
544 }
545 }
546
547 /// remove a range of characters, in bytes, start..end
548 /// assumes that the range is in correct graphemes

Callers 5

undoMethod · 0.80
redoMethod · 0.80

Calls 4

clear_selectionMethod · 0.80
do_changeMethod · 0.80
to_stringMethod · 0.80
get_cursor_columnMethod · 0.80

Tested by

no test coverage detected