Fomatted with recent rustfmt. No actual code changes.

This commit is contained in:
Nathan Vegdahl 2018-07-05 02:00:10 -07:00
parent de625e71dc
commit 5ca0f5e406
10 changed files with 108 additions and 196 deletions

View File

@ -1,16 +1,16 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::path::{Path, PathBuf};
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::io::{BufReader, BufWriter, Write}; 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;
use ropey::{Rope, RopeSlice}; use ropey::{Rope, RopeSlice};
use self::undo_stack::UndoStack;
use self::undo_stack::Operation::*;
use string_utils::char_count; 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; mod undo_stack;

View File

@ -1,8 +1,8 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::slice::{Iter, IterMut};
use std::ops::{Index, IndexMut};
use std::cmp::Ordering; use std::cmp::Ordering;
use std::ops::{Index, IndexMut};
use std::slice::{Iter, IterMut};
use buffer::Buffer; use buffer::Buffer;
use formatter::LineFormatter; use formatter::LineFormatter;

View File

@ -2,14 +2,14 @@
use std::collections::HashMap; use std::collections::HashMap;
use self::cursor::CursorSet;
use buffer::Buffer; use buffer::Buffer;
use formatter::LineFormatter; use formatter::LineFormatter;
use formatter::RoundingBehavior::*; use formatter::RoundingBehavior::*;
use std::path::{Path, PathBuf};
use std::cmp::{max, min}; use std::cmp::{max, min};
use std::path::{Path, PathBuf};
use string_utils::{char_count, rope_slice_to_line_ending, LineEnding}; use string_utils::{char_count, rope_slice_to_line_ending, LineEnding};
use utils::{digit_count, RopeGraphemes}; use utils::{digit_count, RopeGraphemes};
use self::cursor::CursorSet;
mod cursor; mod cursor;
@ -378,7 +378,7 @@ impl<T: LineFormatter> Editor<T> {
// Insert spaces // Insert spaces
let space_strs = [ let space_strs = [
"", " ", " ", " ", " ", " ", " ", " ", " " "", " ", " ", " ", " ", " ", " ", " ", " ",
]; ];
self.buffer.insert_text(space_strs[space_count], c.range.0); self.buffer.insert_text(space_strs[space_count], c.range.0);
self.dirty = true; self.dirty = true;

View File

@ -1,8 +1,8 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::cmp::min;
use ropey::RopeSlice;
use buffer::Buffer; use buffer::Buffer;
use ropey::RopeSlice;
use std::cmp::min;
use utils::RopeGraphemes; use utils::RopeGraphemes;
// Maximum graphemes in a line before a soft line break is forced. // Maximum graphemes in a line before a soft line break is forced.

View File

@ -8,18 +8,18 @@ extern crate termion;
extern crate unicode_segmentation; extern crate unicode_segmentation;
extern crate unicode_width; extern crate unicode_width;
use std::path::Path;
use docopt::Docopt; use docopt::Docopt;
use editor::Editor; use editor::Editor;
use term_ui::TermUI; use std::path::Path;
use term_ui::formatter::ConsoleLineFormatter; use term_ui::formatter::ConsoleLineFormatter;
use term_ui::TermUI;
mod string_utils;
mod utils;
mod buffer; mod buffer;
mod formatter;
mod editor; mod editor;
mod formatter;
mod string_utils;
mod term_ui; mod term_ui;
mod utils;
// Usage documentation string // Usage documentation string
static USAGE: &'static str = " static USAGE: &'static str = "

View File

@ -1,8 +1,8 @@
#![allow(dead_code)] #![allow(dead_code)]
//! Misc helpful utility functions for TextBuffer related stuff. //! Misc helpful utility functions for TextBuffer related stuff.
use std::iter::repeat;
use ropey::RopeSlice; use ropey::RopeSlice;
use std::iter::repeat;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
pub fn is_line_ending(text: &str) -> bool { pub fn is_line_ending(text: &str) -> bool {

View File

@ -1,9 +1,9 @@
use std::cmp::max; 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 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 { pub enum WrapType {
NoWrap, NoWrap,
@ -315,12 +315,12 @@ fn grapheme_vis_width_at_vis_pos(g: RopeSlice, pos: usize, tab_width: usize) ->
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#![allow(unused_imports)] #![allow(unused_imports)]
use super::*;
use buffer::Buffer;
use formatter::RoundingBehavior::{Ceiling, Floor, Round};
use formatter::{LineFormatter, LINE_BLOCK_LENGTH};
use ropey::Rope; use ropey::Rope;
use utils::RopeGraphemes; use utils::RopeGraphemes;
use super::*;
use formatter::{LineFormatter, LINE_BLOCK_LENGTH};
use formatter::RoundingBehavior::{Ceiling, Floor, Round};
use buffer::Buffer;
#[test] #[test]
fn dimensions_1() { fn dimensions_1() {
@ -332,10 +332,7 @@ mod tests {
f.wrap_additional_indent = 0; f.wrap_additional_indent = 0;
f.set_wrap_width(80); f.set_wrap_width(80);
assert_eq!( assert_eq!(f.dimensions(RopeGraphemes::new(&text.slice(..))), (1, 22));
f.dimensions(RopeGraphemes::new(&text.slice(..))),
(1, 22)
);
} }
#[test] #[test]
@ -348,16 +345,14 @@ mod tests {
f.wrap_additional_indent = 0; f.wrap_additional_indent = 0;
f.set_wrap_width(12); f.set_wrap_width(12);
assert_eq!( assert_eq!(f.dimensions(RopeGraphemes::new(&text.slice(..))), (5, 12));
f.dimensions(RopeGraphemes::new(&text.slice(..))),
(5, 12)
);
} }
#[test] #[test]
fn dimensions_3() { fn dimensions_3() {
// 55 graphemes long // 55 graphemes long
let text = Rope::from_str("税マイミ文末\ let text = Rope::from_str(
"税マイミ文末\
\ \
\ \
\ \
@ -366,7 +361,8 @@ mod tests {
\ \
\ \
\ \
"); ",
);
let mut f = ConsoleLineFormatter::new(4); let mut f = ConsoleLineFormatter::new(4);
f.wrap_type = WrapType::CharWrap(0); f.wrap_type = WrapType::CharWrap(0);
@ -374,16 +370,14 @@ mod tests {
f.wrap_additional_indent = 0; f.wrap_additional_indent = 0;
f.set_wrap_width(12); f.set_wrap_width(12);
assert_eq!( assert_eq!(f.dimensions(RopeGraphemes::new(&text.slice(..))), (10, 12));
f.dimensions(RopeGraphemes::new(&text.slice(..))),
(10, 12)
);
} }
#[test] #[test]
fn dimensions_4() { fn dimensions_4() {
// 55 graphemes long // 55 graphemes long
let text = Rope::from_str("税マイミ文末\ let text = Rope::from_str(
"税マイミ文末\
\ \
\ \
\ \
@ -392,7 +386,8 @@ mod tests {
\ \
\ \
\ \
"); ",
);
let mut f = ConsoleLineFormatter::new(4); let mut f = ConsoleLineFormatter::new(4);
f.wrap_type = WrapType::WordWrap(0); f.wrap_type = WrapType::WordWrap(0);
@ -400,10 +395,7 @@ mod tests {
f.wrap_additional_indent = 0; f.wrap_additional_indent = 0;
f.set_wrap_width(12); f.set_wrap_width(12);
assert_eq!( assert_eq!(f.dimensions(RopeGraphemes::new(&text.slice(..))), (10, 12));
f.dimensions(RopeGraphemes::new(&text.slice(..))),
(10, 12)
);
} }
#[test] #[test]
@ -526,51 +518,27 @@ mod tests {
f.set_wrap_width(80); f.set_wrap_width(80);
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (0, 0), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(0, 0),
(Floor, Floor)
),
0 0
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (0, 5), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(0, 5),
(Floor, Floor)
),
5 5
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (0, 22), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(0, 22),
(Floor, Floor)
),
22 22
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (0, 23), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(0, 23),
(Floor, Floor)
),
22 22
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (1, 0), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(1, 0),
(Floor, Floor)
),
22 22
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (1, 1), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(1, 1),
(Floor, Floor)
),
22 22
); );
} }
@ -586,135 +554,71 @@ mod tests {
f.set_wrap_width(12); f.set_wrap_width(12);
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (0, 0), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(0, 0),
(Floor, Floor)
),
0 0
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (0, 11), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(0, 11),
(Floor, Floor)
),
11 11
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (0, 12), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(0, 12),
(Floor, Floor)
),
11 11
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (1, 0), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(1, 0),
(Floor, Floor)
),
12 12
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (1, 11), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(1, 11),
(Floor, Floor)
),
23 23
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (1, 12), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(1, 12),
(Floor, Floor)
),
23 23
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (2, 0), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(2, 0),
(Floor, Floor)
),
24 24
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (2, 11), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(2, 11),
(Floor, Floor)
),
35 35
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (2, 12), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(2, 12),
(Floor, Floor)
),
35 35
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (3, 0), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(3, 0),
(Floor, Floor)
),
36 36
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (3, 11), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(3, 11),
(Floor, Floor)
),
47 47
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (3, 12), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(3, 12),
(Floor, Floor)
),
47 47
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (4, 0), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(4, 0),
(Floor, Floor)
),
48 48
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (4, 7), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(4, 7),
(Floor, Floor)
),
55 55
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (4, 8), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(4, 8),
(Floor, Floor)
),
56 56
); );
assert_eq!( assert_eq!(
f.v2d_to_index( f.v2d_to_index(RopeGraphemes::new(&text.slice(..)), (4, 9), (Floor, Floor)),
RopeGraphemes::new(&text.slice(..)),
(4, 9),
(Floor, Floor)
),
56 56
); );
} }

View File

@ -7,10 +7,10 @@ use termion;
use termion::event::{Event, Key}; use termion::event::{Event, Key};
use termion::input::TermRead; use termion::input::TermRead;
use self::formatter::ConsoleLineFormatter;
use editor::Editor; use editor::Editor;
use formatter::{block_index_and_offset, LineFormatter, LINE_BLOCK_LENGTH}; use formatter::{block_index_and_offset, LineFormatter, LINE_BLOCK_LENGTH};
use self::formatter::ConsoleLineFormatter; use string_utils::{line_ending_to_str, rope_slice_is_line_ending, LineEnding};
use string_utils::{rope_slice_is_line_ending, line_ending_to_str, LineEnding};
use utils::{digit_count, RopeGraphemes}; use utils::{digit_count, RopeGraphemes};
pub mod formatter; pub mod formatter;
@ -21,11 +21,13 @@ use self::screen::{Color, Screen, Style};
/// Generalized ui loop. /// Generalized ui loop.
macro_rules! 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; let mut stop = false;
// Draw the editor to screen for the first time // Draw the editor to screen for the first time
{$draw}; {
$draw
};
$term_ui.screen.present(); $term_ui.screen.present();
// UI loop // UI loop
@ -36,9 +38,7 @@ macro_rules! ui_loop {
loop { loop {
match $term_ui.inp.next() { match $term_ui.inp.next() {
Some(Ok(Event::Key($key))) => { Some(Ok(Event::Key($key))) => {
let (status, state_changed) = || -> (LoopStatus, bool) { let (status, state_changed) = || -> (LoopStatus, bool) { $key_press }();
$key_press
}();
should_redraw |= state_changed; should_redraw |= state_changed;
if status == LoopStatus::Done { if status == LoopStatus::Done {
stop = true; stop = true;
@ -62,16 +62,23 @@ macro_rules! ui_loop {
if $term_ui.width != w as usize || $term_ui.height != h as usize { if $term_ui.width != w as usize || $term_ui.height != h as usize {
$term_ui.width = w as usize; $term_ui.width = w as usize;
$term_ui.height = h 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.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); $term_ui.screen.resize(w as usize, h as usize);
should_redraw = true; should_redraw = true;
} }
// Draw the editor to screen // Draw the editor to screen
if should_redraw { if should_redraw {
{$draw}; {
$draw
};
$term_ui.screen.present(); $term_ui.screen.present();
} }
@ -367,8 +374,7 @@ impl TermUI {
.line_col_to_index((line_index, line_block_index * LINE_BLOCK_LENGTH)); .line_col_to_index((line_index, line_block_index * LINE_BLOCK_LENGTH));
let temp_line = editor.buffer.get_line(line_index); let temp_line = editor.buffer.get_line(line_index);
let (vis_line_offset, _) = editor.formatter.index_to_v2d( let (vis_line_offset, _) = editor.formatter.index_to_v2d(
RopeGraphemes::new(&temp_line RopeGraphemes::new(&temp_line.slice(
.slice(
(line_block_index * LINE_BLOCK_LENGTH) (line_block_index * LINE_BLOCK_LENGTH)
..min( ..min(
temp_line.len_chars(), temp_line.len_chars(),
@ -534,7 +540,9 @@ impl TermUI {
let px = pos_x as isize + screen_col - editor.view_pos.1 as isize; let px = pos_x as isize + screen_col - editor.view_pos.1 as isize;
let py = screen_line - 1; 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) && (py <= c2.0 as isize)
{ {
self.screen.draw( self.screen.draw(

View File

@ -3,15 +3,15 @@ use std::cell::RefCell;
use std::io; use std::io;
use std::io::{BufWriter, Write}; use std::io::{BufWriter, Write};
use ropey::RopeSlice;
use utils::{RopeGraphemes, grapheme_width};
use super::smallstring::SmallString; use super::smallstring::SmallString;
use unicode_width::UnicodeWidthStr; use ropey::RopeSlice;
use unicode_segmentation::UnicodeSegmentation;
use termion; use termion;
use termion::screen::AlternateScreen;
use termion::color; use termion::color;
use termion::raw::{IntoRawMode, RawTerminal}; 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 { pub(crate) struct Screen {
out: RefCell<AlternateScreen<RawTerminal<BufWriter<io::Stdout>>>>, out: RefCell<AlternateScreen<RawTerminal<BufWriter<io::Stdout>>>>,

View File

@ -1,4 +1,4 @@
use ropey::{RopeSlice, iter::Chunks}; use ropey::{iter::Chunks, RopeSlice};
use unicode_segmentation::{GraphemeCursor, GraphemeIncomplete}; use unicode_segmentation::{GraphemeCursor, GraphemeIncomplete};
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;