Move caret top

This commit is contained in:
Adrian Wozniak 2019-05-20 18:52:41 +02:00
parent efcdcac1fb
commit 3daf214a3f
2 changed files with 100 additions and 61 deletions

View File

@ -1,84 +1,123 @@
use crate::ui::*; use crate::ui::*;
use sdl2::rect::Point;
pub fn move_caret_right(file_editor: &mut FileEditor) { pub fn move_caret_right(file_editor: &mut FileEditor) -> Option<TextCharacter> {
let file: &EditorFile = match file_editor.file() { let text_character: TextCharacter = file_editor
None => return, .file()
Some(f) => f, .map(|file| file.get_character_at(file_editor.caret().text_position() + 1))??;
};
let c: TextCharacter = match file.get_character_at(file_editor.caret().text_position() + 1) {
Some(text_character) => text_character,
None => return, // EOF
};
let pos = file_editor.caret().position(); let pos = file_editor.caret().position();
let d = c.dest().clone(); let dest = text_character.dest().clone();
let p = pos.moved(1, 0, 0); let new_pos = pos.moved(1, 0, 0);
file_editor.caret_mut().move_caret(p, d.top_left()); file_editor.caret_mut().move_caret(new_pos, dest.top_left());
Some(text_character)
} }
pub fn move_caret_left(file_editor: &mut FileEditor) { pub fn move_caret_left(file_editor: &mut FileEditor) -> Option<TextCharacter> {
let file: &EditorFile = match file_editor.file() {
None => return,
Some(f) => f,
};
if file_editor.caret().text_position() == 0 { if file_editor.caret().text_position() == 0 {
return; return None;
} }
let text_character: TextCharacter = let text_character: TextCharacter = file_editor
match file.get_character_at(file_editor.caret().text_position() - 1) { .file()
Some(text_character) => text_character, .map(|file| file.get_character_at(file_editor.caret().text_position() - 1))??;
None => return, // EOF
};
let pos = file_editor.caret().position(); let pos = file_editor.caret().position();
let character_destination = text_character.dest().clone(); let character_destination = text_character.dest().clone();
let p = pos.moved(-1, 0, 0); let p = pos.moved(-1, 0, 0);
file_editor file_editor
.caret_mut() .caret_mut()
.move_caret(p, character_destination.top_left()); .move_caret(p, character_destination.top_left());
Some(text_character)
} }
pub fn move_caret_down(file_editor: &mut FileEditor) { pub fn move_caret_down(file_editor: &mut FileEditor) -> Option<TextCharacter> {
let file: &EditorFile = match file_editor.file() {
None => return,
Some(f) => f,
};
if file_editor.caret().text_position() == 0 { if file_editor.caret().text_position() == 0 {
return; return None;
} }
let current_line_number = file_editor.caret().line_number(); let current_line_number = file_editor.caret().line_number();
let mut next_line_position = 0; let mut next_line_position = 0;
let mut desired_line_position = 0; let text_character = file_editor.file().map(|file| {
let mut text_character: Option<&TextCharacter> = None; let mut desired_line_position = 0;
for c in file.iter_char() { let mut text_character: Option<&TextCharacter> = None;
match c.line() { for c in file.iter_char() {
line if c.position() < file_editor.caret().text_position() match c.line() {
&& current_line_number == line => line if c.position() < file_editor.caret().text_position()
{ && current_line_number == line =>
desired_line_position += 1; {
} desired_line_position += 1
line if line == current_line_number + 1 => {
text_character = Some(c);
if next_line_position == desired_line_position {
break;
} }
next_line_position += 1; line if line == current_line_number + 1 => {
text_character = Some(c);
if next_line_position == desired_line_position {
break;
}
next_line_position += 1;
}
line if line == current_line_number + 2 => break,
_ => {}
} }
line if line == current_line_number + 2 => {
break;
}
_ => {}
} }
} let text_character = text_character?;
let text_character: &TextCharacter = match text_character { // let character_destination = text_character.dest().clone();
Some(text_character) => text_character, // let pos = text_character.position().clone();
None => return, // EOF Some(text_character.clone())
}; })??;
let character_destination = text_character.dest().clone(); let character_destination = text_character.dest().clone();
let pos = text_character.position().clone(); let pos = text_character.position().clone();
file_editor.caret_mut().move_caret( file_editor.caret_mut().move_caret(
CaretPosition::new(pos, current_line_number + 1, next_line_position), CaretPosition::new(pos, current_line_number + 1, next_line_position),
character_destination.top_left(), character_destination.top_left(),
); );
Some(text_character.clone())
}
pub fn move_caret_up(file_editor: &mut FileEditor) -> Option<TextCharacter> {
if file_editor.caret().text_position() == 0 {
return None;
}
let current_line_number = file_editor.caret().line_number();
if current_line_number == 0 {
return None;
}
let mut desired_line_position = 0;
let text_character: TextCharacter = file_editor.file().map(|file| {
let mut prev_line = vec![];
let mut found = false;
for c in file.iter_char() {
match c.line() {
line if c.position() < file_editor.caret().text_position()
&& current_line_number == line
&& !found =>
{
desired_line_position += 1;
}
line if line == current_line_number
&& c.position() == file_editor.caret().text_position() =>
{
found = true
}
line if line == current_line_number - 1 => prev_line.push(c),
line if line == current_line_number + 1 => break,
_ => {}
}
}
prev_line
.get(desired_line_position as usize)
.cloned()
.or_else(|| {
desired_line_position = 0;
prev_line.first().cloned()
})
.cloned()
})??;
let character_destination = text_character.dest().clone();
let pos = text_character.position().clone();
file_editor.caret_mut().move_caret(
CaretPosition::new(pos, text_character.line(), desired_line_position),
character_destination.top_left(),
);
Some(text_character)
} }
#[cfg(test)] #[cfg(test)]
@ -147,7 +186,7 @@ mod test_move_right {
let config = support::build_config(); let config = support::build_config();
let mut editor = FileEditor::new(config); let mut editor = FileEditor::new(config);
assert_eq!(move_caret_right(&mut editor), ()); assert_eq!(move_caret_right(&mut editor).is_some(), false);
} }
#[test] #[test]
@ -161,7 +200,7 @@ mod test_move_right {
editor.prepare_ui(&mut renderer); editor.prepare_ui(&mut renderer);
editor.move_caret(MoveDirection::Left); editor.move_caret(MoveDirection::Left);
assert_eq!(move_caret_right(&mut editor), ()); assert_eq!(move_caret_right(&mut editor).is_some(), false);
} }
#[test] #[test]
@ -175,7 +214,7 @@ mod test_move_right {
editor.prepare_ui(&mut renderer); editor.prepare_ui(&mut renderer);
editor.move_caret(MoveDirection::Left); editor.move_caret(MoveDirection::Left);
assert_eq!(move_caret_right(&mut editor), ()); assert_eq!(move_caret_right(&mut editor).is_some(), true);
} }
} }
@ -245,7 +284,7 @@ mod test_move_left {
let config = support::build_config(); let config = support::build_config();
let mut editor = FileEditor::new(config); let mut editor = FileEditor::new(config);
assert_eq!(move_caret_left(&mut editor), ()); assert_eq!(move_caret_left(&mut editor).is_some(), false);
} }
#[test] #[test]
@ -259,7 +298,7 @@ mod test_move_left {
editor.prepare_ui(&mut renderer); editor.prepare_ui(&mut renderer);
editor.move_caret(MoveDirection::Right); editor.move_caret(MoveDirection::Right);
assert_eq!(move_caret_left(&mut editor), ()); assert_eq!(move_caret_left(&mut editor).is_some(), false);
} }
#[test] #[test]
@ -273,6 +312,6 @@ mod test_move_left {
editor.prepare_ui(&mut renderer); editor.prepare_ui(&mut renderer);
editor.move_caret(MoveDirection::Right); editor.move_caret(MoveDirection::Right);
assert_eq!(move_caret_left(&mut editor), ()); assert_eq!(move_caret_left(&mut editor).is_some(), true);
} }
} }

View File

@ -220,9 +220,9 @@ impl CaretAccess for FileEditor {
match dir { match dir {
MoveDirection::Left => caret_manager::move_caret_left(self), MoveDirection::Left => caret_manager::move_caret_left(self),
MoveDirection::Right => caret_manager::move_caret_right(self), MoveDirection::Right => caret_manager::move_caret_right(self),
MoveDirection::Up => {} MoveDirection::Up => caret_manager::move_caret_up(self),
MoveDirection::Down => caret_manager::move_caret_down(self), MoveDirection::Down => caret_manager::move_caret_down(self),
} };
} }
fn set_caret_to_end_of_line(&mut self, line: i32) { fn set_caret_to_end_of_line(&mut self, line: i32) {