Skip to content

Commit

Permalink
slightly change code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-streltsov committed Feb 26, 2024
1 parent 18fc687 commit 93c57a4
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 25 deletions.
52 changes: 29 additions & 23 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ impl Document {
file_type
})
}

pub fn row(&self, index: usize) -> Option<&Row> {
self.rows.get(index)
}

pub fn is_empty(&self) -> bool {
self.rows.is_empty()
}

pub fn len(&self) -> usize {
self.rows.len()
}

pub fn insert(&mut self, at: &Position, c: char) {
if at.y > self.rows.len() {
return;
Expand All @@ -56,21 +60,27 @@ impl Document {
self.unhighlight_rows(at.y);
}

fn unhighlight_rows(&mut self, start: usize) {
let start = start.saturating_sub(1);
for row in self.rows.iter_mut().skip(start) {
row.is_highlighted = false;
fn insert_newline(&mut self, at: &Position) {
if at.y > self.rows.len() {
return;
}
if at.y == self.rows.len() {
self.rows.push(Row::default());
return;
}
#[allow(clippy::indexing_slicing)]
let current_row = &mut self.rows[at.y];
let new_row = current_row.split(at.x);
self.rows.insert(at.y + 1, new_row);
}

#[allow(clippy::indexing_slicing)]
pub fn delete(&mut self, at: &Position) {
let len = self.rows.len();
if at.y >= len {
return;
}
self.dirty = true;

if at.x == self.rows[at.y].len() && at.y + 1 < len {
let next_row = self.rows.remove(at.y + 1);
let row = &mut self.rows[at.y];
Expand All @@ -81,19 +91,7 @@ impl Document {
}
self.unhighlight_rows(at.y)
}
fn insert_newline(&mut self, at: &Position) {
if at.y > self.rows.len() {
return;
}
if at.y == self.rows.len() {
self.rows.push(Row::default());
return;
}
#[allow(clippy::indexing_slicing)]
let current_row = &mut self.rows[at.y];
let new_row = current_row.split(at.x);
self.rows.insert(at.y + 1, new_row);
}

pub fn save(&mut self) -> Result<(), Error> {
if let Some(file_name) = &self.file_name {
let mut file = fs::File::create(file_name)?;
Expand All @@ -106,28 +104,27 @@ impl Document {
}
Ok(())
}

pub fn is_dirty(&self) -> bool {
self.dirty
}

#[allow(clippy::indexing_slicing)]
pub fn find(&self, query: &str, at: &Position, direction: SearchDirection) -> Option<Position> {
if at.y >= self.rows.len() {
return None;
}
let mut position = Position{x: at.x, y: at.y};

let mut position = Position { x: at.x, y: at.y };
let start = if direction == SearchDirection::Forward {
at.y
} else {
0
};

let end = if direction == SearchDirection::Forward {
self.rows.len()
} else {
at.y.saturating_add(1)
};

for _ in start..end {
if let Some(row) = self.rows.get(position.y) {
if let Some(x) = row.find(&query, position.x, direction) {
Expand All @@ -147,6 +144,7 @@ impl Document {
}
None
}

pub fn highlight(&mut self, word: &Option<String>, until: Option<usize>) {
let mut start_with_comment = false;
let until = if let Some(until) = until {
Expand All @@ -163,6 +161,14 @@ impl Document {
start_with_comment = row.highlight(&self.file_type.highlighting_options(), word, start_with_comment);
}
}

fn unhighlight_rows(&mut self, start: usize) {
let start = start.saturating_sub(1);
for row in self.rows.iter_mut().skip(start) {
row.is_highlighted = false;
}
}

pub fn file_type(&self) -> String {
self.file_type.name()
}
Expand Down
12 changes: 12 additions & 0 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl Editor {
}
}
}

pub fn default() -> Self {
let args: Vec<String> = env::args().collect();
let mut initial_status = String::from("HELP: Ctrl-F = find | Ctrl-S = save | Ctrl-Q = quit");
Expand Down Expand Up @@ -112,6 +113,7 @@ impl Editor {
Terminal::cursor_show();
Terminal::flush()
}

fn process_keypress(&mut self) -> Result<(), std::io::Error> {
let pressed_key = Terminal::read_key()?;
match pressed_key {
Expand Down Expand Up @@ -156,13 +158,15 @@ impl Editor {
}
Ok(())
}

pub fn draw_row(&self, row: &Row) {
let width = self.terminal.size().width as usize;
let start = self.offset.x;
let end = self.offset.x.saturating_add(width);
let row = row.render(start, end);
println!("{}\r", row)
}

#[allow(clippy::integer_division)]
fn draw_rows(&self) {
let height = self.terminal.size().height;
Expand All @@ -180,6 +184,7 @@ impl Editor {
}
}
}

fn draw_welcome_message(&self) {
let mut welcome_message = format!("Artem's editor -- version {}\r", VERSION);
let width = self.terminal.size().width as usize;
Expand All @@ -191,6 +196,7 @@ impl Editor {
welcome_message.truncate(width);
println!("{}\r", welcome_message);
}

fn move_cursor(&mut self, key: Key) {
let terminal_height = self.terminal.size().height as usize;
let Position{ mut x, mut y } = self.cursor_position;
Expand Down Expand Up @@ -258,6 +264,7 @@ impl Editor {

self.cursor_position = Position{ x, y }
}

fn scroll(&mut self) {
let Position {x, y} = self.cursor_position;
let width = self.terminal.size().width as usize;
Expand All @@ -276,6 +283,7 @@ impl Editor {
offset.x = x.saturating_sub(width).saturating_add(1);
}
}

fn draw_status_bar(&self) {
let mut status;
let width = self.terminal.size().width as usize;
Expand Down Expand Up @@ -312,6 +320,7 @@ impl Editor {
Terminal::reset_fg_color();
Terminal::reset_bg_color();
}

fn draw_message_bar(&self) {
Terminal::clear_current_line();
let message = &self.status_message;
Expand All @@ -321,6 +330,7 @@ impl Editor {
print!("{}", text);
}
}

fn prompt<C>(&mut self, prompt: &str, mut callback: C) -> Result<Option<String>, std::io::Error>
where
C: FnMut(&mut Self, Key, &String),
Expand Down Expand Up @@ -352,6 +362,7 @@ impl Editor {
}
Ok(Some(result))
}

fn save(&mut self) {
if self.document.file_name.is_none() {
let new_name = self.prompt("Save as: ", |_, _, _| {}).unwrap_or(None);
Expand All @@ -368,6 +379,7 @@ impl Editor {
self.status_message = StatusMessage::from("Error writing file!".to_string());
}
}

fn search(&mut self) {
let old_position = self.cursor_position.clone();
let mut direction = SearchDirection::Forward;
Expand Down
8 changes: 8 additions & 0 deletions src/filetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ impl FileType {
pub fn name(&self) -> String {
self.name.clone()
}

pub fn highlighting_options(&self) -> &HighlightingOptions {
&self.hl_opts
}

pub fn from(file_name: &str) -> Self {
if file_name.ends_with(".rs") {
return Self {
Expand Down Expand Up @@ -120,21 +122,27 @@ impl HighlightingOptions {
pub fn numbers(&self) -> bool {
self.numbers
}

pub fn strings(&self) -> bool {
self.strings
}

pub fn characters(&self) -> bool {
self.characters
}

pub fn comments(&self) -> bool {
self.comments
}

pub fn primary_keywords(&self) -> &Vec<String> {
&self.primary_keywords
}

pub fn secondary_keywords(&self) -> &Vec<String> {
&self.secondary_keywords
}

pub fn multiline_comments(&self) -> bool {
self.multiline_comments
}
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
clippy::wildcard_enum_match_arm,
clippy::else_if_without_else
)]

mod document;
mod editor;
mod filetype;
mod row;
mod terminal;
mod highlighting;

use editor::Editor;
pub use document::Document;
pub use row::Row;
pub use editor::SearchDirection;
use editor::Editor;
pub use filetype::FileType;
pub use filetype::HighlightingOptions;
pub use terminal::Terminal;
Expand Down
9 changes: 9 additions & 0 deletions src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ impl Row {
result.push_str(&end_highlight[..]);
result
}

pub fn len(&self) -> usize {
self.len
}

pub fn is_empty(&self) -> bool {
self.len == 0
}

pub fn insert(&mut self, at: usize, c: char) {
if at >= self.len() {
self.string.push(c);
Expand All @@ -77,6 +80,7 @@ impl Row {
self.len = length;
self.string = result;
}

pub fn delete(&mut self, at: usize) {
if at >= self.len() {
return;
Expand All @@ -92,10 +96,12 @@ impl Row {
self.len = length;
self.string = result;
}

pub fn append(&mut self, new: &Self) {
self.string = format!("{}{}", self.string, new.string);
self.len += new.len;
}

pub fn split(&mut self, at: usize) -> Self {
let mut row: String = String::new();
let mut length = 0;
Expand All @@ -120,9 +126,11 @@ impl Row {
is_highlighted: false
}
}

pub fn as_bytes(&self) -> &[u8] {
self.string.as_bytes()
}

pub fn find(&self, query: &str, at: usize, direction: SearchDirection) -> Option<usize> {
if at > self.len || query.is_empty() {
return None;
Expand Down Expand Up @@ -230,6 +238,7 @@ impl Row {
highlighting::Type::PrimaryKeywords,
)
}

fn highlight_secondary_keywords(&mut self, index: &mut usize, opts: &HighlightingOptions, chars: &[char]) -> bool {
self.highlight_keywords(
index,
Expand Down
Loading

0 comments on commit 93c57a4

Please sign in to comment.