Got basic editing functionality up and running again after the refactor.
This commit is contained in:
parent
71913eed31
commit
49b34e78d5
|
@ -4,6 +4,7 @@ use std::mem;
|
|||
|
||||
use self::node::{BufferNode, BufferNodeGraphemeIter};
|
||||
use self::line::{Line};
|
||||
use string_utils::{is_line_ending};
|
||||
|
||||
mod line;
|
||||
mod node;
|
||||
|
@ -170,7 +171,22 @@ impl<'a> BufferGraphemeIter<'a> {
|
|||
self.gi.skip_graphemes(n)
|
||||
}
|
||||
|
||||
|
||||
pub fn skip_non_newline_graphemes(&mut self, n: uint) -> bool {
|
||||
let mut i: uint = 0;
|
||||
|
||||
for g in self.gi {
|
||||
if is_line_ending(g) {
|
||||
return true;
|
||||
}
|
||||
|
||||
i += 1;
|
||||
if i >= n {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
|
||||
use buffer::Buffer;
|
||||
use std::path::Path;
|
||||
use files::{load_file_to_buffer, save_buffer_to_file};
|
||||
use string_utils::char_count;
|
||||
//use files::{load_file_to_buffer, save_buffer_to_file};
|
||||
use string_utils::grapheme_count;
|
||||
|
||||
|
||||
pub struct Editor {
|
||||
pub buffer: TextBuffer,
|
||||
pub buffer: Buffer,
|
||||
pub file_path: Path,
|
||||
pub dirty: bool,
|
||||
|
||||
|
@ -24,7 +24,7 @@ impl Editor {
|
|||
/// Create a new blank editor
|
||||
pub fn new() -> Editor {
|
||||
Editor {
|
||||
buffer: TextBuffer::new(),
|
||||
buffer: Buffer::new(),
|
||||
file_path: Path::new(""),
|
||||
dirty: false,
|
||||
view_dim: (0, 0),
|
||||
|
@ -33,25 +33,25 @@ impl Editor {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn new_from_file(path: &Path) -> Editor {
|
||||
let buf = load_file_to_buffer(path).unwrap();
|
||||
|
||||
Editor {
|
||||
buffer: buf,
|
||||
file_path: path.clone(),
|
||||
dirty: false,
|
||||
view_dim: (0, 0),
|
||||
view_pos: (0, 0),
|
||||
cursor: (0, 0),
|
||||
}
|
||||
}
|
||||
// pub fn new_from_file(path: &Path) -> Editor {
|
||||
// let buf = load_file_to_buffer(path).unwrap();
|
||||
//
|
||||
// Editor {
|
||||
// buffer: buf,
|
||||
// file_path: path.clone(),
|
||||
// dirty: false,
|
||||
// view_dim: (0, 0),
|
||||
// view_pos: (0, 0),
|
||||
// cursor: (0, 0),
|
||||
// }
|
||||
// }
|
||||
|
||||
pub fn save_if_dirty(&mut self) {
|
||||
if self.dirty && self.file_path != Path::new("") {
|
||||
let _ = save_buffer_to_file(&self.buffer, &self.file_path);
|
||||
self.dirty = false;
|
||||
}
|
||||
}
|
||||
// pub fn save_if_dirty(&mut self) {
|
||||
// if self.dirty && self.file_path != Path::new("") {
|
||||
// let _ = save_buffer_to_file(&self.buffer, &self.file_path);
|
||||
// self.dirty = false;
|
||||
// }
|
||||
// }
|
||||
|
||||
pub fn update_dim(&mut self, h: uint, w: uint) {
|
||||
self.view_dim = (h, w);
|
||||
|
@ -79,7 +79,7 @@ impl Editor {
|
|||
|
||||
pub fn insert_text_at_cursor(&mut self, text: &str) {
|
||||
let pos = self.buffer.pos_2d_to_closest_1d(self.cursor);
|
||||
let str_len = char_count(text);
|
||||
let str_len = grapheme_count(text);
|
||||
let p = self.buffer.pos_2d_to_closest_1d(self.cursor);
|
||||
|
||||
// Insert text
|
||||
|
@ -92,15 +92,15 @@ impl Editor {
|
|||
self.move_view_to_cursor();
|
||||
}
|
||||
|
||||
pub fn insert_text_at_char(&mut self, text: &str, pos: uint) {
|
||||
pub fn insert_text_at_grapheme(&mut self, text: &str, pos: uint) {
|
||||
self.dirty = true;
|
||||
let buf_len = self.buffer.len();
|
||||
self.buffer.insert_text(text, if pos < buf_len {pos} else {buf_len});
|
||||
}
|
||||
|
||||
pub fn remove_text_behind_cursor(&mut self, char_count: uint) {
|
||||
pub fn remove_text_behind_cursor(&mut self, grapheme_count: uint) {
|
||||
let pos_b = self.buffer.pos_2d_to_closest_1d(self.cursor);
|
||||
let pos_a = if pos_b >= char_count {pos_b - char_count} else {0};
|
||||
let pos_a = if pos_b >= grapheme_count {pos_b - grapheme_count} else {0};
|
||||
|
||||
// Move cursor
|
||||
self.cursor = self.buffer.pos_1d_to_closest_2d(pos_a);
|
||||
|
@ -153,7 +153,7 @@ impl Editor {
|
|||
}
|
||||
|
||||
pub fn cursor_down(&mut self) {
|
||||
if self.cursor.0 < self.buffer.newline_count() {
|
||||
if self.cursor.0 < (self.buffer.line_count() - 1) {
|
||||
self.cursor.0 += 1;
|
||||
}
|
||||
else {
|
||||
|
@ -190,7 +190,7 @@ impl Editor {
|
|||
}
|
||||
|
||||
pub fn page_down(&mut self) {
|
||||
let nlc = self.buffer.newline_count();
|
||||
let nlc = self.buffer.line_count() - 1;
|
||||
|
||||
if self.view_pos.0 < nlc {
|
||||
let move_amount = self.view_dim.0 - (self.view_dim.0 / 8);
|
||||
|
|
19
src/main.rs
19
src/main.rs
|
@ -4,14 +4,14 @@ extern crate "rustc-serialize" as rustc_serialize;
|
|||
|
||||
use std::path::Path;
|
||||
use docopt::Docopt;
|
||||
//use editor::Editor;
|
||||
//use term_ui::TermUI;
|
||||
use editor::Editor;
|
||||
use term_ui::TermUI;
|
||||
|
||||
mod string_utils;
|
||||
mod buffer;
|
||||
//mod files;
|
||||
//mod editor;
|
||||
//mod term_ui;
|
||||
mod editor;
|
||||
mod term_ui;
|
||||
|
||||
|
||||
|
||||
|
@ -47,10 +47,13 @@ fn main() {
|
|||
// else {
|
||||
// Editor::new()
|
||||
// };
|
||||
//
|
||||
// // Initialize and start UI
|
||||
// let mut ui = TermUI::new_from_editor(editor);
|
||||
// ui.ui_loop();
|
||||
//
|
||||
|
||||
let editor = Editor::new();
|
||||
|
||||
// Initialize and start UI
|
||||
let mut ui = TermUI::new_from_editor(editor);
|
||||
ui.ui_loop();
|
||||
|
||||
//println!("{}", editor.buffer.root.tree_height);
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ use rustbox::Color;
|
|||
use editor::Editor;
|
||||
use std::char;
|
||||
use std::time::duration::Duration;
|
||||
|
||||
use string_utils::{is_line_ending};
|
||||
|
||||
// Key codes
|
||||
const K_ENTER: u16 = 13;
|
||||
|
@ -74,9 +74,9 @@ impl TermUI {
|
|||
break;
|
||||
},
|
||||
|
||||
K_CTRL_S => {
|
||||
self.editor.save_if_dirty();
|
||||
},
|
||||
// K_CTRL_S => {
|
||||
// self.editor.save_if_dirty();
|
||||
// },
|
||||
|
||||
K_PAGEUP => {
|
||||
self.editor.page_up();
|
||||
|
@ -152,7 +152,7 @@ impl TermUI {
|
|||
}
|
||||
|
||||
pub fn draw_editor(&self, editor: &Editor, c1: (uint, uint), c2: (uint, uint)) {
|
||||
let mut tb_iter = editor.buffer.iter_at_char(editor.buffer.pos_2d_to_closest_1d(editor.view_pos));
|
||||
let mut tb_iter = editor.buffer.grapheme_iter_at_index(editor.buffer.pos_2d_to_closest_1d(editor.view_pos));
|
||||
let mut pline = c1.0;
|
||||
let mut pcol = c1.1;
|
||||
let mut line = editor.view_pos.0;
|
||||
|
@ -164,10 +164,10 @@ impl TermUI {
|
|||
let cursor_pos = editor.buffer.pos_2d_to_closest_1d(editor.cursor);
|
||||
|
||||
loop {
|
||||
if let Option::Some(c) = tb_iter.next() {
|
||||
if c == '\n' {
|
||||
if let Some(g) = tb_iter.next() {
|
||||
if is_line_ending(g) {
|
||||
if pos == cursor_pos {
|
||||
self.rb.print(pcol, pline, rustbox::RB_NORMAL, Color::Black, Color::White, " ".to_string().as_slice());
|
||||
self.rb.print(pcol, pline, rustbox::RB_NORMAL, Color::Black, Color::White, " ");
|
||||
}
|
||||
|
||||
pline += 1;
|
||||
|
@ -177,10 +177,10 @@ impl TermUI {
|
|||
}
|
||||
else {
|
||||
if pos == cursor_pos {
|
||||
self.rb.print(pcol, pline, rustbox::RB_NORMAL, Color::Black, Color::White, c.to_string().as_slice());
|
||||
self.rb.print(pcol, pline, rustbox::RB_NORMAL, Color::Black, Color::White, g);
|
||||
}
|
||||
else {
|
||||
self.rb.print(pcol, pline, rustbox::RB_NORMAL, Color::White, Color::Black, c.to_string().as_slice());
|
||||
self.rb.print(pcol, pline, rustbox::RB_NORMAL, Color::White, Color::Black, g);
|
||||
}
|
||||
|
||||
pcol += 1;
|
||||
|
@ -219,7 +219,7 @@ impl TermUI {
|
|||
// to it.
|
||||
loop {
|
||||
if column < editor.view_pos.1 {
|
||||
let nl = tb_iter.skip_non_newline_chars(editor.view_pos.1);
|
||||
let nl = tb_iter.skip_non_newline_graphemes(editor.view_pos.1);
|
||||
if !nl {
|
||||
column = editor.view_pos.1;
|
||||
break;
|
||||
|
|
Loading…
Reference in New Issue
Block a user