Skip to content

Commit

Permalink
No need to pass in &mut prev_text everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed May 26, 2021
1 parent 265190a commit 77b3a15
Showing 1 changed file with 41 additions and 63 deletions.
104 changes: 41 additions & 63 deletions egui/src/widgets/text_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl<'t> TextEdit<'t> {
lock_focus,
} = self;

let mut prev_text = text.clone();
let prev_text = text.clone();
let text_style = text_style.unwrap_or_else(|| ui.style().body_text_style);
let line_spacing = ui.fonts().row_height(text_style);
let available_width = ui.available_width();
Expand Down Expand Up @@ -492,11 +492,7 @@ impl<'t> TextEdit<'t> {
Some(CCursorPair::default())
} else {
copy_if_not_password(ui, selected_str(text, &cursorp).to_owned());
Some(CCursorPair::one(delete_selected(
text,
&mut prev_text,
&cursorp,
)))
Some(CCursorPair::one(delete_selected(text, &cursorp)))
}
}
Event::Text(text_to_insert) => {
Expand All @@ -505,8 +501,8 @@ impl<'t> TextEdit<'t> {
&& text_to_insert != "\n"
&& text_to_insert != "\r"
{
let mut ccursor = delete_selected(text, &mut prev_text, &cursorp);
insert_text(&mut ccursor, text, &mut prev_text, text_to_insert);
let mut ccursor = delete_selected(text, &cursorp);
insert_text(&mut ccursor, text, text_to_insert);
Some(CCursorPair::one(ccursor))
} else {
None
Expand All @@ -518,12 +514,12 @@ impl<'t> TextEdit<'t> {
modifiers,
} => {
if multiline && ui.memory().has_lock_focus(id) {
let mut ccursor = delete_selected(text, &mut prev_text, &cursorp);
let mut ccursor = delete_selected(text, &cursorp);
if modifiers.shift {
// TODO: support removing indentation over a selection?
decrease_identation(&mut ccursor, text);
} else {
insert_text(&mut ccursor, text, &mut prev_text, "\t");
insert_text(&mut ccursor, text, "\t");
}
Some(CCursorPair::one(ccursor))
} else {
Expand All @@ -536,8 +532,8 @@ impl<'t> TextEdit<'t> {
..
} => {
if multiline {
let mut ccursor = delete_selected(text, &mut prev_text, &cursorp);
insert_text(&mut ccursor, text, &mut prev_text, "\n");
let mut ccursor = delete_selected(text, &cursorp);
insert_text(&mut ccursor, text, "\n");
Some(CCursorPair::one(ccursor))
} else {
ui.memory().surrender_focus(id); // End input with enter
Expand All @@ -564,7 +560,7 @@ impl<'t> TextEdit<'t> {
key,
pressed: true,
modifiers,
} => on_key_press(&mut cursorp, text, &mut prev_text, &galley, *key, modifiers),
} => on_key_press(&mut cursorp, text, &galley, *key, modifiers),

Event::CompositionStart => {
state.has_ime = true;
Expand All @@ -577,9 +573,9 @@ impl<'t> TextEdit<'t> {
&& text_mark != "\r"
&& state.has_ime
{
let mut ccursor = delete_selected(text, &mut prev_text, &cursorp);
let mut ccursor = delete_selected(text, &cursorp);
let start_cursor = ccursor;
insert_text(&mut ccursor, text, &mut prev_text, text_mark);
insert_text(&mut ccursor, text, text_mark);
Some(CCursorPair::two(start_cursor, ccursor))
} else {
None
Expand All @@ -593,8 +589,8 @@ impl<'t> TextEdit<'t> {
&& state.has_ime
{
state.has_ime = false;
let mut ccursor = delete_selected(text, &mut prev_text, &cursorp);
insert_text(&mut ccursor, text, &mut prev_text, prediction);
let mut ccursor = delete_selected(text, &cursorp);
insert_text(&mut ccursor, text, prediction);
Some(CCursorPair::one(ccursor))
} else {
None
Expand Down Expand Up @@ -769,13 +765,7 @@ fn byte_index_from_char_index(s: &str, char_index: usize) -> usize {
s.len()
}

fn insert_text(
ccursor: &mut CCursor,
text: &mut String,
prev_text: &mut String,
text_to_insert: &str,
) {
*prev_text = text.clone();
fn insert_text(ccursor: &mut CCursor, text: &mut String, text_to_insert: &str) {
let mut char_it = text.chars();
let mut new_text = String::with_capacity(text.len() + text_to_insert.len());
for _ in 0..ccursor.index {
Expand All @@ -790,20 +780,15 @@ fn insert_text(

// ----------------------------------------------------------------------------

fn delete_selected(text: &mut String, prev_text: &mut String, cursorp: &CursorPair) -> CCursor {
fn delete_selected(text: &mut String, cursorp: &CursorPair) -> CCursor {
let [min, max] = cursorp.sorted();
delete_selected_ccursor_range(text, prev_text, [min.ccursor, max.ccursor])
delete_selected_ccursor_range(text, [min.ccursor, max.ccursor])
}

fn delete_selected_ccursor_range(
text: &mut String,
prev_text: &mut String,
[min, max]: [CCursor; 2],
) -> CCursor {
fn delete_selected_ccursor_range(text: &mut String, [min, max]: [CCursor; 2]) -> CCursor {
let [min, max] = [min.index, max.index];
assert!(min <= max);
if min < max {
*prev_text = text.clone();
let mut char_it = text.chars();
let mut new_text = String::with_capacity(text.len());
for _ in 0..min {
Expand All @@ -818,37 +803,32 @@ fn delete_selected_ccursor_range(
}
}

fn delete_previous_char(text: &mut String, prev_text: &mut String, ccursor: CCursor) -> CCursor {
fn delete_previous_char(text: &mut String, ccursor: CCursor) -> CCursor {
if ccursor.index > 0 {
let max_ccursor = ccursor;
let min_ccursor = max_ccursor - 1;
delete_selected_ccursor_range(text, prev_text, [min_ccursor, max_ccursor])
delete_selected_ccursor_range(text, [min_ccursor, max_ccursor])
} else {
ccursor
}
}

fn delete_next_char(text: &mut String, prev_text: &mut String, ccursor: CCursor) -> CCursor {
delete_selected_ccursor_range(text, prev_text, [ccursor, ccursor + 1])
fn delete_next_char(text: &mut String, ccursor: CCursor) -> CCursor {
delete_selected_ccursor_range(text, [ccursor, ccursor + 1])
}

fn delete_previous_word(
text: &mut String,
prev_text: &mut String,
max_ccursor: CCursor,
) -> CCursor {
fn delete_previous_word(text: &mut String, max_ccursor: CCursor) -> CCursor {
let min_ccursor = ccursor_previous_word(text, max_ccursor);
delete_selected_ccursor_range(text, prev_text, [min_ccursor, max_ccursor])
delete_selected_ccursor_range(text, [min_ccursor, max_ccursor])
}

fn delete_next_word(text: &mut String, prev_text: &mut String, min_ccursor: CCursor) -> CCursor {
fn delete_next_word(text: &mut String, min_ccursor: CCursor) -> CCursor {
let max_ccursor = ccursor_next_word(text, min_ccursor);
delete_selected_ccursor_range(text, prev_text, [min_ccursor, max_ccursor])
delete_selected_ccursor_range(text, [min_ccursor, max_ccursor])
}

fn delete_paragraph_before_cursor(
text: &mut String,
prev_text: &mut String,
galley: &Galley,
cursorp: &CursorPair,
) -> CCursor {
Expand All @@ -859,15 +839,14 @@ fn delete_paragraph_before_cursor(
prefer_next_row: true,
});
if min.ccursor == max.ccursor {
delete_previous_char(text, prev_text, min.ccursor)
delete_previous_char(text, min.ccursor)
} else {
delete_selected(text, prev_text, &CursorPair::two(min, max))
delete_selected(text, &CursorPair::two(min, max))
}
}

fn delete_paragraph_after_cursor(
text: &mut String,
prev_text: &mut String,
galley: &Galley,
cursorp: &CursorPair,
) -> CCursor {
Expand All @@ -878,9 +857,9 @@ fn delete_paragraph_after_cursor(
prefer_next_row: false,
});
if min.ccursor == max.ccursor {
delete_next_char(text, prev_text, min.ccursor)
delete_next_char(text, min.ccursor)
} else {
delete_selected(text, prev_text, &CursorPair::two(min, max))
delete_selected(text, &CursorPair::two(min, max))
}
}

Expand All @@ -890,39 +869,38 @@ fn delete_paragraph_after_cursor(
fn on_key_press(
cursorp: &mut CursorPair,
text: &mut String,
prev_text: &mut String,
galley: &Galley,
key: Key,
modifiers: &Modifiers,
) -> Option<CCursorPair> {
match key {
Key::Backspace => {
let ccursor = if modifiers.mac_cmd {
delete_paragraph_before_cursor(text, prev_text, galley, cursorp)
delete_paragraph_before_cursor(text, galley, cursorp)
} else if let Some(cursor) = cursorp.single() {
if modifiers.alt || modifiers.ctrl {
// alt on mac, ctrl on windows
delete_previous_word(text, prev_text, cursor.ccursor)
delete_previous_word(text, cursor.ccursor)
} else {
delete_previous_char(text, prev_text, cursor.ccursor)
delete_previous_char(text, cursor.ccursor)
}
} else {
delete_selected(text, prev_text, cursorp)
delete_selected(text, cursorp)
};
Some(CCursorPair::one(ccursor))
}
Key::Delete => {
let ccursor = if modifiers.mac_cmd {
delete_paragraph_after_cursor(text, prev_text, galley, cursorp)
delete_paragraph_after_cursor(text, galley, cursorp)
} else if let Some(cursor) = cursorp.single() {
if modifiers.alt || modifiers.ctrl {
// alt on mac, ctrl on windows
delete_next_word(text, prev_text, cursor.ccursor)
delete_next_word(text, cursor.ccursor)
} else {
delete_next_char(text, prev_text, cursor.ccursor)
delete_next_char(text, cursor.ccursor)
}
} else {
delete_selected(text, prev_text, cursorp)
delete_selected(text, cursorp)
};
let ccursor = CCursor {
prefer_next_row: true,
Expand All @@ -938,20 +916,20 @@ fn on_key_press(
}

Key::K if modifiers.ctrl => {
let ccursor = delete_paragraph_after_cursor(text, prev_text, galley, cursorp);
let ccursor = delete_paragraph_after_cursor(text, galley, cursorp);
Some(CCursorPair::one(ccursor))
}

Key::U if modifiers.ctrl => {
let ccursor = delete_paragraph_before_cursor(text, prev_text, galley, cursorp);
let ccursor = delete_paragraph_before_cursor(text, galley, cursorp);
Some(CCursorPair::one(ccursor))
}

Key::W if modifiers.ctrl => {
let ccursor = if let Some(cursor) = cursorp.single() {
delete_previous_word(text, prev_text, cursor.ccursor)
delete_previous_word(text, cursor.ccursor)
} else {
delete_selected(text, prev_text, cursorp)
delete_selected(text, cursorp)
};
Some(CCursorPair::one(ccursor))
}
Expand Down

0 comments on commit 77b3a15

Please sign in to comment.