-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move actions to motions folder
- Loading branch information
1 parent
47d768a
commit 510dffb
Showing
8 changed files
with
155 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod content_reader; | ||
pub mod motions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |