1mod actions;
2pub(crate) mod autoscroll;
3pub(crate) mod scroll_amount;
4
5use crate::editor_settings::ScrollBeyondLastLine;
6use crate::{
7 Anchor, DisplayPoint, DisplayRow, Editor, EditorEvent, EditorMode, EditorSettings,
8 InlayHintRefreshReason, MultiBufferSnapshot, RowExt, ToPoint,
9 display_map::{DisplaySnapshot, ToDisplayPoint},
10 hover_popover::hide_hover,
11 persistence::DB,
12};
13pub use autoscroll::{Autoscroll, AutoscrollStrategy};
14use core::fmt::Debug;
15use gpui::{App, Axis, Context, Global, Pixels, Task, Window, point, px};
16use language::language_settings::{AllLanguageSettings, SoftWrap};
17use language::{Bias, Point};
18pub use scroll_amount::ScrollAmount;
19use settings::Settings;
20use std::{
21 cmp::Ordering,
22 time::{Duration, Instant},
23};
24use util::ResultExt;
25use workspace::{ItemId, WorkspaceId};
26
27pub const SCROLL_EVENT_SEPARATION: Duration = Duration::from_millis(28);
28const SCROLLBAR_SHOW_INTERVAL: Duration = Duration::from_secs(1);
29
30#[derive(Default)]
31pub struct ScrollbarAutoHide(pub bool);
32
33impl Global for ScrollbarAutoHide {}
34
35#[derive(Clone, Copy, Debug, PartialEq)]
36pub struct ScrollAnchor {
37 pub offset: gpui::Point<f32>,
38 pub anchor: Anchor,
39}
40
41impl ScrollAnchor {
42 pub(super) fn new() -> Self {
43 Self {
44 offset: gpui::Point::default(),
45 anchor: Anchor::min(),
46 }
47 }
48
49 pub fn scroll_position(&self, snapshot: &DisplaySnapshot) -> gpui::Point<f32> {
50 let mut scroll_position = self.offset;
51 if self.anchor == Anchor::min() {
52 scroll_position.y = 0.;
53 } else {
54 let scroll_top = self.anchor.to_display_point(snapshot).row().as_f32();
55 scroll_position.y += scroll_top;
56 }
57 scroll_position
58 }
59
60 pub fn top_row(&self, buffer: &MultiBufferSnapshot) -> u32 {
61 self.anchor.to_point(buffer).row
62 }
63}
64
65#[derive(Clone, Copy, Debug)]
66pub struct OngoingScroll {
67 last_event: Instant,
68 axis: Option<Axis>,
69}
70
71impl OngoingScroll {
72 fn new() -> Self {
73 Self {
74 last_event: Instant::now() - SCROLL_EVENT_SEPARATION,
75 axis: None,
76 }
77 }
78
79 pub fn filter(&self, delta: &mut gpui::Point<Pixels>) -> Option<Axis> {
80 const UNLOCK_PERCENT: f32 = 1.9;
81 const UNLOCK_LOWER_BOUND: Pixels = px(6.);
82 let mut axis = self.axis;
83
84 let x = delta.x.abs();
85 let y = delta.y.abs();
86 let duration = Instant::now().duration_since(self.last_event);
87 if duration > SCROLL_EVENT_SEPARATION {
88 //New ongoing scroll will start, determine axis
89 axis = if x <= y {
90 Some(Axis::Vertical)
91 } else {
92 Some(Axis::Horizontal)
93 };
94 } else if x.max(y) >= UNLOCK_LOWER_BOUND {
95 //Check if the current ongoing will need to unlock
96 match axis {
97 Some(Axis::Vertical) => {
98 if x > y && x >= y * UNLOCK_PERCENT {
99 axis = None;
100 }
101 }
102
103 Some(Axis::Horizontal) => {
104 if y > x && y >= x * UNLOCK_PERCENT {
105 axis = None;
106 }
107 }
108
109 None => {}
110 }
111 }
112
113 match axis {
114 Some(Axis::Vertical) => {
115 *delta = point(px(0.), delta.y);
116 }
117 Some(Axis::Horizontal) => {
118 *delta = point(delta.x, px(0.));
119 }
120 None => {}
121 }
122
123 axis
124 }
125}
126
127#[derive(Copy, Clone, Default, PartialEq, Eq)]
128pub enum ScrollbarThumbState {
129 #[default]
130 Idle,
131 Hovered,
132 Dragging,
133}
134
135#[derive(PartialEq, Eq)]
136pub struct ActiveScrollbarState {
137 axis: Axis,
138 thumb_state: ScrollbarThumbState,
139}
140
141impl ActiveScrollbarState {
142 pub fn new(axis: Axis, thumb_state: ScrollbarThumbState) -> Self {
143 ActiveScrollbarState { axis, thumb_state }
144 }
145
146 pub fn thumb_state_for_axis(&self, axis: Axis) -> Option<ScrollbarThumbState> {
147 (self.axis == axis).then_some(self.thumb_state)
148 }
149}
150
151pub struct ScrollManager {
152 pub(crate) vertical_scroll_margin: f32,
153 anchor: ScrollAnchor,
154 ongoing: OngoingScroll,
155 /// The second element indicates whether the autoscroll request is local
156 /// (true) or remote (false). Local requests are initiated by user actions,
157 /// while remote requests come from external sources.
158 autoscroll_request: Option<(Autoscroll, bool)>,
159 last_autoscroll: Option<(gpui::Point<f32>, f32, f32, AutoscrollStrategy)>,
160 show_scrollbars: bool,
161 hide_scrollbar_task: Option<Task<()>>,
162 active_scrollbar: Option<ActiveScrollbarState>,
163 visible_line_count: Option<f32>,
164 visible_column_count: Option<f32>,
165 forbid_vertical_scroll: bool,
166 minimap_thumb_state: Option<ScrollbarThumbState>,
167}
168
169impl ScrollManager {
170 pub fn new(cx: &mut App) -> Self {
171 ScrollManager {
172 vertical_scroll_margin: EditorSettings::get_global(cx).vertical_scroll_margin,
173 anchor: ScrollAnchor::new(),
174 ongoing: OngoingScroll::new(),
175 autoscroll_request: None,
176 show_scrollbars: true,
177 hide_scrollbar_task: None,
178 active_scrollbar: None,
179 last_autoscroll: None,
180 visible_line_count: None,
181 visible_column_count: None,
182 forbid_vertical_scroll: false,
183 minimap_thumb_state: None,
184 }
185 }
186
187 pub fn clone_state(&mut self, other: &Self) {
188 self.anchor = other.anchor;
189 self.ongoing = other.ongoing;
190 }
191
192 pub fn anchor(&self) -> ScrollAnchor {
193 self.anchor
194 }
195
196 pub fn ongoing_scroll(&self) -> OngoingScroll {
197 self.ongoing
198 }
199
200 pub fn update_ongoing_scroll(&mut self, axis: Option<Axis>) {
201 self.ongoing.last_event = Instant::now();
202 self.ongoing.axis = axis;
203 }
204
205 pub fn scroll_position(&self, snapshot: &DisplaySnapshot) -> gpui::Point<f32> {
206 self.anchor.scroll_position(snapshot)
207 }
208
209 fn set_scroll_position(
210 &mut self,
211 scroll_position: gpui::Point<f32>,
212 map: &DisplaySnapshot,
213 local: bool,
214 autoscroll: bool,
215 workspace_id: Option<WorkspaceId>,
216 window: &mut Window,
217 cx: &mut Context<Editor>,
218 ) {
219 let (new_anchor, top_row) = if scroll_position.y <= 0. && scroll_position.x <= 0. {
220 (
221 ScrollAnchor {
222 anchor: Anchor::min(),
223 offset: scroll_position.max(&gpui::Point::default()),
224 },
225 0,
226 )
227 } else if scroll_position.y <= 0. {
228 let buffer_point = map
229 .clip_point(
230 DisplayPoint::new(DisplayRow(0), scroll_position.x as u32),
231 Bias::Left,
232 )
233 .to_point(map);
234 let anchor = map.buffer_snapshot.anchor_at(buffer_point, Bias::Right);
235
236 (
237 ScrollAnchor {
238 anchor: anchor,
239 offset: scroll_position.max(&gpui::Point::default()),
240 },
241 0,
242 )
243 } else {
244 let scroll_top = scroll_position.y;
245 let scroll_top = match EditorSettings::get_global(cx).scroll_beyond_last_line {
246 ScrollBeyondLastLine::OnePage => scroll_top,
247 ScrollBeyondLastLine::Off => {
248 if let Some(height_in_lines) = self.visible_line_count {
249 let max_row = map.max_point().row().0 as f32;
250 scroll_top.min(max_row - height_in_lines + 1.).max(0.)
251 } else {
252 scroll_top
253 }
254 }
255 ScrollBeyondLastLine::VerticalScrollMargin => {
256 if let Some(height_in_lines) = self.visible_line_count {
257 let max_row = map.max_point().row().0 as f32;
258 scroll_top
259 .min(max_row - height_in_lines + 1. + self.vertical_scroll_margin)
260 .max(0.)
261 } else {
262 scroll_top
263 }
264 }
265 };
266
267 let scroll_top_row = DisplayRow(scroll_top as u32);
268 let scroll_top_buffer_point = map
269 .clip_point(
270 DisplayPoint::new(scroll_top_row, scroll_position.x as u32),
271 Bias::Left,
272 )
273 .to_point(map);
274 let top_anchor = map
275 .buffer_snapshot
276 .anchor_at(scroll_top_buffer_point, Bias::Right);
277
278 (
279 ScrollAnchor {
280 anchor: top_anchor,
281 offset: point(
282 scroll_position.x.max(0.),
283 scroll_top - top_anchor.to_display_point(map).row().as_f32(),
284 ),
285 },
286 scroll_top_buffer_point.row,
287 )
288 };
289
290 self.set_anchor(
291 new_anchor,
292 top_row,
293 local,
294 autoscroll,
295 workspace_id,
296 window,
297 cx,
298 );
299 }
300
301 fn set_anchor(
302 &mut self,
303 anchor: ScrollAnchor,
304 top_row: u32,
305 local: bool,
306 autoscroll: bool,
307 workspace_id: Option<WorkspaceId>,
308 window: &mut Window,
309 cx: &mut Context<Editor>,
310 ) {
311 let adjusted_anchor = if self.forbid_vertical_scroll {
312 ScrollAnchor {
313 offset: gpui::Point::new(anchor.offset.x, self.anchor.offset.y),
314 anchor: self.anchor.anchor,
315 }
316 } else {
317 anchor
318 };
319
320 self.anchor = adjusted_anchor;
321 cx.emit(EditorEvent::ScrollPositionChanged { local, autoscroll });
322 self.show_scrollbars(window, cx);
323 self.autoscroll_request.take();
324 if let Some(workspace_id) = workspace_id {
325 let item_id = cx.entity().entity_id().as_u64() as ItemId;
326
327 cx.foreground_executor()
328 .spawn(async move {
329 log::debug!(
330 "Saving scroll position for item {item_id:?} in workspace {workspace_id:?}"
331 );
332 DB.save_scroll_position(
333 item_id,
334 workspace_id,
335 top_row,
336 anchor.offset.x,
337 anchor.offset.y,
338 )
339 .await
340 .log_err()
341 })
342 .detach()
343 }
344 cx.notify();
345 }
346
347 pub fn show_scrollbars(&mut self, window: &mut Window, cx: &mut Context<Editor>) {
348 if !self.show_scrollbars {
349 self.show_scrollbars = true;
350 cx.notify();
351 }
352
353 if cx.default_global::<ScrollbarAutoHide>().0 {
354 self.hide_scrollbar_task = Some(cx.spawn_in(window, async move |editor, cx| {
355 cx.background_executor()
356 .timer(SCROLLBAR_SHOW_INTERVAL)
357 .await;
358 editor
359 .update(cx, |editor, cx| {
360 editor.scroll_manager.show_scrollbars = false;
361 cx.notify();
362 })
363 .log_err();
364 }));
365 } else {
366 self.hide_scrollbar_task = None;
367 }
368 }
369
370 pub fn scrollbars_visible(&self) -> bool {
371 self.show_scrollbars
372 }
373
374 pub fn autoscroll_request(&self) -> Option<Autoscroll> {
375 self.autoscroll_request.map(|(autoscroll, _)| autoscroll)
376 }
377
378 pub fn active_scrollbar_state(&self) -> Option<&ActiveScrollbarState> {
379 self.active_scrollbar.as_ref()
380 }
381
382 pub fn dragging_scrollbar_axis(&self) -> Option<Axis> {
383 self.active_scrollbar
384 .as_ref()
385 .filter(|scrollbar| scrollbar.thumb_state == ScrollbarThumbState::Dragging)
386 .map(|scrollbar| scrollbar.axis)
387 }
388
389 pub fn any_scrollbar_dragged(&self) -> bool {
390 self.active_scrollbar
391 .as_ref()
392 .is_some_and(|scrollbar| scrollbar.thumb_state == ScrollbarThumbState::Dragging)
393 }
394
395 pub fn set_hovered_scroll_thumb_axis(&mut self, axis: Axis, cx: &mut Context<Editor>) {
396 self.update_active_scrollbar_state(
397 Some(ActiveScrollbarState::new(
398 axis,
399 ScrollbarThumbState::Hovered,
400 )),
401 cx,
402 );
403 }
404
405 pub fn set_dragged_scroll_thumb_axis(&mut self, axis: Axis, cx: &mut Context<Editor>) {
406 self.update_active_scrollbar_state(
407 Some(ActiveScrollbarState::new(
408 axis,
409 ScrollbarThumbState::Dragging,
410 )),
411 cx,
412 );
413 }
414
415 pub fn reset_scrollbar_state(&mut self, cx: &mut Context<Editor>) {
416 self.update_active_scrollbar_state(None, cx);
417 }
418
419 fn update_active_scrollbar_state(
420 &mut self,
421 new_state: Option<ActiveScrollbarState>,
422 cx: &mut Context<Editor>,
423 ) {
424 if self.active_scrollbar != new_state {
425 self.active_scrollbar = new_state;
426 cx.notify();
427 }
428 }
429
430 pub fn set_is_hovering_minimap_thumb(&mut self, hovered: bool, cx: &mut Context<Editor>) {
431 self.update_minimap_thumb_state(
432 Some(if hovered {
433 ScrollbarThumbState::Hovered
434 } else {
435 ScrollbarThumbState::Idle
436 }),
437 cx,
438 );
439 }
440
441 pub fn set_is_dragging_minimap(&mut self, cx: &mut Context<Editor>) {
442 self.update_minimap_thumb_state(Some(ScrollbarThumbState::Dragging), cx);
443 }
444
445 pub fn hide_minimap_thumb(&mut self, cx: &mut Context<Editor>) {
446 self.update_minimap_thumb_state(None, cx);
447 }
448
449 pub fn is_dragging_minimap(&self) -> bool {
450 self.minimap_thumb_state
451 .is_some_and(|state| state == ScrollbarThumbState::Dragging)
452 }
453
454 fn update_minimap_thumb_state(
455 &mut self,
456 thumb_state: Option<ScrollbarThumbState>,
457 cx: &mut Context<Editor>,
458 ) {
459 if self.minimap_thumb_state != thumb_state {
460 self.minimap_thumb_state = thumb_state;
461 cx.notify();
462 }
463 }
464
465 pub fn minimap_thumb_state(&self) -> Option<ScrollbarThumbState> {
466 self.minimap_thumb_state
467 }
468
469 pub fn clamp_scroll_left(&mut self, max: f32) -> bool {
470 if max < self.anchor.offset.x {
471 self.anchor.offset.x = max;
472 true
473 } else {
474 false
475 }
476 }
477
478 pub fn set_forbid_vertical_scroll(&mut self, forbid: bool) {
479 self.forbid_vertical_scroll = forbid;
480 }
481
482 pub fn forbid_vertical_scroll(&self) -> bool {
483 self.forbid_vertical_scroll
484 }
485}
486
487impl Editor {
488 pub fn vertical_scroll_margin(&self) -> usize {
489 self.scroll_manager.vertical_scroll_margin as usize
490 }
491
492 pub fn set_vertical_scroll_margin(&mut self, margin_rows: usize, cx: &mut Context<Self>) {
493 self.scroll_manager.vertical_scroll_margin = margin_rows as f32;
494 cx.notify();
495 }
496
497 pub fn visible_line_count(&self) -> Option<f32> {
498 self.scroll_manager.visible_line_count
499 }
500
501 pub fn visible_row_count(&self) -> Option<u32> {
502 self.visible_line_count()
503 .map(|line_count| line_count as u32 - 1)
504 }
505
506 pub fn visible_column_count(&self) -> Option<f32> {
507 self.scroll_manager.visible_column_count
508 }
509
510 pub(crate) fn set_visible_line_count(
511 &mut self,
512 lines: f32,
513 window: &mut Window,
514 cx: &mut Context<Self>,
515 ) {
516 let opened_first_time = self.scroll_manager.visible_line_count.is_none();
517 self.scroll_manager.visible_line_count = Some(lines);
518 if opened_first_time {
519 cx.spawn_in(window, async move |editor, cx| {
520 editor
521 .update_in(cx, |editor, window, cx| {
522 editor.refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx);
523 editor.refresh_colors(false, None, window, cx);
524 })
525 .ok()
526 })
527 .detach()
528 }
529 }
530
531 pub(crate) fn set_visible_column_count(&mut self, columns: f32) {
532 self.scroll_manager.visible_column_count = Some(columns);
533 }
534
535 pub fn apply_scroll_delta(
536 &mut self,
537 scroll_delta: gpui::Point<f32>,
538 window: &mut Window,
539 cx: &mut Context<Self>,
540 ) {
541 let mut delta = scroll_delta;
542 if self.scroll_manager.forbid_vertical_scroll {
543 delta.y = 0.0;
544 }
545 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
546 let position = self.scroll_manager.anchor.scroll_position(&display_map) + delta;
547 self.set_scroll_position_taking_display_map(position, true, false, display_map, window, cx);
548 }
549
550 pub fn set_scroll_position(
551 &mut self,
552 scroll_position: gpui::Point<f32>,
553 window: &mut Window,
554 cx: &mut Context<Self>,
555 ) {
556 let mut position = scroll_position;
557 if self.scroll_manager.forbid_vertical_scroll {
558 let current_position = self.scroll_position(cx);
559 position.y = current_position.y;
560 }
561 self.set_scroll_position_internal(position, true, false, window, cx);
562 }
563
564 /// Scrolls so that `row` is at the top of the editor view.
565 pub fn set_scroll_top_row(
566 &mut self,
567 row: DisplayRow,
568 window: &mut Window,
569 cx: &mut Context<Editor>,
570 ) {
571 let snapshot = self.snapshot(window, cx).display_snapshot;
572 let new_screen_top = DisplayPoint::new(row, 0);
573 let new_screen_top = new_screen_top.to_offset(&snapshot, Bias::Left);
574 let new_anchor = snapshot.buffer_snapshot.anchor_before(new_screen_top);
575
576 self.set_scroll_anchor(
577 ScrollAnchor {
578 anchor: new_anchor,
579 offset: Default::default(),
580 },
581 window,
582 cx,
583 );
584 }
585
586 pub(crate) fn set_scroll_position_internal(
587 &mut self,
588 scroll_position: gpui::Point<f32>,
589 local: bool,
590 autoscroll: bool,
591 window: &mut Window,
592 cx: &mut Context<Self>,
593 ) {
594 let map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
595 self.set_scroll_position_taking_display_map(
596 scroll_position,
597 local,
598 autoscroll,
599 map,
600 window,
601 cx,
602 );
603 }
604
605 fn set_scroll_position_taking_display_map(
606 &mut self,
607 scroll_position: gpui::Point<f32>,
608 local: bool,
609 autoscroll: bool,
610 display_map: DisplaySnapshot,
611 window: &mut Window,
612 cx: &mut Context<Self>,
613 ) {
614 hide_hover(self, cx);
615 let workspace_id = self.workspace.as_ref().and_then(|workspace| workspace.1);
616
617 self.edit_prediction_preview
618 .set_previous_scroll_position(None);
619
620 let adjusted_position = if self.scroll_manager.forbid_vertical_scroll {
621 let current_position = self.scroll_manager.anchor.scroll_position(&display_map);
622 gpui::Point::new(scroll_position.x, current_position.y)
623 } else {
624 scroll_position
625 };
626
627 self.scroll_manager.set_scroll_position(
628 adjusted_position,
629 &display_map,
630 local,
631 autoscroll,
632 workspace_id,
633 window,
634 cx,
635 );
636
637 self.refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx);
638 self.refresh_colors(false, None, window, cx);
639 }
640
641 pub fn scroll_position(&self, cx: &mut Context<Self>) -> gpui::Point<f32> {
642 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
643 self.scroll_manager.anchor.scroll_position(&display_map)
644 }
645
646 pub fn set_scroll_anchor(
647 &mut self,
648 scroll_anchor: ScrollAnchor,
649 window: &mut Window,
650 cx: &mut Context<Self>,
651 ) {
652 hide_hover(self, cx);
653 let workspace_id = self.workspace.as_ref().and_then(|workspace| workspace.1);
654 let top_row = scroll_anchor
655 .anchor
656 .to_point(&self.buffer().read(cx).snapshot(cx))
657 .row;
658 self.scroll_manager.set_anchor(
659 scroll_anchor,
660 top_row,
661 true,
662 false,
663 workspace_id,
664 window,
665 cx,
666 );
667 }
668
669 pub(crate) fn set_scroll_anchor_remote(
670 &mut self,
671 scroll_anchor: ScrollAnchor,
672 window: &mut Window,
673 cx: &mut Context<Self>,
674 ) {
675 hide_hover(self, cx);
676 let workspace_id = self.workspace.as_ref().and_then(|workspace| workspace.1);
677 let snapshot = &self.buffer().read(cx).snapshot(cx);
678 if !scroll_anchor.anchor.is_valid(snapshot) {
679 log::warn!("Invalid scroll anchor: {:?}", scroll_anchor);
680 return;
681 }
682 let top_row = scroll_anchor.anchor.to_point(snapshot).row;
683 self.scroll_manager.set_anchor(
684 scroll_anchor,
685 top_row,
686 false,
687 false,
688 workspace_id,
689 window,
690 cx,
691 );
692 }
693
694 pub fn scroll_screen(
695 &mut self,
696 amount: &ScrollAmount,
697 window: &mut Window,
698 cx: &mut Context<Self>,
699 ) {
700 if matches!(self.mode, EditorMode::SingleLine { .. }) {
701 cx.propagate();
702 return;
703 }
704
705 if self.take_rename(true, window, cx).is_some() {
706 return;
707 }
708
709 let mut current_position = self.scroll_position(cx);
710 let Some(visible_line_count) = self.visible_line_count() else {
711 return;
712 };
713 let Some(mut visible_column_count) = self.visible_column_count() else {
714 return;
715 };
716
717 // If the user has a preferred line length, and has the editor
718 // configured to wrap at the preferred line length, or bounded to it,
719 // use that value over the visible column count. This was mostly done so
720 // that tests could actually be written for vim's `z l`, `z h`, `z
721 // shift-l` and `z shift-h` commands, as there wasn't a good way to
722 // configure the editor to only display a certain number of columns. If
723 // that ever happens, this could probably be removed.
724 let settings = AllLanguageSettings::get_global(cx);
725 if matches!(
726 settings.defaults.soft_wrap,
727 SoftWrap::PreferredLineLength | SoftWrap::Bounded
728 ) {
729 if (settings.defaults.preferred_line_length as f32) < visible_column_count {
730 visible_column_count = settings.defaults.preferred_line_length as f32;
731 }
732 }
733
734 // If the scroll position is currently at the left edge of the document
735 // (x == 0.0) and the intent is to scroll right, the gutter's margin
736 // should first be added to the current position, otherwise the cursor
737 // will end at the column position minus the margin, which looks off.
738 if current_position.x == 0.0 && amount.columns(visible_column_count) > 0. {
739 if let Some(last_position_map) = &self.last_position_map {
740 current_position.x += self.gutter_dimensions.margin / last_position_map.em_advance;
741 }
742 }
743 let new_position = current_position
744 + point(
745 amount.columns(visible_column_count),
746 amount.lines(visible_line_count),
747 );
748 self.set_scroll_position(new_position, window, cx);
749 }
750
751 /// Returns an ordering. The newest selection is:
752 /// Ordering::Equal => on screen
753 /// Ordering::Less => above or to the left of the screen
754 /// Ordering::Greater => below or to the right of the screen
755 pub fn newest_selection_on_screen(&self, cx: &mut App) -> Ordering {
756 let snapshot = self.display_map.update(cx, |map, cx| map.snapshot(cx));
757 let newest_head = self
758 .selections
759 .newest_anchor()
760 .head()
761 .to_display_point(&snapshot);
762 let screen_top = self
763 .scroll_manager
764 .anchor
765 .anchor
766 .to_display_point(&snapshot);
767
768 if screen_top > newest_head {
769 return Ordering::Less;
770 }
771
772 if let (Some(visible_lines), Some(visible_columns)) =
773 (self.visible_line_count(), self.visible_column_count())
774 {
775 if newest_head.row() <= DisplayRow(screen_top.row().0 + visible_lines as u32)
776 && newest_head.column() <= screen_top.column() + visible_columns as u32
777 {
778 return Ordering::Equal;
779 }
780 }
781
782 Ordering::Greater
783 }
784
785 pub fn read_scroll_position_from_db(
786 &mut self,
787 item_id: u64,
788 workspace_id: WorkspaceId,
789 window: &mut Window,
790 cx: &mut Context<Editor>,
791 ) {
792 let scroll_position = DB.get_scroll_position(item_id, workspace_id);
793 if let Ok(Some((top_row, x, y))) = scroll_position {
794 let top_anchor = self
795 .buffer()
796 .read(cx)
797 .snapshot(cx)
798 .anchor_at(Point::new(top_row, 0), Bias::Left);
799 let scroll_anchor = ScrollAnchor {
800 offset: gpui::Point::new(x, y),
801 anchor: top_anchor,
802 };
803 self.set_scroll_anchor(scroll_anchor, window, cx);
804 }
805 }
806}