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 sdl2::rect::Point;
pub fn move_caret_right(file_editor: &mut FileEditor) {
let file: &EditorFile = match file_editor.file() {
None => return,
Some(f) => f,
};
let c: TextCharacter = match file.get_character_at(file_editor.caret().text_position() + 1) {
Some(text_character) => text_character,
None => return, // EOF
};
pub fn move_caret_right(file_editor: &mut FileEditor) -> Option<TextCharacter> {
let text_character: TextCharacter = file_editor
.file()
.map(|file| file.get_character_at(file_editor.caret().text_position() + 1))??;
let pos = file_editor.caret().position();
let d = c.dest().clone();
let p = pos.moved(1, 0, 0);
file_editor.caret_mut().move_caret(p, d.top_left());
let dest = text_character.dest().clone();
let new_pos = pos.moved(1, 0, 0);
file_editor.caret_mut().move_caret(new_pos, dest.top_left());
Some(text_character)
}
pub fn move_caret_left(file_editor: &mut FileEditor) {
let file: &EditorFile = match file_editor.file() {
None => return,
Some(f) => f,
};
pub fn move_caret_left(file_editor: &mut FileEditor) -> Option<TextCharacter> {
if file_editor.caret().text_position() == 0 {
return;
return None;
}
let text_character: TextCharacter =
match file.get_character_at(file_editor.caret().text_position() - 1) {
Some(text_character) => text_character,
None => return, // EOF
};
let text_character: TextCharacter = file_editor
.file()
.map(|file| file.get_character_at(file_editor.caret().text_position() - 1))??;
let pos = file_editor.caret().position();
let character_destination = text_character.dest().clone();
let p = pos.moved(-1, 0, 0);
file_editor
.caret_mut()
.move_caret(p, character_destination.top_left());
Some(text_character)
}
pub fn move_caret_down(file_editor: &mut FileEditor) {
let file: &EditorFile = match file_editor.file() {
None => return,
Some(f) => f,
};
pub fn move_caret_down(file_editor: &mut FileEditor) -> Option<TextCharacter> {
if file_editor.caret().text_position() == 0 {
return;
return None;
}
let current_line_number = file_editor.caret().line_number();
let mut next_line_position = 0;
let mut desired_line_position = 0;
let mut text_character: Option<&TextCharacter> = None;
for c in file.iter_char() {
match c.line() {
line if c.position() < file_editor.caret().text_position()
&& current_line_number == line =>
{
desired_line_position += 1;
}
line if line == current_line_number + 1 => {
text_character = Some(c);
if next_line_position == desired_line_position {
break;
let text_character = file_editor.file().map(|file| {
let mut desired_line_position = 0;
let mut text_character: Option<&TextCharacter> = None;
for c in file.iter_char() {
match c.line() {
line if c.position() < file_editor.caret().text_position()
&& current_line_number == line =>
{
desired_line_position += 1
}
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: &TextCharacter = match text_character {
Some(text_character) => text_character,
None => return, // EOF
};
let text_character = text_character?;
// let character_destination = text_character.dest().clone();
// let pos = text_character.position().clone();
Some(text_character.clone())
})??;
let character_destination = text_character.dest().clone();
let pos = text_character.position().clone();
file_editor.caret_mut().move_caret(
CaretPosition::new(pos, current_line_number + 1, next_line_position),
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)]
@ -147,7 +186,7 @@ mod test_move_right {
let config = support::build_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]
@ -161,7 +200,7 @@ mod test_move_right {
editor.prepare_ui(&mut renderer);
editor.move_caret(MoveDirection::Left);
assert_eq!(move_caret_right(&mut editor), ());
assert_eq!(move_caret_right(&mut editor).is_some(), false);
}
#[test]
@ -175,7 +214,7 @@ mod test_move_right {
editor.prepare_ui(&mut renderer);
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 mut editor = FileEditor::new(config);
assert_eq!(move_caret_left(&mut editor), ());
assert_eq!(move_caret_left(&mut editor).is_some(), false);
}
#[test]
@ -259,7 +298,7 @@ mod test_move_left {
editor.prepare_ui(&mut renderer);
editor.move_caret(MoveDirection::Right);
assert_eq!(move_caret_left(&mut editor), ());
assert_eq!(move_caret_left(&mut editor).is_some(), false);
}
#[test]
@ -273,6 +312,6 @@ mod test_move_left {
editor.prepare_ui(&mut renderer);
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 {
MoveDirection::Left => caret_manager::move_caret_left(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),
}
};
}
fn set_caret_to_end_of_line(&mut self, line: i32) {