1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
use super::TextLine;
#[derive(Copy, Clone, PartialEq, Eq)]
enum CharKind {
Whitespace,
Alphanumeric,
Other,
}
impl CharKind {
fn of(c: char) -> Self {
use CharKind::*;
if c.is_whitespace() {
Whitespace
} else if c.is_alphanumeric() {
Alphanumeric
} else {
Other
}
}
}
impl TextLine {
/// Returns the appropriate movement function based on parameters `forward` and `skip`.
pub fn cursor_movement(forward: bool, skip: bool) -> fn(&TextLine, &mut usize) {
match (forward, skip) {
(false, false) => Self::back,
(false, true) => Self::skip_back,
(true, false) => Self::forward,
(true, true) => Self::skip_forward,
}
}
/// Moves the text cursor forward by one.
pub fn forward(&self, text_cursor: &mut usize) {
if *text_cursor < self.len() {
*text_cursor += 1;
}
}
/// Moves the text cursor back by one.
pub fn back(&self, text_cursor: &mut usize) {
if *text_cursor > 0 {
*text_cursor -= 1;
}
}
/// Moves the text cursor forward until the end of the current word.
pub fn skip_forward(&self, text_cursor: &mut usize) {
let len = self.len();
let start_kind = loop {
if *text_cursor == len {
return;
}
match CharKind::of(self.char_at(*text_cursor)) {
CharKind::Whitespace => (),
kind => break kind,
}
};
loop {
*text_cursor += 1;
if *text_cursor == len {
return;
}
if CharKind::of(self.char_at(*text_cursor)) == start_kind {
return;
}
}
}
/// Moves the text cursor back until the start of the current word.
pub fn skip_back(&self, text_cursor: &mut usize) {
if *text_cursor == 0 {
return;
}
let start_kind = loop {
*text_cursor -= 1;
if *text_cursor == 0 {
return;
}
match CharKind::of(self.char_at(*text_cursor)) {
CharKind::Whitespace => (),
kind => break kind,
}
};
loop {
*text_cursor -= 1;
if CharKind::of(self.char_at(*text_cursor)) == start_kind {
*text_cursor += 1;
return;
}
if *text_cursor == 0 {
return;
}
}
}
}
/*
pub fn text_input(&mut self, character: char, skip: bool) {
fn edit_string(
back_multi: bool,
text: &mut TextLine,
index: &mut usize,
character: char,
) -> bool {
let whitespace = character.is_whitespace();
let control = character.is_control();
if !whitespace && !control {
text.insert(*index, character);
*index += 1;
} else if whitespace && character != '\t' {
if *index != 0 && text.char_at(*index - 1) != ' ' {
if *index == text.len() || text.char_at(*index) != ' ' {
text.insert(*index, ' ');
}
*index += 1;
}
} else if character == '\x08' && *index > 0 {
if back_multi {
while *index > 0 {
*index -= 1;
let c = text.remove(*index);
if !c.is_alphanumeric() {
break;
}
}
} else {
*index -= 1;
text.remove(*index);
}
if *index == 0 && text.len() > 0 && text.char_at(0).is_whitespace() {
text.remove(0);
}
} else if character == '\x7F' && *index < text.len() {
if back_multi {
while *index < text.len() {
let c = text.remove(*index);
if !c.is_alphanumeric() {
break;
}
}
} else {
text.remove(*index);
}
} else {
return false;
}
true
}
if character == '\n' || character == '\r' {
return;
}
if character == '\x08' || character == '\x7F' || !skip {
for &selected in &self.selection {
let selected_info = &mut self.graph.nodes_mut(selected.kind)[selected.index];
edit_string(
skip,
&mut selected_info.name,
&mut selected_info.name_cursor,
character,
);
}
}
}
*/