1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use super::Axis;
use crate::{
    Autoscroll, Bias, Editor, EditorMode, NextScreen, ScrollAnchor, ScrollCursorBottom,
    ScrollCursorCenter, ScrollCursorTop,
};
use gpui::{actions, AppContext, Point, ViewContext};

impl Editor {
    pub fn next_screen(&mut self, _: &NextScreen, cx: &mut ViewContext<Editor>) {
        if self.take_rename(true, cx).is_some() {
            return;
        }

        // todo!()
        // if self.mouse_context_menu.read(cx).visible() {
        //     return None;
        // }

        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }
        self.request_autoscroll(Autoscroll::Next, cx);
    }

    pub fn scroll(
        &mut self,
        scroll_position: Point<f32>,
        axis: Option<Axis>,
        cx: &mut ViewContext<Self>,
    ) {
        self.scroll_manager.update_ongoing_scroll(axis);
        self.set_scroll_position(scroll_position, cx);
    }

    pub fn scroll_cursor_top(&mut self, _: &ScrollCursorTop, cx: &mut ViewContext<Editor>) {
        let snapshot = self.snapshot(cx).display_snapshot;
        let scroll_margin_rows = self.vertical_scroll_margin() as u32;

        let mut new_screen_top = self.selections.newest_display(cx).head();
        *new_screen_top.row_mut() = new_screen_top.row().saturating_sub(scroll_margin_rows);
        *new_screen_top.column_mut() = 0;
        let new_screen_top = new_screen_top.to_offset(&snapshot, Bias::Left);
        let new_anchor = snapshot.buffer_snapshot.anchor_before(new_screen_top);

        self.set_scroll_anchor(
            ScrollAnchor {
                anchor: new_anchor,
                offset: Default::default(),
            },
            cx,
        )
    }

    pub fn scroll_cursor_center(&mut self, _: &ScrollCursorCenter, cx: &mut ViewContext<Editor>) {
        let snapshot = self.snapshot(cx).display_snapshot;
        let visible_rows = if let Some(visible_rows) = self.visible_line_count() {
            visible_rows as u32
        } else {
            return;
        };

        let mut new_screen_top = self.selections.newest_display(cx).head();
        *new_screen_top.row_mut() = new_screen_top.row().saturating_sub(visible_rows / 2);
        *new_screen_top.column_mut() = 0;
        let new_screen_top = new_screen_top.to_offset(&snapshot, Bias::Left);
        let new_anchor = snapshot.buffer_snapshot.anchor_before(new_screen_top);

        self.set_scroll_anchor(
            ScrollAnchor {
                anchor: new_anchor,
                offset: Default::default(),
            },
            cx,
        )
    }

    pub fn scroll_cursor_bottom(&mut self, _: &ScrollCursorBottom, cx: &mut ViewContext<Editor>) {
        let snapshot = self.snapshot(cx).display_snapshot;
        let scroll_margin_rows = self.vertical_scroll_margin() as u32;
        let visible_rows = if let Some(visible_rows) = self.visible_line_count() {
            visible_rows as u32
        } else {
            return;
        };

        let mut new_screen_top = self.selections.newest_display(cx).head();
        *new_screen_top.row_mut() = new_screen_top
            .row()
            .saturating_sub(visible_rows.saturating_sub(scroll_margin_rows));
        *new_screen_top.column_mut() = 0;
        let new_screen_top = new_screen_top.to_offset(&snapshot, Bias::Left);
        let new_anchor = snapshot.buffer_snapshot.anchor_before(new_screen_top);

        self.set_scroll_anchor(
            ScrollAnchor {
                anchor: new_anchor,
                offset: Default::default(),
            },
            cx,
        )
    }
}