Skip to main content

gpui_base/input/editor/display_map/
display_map.rs

1/// DisplayMap: Public facade for Editor/Input display mapping.
2///
3/// This combines WrapMap and FoldMap to provide a unified API:
4/// - BufferPoint ↔ DisplayPoint conversion
5/// - Fold management (candidates, toggle, query)
6/// - Automatic projection updates on text/layout changes
7use std::ops::Range;
8
9use gpui::{App, Font, Pixels};
10use ropey::Rope;
11
12use super::fold_map::FoldMap;
13use super::folding::FoldRange;
14pub use super::text_wrapper::WrappingIndent;
15use super::text_wrapper::{LineItem, WrapDisplayPoint};
16use super::wrap_map::WrapMap;
17use super::{BufferPoint, DisplayPoint};
18use crate::input::Point as TreeSitterPoint;
19use crate::input::display_map::WrapPoint;
20use crate::input::rope_ext::RopeExt as _;
21
22/// DisplayMap is the main interface for Editor/Input coordinate mapping.
23///
24/// It manages the two-layer projection:
25/// 1. Buffer → Wrap (soft-wrapping)
26/// 2. Wrap → Display (folding)
27///
28/// Editor/Input only needs to work with BufferPoint and DisplayPoint.
29pub struct DisplayMap {
30    wrap_map: WrapMap,
31    fold_map: FoldMap,
32}
33
34impl DisplayMap {
35    pub fn new(font: Font, font_size: Pixels, wrap_width: Option<Pixels>) -> Self {
36        Self {
37            wrap_map: WrapMap::new(font, font_size, wrap_width),
38            fold_map: FoldMap::new(),
39        }
40    }
41
42    // ==================== Core Coordinate Mapping ====================
43
44    /// Convert buffer position to display position
45    pub fn buffer_pos_to_display_pos(&self, pos: BufferPoint) -> DisplayPoint {
46        // Buffer → Wrap
47        let wrap_pos = self.wrap_map.buffer_pos_to_wrap_pos(pos);
48
49        // Wrap → Display
50        if let Some(display_row) = self.fold_map.wrap_row_to_display_row(wrap_pos.row) {
51            DisplayPoint::new(display_row, wrap_pos.col)
52        } else {
53            // Cursor is in a folded region, find nearest visible row
54            let display_row = self.fold_map.nearest_visible_display_row(wrap_pos.row);
55            DisplayPoint::new(display_row, 0) // Column 0 at fold boundary
56        }
57    }
58
59    /// Convert display position to buffer position
60    pub fn display_pos_to_buffer_pos(&self, pos: DisplayPoint) -> BufferPoint {
61        // Display → Wrap
62        let wrap_row = self.fold_map.display_row_to_wrap_row(pos.row).unwrap_or(0);
63
64        // Wrap → Buffer
65        let wrap_pos = WrapPoint::new(wrap_row, pos.col);
66        self.wrap_map.wrap_pos_to_buffer_pos(wrap_pos)
67    }
68
69    /// Get total number of visible display rows
70    #[inline]
71    pub fn display_row_count(&self) -> usize {
72        self.fold_map.display_row_count()
73    }
74
75    /// Get the buffer line for a given display row
76    pub fn display_row_to_buffer_line(&self, display_row: usize) -> usize {
77        // Display → Wrap
78        let wrap_row = self
79            .fold_map
80            .display_row_to_wrap_row(display_row)
81            .unwrap_or(0);
82
83        // Wrap → Buffer line
84        self.wrap_map.wrap_row_to_buffer_line(wrap_row)
85    }
86
87    /// Get the display row range for a buffer line: [start, end)
88    /// Returns None if the buffer line is completely hidden
89    pub fn buffer_line_to_display_row_range(&self, line: usize) -> Option<Range<usize>> {
90        // Buffer line → Wrap row range
91        let wrap_row_range = self.wrap_map.buffer_line_to_wrap_row_range(line);
92
93        // Find first and last visible display rows in this range
94        let mut first_display_row = None;
95        let mut last_display_row = None;
96
97        for wrap_row in wrap_row_range {
98            if let Some(display_row) = self.fold_map.wrap_row_to_display_row(wrap_row) {
99                if first_display_row.is_none() {
100                    first_display_row = Some(display_row);
101                }
102                last_display_row = Some(display_row);
103            }
104        }
105
106        if let (Some(start), Some(end)) = (first_display_row, last_display_row) {
107            Some(start..end + 1)
108        } else {
109            None // Completely folded
110        }
111    }
112
113    /// Check if a buffer line is completely hidden
114    #[inline]
115    pub fn is_buffer_line_hidden(&self, line: usize) -> bool {
116        self.buffer_line_to_display_row_range(line).is_none()
117    }
118
119    /// First display row of a buffer line. If the line is fully folded, returns the
120    /// nearest visible display row.
121    pub fn buffer_line_to_display_row(&self, line: usize) -> usize {
122        match self.buffer_line_to_display_row_range(line) {
123            Some(range) => range.start,
124            None => {
125                let wrap_row = self.wrap_map.buffer_line_to_first_wrap_row(line);
126                self.fold_map.nearest_visible_display_row(wrap_row)
127            }
128        }
129    }
130
131    /// Set fold candidates (from tree-sitter/LSP)
132    pub fn set_fold_candidates(&mut self, candidates: Vec<FoldRange>) {
133        self.fold_map.set_candidates(candidates);
134        self.rebuild_fold_projection();
135    }
136
137    /// Set a fold at the given start_line (must be in candidates)
138    pub fn set_folded(&mut self, start_line: usize, folded: bool) {
139        self.fold_map.set_folded(start_line, folded);
140        self.rebuild_fold_projection();
141    }
142
143    /// Toggle fold at the given start_line
144    pub fn toggle_fold(&mut self, start_line: usize) {
145        self.fold_map.toggle_fold(start_line);
146        self.rebuild_fold_projection();
147    }
148
149    /// Check if a line is currently folded
150    #[inline]
151    pub fn is_folded_at(&self, start_line: usize) -> bool {
152        self.fold_map.is_folded_at(start_line)
153    }
154
155    /// Check if a line is a fold candidate
156    #[inline]
157    pub fn is_fold_candidate(&self, start_line: usize) -> bool {
158        self.fold_map.is_fold_candidate(start_line)
159    }
160
161    /// Get all currently folded ranges
162    #[inline]
163    pub fn folded_ranges(&self) -> &[FoldRange] {
164        self.fold_map.folded_ranges()
165    }
166
167    /// Clear all folds
168    pub fn clear_folds(&mut self) {
169        self.fold_map.clear_folds();
170        self.rebuild_fold_projection();
171    }
172
173    /// Get all fold candidates (the ranges that can be folded)
174    #[inline]
175    pub(crate) fn fold_candidates(&self) -> &[FoldRange] {
176        self.fold_map.fold_candidates()
177    }
178
179    /// Replace the folded ranges: fold each candidate starting at one of
180    /// `start_lines` and unfold all others. Lines that are not candidates are
181    /// ignored. The fold projection is rebuilt once.
182    pub(crate) fn set_folded_lines(&mut self, start_lines: impl IntoIterator<Item = usize>) {
183        self.fold_map.clear_folds();
184        for line in start_lines {
185            self.fold_map.set_folded(line, true);
186        }
187        self.rebuild_fold_projection();
188    }
189
190    // ==================== Text and Layout Updates ====================
191
192    /// Adjust folds and candidates for a text edit before updating the wrap map.
193    ///
194    /// Must be called with the OLD text (before replacement) and the edit range/new_text
195    /// so we can compute which old lines were affected.
196    pub fn adjust_folds_for_edit(&mut self, old_text: &Rope, range: &Range<usize>, new_text: &str) {
197        if self.fold_map.folded_ranges().is_empty() && self.fold_map.fold_candidates().is_empty() {
198            return;
199        }
200
201        let edit_start_line = old_text.offset_to_point(range.start).row;
202        let edit_end_line = old_text.offset_to_point(range.end.min(old_text.len())).row;
203
204        let old_lines_in_range = edit_end_line.saturating_sub(edit_start_line);
205        let new_lines_in_range = new_text.chars().filter(|c| *c == '\n').count();
206        let line_delta = new_lines_in_range as isize - old_lines_in_range as isize;
207
208        self.fold_map
209            .adjust_folds_for_edit(edit_start_line, edit_end_line, line_delta);
210    }
211
212    /// Incrementally update fold candidates after a text edit.
213    ///
214    /// Extracts new fold candidates only within the edited byte range
215    /// and merges them with existing (already adjusted) candidates.
216    pub fn update_fold_candidates_for_edit(
217        &mut self,
218        extract_fold_ranges: impl FnOnce(Range<usize>, &Rope) -> Vec<FoldRange>,
219        edit_byte_range: Range<usize>,
220        new_text: &Rope,
221    ) {
222        let new_start_line = new_text.offset_to_point(edit_byte_range.start).row;
223        let new_end_line = new_text
224            .offset_to_point(edit_byte_range.end.min(new_text.len()))
225            .row;
226
227        let new_candidates = extract_fold_ranges(edit_byte_range, new_text);
228        self.fold_map
229            .merge_candidates_for_edit(new_start_line, new_end_line, new_candidates);
230    }
231
232    /// Update text (incremental or full)
233    pub fn on_text_changed(
234        &mut self,
235        changed_text: &Rope,
236        range: &Range<usize>,
237        new_text: &Rope,
238        cx: &mut App,
239    ) {
240        self.wrap_map
241            .on_text_changed(changed_text, range, new_text, cx);
242        self.rebuild_fold_projection();
243    }
244
245    /// Update layout parameters (wrap width or font)
246    pub(crate) fn set_inline_metrics(
247        &mut self,
248        metrics: std::rc::Rc<[(std::ops::Range<usize>, Pixels)]>,
249        cx: &mut App,
250    ) {
251        self.wrap_map.set_inline_metrics(metrics, cx);
252        self.rebuild_fold_projection();
253    }
254
255    pub fn on_layout_changed(&mut self, wrap_width: Option<Pixels>, cx: &mut App) {
256        self.wrap_map.on_layout_changed(wrap_width, cx);
257        self.rebuild_fold_projection();
258    }
259
260    /// Set the wrapping indent for continuation lines.
261    pub fn set_wrapping_indent(&mut self, wrapping_indent: WrappingIndent, cx: &mut App) {
262        self.wrap_map.set_wrapping_indent(wrapping_indent, cx);
263        self.rebuild_fold_projection();
264    }
265
266    /// Set the number of spaces per tab stop.
267    pub(crate) fn set_tab_size(&mut self, tab_size: usize, cx: &mut App) {
268        if self.wrap_map.set_tab_size(tab_size, cx) {
269            self.rebuild_fold_projection();
270        }
271    }
272
273    /// The width text is wrapped at, or `None` when it is not wrapped.
274    pub(crate) fn wrap_width(&self) -> Option<Pixels> {
275        self.wrap_map.wrapper().wrap_width()
276    }
277
278    /// Wrap the next replacement of the whole text at `wrap_width`, leaving the current rows as
279    /// they are. Only valid right before such a replacement, which rewraps every line.
280    pub(crate) fn prepare_wrap_width(&mut self, wrap_width: Pixels) {
281        self.wrap_map.prepare_wrap_width(wrap_width);
282    }
283
284    /// The font and size text is measured in.
285    pub(crate) fn font(&self) -> (&Font, Pixels) {
286        self.wrap_map.wrapper().font()
287    }
288
289    /// Set font parameters
290    pub fn set_font(&mut self, font: Font, font_size: Pixels, cx: &mut App) {
291        self.wrap_map.set_font(font, font_size, cx);
292        self.rebuild_fold_projection();
293    }
294
295    /// Ensure text is prepared (initializes wrapper if needed)
296    pub fn ensure_text_prepared(&mut self, text: &Rope, cx: &mut App) {
297        let did_initialize = self.wrap_map.ensure_text_prepared(text, cx);
298        if did_initialize {
299            self.rebuild_fold_projection();
300        }
301    }
302
303    /// Initialize with text
304    pub fn set_text(&mut self, text: &Rope, cx: &mut App) {
305        self.wrap_map.set_text(text, cx);
306        self.rebuild_fold_projection();
307    }
308
309    // ==================== Internal Helpers ====================
310
311    /// Rebuild fold projection after wrap_map or fold state changes
312    /// Only rebuilds if there are actually folded ranges
313    fn rebuild_fold_projection(&mut self) {
314        if !self.fold_map.folded_ranges().is_empty() {
315            self.fold_map.rebuild(&self.wrap_map);
316        } else {
317            // No active folds: identity mapping (wrap_row == display_row).
318            // Just update cached count so query methods work without Vec allocation.
319            self.fold_map
320                .mark_dirty_with_wrap_count(self.wrap_map.wrap_row_count());
321        }
322    }
323
324    // ==================== Wrap Display Point Operations ====================
325
326    /// Convert byte offset to wrap display point (with soft wrap info).
327    #[inline]
328    pub(crate) fn offset_to_wrap_display_point(&self, offset: usize) -> WrapDisplayPoint {
329        self.wrap_map.wrapper().offset_to_display_point(offset)
330    }
331
332    /// Like [`Self::offset_to_wrap_display_point`], but honours the caret's line-end affinity so
333    /// an offset on a soft wrap boundary resolves to the row the caret is drawn on.
334    #[inline]
335    pub(crate) fn offset_to_wrap_display_point_with_affinity(
336        &self,
337        offset: usize,
338        line_end_affinity: bool,
339    ) -> WrapDisplayPoint {
340        self.wrap_map
341            .wrapper()
342            .offset_to_display_point_with_affinity(offset, line_end_affinity)
343    }
344
345    /// Resolve the caret's visual row through both wrapping and folded regions.
346    pub(crate) fn offset_to_display_row(&self, offset: usize, line_end_affinity: bool) -> usize {
347        let point = self.offset_to_wrap_display_point_with_affinity(offset, line_end_affinity);
348        self.wrap_row_to_display_row(point.row)
349            .unwrap_or_else(|| self.nearest_visible_display_row(point.row))
350    }
351
352    /// Convert wrap display point to byte offset.
353    #[inline]
354    pub(crate) fn wrap_display_point_to_offset(&self, point: WrapDisplayPoint) -> usize {
355        self.wrap_map.wrapper().display_point_to_offset(point)
356    }
357
358    /// Convert wrap display point to TreeSitterPoint (buffer line/col).
359    #[inline]
360    pub(crate) fn wrap_display_point_to_point(&self, point: WrapDisplayPoint) -> TreeSitterPoint {
361        self.wrap_map.wrapper().display_point_to_point(point)
362    }
363
364    /// Convert a wrap row to a display row (skipping folded rows).
365    /// Returns None if the wrap row is folded.
366    #[inline]
367    pub fn wrap_row_to_display_row(&self, wrap_row: usize) -> Option<usize> {
368        self.fold_map.wrap_row_to_display_row(wrap_row)
369    }
370
371    /// Find the nearest visible display row for a given wrap row.
372    #[inline]
373    pub fn nearest_visible_display_row(&self, wrap_row: usize) -> usize {
374        self.fold_map.nearest_visible_display_row(wrap_row)
375    }
376
377    /// Convert a display row to a wrap row.
378    #[inline]
379    pub fn display_row_to_wrap_row(&self, display_row: usize) -> Option<usize> {
380        self.fold_map.display_row_to_wrap_row(display_row)
381    }
382
383    pub(crate) fn display_row_column_to_offset(&self, display_row: usize, column: usize) -> usize {
384        let wrap_row = self
385            .fold_map
386            .display_row_to_wrap_row(display_row)
387            .unwrap_or(0);
388        self.wrap_map
389            .wrapper()
390            .display_point_to_offset(WrapDisplayPoint::new(wrap_row, 0, column))
391    }
392
393    /// Get the longest row index (by byte length).
394    #[inline]
395    pub(crate) fn longest_row(&self) -> usize {
396        self.wrap_map.wrapper().longest_row()
397    }
398
399    // ==================== Access Methods ====================
400
401    /// Get the line item by buffer row index.
402    #[inline]
403    pub(crate) fn line(&self, row: usize) -> Option<&LineItem> {
404        self.wrap_map.line(row)
405    }
406
407    /// Get the rope text
408    #[inline]
409    pub fn text(&self) -> &Rope {
410        self.wrap_map.text()
411    }
412
413    /// Calculate how many wrap rows of a buffer line are visible (not folded)
414    #[inline]
415    pub fn visible_wrap_row_count_for_buffer_line(&self, line: usize) -> usize {
416        self.wrap_map
417            .visible_wrap_row_count_for_line(line, &self.fold_map)
418    }
419
420    /// Get the wrap row count (before folding)
421    #[inline]
422    pub fn wrap_row_count(&self) -> usize {
423        self.wrap_map.wrap_row_count()
424    }
425
426    /// Get the buffer line count (logical lines)
427    #[inline]
428    pub fn buffer_line_count(&self) -> usize {
429        self.wrap_map.buffer_line_count()
430    }
431}