From 7b87db4e419cc4c0eba95ac72699bd7af250ae7d Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Sat, 31 Jan 2015 13:19:14 -0800 Subject: [PATCH] Line wrapping width is now determined by terminal width. --- src/buffer/mod.rs | 9 +++++++++ src/buffer/node.rs | 10 ++++++++++ src/term_ui/mod.rs | 4 ++++ todo.md | 4 ++-- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/buffer/mod.rs b/src/buffer/mod.rs index 2ccbb10..ba903ba 100644 --- a/src/buffer/mod.rs +++ b/src/buffer/mod.rs @@ -208,6 +208,15 @@ impl Buffer { } + /// Runs the formatter on all of the text. Should be run whenever the + /// formatter has been changed. + pub fn reformat<'a>(&'a mut self) { + self.text.reformat_recursive(&self.formatter); + } + + + + //------------------------------------------------------------------------ // Undo/redo functionality diff --git a/src/buffer/node.rs b/src/buffer/node.rs index 0449eec..1925e2e 100644 --- a/src/buffer/node.rs +++ b/src/buffer/node.rs @@ -195,6 +195,16 @@ impl BufferNode { } + pub fn reformat_recursive(&mut self, f: &T) { + if let BufferNodeData::Branch(ref mut left, ref mut right) = self.data { + left.update_stats(f); + right.update_stats(f); + } + + self.update_stats(f); + } + + pub fn get_grapheme_recursive<'a>(&'a self, index: usize) -> &'a str { match self.data { BufferNodeData::Leaf(ref line) => { diff --git a/src/term_ui/mod.rs b/src/term_ui/mod.rs index 3fce677..1d651fa 100644 --- a/src/term_ui/mod.rs +++ b/src/term_ui/mod.rs @@ -83,6 +83,8 @@ impl TermUI { let mut quit = false; self.editor.update_dim(self.height-1, self.width); + self.editor.buffer.formatter.wrap_width = self.width as usize; + self.editor.buffer.reformat(); loop { // Draw the editor to screen @@ -182,6 +184,8 @@ impl TermUI { self.width = w as usize; self.height = h as usize; self.editor.update_dim(self.height-1, self.width); + self.editor.buffer.formatter.wrap_width = w as usize; + self.editor.buffer.reformat(); }, _ => { diff --git a/todo.md b/todo.md index d489058..ca7075e 100644 --- a/todo.md +++ b/todo.md @@ -15,8 +15,8 @@ The freetype formatter should not reference SDL at all, and should have only the freetype library itself as a dependency. //- Handle tab settings properly after the refactor - - Buffer needs a "reformat" method, which can be run after the formatter - is changed in some way (e.g. toggling line wrapping). + //- Buffer needs a "reformat" method, which can be run after the formatter + // is changed in some way (e.g. toggling line wrapping). - Possibly split the text buffer out into its own library...? Would likely be useful to other people as well, and would encourage me to keep the API clean.