@@ -0,0 +1,213 @@
1+// Based on https://github.com/cessen/led/blob/c4fa72405f510b7fd16052f90a598c429b3104a6/src/graphemes.rs
2+use ropey::{iter::Chunks, str_utils::byte_to_char_idx, RopeSlice};
3+use unicode_segmentation::{GraphemeCursor, GraphemeIncomplete};
4+use unicode_width::UnicodeWidthStr;
6+pub fn grapheme_width(g: &str) -> usize {
7+ if g.as_bytes()[0] <= 127 {
9+ // Point 1: theoretically, ascii control characters should have zero
10+ // width, but in our case we actually want them to have width: if they
11+ // show up in text, we want to treat them as textual elements that can
12+ // be editied. So we can get away with making all ascii single width
14+ // Point 2: we're only examining the first codepoint here, which means
15+ // we're ignoring graphemes formed with combining characters. However,
16+ // if it starts with ascii, it's going to be a single-width grapeheme
17+ // regardless, so, again, we can get away with that here.
18+ // Point 3: we're only examining the first _byte_. But for utf8, when
19+ // checking for ascii range values only, that works.
22+ // We use max(1) here because all grapeheme clusters--even illformed
23+ // ones--should have at least some width so they can be edited
25+ UnicodeWidthStr::width(g).max(1)
29+pub fn nth_prev_grapheme_boundary(slice: &RopeSlice, char_idx: usize, n: usize) -> usize {
30+ // TODO: implement this more efficiently. This has to do a lot of
31+ // re-scanning of rope chunks. Probably move the main implementation here,
32+ // and have prev_grapheme_boundary call this instead.
33+ let mut char_idx = char_idx;
35+ char_idx = prev_grapheme_boundary(slice, char_idx);
40+/// Finds the previous grapheme boundary before the given char position.
41+pub fn prev_grapheme_boundary(slice: &RopeSlice, char_idx: usize) -> usize {
43+ debug_assert!(char_idx <= slice.len_chars());
45+ // We work with bytes for this, so convert.
46+ let byte_idx = slice.char_to_byte(char_idx);
48+ // Get the chunk with our byte index in it.
49+ let (mut chunk, mut chunk_byte_idx, mut chunk_char_idx, _) = slice.chunk_at_byte(byte_idx);
51+ // Set up the grapheme cursor.
52+ let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true);
54+ // Find the previous grapheme cluster boundary.
56+ match gc.prev_boundary(chunk, chunk_byte_idx) {
57+ Ok(None) => return 0,
59+ let tmp = byte_to_char_idx(chunk, n - chunk_byte_idx);
60+ return chunk_char_idx + tmp;
62+ Err(GraphemeIncomplete::PrevChunk) => {
63+ let (a, b, c, _) = slice.chunk_at_byte(chunk_byte_idx - 1);
68+ Err(GraphemeIncomplete::PreContext(n)) => {
69+ let ctx_chunk = slice.chunk_at_byte(n - 1).0;
70+ gc.provide_context(ctx_chunk, n - ctx_chunk.len());
77+pub fn nth_next_grapheme_boundary(slice: &RopeSlice, char_idx: usize, n: usize) -> usize {
78+ // TODO: implement this more efficiently. This has to do a lot of
79+ // re-scanning of rope chunks. Probably move the main implementation here,
80+ // and have next_grapheme_boundary call this instead.
81+ let mut char_idx = char_idx;
83+ char_idx = next_grapheme_boundary(slice, char_idx);
88+/// Finds the next grapheme boundary after the given char position.
89+pub fn next_grapheme_boundary(slice: &RopeSlice, char_idx: usize) -> usize {
91+ debug_assert!(char_idx <= slice.len_chars());
93+ // We work with bytes for this, so convert.
94+ let byte_idx = slice.char_to_byte(char_idx);
96+ // Get the chunk with our byte index in it.
97+ let (mut chunk, mut chunk_byte_idx, mut chunk_char_idx, _) = slice.chunk_at_byte(byte_idx);
99+ // Set up the grapheme cursor.
100+ let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true);
102+ // Find the next grapheme cluster boundary.
104+ match gc.next_boundary(chunk, chunk_byte_idx) {
105+ Ok(None) => return slice.len_chars(),
107+ let tmp = byte_to_char_idx(chunk, n - chunk_byte_idx);
108+ return chunk_char_idx + tmp;
110+ Err(GraphemeIncomplete::NextChunk) => {
111+ chunk_byte_idx += chunk.len();
112+ let (a, _, c, _) = slice.chunk_at_byte(chunk_byte_idx);
116+ Err(GraphemeIncomplete::PreContext(n)) => {
117+ let ctx_chunk = slice.chunk_at_byte(n - 1).0;
118+ gc.provide_context(ctx_chunk, n - ctx_chunk.len());
120+ _ => unreachable!(),
125+/// Returns whether the given char position is a grapheme boundary.
126+pub fn is_grapheme_boundary(slice: &RopeSlice, char_idx: usize) -> bool {
128+ debug_assert!(char_idx <= slice.len_chars());
130+ // We work with bytes for this, so convert.
131+ let byte_idx = slice.char_to_byte(char_idx);
133+ // Get the chunk with our byte index in it.
134+ let (chunk, chunk_byte_idx, _, _) = slice.chunk_at_byte(byte_idx);
136+ // Set up the grapheme cursor.
137+ let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true);
139+ // Determine if the given position is a grapheme cluster boundary.
141+ match gc.is_boundary(chunk, chunk_byte_idx) {
143+ Err(GraphemeIncomplete::PreContext(n)) => {
144+ let (ctx_chunk, ctx_byte_start, _, _) = slice.chunk_at_byte(n - 1);
145+ gc.provide_context(ctx_chunk, ctx_byte_start);
147+ _ => unreachable!(),
152+/// An iterator over the graphemes of a RopeSlice.
154+pub struct RopeGraphemes<'a> {
155+ text: RopeSlice<'a>,
158+ cur_chunk_start: usize,
159+ cursor: GraphemeCursor,
162+impl<'a> RopeGraphemes<'a> {
163+ pub fn new<'b>(slice: &RopeSlice<'b>) -> RopeGraphemes<'b> {
164+ let mut chunks = slice.chunks();
165+ let first_chunk = chunks.next().unwrap_or("");
169+ cur_chunk: first_chunk,
171+ cursor: GraphemeCursor::new(0, slice.len_bytes(), true),
176+impl<'a> Iterator for RopeGraphemes<'a> {
177+ type Item = RopeSlice<'a>;
179+ fn next(&mut self) -> Option<RopeSlice<'a>> {
180+ let a = self.cursor.cur_cursor();
185+ .next_boundary(self.cur_chunk, self.cur_chunk_start)
194+ Err(GraphemeIncomplete::NextChunk) => {
195+ self.cur_chunk_start += self.cur_chunk.len();
196+ self.cur_chunk = self.chunks.next().unwrap_or("");
198+ _ => unreachable!(),
202+ if a < self.cur_chunk_start {
203+ let a_char = self.text.byte_to_char(a);
204+ let b_char = self.text.byte_to_char(b);
206+ Some(self.text.slice(a_char..b_char))
208+ let a2 = a - self.cur_chunk_start;
209+ let b2 = b - self.cur_chunk_start;
210+ Some((&self.cur_chunk[a2..b2]).into())