@@ -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;
5+
6+pub fn grapheme_width(g: &str) -> usize {
7+ if g.as_bytes()[0] <= 127 {
8+ // Fast-path ascii.
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
13+ // here.
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.
20+ 1
21+ } else {
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
24+ // properly.
25+ UnicodeWidthStr::width(g).max(1)
26+ }
27+}
28+
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;
34+ for _ in 0..n {
35+ char_idx = prev_grapheme_boundary(slice, char_idx);
36+ }
37+ char_idx
38+}
39+
40+/// Finds the previous grapheme boundary before the given char position.
41+pub fn prev_grapheme_boundary(slice: &RopeSlice, char_idx: usize) -> usize {
42+ // Bounds check
43+ debug_assert!(char_idx <= slice.len_chars());
44+
45+ // We work with bytes for this, so convert.
46+ let byte_idx = slice.char_to_byte(char_idx);
47+
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);
50+
51+ // Set up the grapheme cursor.
52+ let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true);
53+
54+ // Find the previous grapheme cluster boundary.
55+ loop {
56+ match gc.prev_boundary(chunk, chunk_byte_idx) {
57+ Ok(None) => return 0,
58+ Ok(Some(n)) => {
59+ let tmp = byte_to_char_idx(chunk, n - chunk_byte_idx);
60+ return chunk_char_idx + tmp;
61+ }
62+ Err(GraphemeIncomplete::PrevChunk) => {
63+ let (a, b, c, _) = slice.chunk_at_byte(chunk_byte_idx - 1);
64+ chunk = a;
65+ chunk_byte_idx = b;
66+ chunk_char_idx = c;
67+ }
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());
71+ }
72+ _ => unreachable!(),
73+ }
74+ }
75+}
76+
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;
82+ for _ in 0..n {
83+ char_idx = next_grapheme_boundary(slice, char_idx);
84+ }
85+ char_idx
86+}
87+
88+/// Finds the next grapheme boundary after the given char position.
89+pub fn next_grapheme_boundary(slice: &RopeSlice, char_idx: usize) -> usize {
90+ // Bounds check
91+ debug_assert!(char_idx <= slice.len_chars());
92+
93+ // We work with bytes for this, so convert.
94+ let byte_idx = slice.char_to_byte(char_idx);
95+
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);
98+
99+ // Set up the grapheme cursor.
100+ let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true);
101+
102+ // Find the next grapheme cluster boundary.
103+ loop {
104+ match gc.next_boundary(chunk, chunk_byte_idx) {
105+ Ok(None) => return slice.len_chars(),
106+ Ok(Some(n)) => {
107+ let tmp = byte_to_char_idx(chunk, n - chunk_byte_idx);
108+ return chunk_char_idx + tmp;
109+ }
110+ Err(GraphemeIncomplete::NextChunk) => {
111+ chunk_byte_idx += chunk.len();
112+ let (a, _, c, _) = slice.chunk_at_byte(chunk_byte_idx);
113+ chunk = a;
114+ chunk_char_idx = c;
115+ }
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());
119+ }
120+ _ => unreachable!(),
121+ }
122+ }
123+}
124+
125+/// Returns whether the given char position is a grapheme boundary.
126+pub fn is_grapheme_boundary(slice: &RopeSlice, char_idx: usize) -> bool {
127+ // Bounds check
128+ debug_assert!(char_idx <= slice.len_chars());
129+
130+ // We work with bytes for this, so convert.
131+ let byte_idx = slice.char_to_byte(char_idx);
132+
133+ // Get the chunk with our byte index in it.
134+ let (chunk, chunk_byte_idx, _, _) = slice.chunk_at_byte(byte_idx);
135+
136+ // Set up the grapheme cursor.
137+ let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true);
138+
139+ // Determine if the given position is a grapheme cluster boundary.
140+ loop {
141+ match gc.is_boundary(chunk, chunk_byte_idx) {
142+ Ok(n) => return n,
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);
146+ }
147+ _ => unreachable!(),
148+ }
149+ }
150+}
151+
152+/// An iterator over the graphemes of a RopeSlice.
153+#[derive(Clone)]
154+pub struct RopeGraphemes<'a> {
155+ text: RopeSlice<'a>,
156+ chunks: Chunks<'a>,
157+ cur_chunk: &'a str,
158+ cur_chunk_start: usize,
159+ cursor: GraphemeCursor,
160+}
161+
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("");
166+ RopeGraphemes {
167+ text: *slice,
168+ chunks: chunks,
169+ cur_chunk: first_chunk,
170+ cur_chunk_start: 0,
171+ cursor: GraphemeCursor::new(0, slice.len_bytes(), true),
172+ }
173+ }
174+}
175+
176+impl<'a> Iterator for RopeGraphemes<'a> {
177+ type Item = RopeSlice<'a>;
178+
179+ fn next(&mut self) -> Option<RopeSlice<'a>> {
180+ let a = self.cursor.cur_cursor();
181+ let b;
182+ loop {
183+ match self
184+ .cursor
185+ .next_boundary(self.cur_chunk, self.cur_chunk_start)
186+ {
187+ Ok(None) => {
188+ return None;
189+ }
190+ Ok(Some(n)) => {
191+ b = n;
192+ break;
193+ }
194+ Err(GraphemeIncomplete::NextChunk) => {
195+ self.cur_chunk_start += self.cur_chunk.len();
196+ self.cur_chunk = self.chunks.next().unwrap_or("");
197+ }
198+ _ => unreachable!(),
199+ }
200+ }
201+
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);
205+
206+ Some(self.text.slice(a_char..b_char))
207+ } else {
208+ let a2 = a - self.cur_chunk_start;
209+ let b2 = b - self.cur_chunk_start;
210+ Some((&self.cur_chunk[a2..b2]).into())
211+ }
212+ }
213+}