From 613d06dfb0e5fe2fc05b94e5c651dfd10af23310 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= <blaz@mxxn.io>
Date: Mon, 1 Jun 2020 17:42:28 +0900
Subject: [PATCH] wip: importing to github

---
 Cargo.lock                  |  44 ++++++++
 helix-core/Cargo.toml       |   2 +
 helix-core/src/graphemes.rs | 213 ++++++++++++++++++++++++++++++++++++
 helix-core/src/lib.rs       |   1 +
 helix-core/src/selection.rs |   4 +-
 helix-core/src/state.rs     |  86 ++++++++++++++-
 helix-term/Cargo.toml       |   1 +
 helix-term/src/component.rs |  18 +++
 helix-term/src/editor.rs    |   7 ++
 helix-term/src/line.rs      |   5 +
 helix-term/src/main.rs      |   1 +
 11 files changed, 375 insertions(+), 7 deletions(-)
 create mode 100644 helix-core/src/graphemes.rs
 create mode 100644 helix-term/src/component.rs

diff --git a/Cargo.lock b/Cargo.lock
index c4f795b9..e7ec2958 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -59,6 +59,12 @@ version = "1.2.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
 
+[[package]]
+name = "cassowary"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53"
+
 [[package]]
 name = "cc"
 version = "1.0.54"
@@ -176,6 +182,12 @@ dependencies = [
  "winapi",
 ]
 
+[[package]]
+name = "either"
+version = "1.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
+
 [[package]]
 name = "futf"
 version = "0.1.4"
@@ -298,6 +310,8 @@ dependencies = [
  "ropey",
  "smallvec",
  "tendril",
+ "unicode-segmentation",
+ "unicode-width",
 ]
 
 [[package]]
@@ -312,6 +326,7 @@ dependencies = [
  "num_cpus",
  "piper",
  "smol",
+ "tui",
 ]
 
 [[package]]
@@ -323,6 +338,15 @@ dependencies = [
  "libc",
 ]
 
+[[package]]
+name = "itertools"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b"
+dependencies = [
+ "either",
+]
+
 [[package]]
 name = "lazy_static"
 version = "1.4.0"
@@ -651,12 +675,32 @@ dependencies = [
  "utf-8",
 ]
 
+[[package]]
+name = "tui"
+version = "0.9.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9533d39bef0ae8f510e8a99d78702e68d1bbf0b98a78ec9740509d287010ae1e"
+dependencies = [
+ "bitflags",
+ "cassowary",
+ "either",
+ "itertools",
+ "unicode-segmentation",
+ "unicode-width",
+]
+
 [[package]]
 name = "unicode-segmentation"
 version = "1.6.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0"
 
+[[package]]
+name = "unicode-width"
+version = "0.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479"
+
 [[package]]
 name = "unicode-xid"
 version = "0.2.0"
diff --git a/helix-core/Cargo.toml b/helix-core/Cargo.toml
index fc6a1b53..fda4e5d9 100644
--- a/helix-core/Cargo.toml
+++ b/helix-core/Cargo.toml
@@ -12,4 +12,6 @@ ropey = { git = "https://github.com/cessen/ropey" }
 anyhow = "1.0.31"
 smallvec = "1.4.0"
 tendril = { git = "https://github.com/servo/tendril" }
+unicode-segmentation = "1.6.0"
+unicode-width = "0.1.7"
 # slab = "0.4.2"
diff --git a/helix-core/src/graphemes.rs b/helix-core/src/graphemes.rs
new file mode 100644
index 00000000..ec4e9d24
--- /dev/null
+++ b/helix-core/src/graphemes.rs
@@ -0,0 +1,213 @@
+// Based on https://github.com/cessen/led/blob/c4fa72405f510b7fd16052f90a598c429b3104a6/src/graphemes.rs
+use ropey::{iter::Chunks, str_utils::byte_to_char_idx, RopeSlice};
+use unicode_segmentation::{GraphemeCursor, GraphemeIncomplete};
+use unicode_width::UnicodeWidthStr;
+
+pub fn grapheme_width(g: &str) -> usize {
+    if g.as_bytes()[0] <= 127 {
+        // Fast-path ascii.
+        // Point 1: theoretically, ascii control characters should have zero
+        // width, but in our case we actually want them to have width: if they
+        // show up in text, we want to treat them as textual elements that can
+        // be editied.  So we can get away with making all ascii single width
+        // here.
+        // Point 2: we're only examining the first codepoint here, which means
+        // we're ignoring graphemes formed with combining characters.  However,
+        // if it starts with ascii, it's going to be a single-width grapeheme
+        // regardless, so, again, we can get away with that here.
+        // Point 3: we're only examining the first _byte_.  But for utf8, when
+        // checking for ascii range values only, that works.
+        1
+    } else {
+        // We use max(1) here because all grapeheme clusters--even illformed
+        // ones--should have at least some width so they can be edited
+        // properly.
+        UnicodeWidthStr::width(g).max(1)
+    }
+}
+
+pub fn nth_prev_grapheme_boundary(slice: &RopeSlice, char_idx: usize, n: usize) -> usize {
+    // TODO: implement this more efficiently.  This has to do a lot of
+    // re-scanning of rope chunks.  Probably move the main implementation here,
+    // and have prev_grapheme_boundary call this instead.
+    let mut char_idx = char_idx;
+    for _ in 0..n {
+        char_idx = prev_grapheme_boundary(slice, char_idx);
+    }
+    char_idx
+}
+
+/// Finds the previous grapheme boundary before the given char position.
+pub fn prev_grapheme_boundary(slice: &RopeSlice, char_idx: usize) -> usize {
+    // Bounds check
+    debug_assert!(char_idx <= slice.len_chars());
+
+    // We work with bytes for this, so convert.
+    let byte_idx = slice.char_to_byte(char_idx);
+
+    // Get the chunk with our byte index in it.
+    let (mut chunk, mut chunk_byte_idx, mut chunk_char_idx, _) = slice.chunk_at_byte(byte_idx);
+
+    // Set up the grapheme cursor.
+    let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true);
+
+    // Find the previous grapheme cluster boundary.
+    loop {
+        match gc.prev_boundary(chunk, chunk_byte_idx) {
+            Ok(None) => return 0,
+            Ok(Some(n)) => {
+                let tmp = byte_to_char_idx(chunk, n - chunk_byte_idx);
+                return chunk_char_idx + tmp;
+            }
+            Err(GraphemeIncomplete::PrevChunk) => {
+                let (a, b, c, _) = slice.chunk_at_byte(chunk_byte_idx - 1);
+                chunk = a;
+                chunk_byte_idx = b;
+                chunk_char_idx = c;
+            }
+            Err(GraphemeIncomplete::PreContext(n)) => {
+                let ctx_chunk = slice.chunk_at_byte(n - 1).0;
+                gc.provide_context(ctx_chunk, n - ctx_chunk.len());
+            }
+            _ => unreachable!(),
+        }
+    }
+}
+
+pub fn nth_next_grapheme_boundary(slice: &RopeSlice, char_idx: usize, n: usize) -> usize {
+    // TODO: implement this more efficiently.  This has to do a lot of
+    // re-scanning of rope chunks.  Probably move the main implementation here,
+    // and have next_grapheme_boundary call this instead.
+    let mut char_idx = char_idx;
+    for _ in 0..n {
+        char_idx = next_grapheme_boundary(slice, char_idx);
+    }
+    char_idx
+}
+
+/// Finds the next grapheme boundary after the given char position.
+pub fn next_grapheme_boundary(slice: &RopeSlice, char_idx: usize) -> usize {
+    // Bounds check
+    debug_assert!(char_idx <= slice.len_chars());
+
+    // We work with bytes for this, so convert.
+    let byte_idx = slice.char_to_byte(char_idx);
+
+    // Get the chunk with our byte index in it.
+    let (mut chunk, mut chunk_byte_idx, mut chunk_char_idx, _) = slice.chunk_at_byte(byte_idx);
+
+    // Set up the grapheme cursor.
+    let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true);
+
+    // Find the next grapheme cluster boundary.
+    loop {
+        match gc.next_boundary(chunk, chunk_byte_idx) {
+            Ok(None) => return slice.len_chars(),
+            Ok(Some(n)) => {
+                let tmp = byte_to_char_idx(chunk, n - chunk_byte_idx);
+                return chunk_char_idx + tmp;
+            }
+            Err(GraphemeIncomplete::NextChunk) => {
+                chunk_byte_idx += chunk.len();
+                let (a, _, c, _) = slice.chunk_at_byte(chunk_byte_idx);
+                chunk = a;
+                chunk_char_idx = c;
+            }
+            Err(GraphemeIncomplete::PreContext(n)) => {
+                let ctx_chunk = slice.chunk_at_byte(n - 1).0;
+                gc.provide_context(ctx_chunk, n - ctx_chunk.len());
+            }
+            _ => unreachable!(),
+        }
+    }
+}
+
+/// Returns whether the given char position is a grapheme boundary.
+pub fn is_grapheme_boundary(slice: &RopeSlice, char_idx: usize) -> bool {
+    // Bounds check
+    debug_assert!(char_idx <= slice.len_chars());
+
+    // We work with bytes for this, so convert.
+    let byte_idx = slice.char_to_byte(char_idx);
+
+    // Get the chunk with our byte index in it.
+    let (chunk, chunk_byte_idx, _, _) = slice.chunk_at_byte(byte_idx);
+
+    // Set up the grapheme cursor.
+    let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true);
+
+    // Determine if the given position is a grapheme cluster boundary.
+    loop {
+        match gc.is_boundary(chunk, chunk_byte_idx) {
+            Ok(n) => return n,
+            Err(GraphemeIncomplete::PreContext(n)) => {
+                let (ctx_chunk, ctx_byte_start, _, _) = slice.chunk_at_byte(n - 1);
+                gc.provide_context(ctx_chunk, ctx_byte_start);
+            }
+            _ => unreachable!(),
+        }
+    }
+}
+
+/// An iterator over the graphemes of a RopeSlice.
+#[derive(Clone)]
+pub struct RopeGraphemes<'a> {
+    text: RopeSlice<'a>,
+    chunks: Chunks<'a>,
+    cur_chunk: &'a str,
+    cur_chunk_start: usize,
+    cursor: GraphemeCursor,
+}
+
+impl<'a> RopeGraphemes<'a> {
+    pub fn new<'b>(slice: &RopeSlice<'b>) -> RopeGraphemes<'b> {
+        let mut chunks = slice.chunks();
+        let first_chunk = chunks.next().unwrap_or("");
+        RopeGraphemes {
+            text: *slice,
+            chunks: chunks,
+            cur_chunk: first_chunk,
+            cur_chunk_start: 0,
+            cursor: GraphemeCursor::new(0, slice.len_bytes(), true),
+        }
+    }
+}
+
+impl<'a> Iterator for RopeGraphemes<'a> {
+    type Item = RopeSlice<'a>;
+
+    fn next(&mut self) -> Option<RopeSlice<'a>> {
+        let a = self.cursor.cur_cursor();
+        let b;
+        loop {
+            match self
+                .cursor
+                .next_boundary(self.cur_chunk, self.cur_chunk_start)
+            {
+                Ok(None) => {
+                    return None;
+                }
+                Ok(Some(n)) => {
+                    b = n;
+                    break;
+                }
+                Err(GraphemeIncomplete::NextChunk) => {
+                    self.cur_chunk_start += self.cur_chunk.len();
+                    self.cur_chunk = self.chunks.next().unwrap_or("");
+                }
+                _ => unreachable!(),
+            }
+        }
+
+        if a < self.cur_chunk_start {
+            let a_char = self.text.byte_to_char(a);
+            let b_char = self.text.byte_to_char(b);
+
+            Some(self.text.slice(a_char..b_char))
+        } else {
+            let a2 = a - self.cur_chunk_start;
+            let b2 = b - self.cur_chunk_start;
+            Some((&self.cur_chunk[a2..b2]).into())
+        }
+    }
+}
diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs
index 421d8f3c..d2c78d3f 100644
--- a/helix-core/src/lib.rs
+++ b/helix-core/src/lib.rs
@@ -1,4 +1,5 @@
 mod buffer;
+mod graphemes;
 mod selection;
 mod state;
 mod transaction;
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index 98bbdb7f..b02560a8 100644
--- a/helix-core/src/selection.rs
+++ b/helix-core/src/selection.rs
@@ -99,8 +99,8 @@ impl Range {
 /// A selection consists of one or more selection ranges.
 pub struct Selection {
     // TODO: decide how many ranges to inline SmallVec<[Range; 1]>
-    ranges: SmallVec<[Range; 1]>,
-    primary_index: usize,
+    pub(crate) ranges: SmallVec<[Range; 1]>,
+    pub(crate) primary_index: usize,
 }
 
 impl Selection {
diff --git a/helix-core/src/state.rs b/helix-core/src/state.rs
index 22de6ca7..682d298a 100644
--- a/helix-core/src/state.rs
+++ b/helix-core/src/state.rs
@@ -1,17 +1,30 @@
-use crate::{Buffer, Selection};
+use crate::graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary};
+use crate::{Buffer, Selection, SelectionRange};
 
 /// A state represents the current editor state of a single buffer.
 pub struct State {
-    // TODO: maybe doc: ?
-    buffer: Buffer,
+    doc: Buffer,
     selection: Selection,
 }
 
+#[derive(Copy, Clone, PartialEq, Eq)]
+pub enum Direction {
+    Forward,
+    Backward,
+}
+#[derive(Copy, Clone, PartialEq, Eq)]
+pub enum Granularity {
+    Character,
+    Word,
+    Line,
+    // LineBoundary
+}
+
 impl State {
     #[must_use]
-    pub fn new(buffer: Buffer) -> Self {
+    pub fn new(doc: Buffer) -> Self {
         Self {
-            buffer,
+            doc,
             selection: Selection::single(0, 0),
         }
     }
@@ -40,4 +53,67 @@ impl State {
     // syntax
     // foldable
     // changeFilter/transactionFilter
+
+    pub fn move_pos(
+        &self,
+        pos: usize,
+        dir: Direction,
+        granularity: Granularity,
+        n: usize,
+    ) -> usize {
+        let text = &self.doc.contents;
+        match (dir, granularity) {
+            (Direction::Backward, Granularity::Character) => {
+                nth_prev_grapheme_boundary(&text.slice(..), pos, n)
+            }
+            (Direction::Forward, Granularity::Character) => {
+                nth_next_grapheme_boundary(&text.slice(..), pos, n)
+            }
+            _ => pos,
+        }
+    }
+
+    pub fn move_selection(
+        &self,
+        sel: Selection,
+        dir: Direction,
+        granularity: Granularity,
+        // TODO: n
+    ) -> Selection {
+        // TODO: move all selections according to normal cursor move semantics by collapsing it
+        // into cursors and moving them vertically
+
+        let ranges = sel.ranges.into_iter().map(|range| {
+            // let pos = if !range.is_empty() {
+            //     // if selection already exists, bump it to the start or end of current select first
+            //     if dir == Direction::Backward {
+            //         range.from()
+            //     } else {
+            //         range.to()
+            //     }
+            // } else {
+                let pos = self.move_pos(range.head, dir, granularity, 1)
+            // };
+            SelectionRange::new(pos, pos)
+        });
+
+        Selection::new(ranges.collect(), sel.primary_index)
+        // TODO: update selection in state via transaction
+    }
+
+    pub fn extend_selection(
+        &self,
+        sel: Selection,
+        dir: Direction,
+        granularity: Granularity,
+        n: usize,
+    ) -> Selection {
+        let ranges = sel.ranges.into_iter().map(|range| {
+            let pos = self.move_pos(range.head, dir, granularity, n);
+            SelectionRange::new(range.anchor, pos)
+        });
+
+        Selection::new(ranges.collect(), sel.primary_index)
+        // TODO: update selection in state via transaction
+    }
 }
diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml
index 0ab6209e..bd488d2f 100644
--- a/helix-term/Cargo.toml
+++ b/helix-term/Cargo.toml
@@ -29,3 +29,4 @@ futures = { version = "0.3.5", default-features = false, features = ["std"] }
 smol = "0.1.10"
 num_cpus = "1.13.0"
 piper = "0.1.2"
+tui = { version = "0.9.5", default-features = false }
diff --git a/helix-term/src/component.rs b/helix-term/src/component.rs
new file mode 100644
index 00000000..8ec5663a
--- /dev/null
+++ b/helix-term/src/component.rs
@@ -0,0 +1,18 @@
+
+// IDEA: render to a cache buffer, then if not changed, copy the buf into the parent
+pub trait Component {
+    /// Process input events, return true if handled.
+    fn process_event(&mut self, event: crossterm::event::Event, args) -> bool;
+    /// Should redraw? Useful for saving redraw cycles if we know component didn't change.
+    fn should_update(&self) -> bool { true }
+
+    fn render(&mut self, surface: &mut Surface, args: ());
+}
+
+// HStack / VStack 
+// focus by component id: each View/Editor gets it's own incremental id at create
+// Component: View(Arc<State>) -> multiple views can point to same state
+// id 0 = prompt?
+// when entering to prompt, it needs to direct Commands to last focus window
+// -> prompt.trigger(focus_id), on_leave -> focus(focus_id)
+// popups on another layer
diff --git a/helix-term/src/editor.rs b/helix-term/src/editor.rs
index 54c70e1b..111c3273 100644
--- a/helix-term/src/editor.rs
+++ b/helix-term/src/editor.rs
@@ -278,4 +278,11 @@ impl Editor {
         println!("The text you entered: {}", typed_text);
         Ok(())
     }
+
+    pub fn render(&self) {
+        // create a new window sized surface
+        // paint all components
+        // diff vs last frame, swap
+        // paint diff
+    }
 }
diff --git a/helix-term/src/line.rs b/helix-term/src/line.rs
index 58d4c9d8..7cbfab61 100644
--- a/helix-term/src/line.rs
+++ b/helix-term/src/line.rs
@@ -11,6 +11,8 @@ use futures::{future::FutureExt, select, StreamExt};
 use smol::Timer;
 // use futures_timer::Delay;
 
+use tui::{backend::CrosstermBackend, Terminal};
+
 use crossterm::{
     cursor::position,
     event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream, KeyCode},
@@ -64,6 +66,9 @@ fn main() -> Result<()> {
     let mut stdout = stdout();
     execute!(stdout, EnableMouseCapture)?;
 
+    let backend = CrosstermBackend::new(stdout);
+    let mut terminal = Terminal::new(backend)?;
+
     use std::thread;
 
     // Same number of threads as there are CPU cores.
diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs
index aaa83a86..e21fc7d7 100644
--- a/helix-term/src/main.rs
+++ b/helix-term/src/main.rs
@@ -1,4 +1,5 @@
 // mod editor;
+mod component;
 
 // use editor::Editor;