mod.rs

  1mod fold_map;
  2
  3use super::{buffer, Anchor, AnchorRangeExt, Buffer, Edit, Point, TextSummary, ToOffset, ToPoint};
  4use anyhow::Result;
  5pub use fold_map::BufferRows;
  6use fold_map::FoldMap;
  7use gpui::{AppContext, Entity, ModelContext, ModelHandle};
  8use std::ops::Range;
  9
 10#[derive(Copy, Clone)]
 11pub enum Bias {
 12    Left,
 13    Right,
 14}
 15
 16pub struct DisplayMap {
 17    buffer: ModelHandle<Buffer>,
 18    fold_map: FoldMap,
 19    tab_size: usize,
 20}
 21
 22impl Entity for DisplayMap {
 23    type Event = ();
 24}
 25
 26impl DisplayMap {
 27    pub fn new(buffer: ModelHandle<Buffer>, tab_size: usize, ctx: &mut ModelContext<Self>) -> Self {
 28        ctx.subscribe(&buffer, Self::handle_buffer_event);
 29
 30        DisplayMap {
 31            buffer: buffer.clone(),
 32            fold_map: FoldMap::new(buffer, ctx.as_ref()),
 33            tab_size,
 34        }
 35    }
 36
 37    pub fn fold<T: ToOffset>(
 38        &mut self,
 39        ranges: impl IntoIterator<Item = Range<T>>,
 40        ctx: &mut ModelContext<Self>,
 41    ) -> Result<()> {
 42        self.fold_map.fold(ranges, ctx.as_ref())?;
 43        ctx.notify();
 44        Ok(())
 45    }
 46
 47    pub fn unfold<T: ToOffset>(
 48        &mut self,
 49        ranges: impl IntoIterator<Item = Range<T>>,
 50        ctx: &mut ModelContext<Self>,
 51    ) -> Result<()> {
 52        self.fold_map.unfold(ranges, ctx.as_ref())?;
 53        ctx.notify();
 54        Ok(())
 55    }
 56
 57    pub fn is_line_folded(&self, display_row: u32) -> bool {
 58        self.fold_map.is_line_folded(display_row)
 59    }
 60
 61    pub fn text(&self, app: &AppContext) -> String {
 62        self.chars_at(DisplayPoint::zero(), app).unwrap().collect()
 63    }
 64
 65    pub fn line(&self, display_row: u32, app: &AppContext) -> Result<String> {
 66        let chars = self.chars_at(DisplayPoint::new(display_row, 0), app)?;
 67        Ok(chars.take_while(|c| *c != '\n').collect())
 68    }
 69
 70    pub fn line_indent(&self, display_row: u32, app: &AppContext) -> Result<(u32, bool)> {
 71        let mut indent = 0;
 72        let mut is_blank = true;
 73        for c in self.chars_at(DisplayPoint::new(display_row, 0), app)? {
 74            if c == ' ' {
 75                indent += 1;
 76            } else {
 77                is_blank = c == '\n';
 78                break;
 79            }
 80        }
 81        Ok((indent, is_blank))
 82    }
 83
 84    pub fn chars_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Result<Chars<'a>> {
 85        let column = point.column() as usize;
 86        let (point, to_next_stop) = point.collapse_tabs(self, Bias::Left, app)?;
 87        let mut fold_chars = self.fold_map.chars_at(point, app)?;
 88        if to_next_stop > 0 {
 89            fold_chars.next();
 90        }
 91
 92        Ok(Chars {
 93            fold_chars,
 94            column,
 95            to_next_stop,
 96            tab_size: self.tab_size,
 97        })
 98    }
 99
100    pub fn buffer_rows(&self, start_row: u32) -> Result<BufferRows> {
101        self.fold_map.buffer_rows(start_row)
102    }
103
104    pub fn line_len(&self, row: u32, ctx: &AppContext) -> Result<u32> {
105        DisplayPoint::new(row, self.fold_map.line_len(row, ctx)?)
106            .expand_tabs(self, ctx)
107            .map(|point| point.column())
108    }
109
110    pub fn max_point(&self, app: &AppContext) -> DisplayPoint {
111        self.fold_map.max_point().expand_tabs(self, app).unwrap()
112    }
113
114    pub fn rightmost_point(&self) -> DisplayPoint {
115        self.fold_map.rightmost_point()
116    }
117
118    pub fn anchor_before(
119        &self,
120        point: DisplayPoint,
121        bias: Bias,
122        app: &AppContext,
123    ) -> Result<Anchor> {
124        self.buffer
125            .read(app)
126            .anchor_before(point.to_buffer_point(self, bias, app)?)
127    }
128
129    pub fn anchor_after(
130        &self,
131        point: DisplayPoint,
132        bias: Bias,
133        app: &AppContext,
134    ) -> Result<Anchor> {
135        self.buffer
136            .read(app)
137            .anchor_after(point.to_buffer_point(self, bias, app)?)
138    }
139
140    fn handle_buffer_event(&mut self, event: &buffer::Event, ctx: &mut ModelContext<Self>) {
141        match event {
142            buffer::Event::Edited(edits) => self.fold_map.apply_edits(edits, ctx.as_ref()).unwrap(),
143            _ => {}
144        }
145    }
146}
147
148#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
149pub struct DisplayPoint(Point);
150
151impl DisplayPoint {
152    pub fn new(row: u32, column: u32) -> Self {
153        Self(Point::new(row, column))
154    }
155
156    pub fn zero() -> Self {
157        Self::new(0, 0)
158    }
159
160    pub fn row(self) -> u32 {
161        self.0.row
162    }
163
164    pub fn column(self) -> u32 {
165        self.0.column
166    }
167
168    pub fn row_mut(&mut self) -> &mut u32 {
169        &mut self.0.row
170    }
171
172    pub fn column_mut(&mut self) -> &mut u32 {
173        &mut self.0.column
174    }
175
176    pub fn to_buffer_point(self, map: &DisplayMap, bias: Bias, app: &AppContext) -> Result<Point> {
177        Ok(map
178            .fold_map
179            .to_buffer_point(self.collapse_tabs(map, bias, app)?.0))
180    }
181
182    fn expand_tabs(mut self, map: &DisplayMap, app: &AppContext) -> Result<Self> {
183        let chars = map
184            .fold_map
185            .chars_at(DisplayPoint(Point::new(self.row(), 0)), app)?;
186        let expanded = expand_tabs(chars, self.column() as usize, map.tab_size);
187        *self.column_mut() = expanded as u32;
188
189        Ok(self)
190    }
191
192    fn collapse_tabs(
193        mut self,
194        map: &DisplayMap,
195        bias: Bias,
196        app: &AppContext,
197    ) -> Result<(Self, usize)> {
198        let chars = map
199            .fold_map
200            .chars_at(DisplayPoint(Point::new(self.0.row, 0)), app)?;
201        let expanded = self.column() as usize;
202        let (collapsed, to_next_stop) = collapse_tabs(chars, expanded, bias, map.tab_size);
203        *self.column_mut() = collapsed as u32;
204
205        Ok((self, to_next_stop))
206    }
207}
208
209impl Point {
210    pub fn to_display_point(self, map: &DisplayMap, app: &AppContext) -> Result<DisplayPoint> {
211        let mut display_point = map.fold_map.to_display_point(self);
212        let chars = map
213            .fold_map
214            .chars_at(DisplayPoint::new(display_point.row(), 0), app)?;
215        *display_point.column_mut() =
216            expand_tabs(chars, display_point.column() as usize, map.tab_size) as u32;
217        Ok(display_point)
218    }
219}
220
221impl Anchor {
222    pub fn to_display_point(&self, map: &DisplayMap, app: &AppContext) -> Result<DisplayPoint> {
223        self.to_point(map.buffer.read(app))?
224            .to_display_point(map, app)
225    }
226}
227
228pub struct Chars<'a> {
229    fold_chars: fold_map::Chars<'a>,
230    column: usize,
231    to_next_stop: usize,
232    tab_size: usize,
233}
234
235impl<'a> Iterator for Chars<'a> {
236    type Item = char;
237
238    fn next(&mut self) -> Option<Self::Item> {
239        if self.to_next_stop > 0 {
240            self.to_next_stop -= 1;
241            self.column += 1;
242            Some(' ')
243        } else {
244            self.fold_chars.next().map(|c| match c {
245                '\t' => {
246                    self.to_next_stop = self.tab_size - self.column % self.tab_size - 1;
247                    self.column += 1;
248                    ' '
249                }
250                '\n' => {
251                    self.column = 0;
252                    c
253                }
254                _ => {
255                    self.column += 1;
256                    c
257                }
258            })
259        }
260    }
261}
262
263pub fn expand_tabs(chars: impl Iterator<Item = char>, column: usize, tab_size: usize) -> usize {
264    let mut expanded = 0;
265    for c in chars.take(column) {
266        if c == '\t' {
267            expanded += tab_size - expanded % tab_size;
268        } else {
269            expanded += 1;
270        }
271    }
272    expanded
273}
274
275pub fn collapse_tabs(
276    mut chars: impl Iterator<Item = char>,
277    column: usize,
278    bias: Bias,
279    tab_size: usize,
280) -> (usize, usize) {
281    let mut expanded = 0;
282    let mut collapsed = 0;
283    while let Some(c) = chars.next() {
284        if expanded == column {
285            break;
286        }
287
288        if c == '\t' {
289            expanded += tab_size - (expanded % tab_size);
290            if expanded > column {
291                return match bias {
292                    Bias::Left => (collapsed, expanded - column),
293                    Bias::Right => (collapsed + 1, 0),
294                };
295            }
296            collapsed += 1;
297        } else {
298            expanded += 1;
299            collapsed += 1;
300        }
301    }
302    (collapsed, 0)
303}
304
305#[cfg(test)]
306mod tests {
307    use super::*;
308    use crate::test::*;
309    use gpui::App;
310
311    #[test]
312    fn test_chars_at() {
313        App::test((), |app| {
314            let text = sample_text(6, 6);
315            let buffer = app.add_model(|ctx| Buffer::new(0, text, ctx));
316            let map = app.add_model(|ctx| DisplayMap::new(buffer.clone(), 4, ctx));
317            buffer
318                .update(app, |buffer, ctx| {
319                    buffer.edit(
320                        vec![
321                            Point::new(1, 0)..Point::new(1, 0),
322                            Point::new(1, 1)..Point::new(1, 1),
323                            Point::new(2, 1)..Point::new(2, 1),
324                        ],
325                        "\t",
326                        Some(ctx),
327                    )
328                })
329                .unwrap();
330
331            let map = map.read(app);
332            assert_eq!(
333                map.chars_at(DisplayPoint::new(1, 0), app.as_ref())
334                    .unwrap()
335                    .take(10)
336                    .collect::<String>(),
337                "    b   bb"
338            );
339            assert_eq!(
340                map.chars_at(DisplayPoint::new(1, 2), app.as_ref())
341                    .unwrap()
342                    .take(10)
343                    .collect::<String>(),
344                "  b   bbbb"
345            );
346            assert_eq!(
347                map.chars_at(DisplayPoint::new(1, 6), app.as_ref())
348                    .unwrap()
349                    .take(13)
350                    .collect::<String>(),
351                "  bbbbb\nc   c"
352            );
353        });
354    }
355
356    #[test]
357    fn test_expand_tabs() {
358        assert_eq!(expand_tabs("\t".chars(), 0, 4), 0);
359        assert_eq!(expand_tabs("\t".chars(), 1, 4), 4);
360        assert_eq!(expand_tabs("\ta".chars(), 2, 4), 5);
361    }
362
363    #[test]
364    fn test_collapse_tabs() {
365        assert_eq!(collapse_tabs("\t".chars(), 0, Bias::Left, 4), (0, 0));
366        assert_eq!(collapse_tabs("\t".chars(), 0, Bias::Right, 4), (0, 0));
367        assert_eq!(collapse_tabs("\t".chars(), 1, Bias::Left, 4), (0, 3));
368        assert_eq!(collapse_tabs("\t".chars(), 1, Bias::Right, 4), (1, 0));
369        assert_eq!(collapse_tabs("\t".chars(), 2, Bias::Left, 4), (0, 2));
370        assert_eq!(collapse_tabs("\t".chars(), 2, Bias::Right, 4), (1, 0));
371        assert_eq!(collapse_tabs("\t".chars(), 3, Bias::Left, 4), (0, 1));
372        assert_eq!(collapse_tabs("\t".chars(), 3, Bias::Right, 4), (1, 0));
373        assert_eq!(collapse_tabs("\t".chars(), 4, Bias::Left, 4), (1, 0));
374        assert_eq!(collapse_tabs("\t".chars(), 4, Bias::Right, 4), (1, 0));
375        assert_eq!(collapse_tabs("\ta".chars(), 5, Bias::Left, 4), (2, 0));
376        assert_eq!(collapse_tabs("\ta".chars(), 5, Bias::Right, 4), (2, 0));
377    }
378
379    #[test]
380    fn test_max_point() {
381        App::test((), |app| {
382            let buffer = app.add_model(|ctx| Buffer::new(0, "aaa\n\t\tbbb", ctx));
383            let map = app.add_model(|ctx| DisplayMap::new(buffer.clone(), 4, ctx));
384            assert_eq!(
385                map.read(app).max_point(app.as_ref()),
386                DisplayPoint::new(1, 11)
387            )
388        });
389    }
390}