Skip to content

Commit

Permalink
refactor: move actions to motions folder
Browse files Browse the repository at this point in the history
  • Loading branch information
metiftikci committed Oct 17, 2023
1 parent 47d768a commit 510dffb
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 95 deletions.
1 change: 0 additions & 1 deletion src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod actions;
pub mod maps;
pub mod mode;
pub mod movement;
Expand Down
1 change: 0 additions & 1 deletion src/buffer/actions.rs

This file was deleted.

88 changes: 0 additions & 88 deletions src/buffer/actions/find_next_word_position.rs

This file was deleted.

8 changes: 3 additions & 5 deletions src/buffer/maps.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::collections::HashMap;

use crate::buffer::actions::find_next_word_position::find_next_word_position;

use super::ActionMap;
use crate::{buffer::ActionMap, motions::motions::get_word_end_position};

pub fn get_default_insert_maps() -> ActionMap {
let mut map: ActionMap = HashMap::new();
Expand Down Expand Up @@ -52,9 +50,9 @@ pub fn get_default_normal_maps() -> ActionMap {
editor.get_active_buffer_mut().move_last_column()
});

map.insert("w", |editor| {
map.insert("e", |editor| {
let buffer = editor.get_active_buffer_mut();
let new_position = find_next_word_position(buffer);
let new_position = get_word_end_position(buffer);
buffer.move_cursor(new_position.y, new_position.x);
});

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod buffer;
pub mod commands;
pub mod core;
pub mod editor;
pub mod motions;
pub mod screen;
pub mod tab;
pub mod terminal;
Expand Down
118 changes: 118 additions & 0 deletions src/motions/content_reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
use crate::core::Point;

#[derive(Clone, Eq, PartialEq)]
pub enum CharType {
Whitespace,
Word,
Special,
}

#[derive(Eq, PartialEq)]
pub enum Direction {
Backward,
Forward,
}

pub struct ContentReader<'a> {
pub lines: Vec<&'a str>,
pub position: Point<usize>,
}

impl<'a> ContentReader<'a> {
pub fn move_backward(&mut self) -> bool {
if self.is_file_first_char() {
return false;
}

if self.is_line_first_char() {
self.position.y -= 1;
self.position.x = self.lines.get(self.position.y).unwrap().len() - 1;
} else {
self.position.x -= 1;
}

true
}

pub fn move_forward(&mut self) -> bool {
if self.is_file_last_char() {
return false;
}

if self.is_line_last_char() {
self.position.y += 1;
self.position.x = 0;
} else {
self.position.x += 1;
}

true
}

pub fn is_file_first_char(&self) -> bool {
self.position.x == 0 && self.position.y == 0
}

pub fn is_file_last_char(&self) -> bool {
if self.position.y != self.lines.len() - 1 {
return false;
}

self.is_line_last_char()
}

pub fn is_line_first_char(&self) -> bool {
self.position.x == 0
}

pub fn is_line_last_char(&self) -> bool {
let line = self.lines.get(self.position.y).unwrap();

line.len() == 0 || self.position.x == line.len() - 1
}

pub fn get_char(&self) -> Option<char> {
self.lines
.get(self.position.y)
.unwrap()
.chars()
.nth(self.position.x)
}

pub fn get_char_type(&self) -> CharType {
let ch = self.get_char().unwrap_or(' ');

if ch.is_whitespace() {
CharType::Whitespace
} else if ch.is_alphanumeric() {
CharType::Word
} else {
CharType::Special
}
}

pub fn move_while(&mut self, char_type: CharType, direction: Direction) {
while self.get_char_type() == char_type {
match direction {
Direction::Backward => {
if self.is_file_first_char() {
break;
}
if char_type != CharType::Whitespace && self.is_line_first_char() {
break;
}
self.move_backward();
}
Direction::Forward => {
if self.is_file_last_char() {
break;
}
if char_type != CharType::Whitespace && self.is_line_last_char() {
break;
}
self.move_forward();
}
};
}
}
}
2 changes: 2 additions & 0 deletions src/motions/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod content_reader;
pub mod motions;
31 changes: 31 additions & 0 deletions src/motions/motions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::{
buffer::Buffer,
core::Point,
motions::content_reader::{CharType, ContentReader, Direction},
};

pub fn get_word_end_position(buffer: &mut Buffer) -> Point<usize> {
let mut lines: Vec<&str> = vec![];

for line in buffer.lines.iter() {
lines.push(line.as_str());
}

let mut reader = ContentReader {
position: buffer.cursor.clone(),
lines,
};

reader.move_forward();
reader.move_while(CharType::Whitespace, Direction::Forward);

if !reader.is_file_last_char() {
let char_type = reader.get_char_type();
reader.move_while(char_type.clone(), Direction::Forward);
if reader.get_char_type() != char_type {
reader.move_backward();
}
}

reader.position
}

0 comments on commit 510dffb

Please sign in to comment.