diff --git a/src/caret/movement.rs b/src/caret/movement.rs index c8d73594139d23e0c79ff8057fecf41c0bdc10d4..0fb1c5973a6a8552b64806bb825d390d985e9318 100644 --- a/src/caret/movement.rs +++ b/src/caret/movement.rs @@ -94,13 +94,13 @@ impl Editor { pub fn next_word(&self, n: usize, tight: bool) -> (usize, usize) { let next_n = self.next_word_forward(n); self.bound_hor((self.x() + (next_n), self.y()), tight) - } + } /// Get the position of the first word end right of the cursor (horizontally bounded) #[inline] pub fn next_word_end(&self, n: usize, tight: bool) -> (usize, usize) { let next_n = self.next_word_end_forward(n); self.bound_hor((self.x() + (next_n), self.y()), tight) - } + } /// Get the position of the character left to the cursor (horizontally bounded) #[inline] pub fn left(&self, n: usize) -> (usize, usize) { @@ -202,7 +202,7 @@ impl Editor { .chars() .skip(x) .enumerate() - { + { if current_char.is_whitespace() { has_ws = true; } else if has_ws && !current_char.is_whitespace() { @@ -210,14 +210,13 @@ impl Editor { if word_count < n_opt - 1 { has_ws = false; } else { - return i; + return i; } } } 0 } - /// Get beginning of next WORD forward /// "A WORD consists of a sequence of non-blank characters, separated with /// whitespace. An empty line is also considered to be a WORD." @@ -231,7 +230,7 @@ impl Editor { .chars() .skip(x) .enumerate() - { + { // if a word_char if !current_char.is_whitespace() { word_char = true; @@ -239,11 +238,11 @@ impl Editor { word_count += 1; } else if current_char.is_whitespace() { if word_char && word_count > n_opt { - return i - 1; + return i - 1; } word_char = false; } } last } -} \ No newline at end of file +} diff --git a/src/core/exec.rs b/src/core/exec.rs index bce3f59df874b33127c455730d9a1470c1ab526d..b392a1fb5870ef4272432f0804e01b24d05cbaa7 100644 --- a/src/core/exec.rs +++ b/src/core/exec.rs @@ -1,19 +1,19 @@ -use state::editor::Editor; -use io::parse::{Inst, Parameter}; -use state::mode::{CommandMode, Mode, PrimitiveMode}; +use core::prompt::PromptCommand; +use edit::buffer::TextBuffer; use edit::insert::{InsertMode, InsertOptions}; +use io::parse::{Inst, Parameter}; use io::redraw::RedrawTask; -use edit::buffer::TextBuffer; -use core::prompt::PromptCommand; +use state::editor::Editor; +use state::mode::{CommandMode, Mode, PrimitiveMode}; // TODO: Move the command definitions outta here impl Editor { /// Execute an instruction pub fn exec(&mut self, Inst(para, cmd): Inst) { use io::key::Key::*; + use state::mode::CommandMode::*; use state::mode::Mode::*; use state::mode::PrimitiveMode::*; - use state::mode::CommandMode::*; let n = para.d(); let bef = self.pos(); @@ -38,9 +38,11 @@ impl Editor { self.cursor_mut().mode = Mode::Command(CommandMode::Normal) } (_, Char(' ')) if self.key_state.alt => self.next_cursor(), - _ if self.key_state.alt => if let Some(m) = self.to_motion(Inst(para, cmd)) { - self.goto(m); - }, + _ if self.key_state.alt => { + if let Some(m) = self.to_motion(Inst(para, cmd)) { + self.goto(m); + } + } (Command(Normal), Char('i')) => { self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions { mode: InsertMode::Insert, @@ -183,9 +185,10 @@ impl Editor { let ins = self.get_inst(); if let Some(m) = self.to_motion_unbounded(ins) { self.remove_rb(m); - self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions { - mode: InsertMode::Insert, - })); + self.cursor_mut().mode = + Mode::Primitive(PrimitiveMode::Insert(InsertOptions { + mode: InsertMode::Insert, + })); } } (Command(Normal), Char('G')) => { @@ -268,10 +271,12 @@ impl Editor { (Command(Normal), Char('z')) => { let Inst(param, cmd) = self.get_inst(); match param { - Parameter::Null => if let Some(m) = self.to_motion(Inst(param, cmd)) { - self.buffers.current_buffer_info_mut().scroll_y = m.1; - self.goto(m); - }, + Parameter::Null => { + if let Some(m) = self.to_motion(Inst(param, cmd)) { + self.buffers.current_buffer_info_mut().scroll_y = m.1; + self.goto(m); + } + } Parameter::Int(n) => { self.buffers.current_buffer_info_mut().scroll_y = n; } @@ -285,12 +290,14 @@ impl Editor { (Command(Normal), Char('~')) => { self.invert_chars(n); } - (Command(Normal), Char('.')) => if let Some(inst) = self.previous_instruction { - self.exec(inst); - } else { - self.status_bar.msg = "No previous command".into(); - self.redraw_task = RedrawTask::StatusBar; - }, + (Command(Normal), Char('.')) => { + if let Some(inst) = self.previous_instruction { + self.exec(inst); + } else { + self.status_bar.msg = "No previous command".into(); + self.redraw_task = RedrawTask::StatusBar; + } + } (Command(Normal), Char(c)) => { self.status_bar.msg = format!("Unknown command: {}", c); self.redraw_task = RedrawTask::StatusBar; @@ -302,7 +309,8 @@ impl Editor { self.invoke(cmd); self.redraw_task = RedrawTask::StatusBar; } else { - self.status_bar.msg = format!("Unknown command: {}", self.prompt[self.prompt_index]); + self.status_bar.msg = + format!("Unknown command: {}", self.prompt[self.prompt_index]); } // If we use a command we used before, don't add a new line to the vec diff --git a/src/core/prompt.rs b/src/core/prompt.rs index 8f8c536a99fda1c30e309707ef1224cc52914c7e..e119ebee682838bfe36c5287e8dc544ce42a4441 100644 --- a/src/core/prompt.rs +++ b/src/core/prompt.rs @@ -1,7 +1,7 @@ +use edit::buffer::{SplitBuffer, TextBuffer}; use io::file::FileStatus; use io::redraw::RedrawTask; use state::editor::{Buffer, BufferManager, Editor}; -use edit::buffer::{SplitBuffer, TextBuffer}; use std::process::exit; @@ -158,15 +158,17 @@ impl Editor { self.buffers.switch_to(new_buffer_index); self.redraw_task = RedrawTask::Full; } - SwitchToBuffer { buffer_index: ix } => if !self.buffers.is_buffer_index_valid(ix) { - self.status_bar.msg = format!("Invalid buffer #{}", ix); - } else if self.buffers.current_buffer_index() == ix{ - self.status_bar.msg = format!("Already in buffer #{}", ix); - } else { - self.buffers.switch_to(ix); - self.redraw_task = RedrawTask::Full; - self.status_bar.msg = format!("Switched to buffer #{}", ix); - }, + SwitchToBuffer { buffer_index: ix } => { + if !self.buffers.is_buffer_index_valid(ix) { + self.status_bar.msg = format!("Invalid buffer #{}", ix); + } else if self.buffers.current_buffer_index() == ix { + self.status_bar.msg = format!("Already in buffer #{}", ix); + } else { + self.buffers.switch_to(ix); + self.redraw_task = RedrawTask::Full; + self.status_bar.msg = format!("Switched to buffer #{}", ix); + } + } CreateBuffer => { let new = self.buffers.new_buffer(Buffer::new()); self.buffers.switch_to(new); @@ -198,14 +200,14 @@ fn get_buffers_description(buffers: &BufferManager) -> String { } let descriptions = buffers - .iter() - // don't include transient buffers like the one - // this is going to be shown in - .filter(|b| !b.is_transient) - .enumerate() - .map(|(i, b)| print_buffer(i, b)) - .collect::<Vec<_>>() - .join("\n"); + .iter() + // don't include transient buffers like the one + // this is going to be shown in + .filter(|b| !b.is_transient) + .enumerate() + .map(|(i, b)| print_buffer(i, b)) + .collect::<Vec<_>>() + .join("\n"); format!( "Buffers\n=====================================\n\n{}", diff --git a/src/edit/buffer.rs b/src/edit/buffer.rs index d7714742c1a57861bd183a3a404202dbe5eea7e9..3f0e4ce1b61377f808faa3ce93df88d4956c9d83 100644 --- a/src/edit/buffer.rs +++ b/src/edit/buffer.rs @@ -66,7 +66,6 @@ pub trait TextBuffer<'a> { fn get_indent(&self, n: usize) -> &str; } - /// The buffer data structure, that Sodium is using. /// /// This structure consists of two "subbuffers", which are just vectors over lines (defined by @@ -78,7 +77,8 @@ pub trait TextBuffer<'a> { pub struct SplitBuffer { before: Vec<String>, after: Vec<String>, - #[cfg(debug)] _hinted_since_edit: bool, + #[cfg(debug)] + _hinted_since_edit: bool, } impl SplitBuffer { @@ -228,7 +228,6 @@ impl<'a> TextBuffer<'a> for SplitBuffer { } } - impl Index<usize> for SplitBuffer { type Output = String; diff --git a/src/edit/delete.rs b/src/edit/delete.rs index 80f29cbadade7330b3c98dc0ef78fa3d99cd48a9..3d3b4610f99e37bcbe220b27ea648d0695d8fe1c 100644 --- a/src/edit/delete.rs +++ b/src/edit/delete.rs @@ -1,7 +1,7 @@ use edit::buffer::TextBuffer; +use io::redraw::RedrawTask; use state::cursor::Cursor; use state::editor::Editor; -use io::redraw::RedrawTask; impl Editor { /// Delete a character. diff --git a/src/edit/insert.rs b/src/edit/insert.rs index 851b2bf551140716aebee57a2e1de3d8542d48fc..e4e1ba5f026ecdd52a50f4faac7be8fc8f701184 100644 --- a/src/edit/insert.rs +++ b/src/edit/insert.rs @@ -12,7 +12,6 @@ pub enum InsertMode { Replace, } - #[derive(Clone, PartialEq, Copy)] /// The insert options pub struct InsertOptions { diff --git a/src/edit/invert.rs b/src/edit/invert.rs index 58cf908ec975fd5e7142b08bc0bf6a226638628a..659878b9a2b0452746f8e99afbc73c8289347bbc 100644 --- a/src/edit/invert.rs +++ b/src/edit/invert.rs @@ -46,10 +46,12 @@ pub fn invert(c: char) -> char { '}' => '{', '!' => '?', '?' => '!', - a => if a.is_lowercase() { - a.to_uppercase().next().unwrap_or('?') - } else { - a.to_lowercase().next().unwrap_or('?') - }, + a => { + if a.is_lowercase() { + a.to_uppercase().next().unwrap_or('?') + } else { + a.to_lowercase().next().unwrap_or('?') + } + } } } diff --git a/src/io/file.rs b/src/io/file.rs index fb706499a47a295d40992d787319d0136153b930..d6e26c3e97742ea543767f1af4ece1366b1829e7 100644 --- a/src/io/file.rs +++ b/src/io/file.rs @@ -37,13 +37,14 @@ impl Editor { } /// Write the file. - pub fn write<'a> (&'a mut self, path: &'a str) -> FileStatus { + pub fn write<'a>(&'a mut self, path: &'a str) -> FileStatus { self.buffers.current_buffer_info_mut().title = Some(path.into()); if path == "" { return FileStatus::Other; } if let Some(mut file) = File::create(path).ok() { - if file.write(self.buffers.current_buffer().to_string().as_bytes()) + if file + .write(self.buffers.current_buffer().to_string().as_bytes()) .is_ok() { FileStatus::Ok diff --git a/src/io/graphics.rs b/src/io/graphics.rs index 06b549fa981d88acb9fd575188ceaa963efb8db2..6a8f3eba29854de153bf72ff846e77030911939f 100644 --- a/src/io/graphics.rs +++ b/src/io/graphics.rs @@ -31,8 +31,8 @@ impl Editor { 0 }; - let max_vert_chars = h/self.char_height - 2 - vert_offset; - let max_horz_chars = w/self.char_width - horz_offset; + let max_vert_chars = h / self.char_height - 2 - vert_offset; + let max_horz_chars = w / self.char_width - horz_offset; // Redraw window self.window.set(Color::rgb(25, 25, 25)); @@ -50,7 +50,8 @@ impl Editor { let (pos_x, pos_y) = self.pos(); - let (window_pos_x, window_pos_y) = self.coords_to_window_coords((pos_x, pos_y), max_horz_chars); + let (window_pos_x, window_pos_y) = + self.coords_to_window_coords((pos_x, pos_y), max_horz_chars); self.window.set(Color::rgb(25, 25, 25)); @@ -65,7 +66,7 @@ impl Editor { } self.window.rect( - ((window_pos_x + horz_offset) * self.char_width) as i32, + ((window_pos_x + horz_offset) * self.char_width) as i32, ((window_pos_y + vert_offset) * self.char_height) as i32, self.char_width as u32, self.char_height as u32, @@ -74,19 +75,24 @@ impl Editor { let mut string = false; - 'outer: for (y, row) in self.buffers.current_buffer().lines_from(scroll_y).enumerate() { + 'outer: for (y, row) in self + .buffers + .current_buffer() + .lines_from(scroll_y) + .enumerate() + { // Print line numbers if self.options.line_numbers { let mut line_number = scroll_y + y as usize + 1; // The amount of digits for this line number let mut digit_nr: usize = 0; while line_number >= 10usize.pow(digit_nr as u32) { - digit_nr += 1; + digit_nr += 1; } // Print the digits for this line number for i in 1..digit_nr + 1 { let digit = ((line_number % 10) as u8 + ('0' as u32) as u8) as char; - line_number = (line_number - line_number % 10)/10 as usize; + line_number = (line_number - line_number % 10) / 10 as usize; self.window.char( (self.char_width * (horz_offset - 1 - i)) as i32, (self.char_height * (scr_lines + vert_offset)) as i32, @@ -95,11 +101,17 @@ impl Editor { ); } } - for (x, c) in row.chars().flat_map(|c| if c == '\t' { - iter::repeat(' ').take(4) - } else { - iter::repeat(c).take(1) - }).enumerate() { + for (x, c) in row + .chars() + .flat_map(|c| { + if c == '\t' { + iter::repeat(' ').take(4) + } else { + iter::repeat(c).take(1) + } + }) + .enumerate() + { // New screen line if scr_chars >= max_horz_chars { scr_chars = 0; @@ -117,22 +129,8 @@ impl Editor { (226, 225, 167) //(167, 222, 156) } _ if string => (226, 225, 167), //(167, 222, 156) - '!' | - '@' | - '#' | - '$' | - '%' | - '^' | - '&' | - '|' | - '*' | - '+' | - '-' | - '/' | - ':' | - '=' | - '<' | - '>' => (198, 83, 83), //(228, 190, 175), //(194, 106, 71), + '!' | '@' | '#' | '$' | '%' | '^' | '&' | '|' | '*' | '+' | '-' | '/' + | ':' | '=' | '<' | '>' => (198, 83, 83), //(228, 190, 175), //(194, 106, 71), '.' | ',' => (241, 213, 226), '(' | ')' | '[' | ']' | '{' | '}' => (164, 212, 125), //(195, 139, 75), '0'..='9' => (209, 209, 177), @@ -170,7 +168,11 @@ impl Editor { self.window.sync(); } - fn coords_to_window_coords(&mut self, point: (usize, usize), max_horz_chars: usize) -> (usize, usize) { + fn coords_to_window_coords( + &mut self, + point: (usize, usize), + max_horz_chars: usize, + ) -> (usize, usize) { let (_, scroll_y) = { let current_buffer = self.buffers.current_buffer_info(); @@ -182,7 +184,12 @@ impl Editor { let mut ret_y = 0; let ret_x = point.0 % max_horz_chars; - for (y, row) in self.buffers.current_buffer().lines_from(scroll_y).enumerate() { + for (y, row) in self + .buffers + .current_buffer() + .lines_from(scroll_y) + .enumerate() + { if to_y > y { ret_y += row.len() / max_horz_chars + 1; } else { @@ -199,11 +206,8 @@ impl Editor { if self.buffers.current_buffer_info().scroll_y > 0 && pos_y <= self.buffers.current_buffer_info().scroll_y { - self.buffers.current_buffer_info_mut().scroll_y = if pos_y == 0 { - pos_y - } else { - pos_y - 1 - }; + self.buffers.current_buffer_info_mut().scroll_y = + if pos_y == 0 { pos_y } else { pos_y - 1 }; return; } @@ -211,7 +215,13 @@ impl Editor { let mut line_counter = 0; let mut result_y = 0; - for (y, row) in self.buffers.current_buffer().lines_from(pos_y+1).rev().enumerate() { + for (y, row) in self + .buffers + .current_buffer() + .lines_from(pos_y + 1) + .rev() + .enumerate() + { if pos_y - y < scroll_y { return; } @@ -264,7 +274,8 @@ impl Editor { let mode = self.cursor().mode; - let current_title = self.buffers + let current_title = self + .buffers .current_buffer_info() .title .as_ref() @@ -286,8 +297,9 @@ impl Editor { .collect::<Vec<_>>() } else { text.chars().collect() - }).into_iter() - .enumerate() + }) + .into_iter() + .enumerate() { self.window.char( ((w * a) / b) as i32 + (n as i32 * 8), diff --git a/src/io/key_state.rs b/src/io/key_state.rs index e0ec99761e40525039ef6b95a4a1c43cdb8d3e7a..7212662817cfe82b0c8f986d44158566d37757ce 100644 --- a/src/io/key_state.rs +++ b/src/io/key_state.rs @@ -3,10 +3,10 @@ use io::key::Key; #[cfg(feature = "orbital")] use orbclient::KeyEvent; -#[cfg(feature = "ansi")] -use std::io::Stdin; #[cfg(feature = "ansi")] use std::io::prelude::*; +#[cfg(feature = "ansi")] +use std::io::Stdin; /// Key state pub struct KeyState { diff --git a/src/io/mod.rs b/src/io/mod.rs index deb908b7f2f8df74f073386cfb10b76665f816f8..333ddd520e9b4986f6b01a2e1ccee537fd05e266 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -1,14 +1,14 @@ -/// Partial redraws. -pub mod redraw; -/// Parsing of input commands. -pub mod parse; +/// Loading and writing files. +pub mod file; +/// Graphics and rendering. +pub mod graphics; +/// Key input and parsing. +pub mod key; /// The "key state" of the editor. /// /// The key state contains information about the current state of modifiers. pub mod key_state; -/// Key input and parsing. -pub mod key; -/// Graphics and rendering. -pub mod graphics; -/// Loading and writing files. -pub mod file; +/// Parsing of input commands. +pub mod parse; +/// Partial redraws. +pub mod redraw; diff --git a/src/io/parse.rs b/src/io/parse.rs index af06f62e7157cf7523691d55c640c2873584c5ab..e4ad37cdf7e482eefb545ea8323163aeeef6fb72 100644 --- a/src/io/parse.rs +++ b/src/io/parse.rs @@ -46,11 +46,13 @@ impl Editor { loop { for event in self.window.events() { match event.to_option() { - EventOption::Key(k) => if let Some(Key::Char(c)) = self.key_state.feed(k) { - self.status_bar.cmd.push(c); - self.redraw_task = RedrawTask::StatusBar; - return c; - }, + EventOption::Key(k) => { + if let Some(Key::Char(c)) = self.key_state.feed(k) { + self.status_bar.cmd.push(c); + self.redraw_task = RedrawTask::StatusBar; + return c; + } + } _ => {} } } diff --git a/src/main.rs b/src/main.rs index d63b9b59cdd6d45376dd99efbf880f1da9c2158c..114e2b998ea74fd2fac7eef382b0cdea7fd11ccb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,6 @@ pub mod io; /// State of the editor. pub mod state; - fn main() { self::state::editor::Editor::init(); } diff --git a/src/state/editor.rs b/src/state/editor.rs index 7ea4dbd795a8883bad539e38f7830feb7e1cfe58..7b7040aaa085be5b767904a91574bce89a064a69 100644 --- a/src/state/editor.rs +++ b/src/state/editor.rs @@ -1,12 +1,12 @@ -use std::slice::Iter; use edit::buffer::{SplitBuffer, TextBuffer}; -use state::cursor::Cursor; use io::graphics::StatusBar; use io::key::{Cmd, Key}; use io::key_state::KeyState; -use state::options::Options; use io::parse::Inst; use io::redraw::RedrawTask; +use state::cursor::Cursor; +use state::options::Options; +use std::slice::Iter; #[cfg(feature = "orbital")] use orbclient::Window; diff --git a/src/state/mod.rs b/src/state/mod.rs index 44bca2586829cec522f8c347cbb36707a6c2de6e..9ffec89ee389be4ff43c1bee853bd6d7b01ce130 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -1,11 +1,11 @@ -/// The global editor state. -pub mod editor; /// Cursors. /// /// A cursor contains various non-global information about the editor state. You can switch between /// cursor, for reusing older editor states. pub mod cursor; -/// Options and configuration of the editor. -pub mod options; +/// The global editor state. +pub mod editor; /// Editor modes. pub mod mode; +/// Options and configuration of the editor. +pub mod options; diff --git a/src/state/mode.rs b/src/state/mode.rs index 2ff73924f4b1f4aaa4503d6f48b71a11fee0d692..5f70c3061ae387bfa1161cf5e20143702e57f86c 100644 --- a/src/state/mode.rs +++ b/src/state/mode.rs @@ -17,9 +17,9 @@ pub enum Mode { impl Mode { /// Convert the mode to string pub fn to_string(self) -> &'static str { + use self::CommandMode::*; use self::Mode::*; use self::PrimitiveMode::*; - use self::CommandMode::*; match self { Command(Normal) => "Normal", Primitive(Insert(_)) => "Insert",