diff --git a/src/buffer/mod.rs b/src/buffer/mod.rs index 41ae239..9f85061 100644 --- a/src/buffer/mod.rs +++ b/src/buffer/mod.rs @@ -1,16 +1,16 @@ #![allow(dead_code)] -use std::path::{Path, PathBuf}; use std::fs::File; use std::io; use std::io::{BufReader, BufWriter, Write}; +use std::path::{Path, PathBuf}; +use self::undo_stack::Operation::*; +use self::undo_stack::UndoStack; use ropey; use ropey::{Rope, RopeSlice}; -use self::undo_stack::UndoStack; -use self::undo_stack::Operation::*; use string_utils::char_count; -use utils::{prev_grapheme_boundary, next_grapheme_boundary, is_grapheme_boundary, RopeGraphemes}; +use utils::{is_grapheme_boundary, next_grapheme_boundary, prev_grapheme_boundary, RopeGraphemes}; mod undo_stack; diff --git a/src/editor/cursor.rs b/src/editor/cursor.rs index 593670d..9b41a09 100644 --- a/src/editor/cursor.rs +++ b/src/editor/cursor.rs @@ -1,8 +1,8 @@ #![allow(dead_code)] -use std::slice::{Iter, IterMut}; -use std::ops::{Index, IndexMut}; use std::cmp::Ordering; +use std::ops::{Index, IndexMut}; +use std::slice::{Iter, IterMut}; use buffer::Buffer; use formatter::LineFormatter; diff --git a/src/editor/mod.rs b/src/editor/mod.rs index 7c4016b..303997f 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -2,14 +2,14 @@ use std::collections::HashMap; +use self::cursor::CursorSet; use buffer::Buffer; use formatter::LineFormatter; use formatter::RoundingBehavior::*; -use std::path::{Path, PathBuf}; use std::cmp::{max, min}; +use std::path::{Path, PathBuf}; use string_utils::{char_count, rope_slice_to_line_ending, LineEnding}; use utils::{digit_count, RopeGraphemes}; -use self::cursor::CursorSet; mod cursor; @@ -378,7 +378,7 @@ impl Editor { // Insert spaces let space_strs = [ - "", " ", " ", " ", " ", " ", " ", " ", " " + "", " ", " ", " ", " ", " ", " ", " ", " ", ]; self.buffer.insert_text(space_strs[space_count], c.range.0); self.dirty = true; diff --git a/src/formatter.rs b/src/formatter.rs index 3de0e2d..cb594b8 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -1,8 +1,8 @@ #![allow(dead_code)] -use std::cmp::min; -use ropey::RopeSlice; use buffer::Buffer; +use ropey::RopeSlice; +use std::cmp::min; use utils::RopeGraphemes; // Maximum graphemes in a line before a soft line break is forced. diff --git a/src/main.rs b/src/main.rs index 1bb760b..fdf14a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,18 +8,18 @@ extern crate termion; extern crate unicode_segmentation; extern crate unicode_width; -use std::path::Path; use docopt::Docopt; use editor::Editor; -use term_ui::TermUI; +use std::path::Path; use term_ui::formatter::ConsoleLineFormatter; +use term_ui::TermUI; -mod string_utils; -mod utils; mod buffer; -mod formatter; mod editor; +mod formatter; +mod string_utils; mod term_ui; +mod utils; // Usage documentation string static USAGE: &'static str = " diff --git a/src/string_utils.rs b/src/string_utils.rs index cfd6517..d2d69e7 100644 --- a/src/string_utils.rs +++ b/src/string_utils.rs @@ -1,8 +1,8 @@ #![allow(dead_code)] //! Misc helpful utility functions for TextBuffer related stuff. -use std::iter::repeat; use ropey::RopeSlice; +use std::iter::repeat; use unicode_segmentation::UnicodeSegmentation; pub fn is_line_ending(text: &str) -> bool { @@ -53,7 +53,7 @@ pub fn rope_slice_is_whitespace(text: &RopeSlice) -> bool { // TODO: this is a naive categorization of whitespace characters. // For better categorization these should be split up into groups // based on e.g. breaking vs non-breaking spaces, among other things. - + text == "\u{0020}" // SPACE || text == "\u{0009}" // CHARACTER TABULATION || text == "\u{00A0}" // NO-BREAK SPACE diff --git a/src/term_ui/formatter.rs b/src/term_ui/formatter.rs index fc04ef8..14c6450 100644 --- a/src/term_ui/formatter.rs +++ b/src/term_ui/formatter.rs @@ -1,9 +1,9 @@ use std::cmp::max; -use ropey::RopeSlice; -use utils::grapheme_width; -use string_utils::{rope_slice_is_line_ending, rope_slice_is_whitespace}; use formatter::{LineFormatter, RoundingBehavior}; +use ropey::RopeSlice; +use string_utils::{rope_slice_is_line_ending, rope_slice_is_whitespace}; +use utils::grapheme_width; pub enum WrapType { NoWrap, @@ -315,12 +315,12 @@ fn grapheme_vis_width_at_vis_pos(g: RopeSlice, pos: usize, tab_width: usize) -> #[cfg(test)] mod tests { #![allow(unused_imports)] + use super::*; + use buffer::Buffer; + use formatter::RoundingBehavior::{Ceiling, Floor, Round}; + use formatter::{LineFormatter, LINE_BLOCK_LENGTH}; use ropey::Rope; use utils::RopeGraphemes; - use super::*; - use formatter::{LineFormatter, LINE_BLOCK_LENGTH}; - use formatter::RoundingBehavior::{Ceiling, Floor, Round}; - use buffer::Buffer; #[test] fn dimensions_1() { @@ -332,10 +332,7 @@ mod tests { f.wrap_additional_indent = 0; f.set_wrap_width(80); - assert_eq!( - f.dimensions(RopeGraphemes::new(&text.slice(..))), - (1, 22) - ); + assert_eq!(f.dimensions(RopeGraphemes::new(&text.slice(..))), (1, 22)); } #[test] @@ -348,25 +345,24 @@ mod tests { f.wrap_additional_indent = 0; f.set_wrap_width(12); - assert_eq!( - f.dimensions(RopeGraphemes::new(&text.slice(..))), - (5, 12) - ); + assert_eq!(f.dimensions(RopeGraphemes::new(&text.slice(..))), (5, 12)); } #[test] fn dimensions_3() { // 55 graphemes long - let text = Rope::from_str("税マイミ文末\ - レ日題イぽじ\ - や男目統ス公\ - 身みトしつ結\ - 煮ヱマレ断西\ - ロ領視りいぽ\ - 凱字テ式重反\ - てす献罪がご\ - く官俵呉嫁ー\ - 。"); + let text = Rope::from_str( + "税マイミ文末\ + レ日題イぽじ\ + や男目統ス公\ + 身みトしつ結\ + 煮ヱマレ断西\ + ロ領視りいぽ\ + 凱字テ式重反\ + てす献罪がご\ + く官俵呉嫁ー\ + 。", + ); let mut f = ConsoleLineFormatter::new(4); f.wrap_type = WrapType::CharWrap(0); @@ -374,25 +370,24 @@ mod tests { f.wrap_additional_indent = 0; f.set_wrap_width(12); - assert_eq!( - f.dimensions(RopeGraphemes::new(&text.slice(..))), - (10, 12) - ); + assert_eq!(f.dimensions(RopeGraphemes::new(&text.slice(..))), (10, 12)); } #[test] fn dimensions_4() { // 55 graphemes long - let text = Rope::from_str("税マイミ文末\ - レ日題イぽじ\ - や男目統ス公\ - 身みトしつ結\ - 煮ヱマレ断西\ - ロ領視りいぽ\ - 凱字テ式重反\ - てす献罪がご\ - く官俵呉嫁ー\ - 。"); + let text = Rope::from_str( + "税マイミ文末\ + レ日題イぽじ\ + や男目統ス公\ + 身みトしつ結\ + 煮ヱマレ断西\ + ロ領視りいぽ\ + 凱字テ式重反\ + てす献罪がご\ + く官俵呉嫁ー\ + 。", + ); let mut f = ConsoleLineFormatter::new(4); f.wrap_type = WrapType::WordWrap(0); @@ -400,10 +395,7 @@ mod tests { f.wrap_additional_indent = 0; f.set_wrap_width(12); - assert_eq!( - f.dimensions(RopeGraphemes::new(&text.slice(..))), - (10, 12) - ); + assert_eq!(f.dimensions(RopeGraphemes::new(&text.slice(..))), (10, 12)); } #[test] @@ -526,51 +518,27 @@ mod tests { f.set_wrap_width(80); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (0, 0), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (0, 0), (Floor, Floor)), 0 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (0, 5), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (0, 5), (Floor, Floor)), 5 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (0, 22), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (0, 22), (Floor, Floor)), 22 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (0, 23), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (0, 23), (Floor, Floor)), 22 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (1, 0), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (1, 0), (Floor, Floor)), 22 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (1, 1), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (1, 1), (Floor, Floor)), 22 ); } @@ -586,135 +554,71 @@ mod tests { f.set_wrap_width(12); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (0, 0), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (0, 0), (Floor, Floor)), 0 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (0, 11), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (0, 11), (Floor, Floor)), 11 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (0, 12), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (0, 12), (Floor, Floor)), 11 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (1, 0), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (1, 0), (Floor, Floor)), 12 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (1, 11), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (1, 11), (Floor, Floor)), 23 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (1, 12), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (1, 12), (Floor, Floor)), 23 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (2, 0), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (2, 0), (Floor, Floor)), 24 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (2, 11), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (2, 11), (Floor, Floor)), 35 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (2, 12), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (2, 12), (Floor, Floor)), 35 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (3, 0), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (3, 0), (Floor, Floor)), 36 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (3, 11), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (3, 11), (Floor, Floor)), 47 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (3, 12), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (3, 12), (Floor, Floor)), 47 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (4, 0), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (4, 0), (Floor, Floor)), 48 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (4, 7), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (4, 7), (Floor, Floor)), 55 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (4, 8), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (4, 8), (Floor, Floor)), 56 ); assert_eq!( - f.v2d_to_index( - RopeGraphemes::new(&text.slice(..)), - (4, 9), - (Floor, Floor) - ), + f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (4, 9), (Floor, Floor)), 56 ); } diff --git a/src/term_ui/mod.rs b/src/term_ui/mod.rs index b885733..13cace1 100644 --- a/src/term_ui/mod.rs +++ b/src/term_ui/mod.rs @@ -7,10 +7,10 @@ use termion; use termion::event::{Event, Key}; use termion::input::TermRead; +use self::formatter::ConsoleLineFormatter; use editor::Editor; use formatter::{block_index_and_offset, LineFormatter, LINE_BLOCK_LENGTH}; -use self::formatter::ConsoleLineFormatter; -use string_utils::{rope_slice_is_line_ending, line_ending_to_str, LineEnding}; +use string_utils::{line_ending_to_str, rope_slice_is_line_ending, LineEnding}; use utils::{digit_count, RopeGraphemes}; pub mod formatter; @@ -21,11 +21,13 @@ use self::screen::{Color, Screen, Style}; /// Generalized ui loop. macro_rules! ui_loop { - ($term_ui:ident, draw $draw:block, key_press($key:ident) $key_press:block) => { + ($term_ui:ident,draw $draw:block,key_press($key:ident) $key_press:block) => { let mut stop = false; // Draw the editor to screen for the first time - {$draw}; + { + $draw + }; $term_ui.screen.present(); // UI loop @@ -36,9 +38,7 @@ macro_rules! ui_loop { loop { match $term_ui.inp.next() { Some(Ok(Event::Key($key))) => { - let (status, state_changed) = || -> (LoopStatus, bool) { - $key_press - }(); + let (status, state_changed) = || -> (LoopStatus, bool) { $key_press }(); should_redraw |= state_changed; if status == LoopStatus::Done { stop = true; @@ -62,16 +62,23 @@ macro_rules! ui_loop { if $term_ui.width != w as usize || $term_ui.height != h as usize { $term_ui.width = w as usize; $term_ui.height = h as usize; - $term_ui.editor.update_dim($term_ui.height - 1, $term_ui.width); + $term_ui + .editor + .update_dim($term_ui.height - 1, $term_ui.width); $term_ui.editor.update_view_dim(); - $term_ui.editor.formatter.set_wrap_width($term_ui.editor.view_dim.1); + $term_ui + .editor + .formatter + .set_wrap_width($term_ui.editor.view_dim.1); $term_ui.screen.resize(w as usize, h as usize); should_redraw = true; } // Draw the editor to screen if should_redraw { - {$draw}; + { + $draw + }; $term_ui.screen.present(); } @@ -367,14 +374,13 @@ impl TermUI { .line_col_to_index((line_index, line_block_index * LINE_BLOCK_LENGTH)); let temp_line = editor.buffer.get_line(line_index); let (vis_line_offset, _) = editor.formatter.index_to_v2d( - RopeGraphemes::new(&temp_line - .slice( - (line_block_index * LINE_BLOCK_LENGTH) - ..min( - temp_line.len_chars(), - (line_block_index + 1) * LINE_BLOCK_LENGTH, - ), - )), + RopeGraphemes::new(&temp_line.slice( + (line_block_index * LINE_BLOCK_LENGTH) + ..min( + temp_line.len_chars(), + (line_block_index + 1) * LINE_BLOCK_LENGTH, + ), + )), editor.view_pos.0 - char_index, ); @@ -534,7 +540,9 @@ impl TermUI { let px = pos_x as isize + screen_col - editor.view_pos.1 as isize; let py = screen_line - 1; - if (px >= c1.1 as isize) && (py >= c1.0 as isize) && (px <= c2.1 as isize) + if (px >= c1.1 as isize) + && (py >= c1.0 as isize) + && (px <= c2.1 as isize) && (py <= c2.0 as isize) { self.screen.draw( diff --git a/src/term_ui/screen.rs b/src/term_ui/screen.rs index 02d1e3f..9dac65b 100644 --- a/src/term_ui/screen.rs +++ b/src/term_ui/screen.rs @@ -3,15 +3,15 @@ use std::cell::RefCell; use std::io; use std::io::{BufWriter, Write}; -use ropey::RopeSlice; -use utils::{RopeGraphemes, grapheme_width}; use super::smallstring::SmallString; -use unicode_width::UnicodeWidthStr; -use unicode_segmentation::UnicodeSegmentation; +use ropey::RopeSlice; use termion; -use termion::screen::AlternateScreen; use termion::color; use termion::raw::{IntoRawMode, RawTerminal}; +use termion::screen::AlternateScreen; +use unicode_segmentation::UnicodeSegmentation; +use unicode_width::UnicodeWidthStr; +use utils::{grapheme_width, RopeGraphemes}; pub(crate) struct Screen { out: RefCell>>>, diff --git a/src/utils.rs b/src/utils.rs index a09bf45..c6de2aa 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,4 @@ -use ropey::{RopeSlice, iter::Chunks}; +use ropey::{iter::Chunks, RopeSlice}; use unicode_segmentation::{GraphemeCursor, GraphemeIncomplete}; use unicode_width::UnicodeWidthStr;