More fixing.

This commit is contained in:
Nathan Vegdahl 2017-12-30 23:47:05 -08:00
parent 9c302620e9
commit 47ed45aae6
4 changed files with 35 additions and 22 deletions

View File

@ -270,10 +270,17 @@ impl Buffer {
/// If the index is off the end of the text, returns the line and column /// If the index is off the end of the text, returns the line and column
/// number of the last valid text position. /// number of the last valid text position.
pub fn index_to_line_col(&self, pos: usize) -> (usize, usize) { pub fn index_to_line_col(&self, pos: usize) -> (usize, usize) {
if pos < self.text.len_chars() {
let line = self.text.char_to_line(pos); let line = self.text.char_to_line(pos);
let line_pos = self.text.line_to_char(line); let line_pos = self.text.line_to_char(line);
return (line, pos - line_pos); return (line, pos - line_pos);
} else {
let line = self.text.len_lines() - 1;
let line_pos = self.text.line_to_char(line);
return (line, pos - line_pos);
}
} }
/// Converts a line number and char-column number into a char /// Converts a line number and char-column number into a char

View File

@ -99,7 +99,10 @@ impl<T: LineFormatter> Editor<T> {
for line in self.buffer.line_iter() { for line in self.buffer.line_iter() {
// Get the line ending // Get the line ending
let ending = if line.len_chars() > 0 { let ending = if line.len_chars() > 0 {
let g = line.slice(line.len_chars() - 1, line.len_chars()).graphemes().nth(0).unwrap(); let g = line.slice(line.len_chars() - 1, line.len_chars())
.graphemes()
.nth(0)
.unwrap();
str_to_line_ending(g) str_to_line_ending(g)
} else { } else {
LineEnding::None LineEnding::None

View File

@ -40,8 +40,7 @@ pub fn is_whitespace(text: &str) -> bool {
| "\u{3000}" // IDEOGRAPHIC SPACE | "\u{3000}" // IDEOGRAPHIC SPACE
| "\u{FEFF}" // ZERO WIDTH NO-BREAK SPACE | "\u{FEFF}" // ZERO WIDTH NO-BREAK SPACE
=> true, => true,
_ => false,
_ => false
} }
} }

View File

@ -44,7 +44,7 @@ pub struct TermUI {
impl TermUI { impl TermUI {
pub fn new() -> TermUI { pub fn new() -> TermUI {
let rb = match rustbox::RustBox::init(rustbox::InitOptions { let rb = match rustbox::RustBox::init(rustbox::InitOptions {
buffer_stderr: false, buffer_stderr: true,
..Default::default() ..Default::default()
}) { }) {
Ok(rbox) => rbox, Ok(rbox) => rbox,
@ -65,7 +65,7 @@ impl TermUI {
pub fn new_from_editor(ed: Editor<ConsoleLineFormatter>) -> TermUI { pub fn new_from_editor(ed: Editor<ConsoleLineFormatter>) -> TermUI {
let rb = match rustbox::RustBox::init(rustbox::InitOptions { let rb = match rustbox::RustBox::init(rustbox::InitOptions {
buffer_stderr: false, buffer_stderr: true,
..Default::default() ..Default::default()
}) { }) {
Ok(rbox) => rbox, Ok(rbox) => rbox,
@ -406,13 +406,15 @@ 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(
temp_line.slice( temp_line
.slice(
line_block_index * LINE_BLOCK_LENGTH, line_block_index * LINE_BLOCK_LENGTH,
min( min(
temp_line.len_chars(), temp_line.len_chars(),
(line_block_index + 1) * LINE_BLOCK_LENGTH, (line_block_index + 1) * LINE_BLOCK_LENGTH,
), ),
).graphemes(), )
.graphemes(),
editor.view_pos.0 - grapheme_index, editor.view_pos.0 - grapheme_index,
); );
@ -451,9 +453,10 @@ impl TermUI {
let mut last_pos_y = 0; let mut last_pos_y = 0;
let mut lines_traversed: usize = 0; let mut lines_traversed: usize = 0;
let line_len = line.len_chars(); let line_len = line.len_chars();
let mut g_iter = editor let mut g_iter = editor.formatter.iter(line.slice(
.formatter line_block_index * LINE_BLOCK_LENGTH,
.iter(line.slice(line_block_index * LINE_BLOCK_LENGTH, line_len).graphemes()); line_len,
).graphemes());
loop { loop {
if let Some((g, (pos_y, pos_x), width)) = g_iter.next() { if let Some((g, (pos_y, pos_x), width)) = g_iter.next() {
@ -552,9 +555,10 @@ impl TermUI {
line_block_index += 1; line_block_index += 1;
line_g_index = 0; line_g_index = 0;
let line_len = line.len_chars(); let line_len = line.len_chars();
g_iter = editor g_iter = editor.formatter.iter(line.slice(
.formatter line_block_index * LINE_BLOCK_LENGTH,
.iter(line.slice(line_block_index * LINE_BLOCK_LENGTH, line_len).graphemes()); line_len,
).graphemes());
lines_traversed += 1; lines_traversed += 1;
} }
} }